From c6fe8046dcb5e58a1675739d4cdccc9f1a029777 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 20 Aug 2009 14:20:55 +0000 Subject: [PATCH] Improved the custom cursor code and release and compute settings --- applications/present3D/present3D.cpp | 172 ++++++++++++++++++++++++++- 1 file changed, 168 insertions(+), 4 deletions(-) diff --git a/applications/present3D/present3D.cpp b/applications/present3D/present3D.cpp index 60be9cc97..ae478523b 100644 --- a/applications/present3D/present3D.cpp +++ b/applications/present3D/present3D.cpp @@ -13,14 +13,15 @@ #include #include #include +#include #include +#include #include #include #include #include -#include #include #include @@ -92,6 +93,169 @@ void setViewer(osgViewer::Viewer& viewer, float width, float height, float dista viewer.getCamera()->setProjectionMatrixAsPerspective( vfov, width/height, 0.1, 1000.0); } +#if 1 + +class RayFollowsMouseCallback : public osg::Drawable::EventCallback +{ + RayFollowsMouseCallback() {} + + /** do customized Event code. */ + virtual void event(osg::NodeVisitor* nv, osg::Drawable* drawable) + { + osg::Geometry* geometry = drawable->asGeometry(); + osgGA::EventVisitor* ev = dynamic_cast(nv); + + if (!ev || !geometry) return; + + osgGA::GUIActionAdapter* aa = ev->getActionAdapter(); + osgViewer::View* view = dynamic_cast(aa); + if (!view) return; + + osg::Vec3Array* vertices = dynamic_cast(geometry->getVertexArray()); + if (!vertices) return; + + osg::Camera* camera = view->getCamera(); + osg::Matrix VP = camera->getViewMatrix() * camera->getProjectionMatrix(); + + osg::Matrix inverse_VP; + inverse_VP.invert(VP); + + osgGA::EventQueue::Events& events = ev->getEvents(); + for(osgGA::EventQueue::Events::iterator itr = events.begin(); + itr != events.end(); + ++itr) + { + handle(inverse_VP, *(*itr), vertices); + } + + } + + void handle(const osg::Matrix& inverse_VP, osgGA::GUIEventAdapter& ea, osg::Vec3Array* vertices) + { + osg::Vec3d start_eye(ea.getXnormalized(), ea.getYnormalized(), 0.0); + osg::Vec3d end_eye(ea.getXnormalized(), ea.getYnormalized(), 1.0); + + osg::Vec3d start_world = start_eye * inverse_VP; + osg::Vec3d end_world = start_eye * inverse_VP; + + osg::notify(osg::NOTICE)<<"start_world="<addDrawable(geom); + + osg::AutoTransform* transform = new osg::AutoTransform; + transform->setAutoRotateMode(osg::AutoTransform::ROTATE_TO_CAMERA); + transform->setAutoScaleToScreen(true); + + transform->addChild(geode); + + transform->setEventCallback(new FollowMouseCallback()); + + return transform; + +} +#else class FollowMouseCallback: public osgGA::GUIEventHandler { public: @@ -163,7 +327,7 @@ osg::Node* createCursorSubgraph(const std::string& filename, float size) geode->addDrawable(geom); - osg::CameraNode* camera = new osg::CameraNode; + osg::Camera* camera = new osg::Camera; // set the projection matrix camera->setProjectionMatrix(osg::Matrix::ortho2D(-1,1,-1,1)); @@ -185,6 +349,7 @@ osg::Node* createCursorSubgraph(const std::string& filename, float size) return camera; } +#endif enum P3DApplicationType @@ -384,8 +549,7 @@ int main( int argc, char **argv ) { // make sure that imagery stays around after being applied to textures. viewer.getDatabasePager()->setUnrefImageDataAfterApplyPolicy(true,false); - // optimizer_options &= ~osgUtil::Optimizer::OPTIMIZE_TEXTURE_SETTINGS; - // viewer.setRealizeSceneViewOptions(viewer.getRealizeSceneViewOptions() & ~osgUtil::SceneView::COMPILE_GLOBJECTS_AT_INIT); + optimizer_options &= ~osgUtil::Optimizer::OPTIMIZE_TEXTURE_SETTINGS; } // // osgDB::Registry::instance()->getOrCreateDatabasePager()->setUnrefImageDataAfterApplyPolicy(true,false);