diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index d8b87884f..2521251f4 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -108,6 +108,12 @@ IF(DYNAMIC_OPENSCENEGRAPH) ADD_SUBDIRECTORY(osgvolume) ADD_SUBDIRECTORY(osgwindows) + IF (POPPLER_FOUND AND CAIRO_FOUND) + ADD_SUBDIRECTORY(osgpdf) + ENDIF(POPPLER_FOUND AND CAIRO_FOUND) + + + IF (BUILD_OSG_WRAPPERS) ADD_SUBDIRECTORY(osgintrospection) ENDIF(BUILD_OSG_WRAPPERS) diff --git a/examples/osgpdf/CMakeLists.txt b/examples/osgpdf/CMakeLists.txt new file mode 100644 index 000000000..fdb3a11d6 --- /dev/null +++ b/examples/osgpdf/CMakeLists.txt @@ -0,0 +1,13 @@ +PKG_CHECK_MODULES(POPPLER poppler-glib) + +SET(TARGET_SRC osgpdf.cpp) + +INCLUDE_DIRECTORIES( ${CAIRO_INCLUDE_DIRS} ${POPPLER_INCLUDE_DIRS} ) +LINK_DIRECTORIES(${CAIRO_LIBRARY_DIRS} ${POPPLER_LIB_DIRS}) + +#SET(TARGET_EXTERNAL_LIBRARIES ${CAIRO_LIBRARIES} poppler poppler-glib) +SET(TARGET_EXTERNAL_LIBRARIES ${CAIRO_LIBRARIES} ${POPPLER_LIBRARIES}) + +#### end var setup ### +SETUP_EXAMPLE(osgpdf) + diff --git a/examples/osgpdf/osgpdf.cpp b/examples/osgpdf/osgpdf.cpp new file mode 100644 index 000000000..79b9b6031 --- /dev/null +++ b/examples/osgpdf/osgpdf.cpp @@ -0,0 +1,306 @@ +#include +#include +#include +#include + +#include +#include + +#include + +#include +#include + +class CarioImage : public osg::Image +{ + public: + + CarioImage(): + _surface(0), + _context(0) {} + + + void create(unsigned int width, unsigned int height) + { + if (data() && width==s() && height==t()) return; + + osg::notify(osg::NOTICE)<<"Create cario surface/context "<=getNumOfPages()) return false; + + PopplerPage* page = poppler_document_get_page(_doc, pageNum); + + if(!page) return false; + + _pageNum = pageNum; + + double w = 0.0f; + double h = 0.0f; + + poppler_page_get_size(page, &w, &h); + + create((unsigned int)(w),(unsigned int)(h)); + + double r = 1.0; + double g = 1.0; + double b = 1.0; + double a = 1.0; + + cairo_save(_context); + + cairo_set_source_rgba(_context, r, g, b, a); + cairo_rectangle(_context, 0.0, 0.0, w, h); + cairo_fill(_context); + + poppler_page_render(page, getContext()); + + cairo_restore(_context); + + dirty(); + + } + + +}; + + +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); + + pictureQuad->setEventCallback(new osgViewer::InteractiveImageHandler(image)); + + osg::Geode* geode = new osg::Geode; + geode->addDrawable(pictureQuad); + + return geode; +} + +class PageHandler : public osgGA::GUIEventHandler +{ + public: + + PageHandler() {} + + bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa) + { + if (ea.getHandled()) return false; + + switch(ea.getEventType()) + { + case(osgGA::GUIEventAdapter::KEYDOWN): + { + if (ea.getKey()=='n') + { + osg::notify(osg::NOTICE)<<"Next page"< > Images; + Images images; + + for(int i=1; i pdfImage= new PdfImage; + if (pdfImage->open(arguments[i])) + { + images.push_back(pdfImage.get()); + } + } + } + + 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.addEventHandler(new osgViewer::StatsHandler); + + //viewer.addEventHandler(new PageHandler); + + return viewer.run(); +} +