| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osgDB/ReadFile> |
|---|
| 15 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 16 | #include <osgWidget/VncClient> |
|---|
| 17 | |
|---|
| 18 | using namespace osgWidget; |
|---|
| 19 | |
|---|
| 20 | VncClient::VncClient(const std::string& hostname, const GeometryHints& hints) |
|---|
| 21 | { |
|---|
| 22 | connect(hostname, hints); |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | bool VncClient::assign(VncImage* vncImage, const GeometryHints& hints) |
|---|
| 26 | { |
|---|
| 27 | if (!vncImage) return false; |
|---|
| 28 | |
|---|
| 29 | _vncImage = vncImage; |
|---|
| 30 | |
|---|
| 31 | bool flip = _vncImage->getOrigin()==osg::Image::TOP_LEFT; |
|---|
| 32 | |
|---|
| 33 | float aspectRatio = (_vncImage->t()>0 && _vncImage->s()>0) ? float(_vncImage->t()) / float(_vncImage->s()) : 1.0; |
|---|
| 34 | |
|---|
| 35 | osg::Vec3 widthVec(hints.widthVec); |
|---|
| 36 | osg::Vec3 heightVec(hints.heightVec); |
|---|
| 37 | |
|---|
| 38 | switch(hints.aspectRatioPolicy) |
|---|
| 39 | { |
|---|
| 40 | case(GeometryHints::RESIZE_HEIGHT_TO_MAINTAINCE_ASPECT_RATIO): |
|---|
| 41 | heightVec *= aspectRatio; |
|---|
| 42 | break; |
|---|
| 43 | case(GeometryHints::RESIZE_WIDTH_TO_MAINTAINCE_ASPECT_RATIO): |
|---|
| 44 | widthVec /= aspectRatio; |
|---|
| 45 | break; |
|---|
| 46 | default: |
|---|
| 47 | |
|---|
| 48 | break; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(hints.position, widthVec, heightVec, |
|---|
| 52 | 0.0f, flip ? 1.0f : 0.0f , 1.0f, flip ? 0.0f : 1.0f); |
|---|
| 53 | |
|---|
| 54 | osg::Texture2D* texture = new osg::Texture2D(_vncImage.get()); |
|---|
| 55 | texture->setResizeNonPowerOfTwoHint(false); |
|---|
| 56 | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); |
|---|
| 57 | texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); |
|---|
| 58 | texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); |
|---|
| 59 | |
|---|
| 60 | pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, |
|---|
| 61 | texture, |
|---|
| 62 | osg::StateAttribute::ON); |
|---|
| 63 | |
|---|
| 64 | pictureQuad->setEventCallback(new osgViewer::InteractiveImageHandler(_vncImage.get())); |
|---|
| 65 | |
|---|
| 66 | addDrawable(pictureQuad); |
|---|
| 67 | |
|---|
| 68 | return true; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | bool VncClient::connect(const std::string& hostname, const GeometryHints& hints) |
|---|
| 72 | { |
|---|
| 73 | osg::ref_ptr<osg::Image> image = osgDB::readImageFile(hostname+".vnc"); |
|---|
| 74 | return assign(dynamic_cast<VncImage*>(image.get()), hints); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | void VncClient::close() |
|---|
| 78 | { |
|---|
| 79 | if (!_vncImage) return; |
|---|
| 80 | |
|---|
| 81 | _vncImage->close(); |
|---|
| 82 | } |
|---|