Cleaned up osgWidget::VncClient and osgWidget::Browser so that their implementations are all more consitent with the osgWidget::PdfReader.

This commit is contained in:
Robert Osfield 2008-12-09 11:05:04 +00:00
parent d8c17e4c4c
commit c8190d9c54
11 changed files with 169 additions and 162 deletions

View File

@ -1,50 +1,9 @@
#include <osg/Image>
#include <osg/Geometry>
#include <osg/Texture2D>
#include <osg/io_utils>
#include <osg/GraphicsThread>
#include <osgViewer/Viewer> #include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers> #include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgDB/FileUtils>
#include <osgDB/FileNameUtils>
#include <osgDB/ReadFile>
#include <osgWidget/Browser> #include <osgWidget/Browser>
int main(int argc,char** argv)
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<osgViewer::InteractiveImageHandler> 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[] )
{ {
osg::ArgumentParser arguments(&argc, argv); osg::ArgumentParser arguments(&argc, argv);
@ -52,41 +11,29 @@ int main( int argc, char* argv[] )
osgViewer::Viewer viewer(arguments); osgViewer::Viewer viewer(arguments);
typedef std::list< osg::ref_ptr<osg::Image> > Images; osgWidget::GeometryHints hints(osg::Vec3(0.0f,0.0f,0.0f),
Images images; 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<osg::Group> group = new osg::Group;
for(int i=1; i<arguments.argc(); ++i) for(int i=1; i<arguments.argc(); ++i)
{ {
if (!arguments.isOption(i)) if (!arguments.isOption(i))
{ {
std::string url_browser = std::string(arguments[i])+std::string(".gecko"); osg::ref_ptr<osgWidget::Browser> browser = new osgWidget::Browser;
osg::ref_ptr<osg::Image> image = osgDB::readImageFile(url_browser); if (browser->open(arguments[i], hints))
if (image.valid()) images.push_back(image.get()); {
group->addChild(browser.get());
hints.position.x() += 1.1f;
}
} }
} }
bool xyPlane = false; viewer.setSceneData(group.get());
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.addEventHandler(new osgViewer::StatsHandler); viewer.addEventHandler(new osgViewer::StatsHandler);

View File

@ -1,15 +1,6 @@
#include <osg/Image>
#include <osg/Geometry>
#include <osg/Texture2D>
#include <osg/io_utils>
#include <osgViewer/Viewer> #include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers> #include <osgViewer/ViewerEventHandlers>
#include <osgDB/FileUtils>
#include <osgDB/FileNameUtils>
#include <osgDB/ReadFile>
#include <osgWidget/PdfReader> #include <osgWidget/PdfReader>
int main(int argc,char** argv) int main(int argc,char** argv)
@ -17,7 +8,7 @@ int main(int argc,char** argv)
osg::ArgumentParser arguments(&argc, argv); osg::ArgumentParser arguments(&argc, argv);
osgViewer::Viewer viewer(arguments); 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(1.0f,0.0f,0.0f),
osg::Vec3(0.0f,0.0f,1.0f), osg::Vec3(0.0f,0.0f,1.0f),
osg::Vec4(1.0f,1.0f,1.0f,1.0f), osg::Vec4(1.0f,1.0f,1.0f,1.0f),

View File

@ -1,19 +1,8 @@
#include <osg/Image>
#include <osg/Geometry>
#include <osg/Texture2D>
#include <osgGA/TrackballManipulator>
#include <osgWidget/VncClient> #include <osgWidget/VncClient>
#include <osgViewer/Viewer> #include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers> #include <osgViewer/ViewerEventHandlers>
#include <iostream>
#include <osg/io_utils>
#include <osgDB/ReadFile>
class EscapeHandler : public osgGA::GUIEventHandler class EscapeHandler : public osgGA::GUIEventHandler
{ {
public: public:
@ -49,7 +38,7 @@ int main(int argc,char** argv)
osg::ArgumentParser arguments(&argc, argv); osg::ArgumentParser arguments(&argc, argv);
osgViewer::Viewer viewer(arguments); 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(1.0f,0.0f,0.0f),
osg::Vec3(0.0f,0.0f,1.0f), osg::Vec3(0.0f,0.0f,1.0f),
osg::Vec4(1.0f,1.0f,1.0f,1.0f), osg::Vec4(1.0f,1.0f,1.0f,1.0f),

View File

@ -14,8 +14,7 @@
#ifndef OSGWIDGET_BROWSER #ifndef OSGWIDGET_BROWSER
#define OSGWIDGET_BROWSER #define OSGWIDGET_BROWSER
#include <osg/Image> #include <osgWidget/PdfReader>
#include <osgWidget/Export>
namespace osgWidget { namespace osgWidget {
@ -33,7 +32,6 @@ class OSGWIDGET_EXPORT BrowserManager : public osg::Object
void setApplication(const std::string& application) { _application = application; } void setApplication(const std::string& application) { _application = application; }
const std::string& getApplication() const { return _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); virtual BrowserImage* createBrowserImage(const std::string& url, int width, int height);
protected: 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: public:
BrowserImage(); BrowserImage() {}
virtual void navigateTo(const std::string& url) = 0; virtual void navigateTo(const std::string& url) = 0;
protected: 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> _browserImage;
};
} }
#endif #endif

View File

@ -37,24 +37,35 @@ struct GeometryHints
widthVec(1.0f,0.0f,0.0f), widthVec(1.0f,0.0f,0.0f),
heightVec(0.0f,1.0f,0.0f), heightVec(0.0f,1.0f,0.0f),
backgroundColor(1.0f,1.0f,1.0f,1.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, GeometryHints(const osg::Vec3& pos,
const osg::Vec3& wVec, const osg::Vec3& wVec,
const osg::Vec3& hVec, const osg::Vec3& hVec,
const osg::Vec4& bColor, 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), position(pos),
widthVec(wVec), widthVec(wVec),
heightVec(hVec), heightVec(hVec),
backgroundColor(bColor), backgroundColor(bColor),
aspectRatioPolicy(asp) {} aspectRatioPolicy(asp),
widthResolution(wRes),
heightResolution(hRes) {}
osg::Vec3 position; osg::Vec3 position;
osg::Vec3 widthVec; osg::Vec3 widthVec;
osg::Vec3 heightVec; osg::Vec3 heightVec;
osg::Vec4 backgroundColor;
AspectRatioPolicy aspectRatioPolicy; osg::Vec4 backgroundColor;
AspectRatioPolicy aspectRatioPolicy;
unsigned int widthResolution;
unsigned int heightResolution;
}; };
@ -114,11 +125,11 @@ class OSGWIDGET_EXPORT PdfReader : public osg::Geode
PdfReader() {} 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); bool page(int pageNum);

View File

@ -45,11 +45,11 @@ class OSGWIDGET_EXPORT VncClient : public osg::Geode
VncClient() {} 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(); void close();

View File

@ -72,8 +72,10 @@ class ReaderWriterUBrowser : public osgDB::ReaderWriter
osgWidget::BrowserManager::instance()->init(osgWidget::BrowserManager::instance()->getApplication()); 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); osgDB::ReaderWriter::ReadResult result = readImage(fileName, options);
if (!result.validImage()) return result; if (!result.validImage()) return result;
osg::Image* image = result.getImage(); osg::ref_ptr<osgWidget::Browser> browser = new osgWidget::Browser();
if (browser->assign(dynamic_cast<osgWidget::BrowserImage*>(result.getImage())))
bool xyPlane = false; {
bool flip = image->getOrigin()==osg::Image::TOP_LEFT; return browser.release();
osg::Vec3 origin = osg::Vec3(0.0f,0.0f,0.0f); }
float width = 1.0; else
float height = float(image->t())/float(image->s()); {
osg::Vec3 widthAxis = osg::Vec3(width,0.0f,0.0f); return osgDB::ReaderWriter::ReadResult::FILE_NOT_HANDLED;
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<osgViewer::InteractiveImageHandler> callback = new osgViewer::InteractiveImageHandler(image);
pictureQuad->setEventCallback(callback.get());
pictureQuad->setCullCallback(callback.get());
osg::Geode* geode = new osg::Geode;
geode->addDrawable(pictureQuad);
return geode;
} }
bool _initialized; bool _initialized;

View File

@ -356,7 +356,6 @@ class ReaderWriterVNC : public osgDB::ReaderWriter
osgDB::ReaderWriter::ReadResult result = readImage(fileName, options); osgDB::ReaderWriter::ReadResult result = readImage(fileName, options);
if (!result.validImage()) return result; if (!result.validImage()) return result;
osg::ref_ptr<osgWidget::VncClient> vncClient = new osgWidget::VncClient(); osg::ref_ptr<osgWidget::VncClient> vncClient = new osgWidget::VncClient();
if (vncClient->assign(dynamic_cast<osgWidget::VncImage*>(result.getImage()))) if (vncClient->assign(dynamic_cast<osgWidget::VncImage*>(result.getImage())))
{ {

View File

@ -11,9 +11,13 @@
* OpenSceneGraph Public License for more details. * OpenSceneGraph Public License for more details.
*/ */
#include <osg/Notify>
#include <osgDB/ReadFile>
#include <osgViewer/ViewerEventHandlers>
#include <osgWidget/Browser> #include <osgWidget/Browser>
#include <osg/Notify> #include <osg/io_utils>
using namespace osgWidget; using namespace osgWidget;
@ -39,21 +43,70 @@ void BrowserManager::init(const std::string& application)
_application = 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) BrowserImage* BrowserManager::createBrowserImage(const std::string& url, int width, int height)
{ {
osg::notify(osg::NOTICE)<<"Cannot created browser"<<std::endl; osg::notify(osg::NOTICE)<<"Cannot created browser"<<std::endl;
return 0; return 0;
} }
BrowserImage::BrowserImage() Browser::Browser(const std::string& url, const GeometryHints& hints)
{ {
open(url, hints);
} }
BrowserImage::~BrowserImage() bool Browser::assign(BrowserImage* browserImage, const GeometryHints& hints)
{ {
if (!browserImage) return false;
_browserImage = browserImage;
bool flip = _browserImage->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<osg::Image> image = osgDB::readImageFile(hostname+".gecko");
return assign(dynamic_cast<BrowserImage*>(image.get()), hints);
}
void Browser::navigateTo(const std::string& url)
{
if (_browserImage.valid()) _browserImage->navigateTo(url);
} }

View File

@ -21,12 +21,12 @@
using namespace osgWidget; using namespace osgWidget;
PdfReader::PdfReader(const std::string& filename, GeometryHints hints) PdfReader::PdfReader(const std::string& filename, const GeometryHints& hints)
{ {
open(filename, hints); open(filename, hints);
} }
bool PdfReader::assign(PdfImage* pdfImage, GeometryHints hints) bool PdfReader::assign(PdfImage* pdfImage, const GeometryHints& hints)
{ {
if (!pdfImage) return false; if (!pdfImage) return false;
@ -73,7 +73,7 @@ bool PdfReader::assign(PdfImage* pdfImage, GeometryHints hints)
return true; return true;
} }
bool PdfReader::open(const std::string& filename, GeometryHints hints) bool PdfReader::open(const std::string& filename, const GeometryHints& hints)
{ {
osg::ref_ptr<osg::Image> image = osgDB::readImageFile(filename); osg::ref_ptr<osg::Image> image = osgDB::readImageFile(filename);
return assign(dynamic_cast<PdfImage*>(image.get()), hints); return assign(dynamic_cast<PdfImage*>(image.get()), hints);

View File

@ -17,12 +17,12 @@
using namespace osgWidget; using namespace osgWidget;
VncClient::VncClient(const std::string& hostname, GeometryHints hints) VncClient::VncClient(const std::string& hostname, const GeometryHints& hints)
{ {
connect(hostname, hints); connect(hostname, hints);
} }
bool VncClient::assign(VncImage* vncImage, GeometryHints hints) bool VncClient::assign(VncImage* vncImage, const GeometryHints& hints)
{ {
if (!vncImage) return false; if (!vncImage) return false;
@ -30,7 +30,25 @@ bool VncClient::assign(VncImage* vncImage, GeometryHints hints)
bool flip = _vncImage->getOrigin()==osg::Image::TOP_LEFT; 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); 0.0f, flip ? 1.0f : 0.0f , 1.0f, flip ? 0.0f : 1.0f);
osg::Texture2D* texture = new osg::Texture2D(_vncImage.get()); osg::Texture2D* texture = new osg::Texture2D(_vncImage.get());
@ -50,10 +68,10 @@ bool VncClient::assign(VncImage* vncImage, GeometryHints hints)
return true; return true;
} }
bool VncClient::connect(const std::string& hostname, GeometryHints hints) bool VncClient::connect(const std::string& hostname, const GeometryHints& hints)
{ {
osg::ref_ptr<osg::Image> image = osgDB::readImageFile(hostname+".vnc"); osg::ref_ptr<osg::Image> image = osgDB::readImageFile(hostname+".vnc");
return assign(dynamic_cast<VncImage*>(image.get())); return assign(dynamic_cast<VncImage*>(image.get()), hints);
} }
void VncClient::close() void VncClient::close()