diff --git a/examples/osgbrowser/osgbrowser.cpp b/examples/osgbrowser/osgbrowser.cpp index 695346e6c..3fbcd73d1 100644 --- a/examples/osgbrowser/osgbrowser.cpp +++ b/examples/osgbrowser/osgbrowser.cpp @@ -1,50 +1,9 @@ -#include -#include -#include -#include -#include - #include #include -#include - -#include -#include -#include #include - -osg::Node* createInteractiveQuad(const osg::Vec3& origin, osg::Vec3& widthAxis, osg::Vec3& heightAxis, - osg::Image* image) -{ - bool flip = image->getOrigin()==osg::Image::TOP_LEFT; - - osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(origin, widthAxis, heightAxis, - 0.0f, flip ? 1.0f : 0.0f , 1.0f, flip ? 0.0f : 1.0f); - - osg::Texture2D* texture = new osg::Texture2D(image); - texture->setResizeNonPowerOfTwoHint(false); - texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); - texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); - texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); - - pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, - texture, - osg::StateAttribute::ON); - - osg::ref_ptr callback = new osgViewer::InteractiveImageHandler(image); - - pictureQuad->setEventCallback(callback.get()); - pictureQuad->setCullCallback(callback.get()); - - osg::Geode* geode = new osg::Geode; - geode->addDrawable(pictureQuad); - - return geode; -} - -int main( int argc, char* argv[] ) +int main(int argc,char** argv) { osg::ArgumentParser arguments(&argc, argv); @@ -52,43 +11,31 @@ int main( int argc, char* argv[] ) osgViewer::Viewer viewer(arguments); - typedef std::list< osg::ref_ptr > Images; - Images images; + osgWidget::GeometryHints hints(osg::Vec3(0.0f,0.0f,0.0f), + osg::Vec3(1.0f,0.0f,0.0f), + osg::Vec3(0.0f,0.0f,1.0f), + osg::Vec4(1.0f,1.0f,1.0f,1.0f), + osgWidget::GeometryHints::RESIZE_HEIGHT_TO_MAINTAINCE_ASPECT_RATIO); + + osg::ref_ptr group = new osg::Group; for(int i=1; i image = osgDB::readImageFile(url_browser); - if (image.valid()) images.push_back(image.get()); + osg::ref_ptr browser = new osgWidget::Browser; + if (browser->open(arguments[i], hints)) + { + group->addChild(browser.get()); + + hints.position.x() += 1.1f; + } } } - bool xyPlane = false; - - osg::Group* group = new osg::Group; - - osg::Vec3 origin = osg::Vec3(0.0f,0.0f,0.0f); - for(Images::iterator itr = images.begin(); - itr != images.end(); - ++itr) - { - osg::Image* image = itr->get(); - float width = 1.0; - float height = float(image->t())/float(image->s()); - - osg::Vec3 widthAxis = osg::Vec3(width,0.0f,0.0f); - osg::Vec3 heightAxis = xyPlane ? osg::Vec3(0.0f,height,0.0f) : osg::Vec3(0.0f,0.0f,height); - - group->addChild(createInteractiveQuad(origin, widthAxis, heightAxis, image)); - - origin += widthAxis*1.1f; - } - - viewer.setSceneData(group); + viewer.setSceneData(group.get()); viewer.addEventHandler(new osgViewer::StatsHandler); - + return viewer.run(); } diff --git a/examples/osgpdf/osgpdf.cpp b/examples/osgpdf/osgpdf.cpp index dcbaf9532..a447d36ad 100644 --- a/examples/osgpdf/osgpdf.cpp +++ b/examples/osgpdf/osgpdf.cpp @@ -1,15 +1,6 @@ -#include -#include -#include -#include - #include #include -#include -#include -#include - #include int main(int argc,char** argv) @@ -17,7 +8,7 @@ int main(int argc,char** argv) osg::ArgumentParser arguments(&argc, argv); osgViewer::Viewer viewer(arguments); - osgWidget::GeometryHints hints(osg::Vec3(1.0f,0.0f,0.0f), + osgWidget::GeometryHints hints(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(1.0f,0.0f,0.0f), osg::Vec3(0.0f,0.0f,1.0f), osg::Vec4(1.0f,1.0f,1.0f,1.0f), diff --git a/examples/osgvnc/osgvnc.cpp b/examples/osgvnc/osgvnc.cpp index 21c49715b..c02794338 100644 --- a/examples/osgvnc/osgvnc.cpp +++ b/examples/osgvnc/osgvnc.cpp @@ -1,19 +1,8 @@ -#include -#include -#include - -#include - #include #include #include -#include -#include - -#include - class EscapeHandler : public osgGA::GUIEventHandler { public: @@ -49,7 +38,7 @@ int main(int argc,char** argv) osg::ArgumentParser arguments(&argc, argv); osgViewer::Viewer viewer(arguments); - osgWidget::GeometryHints hints(osg::Vec3(1.0f,0.0f,0.0f), + osgWidget::GeometryHints hints(osg::Vec3(0.0f,0.0f,0.0f), osg::Vec3(1.0f,0.0f,0.0f), osg::Vec3(0.0f,0.0f,1.0f), osg::Vec4(1.0f,1.0f,1.0f,1.0f), diff --git a/include/osgWidget/Browser b/include/osgWidget/Browser index 5b65c775e..61a8b3e0d 100644 --- a/include/osgWidget/Browser +++ b/include/osgWidget/Browser @@ -14,8 +14,7 @@ #ifndef OSGWIDGET_BROWSER #define OSGWIDGET_BROWSER -#include -#include +#include namespace osgWidget { @@ -33,7 +32,6 @@ class OSGWIDGET_EXPORT BrowserManager : public osg::Object void setApplication(const std::string& application) { _application = application; } const std::string& getApplication() const { return _application; } - virtual BrowserImage* createBrowserImage(const std::string& url); virtual BrowserImage* createBrowserImage(const std::string& url, int width, int height); protected: @@ -48,21 +46,43 @@ class OSGWIDGET_EXPORT BrowserManager : public osg::Object }; -class OSGWIDGET_EXPORT BrowserImage : public osg::Image +/** Pure virtual base class for that provides the browser inteface for integration with 3rd party implementations. + * Implementation of BrowserImage are provide via the gecko plugin.*/ +class BrowserImage : public osg::Image { public: - BrowserImage(); + BrowserImage() {} virtual void navigateTo(const std::string& url) = 0; protected: - virtual ~BrowserImage(); + virtual ~BrowserImage() {} }; +/** Convinience class that provides a interactive quad that can be placed directly in the scene.*/ +class OSGWIDGET_EXPORT Browser : public osg::Geode +{ + public: + + Browser() {} + + Browser(const std::string& url, const GeometryHints& hints = GeometryHints()); + + bool assign(BrowserImage* browserImage, const GeometryHints& hints = GeometryHints()); + + bool open(const std::string& url, const GeometryHints& hints = GeometryHints()); + + void navigateTo(const std::string& url); + + protected: + + osg::ref_ptr _browserImage; +}; + } #endif diff --git a/include/osgWidget/PdfReader b/include/osgWidget/PdfReader index f20dd456b..bfd7eb5c6 100644 --- a/include/osgWidget/PdfReader +++ b/include/osgWidget/PdfReader @@ -37,24 +37,35 @@ struct GeometryHints widthVec(1.0f,0.0f,0.0f), heightVec(0.0f,1.0f,0.0f), backgroundColor(1.0f,1.0f,1.0f,1.0f), - aspectRatioPolicy(RESIZE_HEIGHT_TO_MAINTAINCE_ASPECT_RATIO) {} + aspectRatioPolicy(RESIZE_HEIGHT_TO_MAINTAINCE_ASPECT_RATIO), + widthResolution(1024), + heightResolution(1024) {} GeometryHints(const osg::Vec3& pos, const osg::Vec3& wVec, const osg::Vec3& hVec, const osg::Vec4& bColor, - AspectRatioPolicy asp=RESIZE_HEIGHT_TO_MAINTAINCE_ASPECT_RATIO): + AspectRatioPolicy asp=RESIZE_HEIGHT_TO_MAINTAINCE_ASPECT_RATIO, + unsigned int wRes=1024, + unsigned int hRes=1024): position(pos), widthVec(wVec), heightVec(hVec), backgroundColor(bColor), - aspectRatioPolicy(asp) {} + aspectRatioPolicy(asp), + widthResolution(wRes), + heightResolution(hRes) {} - osg::Vec3 position; - osg::Vec3 widthVec; - osg::Vec3 heightVec; - osg::Vec4 backgroundColor; - AspectRatioPolicy aspectRatioPolicy; + osg::Vec3 position; + osg::Vec3 widthVec; + osg::Vec3 heightVec; + + osg::Vec4 backgroundColor; + + AspectRatioPolicy aspectRatioPolicy; + + unsigned int widthResolution; + unsigned int heightResolution; }; @@ -114,11 +125,11 @@ class OSGWIDGET_EXPORT PdfReader : public osg::Geode PdfReader() {} - PdfReader(const std::string& filename, GeometryHints hints = GeometryHints()); + PdfReader(const std::string& filename, const GeometryHints& hints = GeometryHints()); - bool assign(PdfImage* pdfImage, GeometryHints hints = GeometryHints()); + bool assign(PdfImage* pdfImage, const GeometryHints& hints = GeometryHints()); - bool open(const std::string& filename, GeometryHints hints = GeometryHints()); + bool open(const std::string& filename, const GeometryHints& hints = GeometryHints()); bool page(int pageNum); diff --git a/include/osgWidget/VncClient b/include/osgWidget/VncClient index cedb8af24..bb9f9129c 100644 --- a/include/osgWidget/VncClient +++ b/include/osgWidget/VncClient @@ -45,11 +45,11 @@ class OSGWIDGET_EXPORT VncClient : public osg::Geode VncClient() {} - VncClient(const std::string& hostname, GeometryHints hints = GeometryHints()); + VncClient(const std::string& hostname, const GeometryHints& hints = GeometryHints()); - bool assign(VncImage* vncImage, GeometryHints hints = GeometryHints()); + bool assign(VncImage* vncImage, const GeometryHints& hints = GeometryHints()); - bool connect(const std::string& hostname, GeometryHints hints = GeometryHints()); + bool connect(const std::string& hostname, const GeometryHints& hints = GeometryHints()); void close(); diff --git a/src/osgPlugins/gecko/ReaderWriterUBrowser.cpp b/src/osgPlugins/gecko/ReaderWriterUBrowser.cpp index 52b614ad1..dc9bee004 100644 --- a/src/osgPlugins/gecko/ReaderWriterUBrowser.cpp +++ b/src/osgPlugins/gecko/ReaderWriterUBrowser.cpp @@ -72,8 +72,10 @@ class ReaderWriterUBrowser : public osgDB::ReaderWriter osgWidget::BrowserManager::instance()->init(osgWidget::BrowserManager::instance()->getApplication()); } + unsigned int width = 1024; + unsigned int height = 1024; - return osgWidget::BrowserManager::instance()->createBrowserImage(osgDB::getNameLessExtension(file)); + return osgWidget::BrowserManager::instance()->createBrowserImage(osgDB::getNameLessExtension(file), width, height); } @@ -82,38 +84,15 @@ class ReaderWriterUBrowser : public osgDB::ReaderWriter osgDB::ReaderWriter::ReadResult result = readImage(fileName, options); if (!result.validImage()) return result; - osg::Image* image = result.getImage(); - - bool xyPlane = false; - bool flip = image->getOrigin()==osg::Image::TOP_LEFT; - osg::Vec3 origin = osg::Vec3(0.0f,0.0f,0.0f); - float width = 1.0; - float height = float(image->t())/float(image->s()); - osg::Vec3 widthAxis = osg::Vec3(width,0.0f,0.0f); - osg::Vec3 heightAxis = xyPlane ? osg::Vec3(0.0f,height,0.0f) : osg::Vec3(0.0f,0.0f,height); - - osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(origin, widthAxis, heightAxis, - 0.0f, flip ? 1.0f : 0.0f , 1.0f, flip ? 0.0f : 1.0f); - - osg::Texture2D* texture = new osg::Texture2D(image); - texture->setResizeNonPowerOfTwoHint(false); - texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); - texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); - texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); - - pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, - texture, - osg::StateAttribute::ON); - - osg::ref_ptr callback = new osgViewer::InteractiveImageHandler(image); - - pictureQuad->setEventCallback(callback.get()); - pictureQuad->setCullCallback(callback.get()); - - osg::Geode* geode = new osg::Geode; - geode->addDrawable(pictureQuad); - - return geode; + osg::ref_ptr browser = new osgWidget::Browser(); + if (browser->assign(dynamic_cast(result.getImage()))) + { + return browser.release(); + } + else + { + return osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED; + } } bool _initialized; diff --git a/src/osgPlugins/vnc/ReaderWriterVNC.cpp b/src/osgPlugins/vnc/ReaderWriterVNC.cpp index 5e3e56292..3df74f8f6 100644 --- a/src/osgPlugins/vnc/ReaderWriterVNC.cpp +++ b/src/osgPlugins/vnc/ReaderWriterVNC.cpp @@ -356,7 +356,6 @@ class ReaderWriterVNC : public osgDB::ReaderWriter osgDB::ReaderWriter::ReadResult result = readImage(fileName, options); if (!result.validImage()) return result; - osg::ref_ptr vncClient = new osgWidget::VncClient(); if (vncClient->assign(dynamic_cast(result.getImage()))) { diff --git a/src/osgWidget/Browser.cpp b/src/osgWidget/Browser.cpp index 514988db8..37f670b42 100644 --- a/src/osgWidget/Browser.cpp +++ b/src/osgWidget/Browser.cpp @@ -11,9 +11,13 @@ * OpenSceneGraph Public License for more details. */ +#include +#include +#include + #include -#include +#include using namespace osgWidget; @@ -39,21 +43,70 @@ void BrowserManager::init(const std::string& application) _application = application; } -BrowserImage* BrowserManager::createBrowserImage(const std::string& url) -{ - return createBrowserImage(url, 1024, 1024); -} - BrowserImage* BrowserManager::createBrowserImage(const std::string& url, int width, int height) { osg::notify(osg::NOTICE)<<"Cannot created browser"<getOrigin()==osg::Image::TOP_LEFT; + + float aspectRatio = (_browserImage->t()>0 && _browserImage->s()>0) ? float(_browserImage->t()) / float(_browserImage->s()) : 1.0; + + osg::Vec3 widthVec(hints.widthVec); + osg::Vec3 heightVec(hints.heightVec); + + switch(hints.aspectRatioPolicy) + { + case(GeometryHints::RESIZE_HEIGHT_TO_MAINTAINCE_ASPECT_RATIO): + heightVec *= aspectRatio; + break; + case(GeometryHints::RESIZE_WIDTH_TO_MAINTAINCE_ASPECT_RATIO): + widthVec /= aspectRatio; + break; + default: + // no need to adjust aspect ratio + break; + } + + osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(hints.position, widthVec, heightVec, + 0.0f, flip ? 1.0f : 0.0f , 1.0f, flip ? 0.0f : 1.0f); + + osg::Texture2D* texture = new osg::Texture2D(_browserImage.get()); + texture->setResizeNonPowerOfTwoHint(false); + texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); + texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); + texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); + + pictureQuad->getOrCreateStateSet()->setTextureAttributeAndModes(0, + texture, + osg::StateAttribute::ON); + + pictureQuad->setEventCallback(new osgViewer::InteractiveImageHandler(_browserImage.get())); + + addDrawable(pictureQuad); + + return true; +} + +bool Browser::open(const std::string& hostname, const GeometryHints& hints) +{ + osg::ref_ptr image = osgDB::readImageFile(hostname+".gecko"); + return assign(dynamic_cast(image.get()), hints); +} + +void Browser::navigateTo(const std::string& url) +{ + if (_browserImage.valid()) _browserImage->navigateTo(url); } diff --git a/src/osgWidget/PdfReader.cpp b/src/osgWidget/PdfReader.cpp index 20c4665fd..8dcb07b3a 100644 --- a/src/osgWidget/PdfReader.cpp +++ b/src/osgWidget/PdfReader.cpp @@ -21,12 +21,12 @@ using namespace osgWidget; -PdfReader::PdfReader(const std::string& filename, GeometryHints hints) +PdfReader::PdfReader(const std::string& filename, const GeometryHints& hints) { open(filename, hints); } -bool PdfReader::assign(PdfImage* pdfImage, GeometryHints hints) +bool PdfReader::assign(PdfImage* pdfImage, const GeometryHints& hints) { if (!pdfImage) return false; @@ -73,7 +73,7 @@ bool PdfReader::assign(PdfImage* pdfImage, GeometryHints hints) return true; } -bool PdfReader::open(const std::string& filename, GeometryHints hints) +bool PdfReader::open(const std::string& filename, const GeometryHints& hints) { osg::ref_ptr image = osgDB::readImageFile(filename); return assign(dynamic_cast(image.get()), hints); diff --git a/src/osgWidget/VncClient.cpp b/src/osgWidget/VncClient.cpp index 3cff10ae4..65ffae8d9 100644 --- a/src/osgWidget/VncClient.cpp +++ b/src/osgWidget/VncClient.cpp @@ -17,12 +17,12 @@ using namespace osgWidget; -VncClient::VncClient(const std::string& hostname, GeometryHints hints) +VncClient::VncClient(const std::string& hostname, const GeometryHints& hints) { connect(hostname, hints); } -bool VncClient::assign(VncImage* vncImage, GeometryHints hints) +bool VncClient::assign(VncImage* vncImage, const GeometryHints& hints) { if (!vncImage) return false; @@ -30,7 +30,25 @@ bool VncClient::assign(VncImage* vncImage, GeometryHints hints) bool flip = _vncImage->getOrigin()==osg::Image::TOP_LEFT; - osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(hints.position, hints.widthVec, hints.heightVec, + float aspectRatio = (_vncImage->t()>0 && _vncImage->s()>0) ? float(_vncImage->t()) / float(_vncImage->s()) : 1.0; + + osg::Vec3 widthVec(hints.widthVec); + osg::Vec3 heightVec(hints.heightVec); + + switch(hints.aspectRatioPolicy) + { + case(GeometryHints::RESIZE_HEIGHT_TO_MAINTAINCE_ASPECT_RATIO): + heightVec *= aspectRatio; + break; + case(GeometryHints::RESIZE_WIDTH_TO_MAINTAINCE_ASPECT_RATIO): + widthVec /= aspectRatio; + break; + default: + // no need to adjust aspect ratio + break; + } + + osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(hints.position, widthVec, heightVec, 0.0f, flip ? 1.0f : 0.0f , 1.0f, flip ? 0.0f : 1.0f); osg::Texture2D* texture = new osg::Texture2D(_vncImage.get()); @@ -50,10 +68,10 @@ bool VncClient::assign(VncImage* vncImage, GeometryHints hints) return true; } -bool VncClient::connect(const std::string& hostname, GeometryHints hints) +bool VncClient::connect(const std::string& hostname, const GeometryHints& hints) { osg::ref_ptr image = osgDB::readImageFile(hostname+".vnc"); - return assign(dynamic_cast(image.get())); + return assign(dynamic_cast(image.get()), hints); } void VncClient::close()