[Osggtk-devel] [osggtk - Developers] RE: Multiple Cameras; Same Scene; Indepent Cntrl
Status: Beta
Brought to you by:
rvinyard
From: SourceForge.net <no...@so...> - 2009-05-29 15:14:29
|
Read and respond to this message at: https://sourceforge.net/forum/message.php?msg_id=7416690 By: allensaucier777 Here's how I'm actually using it. This code, I hope, will show you the extra methods and functionality I added to further extend your code to better manipulate the manipulator camera. I've inclucde the subclass of CCompositeViewerGtkmm, called CCompositeViewerGtkmmExt and I've included my version of the Trackball Manipulator called CSceneMatrixManipulator ************ HEADER ******************* /** * ***************************************************************************** * \file CCompositeViewerGtkmmExt.h * \brief declaration for class CCompositeViewerGtkmmExt * * ***************************************************************************** */ #ifndef _CCompositeViewerGtkmmExt_ #define _CCompositeViewerGtkmmExt_ #include <gtk/gtk.h> #include <gtkmm.h> #include <libglademm.h> #include <gtkmm/window.h> #include <gtkmm/button.h> #include <gtkmm/menubar.h> #include <gtkmm/menu.h> #include <gtkmm/main.h> #include <glibmm.h> #include <glibmm/thread.h> #include <fstream> #include <iostream> #include "osgGtkmm.h" #include "CSceneMatrixManipulator.h" #include "CCompositeViewerGtkmm.h" namespace osgViewer { /** * *************************************************************************** * \brief This class extends CCompositeViewerGtkmm with methods that * allow the programER * to control the camera manipulator's camera, perspective, etc... * * *************************************************************************** */ class CCompositeViewerGtkmmExt : public CCompositeViewerGtkmm { // ////////////////////////////////////////////////////////////////// public:// CLASS TYPES/CONSTS/ENUMS etc... // ////////////////////////////////////////////////////////////////// /** * *********************************************************************** * \brief class used to track new cameras and their graphics windows as * pairs. * * The base elements of this class, m_pogcCamera & m_pgwGW are NOT * deallocated HERE because these are COPIES of the addresses of these * elements and they are DEALLOCATED ELSE WHERE. * *********************************************************************** */ class CViewAndGraphicsWindowGroup { public: static int g_iGROUP_COUNTER; public: CViewAndGraphicsWindowGroup(GraphicsWindowGtkmm *p_pgwGW, osgViewer::View *p_posvView, int &p_iGroup);///< ctor ~CViewAndGraphicsWindowGroup(){;}; ///< dtor GraphicsWindowGtkmm *m_pgwGetGraphicsWindow(){return m_pgwGW;}; osgViewer::View *m_pogcGetView(){return m_posvView;}; public: osgViewer::View *m_posvView;///< p-ointer oSgCamera; pointer to cam GraphicsWindowGtkmm *m_pgwGW; ///< pointer to graphics window int m_iGroup; ///< group this combination forms }; // class CViewAndGraphicsWindowGroup // ////////////////////////////////////////////////////////////////// public:// Instance Methods // ////////////////////////////////////////////////////////////////// CCompositeViewerGtkmmExt(); ///< ctor 1 CCompositeViewerGtkmmExt ( osg::ArgumentParser& arguments ); ///< ctor 2 CCompositeViewerGtkmmExt ( const osgViewer::CCompositeViewerGtkmmExt& viewer, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY ); ///< cptor virtual ~CCompositeViewerGtkmmExt(); ///< dtor osg::Camera *m_ocmGetMasterCamera(); ///< returns VIEWER's camera const osg::GraphicsContext *m_gcGetGraphicsContext() const;///< returns Viewer's graphics Context as const osg::GraphicsContext *m_gcGetGraphicsContext(); ///< returns Viewer's graphics context const osg::Viewport *m_vpGetViewport() const; ///< returns Viewer's viewport as const osg::Viewport *m_vpGetViewport(); ///< returns Viewer's viewport //osg::Camera *m_ocmNewCamInViewer(); ///< returns a "new" Camera that is instantiated w/in the Viewer by making a copy of the currnet Master Camera in the Viewer void m_vSetMasterCamHome(osg::Vec3d eye, osg::Vec3d focus, osg::Vec3d up); ///< susposed to set the Viewer's camera position - doesnt' work void m_vMoveMasterCamToHome(); ///< susposed to move the Viewer's camera to it's home position - don't remember if it works void m_vLockNodeSceneGraphWhileWorkingOnSceneNodes(); ///< Locks the Root Node from being traversed while an attempt is being made to add to it void m_vUnLockNodeSceneGraphWhileWorkingOnSceneNodes();///< UNLocks the Root Node after adding to it. bool isSameKindAs(const osg::Object* object) const; const char* libraryName() const; const char* className() const; /** set library name */ void m_vSetLibraryName(std::string &p_sLibName); /** set class name */ void m_vSetClassName(std::string &p_sClassName); /** sets the default home position */ void m_vSetNewCamHome(osg::Matrixd &p_mtxdHome); /** returns the new home position or identity matirx if not set */ osg::Matrixd m_mtxdGetNewCamHome(); /** get manipulator's camera's CURRENT position as a matrix */ osg::Matrixd m_mtxdGetManipulatorsCurrentCameraPos(); /** returns the default home position or identity matrix if not set */ osg::Matrixd m_mtxdGetDefaultHomeCamPosition(); /** used to move the manipulator camera to it's NEW HOME position */ void m_vMoveManipulatorCameraToNewHome(); /** used to move the manipulator camera to it's "NEW" Home Positoin */ void m_vMoveManipulatorCameraToNewHome(osg::Matrixd &p_mtxdHome); /** used to move the manipulator camera to it's "Original" Home Positoin */ void m_vMoveManipulatorCameraToDefaultHome(); /** Sets the PRIMARY View's manipulator camera's NEW HOME center */ void m_vSetManipulatorCamNewHomeCenter(osg::Vec3 &p_ov3Center); /** Sets the PRIMARY View's manipulator camer's NEW HOME distance */ void m_vSetmManipulatorCamNewHomeDistance(double p_dDistance); /** GETS the PRIMARY View's manipulator camera's NEW HOME center */ osg::Vec3 m_ov3GetManipulatorCamNewHomeCenter(); /** GETS the PRIMARY View's manipulator camera's NEW HOME Distance */ double m_dGetmManipulatorCamNewHomeDistance(); /** GETS the PRIMARY View's manipulator camera's CURRENT center */ osg::Vec3 m_ov3GetManipulatorCamerasCurrentCenter(); /** GETS the PRIMARY View's manipulator camera's CURRENT distance */ double m_ov3GetManipulatorCamerasCurrentDistance(); /** * *********************************************************************** * \brief returns info on cam position as doubles for easy printing. * rememmber arrays are ALWAYS passed by address. * * *********************************************************************** */ void m_vGetTransRotScaleOfCurrentManipulatorCamsPos( double p_dTrans[3], double p_dRotate[4], double p_dScale[3]); /** * *********************************************************************** * \brief returns a new Graphic Window that represents the viewport on the * "current" scene with a new camera. * * \param p_dWidth - width of window * \param p_dHeight - height of window * \param p_iGwCamGrp - group number of camera and graphics window (gw) * just added. * *********************************************************************** */ GraphicsWindowGtkmm* pgwAddNewCameraToScene(double p_dWidth, double p_dHeight, int &p_iGwCamGrp, osg::Node *p_pognScene); /** * *********************************************************************** * \brief Used to remove view and its associated graphics window from * the vector that tracks them. * * \param p_iGroup - group # of cam & gw to remove from list * \return true if succeeded, false otherwise * Description: * The View will be "deallocated" by the CompositeViewer through the use * of the "remove view" command. The Graphics Window is actually de- * allocated by Gtk. The only thing deallocated by this method is the * an element of type CViewAndGraphicsWindowGroup which populates the * vector. What's inside of the element is NOT deallocated by this * method. * *********************************************************************** */ bool vRemoveViewGwGroup(int p_iGroup); /** * \brief Simply removes all view/graphics window groups from vector */ void vRemoveAllViewGwGroups(); /** * ********************************************************************** * \brief Actually allocates a graphics window given the size stated * * \return a Graphics Window pointer to the graphics window allocated * ********************************************************************** */ GraphicsWindowGtkmm* setup_viewer_in_gtkmm_window(int width, int height, osg::Node *p_pognScene=NULL); /** * ********************************************************************** * \brief Sets teh Scene data for the Primary View only * * \return True if able to set, false otherwise * ********************************************************************** */ bool bSetPrimaryViewsSceneData(osg::Node *p_pognSceneData); /** * ********************************************************************** * \brief allows this method to be linked against by views that need * access to it. * * \return not used. * ********************************************************************** */ bool on_graphics_window_expose_event(GdkEventExpose* event); // /////////////////////////////////////////////////////////////// protected:// Instance Methods // /////////////////////////////////////////////////////////////// void m_vInitializeAttributes(); ///< used to initialize attributes that are not typically set through member initialization virtual int on_run_step(); // /////////////////////////////////////////////////////////////// protected:// Instance Attributes // /////////////////////////////////////////////////////////////// std::vector<CViewAndGraphicsWindowGroup *> m_vgwgViewGrpSets; ///< vector of pointers of type CViewAndGraphicsWindowGroup class std::vector<CViewAndGraphicsWindowGroup *>::iterator m_vgwgItr; Glib::Mutex m_gmtxNodeSceneMutex; ///< is mutex used to lock node scene graph when attempting to add to it. std::string m_sLibraryName; ///< this library's name std::string m_sClassName; ///< this class's name Glib::Mutex m_gmutCurrentCameraPosition;///< controls access to the camera position's matrix variable osg::Matrixd m_mtxCamerasCurrentPos; ///< Last Camera Position - camera is the manipulator's camera!!! This is the CURRENT POSITION!! osg::Vec3 m_ov3CameraCurrentCenter; ///< LAST Camear center - camera is the manipulator's camera!!! This is the CURRENT _center!! double m_dCamearCurrentDistance; ///< LAST Camear distanc - camera is the manipulator's camera!!! This is the CURRENT _distance!! osg::Matrixd m_mtxdDefaultHome; ///< default home position for Manipulator's Camera osg::Matrixd m_mtxdNewHomePos; ///< holds the "new" home position of the manipulator camera;used to restore cam to prev pos double m_dNewHomeDistance; ///< holds the "new" home _distance to be used by the camera manipulator! to restore cam to previous pos osg::Vec3 m_ov3NewHomeCenter; ///< holds the "new" home _center vector to be used by the camear manipulator! to restore cam to previous pos bool m_bDefaultCameraHomeSet; ///< tracks whether the default home position has been set or not bool m_bNewCameraHomeSet; ///< tracks whether a new home position has been set bool m_bMoveToCameraDefaultHome;///< tracks whether object has been told to move camera to home new position; home position is ASSUMEd to have been set... bool m_bMoveToNewCameraHome; ///< tracks whether object has been told to move camera to home position; home position is ASSUMEd to have been set.... bool m_bAllocatedPrimaryView; ///< tracks whether the primary view as been allocated and added to the Composite Viewer bool m_bSceneDataSetForPrimaryView; ///< tracks whether the scene data has been set for the primary view int m_iPrimaryViewsGroupNumber; ///< holds the primary view's group number }; // class CCompositeViewerGtkmmExt }; // ns osgViewer #endif *********** SOURCE ******************* /** * ***************************************************************************** * \file CCompositeViewerGtkmmExt.cpp * \brief implementation of class CCompositeViewerGtkmmExt * * ***************************************************************************** */ #include <CCompositeViewerGtkmmExt.h> namespace osgViewer { // CLASS ATTRIBUTES // int CCompositeViewerGtkmmExt::CViewAndGraphicsWindowGroup::g_iGROUP_COUNTER=0; // CLASS METHODS // CCompositeViewerGtkmmExt::CViewAndGraphicsWindowGroup::CViewAndGraphicsWindowG roup( GraphicsWindowGtkmm *p_pgwGW, osgViewer::View *p_posvView, int &p_iGroup) { m_pgwGW=p_pgwGW; m_posvView=p_posvView; // update group counter & return it p_iGroup=m_iGroup=g_iGROUP_COUNTER; g_iGROUP_COUNTER++; };// ctor // *************************************************************************** // *************************************************************************** // *************************************************************************** CCompositeViewerGtkmmExt::CCompositeViewerGtkmmExt(): CCompositeViewerGtkmm() { m_vInitializeAttributes(); } // ctor 1 //---------------------------------------------------------------------------- -- CCompositeViewerGtkmmExt::CCompositeViewerGtkmmExt( osg::ArgumentParser &arguments ) : CCompositeViewerGtkmm ( arguments ) { m_vInitializeAttributes(); } // ctor 2 //---------------------------------------------------------------------------- -- CCompositeViewerGtkmmExt::CCompositeViewerGtkmmExt ( const osgViewer::CCompositeViewerGtkmmExt &viewer, const osg::CopyOp ©op): CCompositeViewerGtkmm ( viewer, copyop ) { } // cptor //---------------------------------------------------------------------------- -- CCompositeViewerGtkmmExt::~CCompositeViewerGtkmmExt() { // Deallocate pointers to addresses of type CCameraAndGraphicsWindowGroup // but DON'T deallocate actual views and their graphics windows. vRemoveAllViewGwGroups(); std::cout<< "CCompositeViewerGtkmmExt::~CCompositeViewerGtkmmExt()" << std::endl; } // dtor //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::m_vInitializeAttributes() { std::cout<< "CCompositeViewerGtkmmExt::m_vInitializeAttributes: ctor"<< std::endl; m_bDefaultCameraHomeSet = false; m_bMoveToCameraDefaultHome= false; m_bNewCameraHomeSet = false; m_bMoveToNewCameraHome = false; m_dNewHomeDistance = 0.0; m_ov3NewHomeCenter.set(0.0, 0.0, 0.0); m_sLibraryName = "osgGtkmm"; m_sClassName = "CCompositeViewerGtkmmExt"; m_bAllocatedPrimaryView = false; m_bSceneDataSetForPrimaryView = false; m_iPrimaryViewsGroupNumber = -1; } // m_vInitializeAttributes //---------------------------------------------------------------------------- -- bool CCompositeViewerGtkmmExt::isSameKindAs(const osg::Object* object) const { return dynamic_cast<const CCompositeViewerGtkmmExt*>(object)!=0; } // isSameKindAs //---------------------------------------------------------------------------- -- const char* CCompositeViewerGtkmmExt::libraryName() const { return m_sLibraryName.c_str(); } // libraryName //---------------------------------------------------------------------------- -- const char* CCompositeViewerGtkmmExt::className() const { return m_sClassName.c_str(); } // className //---------------------------------------------------------------------------- -- osg::Camera * CCompositeViewerGtkmmExt::m_ocmGetMasterCamera() { if (!m_bAllocatedPrimaryView) return NULL; return m_osvPrimaryView->getCamera(); } // m_ocmGetMasterCamera //---------------------------------------------------------------------------- -- const osg::GraphicsContext * CCompositeViewerGtkmmExt::m_gcGetGraphicsContext() const { if (!m_bAllocatedPrimaryView) return NULL; return m_osvPrimaryView->getCamera()->getGraphicsContext(); } // m_gcGetGraphicsContext //---------------------------------------------------------------------------- -- osg::GraphicsContext * CCompositeViewerGtkmmExt::m_gcGetGraphicsContext() { if (!m_bAllocatedPrimaryView) return NULL; return m_osvPrimaryView->getCamera()->getGraphicsContext(); } // m_gcGetGraphicsContext //---------------------------------------------------------------------------- -- const osg::Viewport * CCompositeViewerGtkmmExt::m_vpGetViewport() const { if (!m_bAllocatedPrimaryView) return NULL; return m_osvPrimaryView->getCamera()->getViewport(); } // m_vpGetViewport //---------------------------------------------------------------------------- -- osg::Viewport * CCompositeViewerGtkmmExt::m_vpGetViewport() { if (!m_bAllocatedPrimaryView) return NULL; return m_osvPrimaryView->getCamera()->getViewport(); } // m_vpGetViewport //---------------------------------------------------------------------------- -- // osg::Camera * CCompositeViewerGtkmmExt::m_ocmNewCamInViewer() // { // return new osg::Camera(*m_ocmGetMasterCamera()); // } // m_ocmNewCamInViewer //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::m_vSetMasterCamHome(osg::Vec3d eye, osg::Vec3d focus, osg::Vec3d up) { if (!m_bAllocatedPrimaryView) return; m_osvPrimaryView->getCameraManipulator()->setHomePosition(eye,focus,up); } // m_vSetMasterCamHome //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::m_vMoveMasterCamToHome() { if (!m_bAllocatedPrimaryView) return; m_osvPrimaryView->getCameraManipulator()->home(1.0); //this->home(); } // m_vMoveMasterCamToHome //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::m_vSetLibraryName(std::string &p_sLibName) { m_sLibraryName=p_sLibName; }; //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::m_vSetClassName(std::string &p_sClassName) { m_sClassName=p_sClassName; }; //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::m_vSetNewCamHome(osg::Matrixd &p_mtxdHome) { m_mtxdNewHomePos = p_mtxdHome; m_bNewCameraHomeSet=true; }; //---------------------------------------------------------------------------- -- osg::Matrixd CCompositeViewerGtkmmExt::m_mtxdGetNewCamHome() { return (m_bNewCameraHomeSet ? m_mtxdNewHomePos : osg::Matrixd()); }; //---------------------------------------------------------------------------- -- osg::Matrixd CCompositeViewerGtkmmExt::m_mtxdGetManipulatorsCurrentCameraPos() { Glib::Mutex::Lock lck(m_gmutCurrentCameraPosition); return m_mtxCamerasCurrentPos; }; //---------------------------------------------------------------------------- -- osg::Matrixd CCompositeViewerGtkmmExt::m_mtxdGetDefaultHomeCamPosition() { return (m_bDefaultCameraHomeSet ? m_mtxdDefaultHome : osg::Matrixd()); }; //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::m_vMoveManipulatorCameraToNewHome() { m_bMoveToNewCameraHome=true; }; //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::m_vMoveManipulatorCameraToNewHome( osg::Matrixd &p_mtxdHome) { m_vSetNewCamHome(p_mtxdHome); m_bMoveToNewCameraHome=true; }; //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::m_vMoveManipulatorCameraToDefaultHome() { m_bMoveToCameraDefaultHome=true; }; //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::m_vSetManipulatorCamNewHomeCenter(osg::Vec3 &p_ov3Center) { m_ov3NewHomeCenter.set(p_ov3Center.x(), p_ov3Center.y(), p_ov3Center.z()); }; //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::m_vSetmManipulatorCamNewHomeDistance(double p_dDistance) { m_dNewHomeDistance=p_dDistance; }; //---------------------------------------------------------------------------- -- osg::Vec3 CCompositeViewerGtkmmExt::m_ov3GetManipulatorCamNewHomeCenter() { return m_ov3NewHomeCenter; }; //---------------------------------------------------------------------------- -- double CCompositeViewerGtkmmExt::m_dGetmManipulatorCamNewHomeDistance() { return m_dNewHomeDistance; }; //---------------------------------------------------------------------------- -- osg::Vec3 CCompositeViewerGtkmmExt::m_ov3GetManipulatorCamerasCurrentCenter() { Glib::Mutex::Lock lck(m_gmutCurrentCameraPosition); return m_ov3CameraCurrentCenter; }; //---------------------------------------------------------------------------- -- double CCompositeViewerGtkmmExt::m_ov3GetManipulatorCamerasCurrentDistance() { Glib::Mutex::Lock lck(m_gmutCurrentCameraPosition); return m_dCamearCurrentDistance; }; //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::m_vLockNodeSceneGraphWhileWorkingOnSceneNodes() { m_gmtxNodeSceneMutex.lock(); } // m_vLockNodeSceneGraphWhileWorkingOnSceneNodes //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::m_vUnLockNodeSceneGraphWhileWorkingOnSceneNodes () { m_gmtxNodeSceneMutex.unlock(); } // m_vUnLockNodeSceneGraphWhileWorkingOnSceneNodes //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::m_vGetTransRotScaleOfCurrentManipulatorCamsPos( double p_dTrans[3], double p_dRotate[4], double p_dScale[3]) { osg::Vec3d v3dT =m_mtxCamerasCurrentPos.getTrans(); osg::Vec3d v3dS =m_mtxCamerasCurrentPos.getScale(); osg::Quat q =m_mtxCamerasCurrentPos.getRotate(); p_dTrans[0]=v3dT.x(); p_dTrans[1]=v3dT.y(); p_dTrans[2]=v3dT.z(); p_dScale[0]=v3dS.x(); p_dScale[1]=v3dS.y(); p_dScale[2]=v3dS.z(); p_dRotate[0]=q.w(); p_dRotate[1]=q.x(); p_dRotate[2]=q.y(); p_dRotate[3]=q.z(); //std::cout<< "trans.x="<< v3d.x() << " trans.y="<< v3d.y() << " trans.z="<< v3d.z() << std::endl; //std::cout<< "scale.x="<< v3d2.x() << " scale.y="<< v3d2.y() << " scale.z="<< v3d2.z() << std::endl; //std::cout<< "q.x="<< q.x() << " q.y="<< q.y() << " q.z="<< q.z() << " q.w="<< q.w() std::endl; } // m_vGetTransRotScaleOfCurrentManipulatorCamsPos //---------------------------------------------------------------------------- -- GraphicsWindowGtkmm * CCompositeViewerGtkmmExt::setup_viewer_in_gtkmm_window( int width, int height, osg::Node *p_pognScene) { //alloc graphics window which graphics will be displayed in GraphicsWindowGtkmm* gw = new GraphicsWindowGtkmm ( width, height ); // alloc PRIMARY VIEW, add to CompositeViewer, indicate Primary View allocated m_osvPrimaryView = new osgViewer::View(); this->addView(m_osvPrimaryView); m_bAllocatedPrimaryView=true; // If have scene, use it. if (p_pognScene!=NULL) m_osvPrimaryView->setSceneData(p_pognScene); // alloc new viewport && set projection m_osvPrimaryView->getCamera()->setViewport( new osg::Viewport(0,0, width, height) ); m_osvPrimaryView->getCamera()->setProjectionMatrixAsPerspective ( 30.0f, static_cast<double> ( width ) /static_cast<double> ( height ), 1.0f, 10000.0f ); // Set graphics context of camera for Primary View m_osvPrimaryView->getCamera()->setGraphicsContext(gw); // Set camera manipulator to be of class CSceneMatrixManipulator m_osvPrimaryView->setCameraManipulator(new CSceneMatrixManipulator()); // setting a stateset manipulator but don't know why. osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator(); statesetManipulator->setStateSet(m_osvPrimaryView->getCamera()-> getOrCreateStateSet()); m_osvPrimaryView->addEventHandler(statesetManipulator.get()); // set graphics window callback to repaint scene if gw becomes covered gw->signal_expose_event().connect(sigc::mem_fun(*this, &CCompositeViewerGtkmmExt::on_graphics_window_expose_event)); // Track View & gw group so that can delete the "views" associated with // this CompositeViewer. int i_grp=0; CViewAndGraphicsWindowGroup *viewGrp = new CViewAndGraphicsWindowGroup(gw, m_osvPrimaryView.get(), i_grp); m_vgwgViewGrpSets.push_back(viewGrp); // return the graphics window return gw; } // setup_viewer_in_gtkmm_window //---------------------------------------------------------------------------- -- bool CCompositeViewerGtkmmExt::on_graphics_window_expose_event( GdkEventExpose * event) { //only called when cover/expose the osg graphics window if (_done) return false; if (_firstFrame) { viewerInit(); //std::cout<< "OsgViewerGtkmm:: graphics window initialized"<< std::endl; if (!isRealized()) { realize(); //std::cout<< "OsgViewerGtkmm:: graphics window realized"<< std::endl; } _firstFrame = false; } // This is the only piece that is different from ViewerBase::frame() // advance(simulationTime); Glib::Mutex::Lock lock(m_gmtxNodeSceneMutex); // waits for lock to occur before continuing eventTraversal(); updateTraversal(); renderingTraversals(); //std::cout<< "OsgViewerGtkmm:: graphics window updated"<< std::endl; return false; } // on_graphics_window_expose_event //---------------------------------------------------------------------------- -- int CCompositeViewerGtkmmExt::on_run_step() { //std::cout<< "CCompositeViewerGtkmm EXT::on_run_step()" << std::endl; // Should we stop or run one frame? or even be here if the Primary View for // this CompositeViewer has been allocated? if ( (m_run_to_frame_number and getViewerFrameStamp()->getFrameNumber() >= m_run_to_frame_number) || !m_bAllocatedPrimaryView) { this->stop(); return 0; } else { //Lock Node Tree so that update & redraw scene w/o worry of new nodes // being added or modified. //Glib::Mutex::Lock lock(m_gmtxNodeSceneMutex); if (m_bMoveToNewCameraHome) { std::cout<< "moved Camera To NEW Home"<< std::endl; // kept for posterity; & as example pos on ypg terrain //osg::Matrixd homePosMtxd( // 0.967571, -0.244315, -0.0641593, 0, // 0.14744, 0.340002, 0.928795, 0 , // -0.205104 , -0.908135, 0.364998 , 0 , // -2.1798e+06, -4.90492e+06, 3.44465e+06, 1); //this->getCameraManipulator()->setByMatrix(homePosMtxd); // set by matrix ((CSceneMatrixManipulator *)m_osvPrimaryView->getCameraManipulator())->setByMatrix(m_mtxdNewHomePos); // THIS CODE for setting the _center & _distance // // setting the _distance & _center AFTER setting the setByMatrix is // OK because w/in the setByMatrix method, what really matters // is the setting of the rotational matrix. even though _center // WILL be calculated w/ an incorrect _distance, it is also already // incorrect because setByMatrix calcluates _center using x==0 & // y==0, which are of NO value. // And setting _center to the correct "previous" value and setting // _distance to the correct previous value that corresponds to teh // rotational matrix, works! when doing it this way. // // Note: m_vSetCenter() & m_vSetDistance() are only available w/in // the CSceneMatrixManipulator class. ((CSceneMatrixManipulator *)m_osvPrimaryView->getCameraManipulator())->m_vSetCenter(m_ov3NewHomeCenter); ((CSceneMatrixManipulator *)m_osvPrimaryView->getCameraManipulator())->m_vSetDistance(m_dNewHomeDistance); m_bMoveToNewCameraHome = false; } // if move to cam's home pos else if (m_bMoveToCameraDefaultHome) { std::cout<< "moved Camera To DEF Home"<< std::endl; ((CSceneMatrixManipulator *)m_osvPrimaryView->getCameraManipulator())->setByMatrix(m_mtxdDefaultHome); m_bMoveToCameraDefaultHome=false; } // move to default home // Get current manipulator cam info EACH iteration of this scene loop // and set internal values accordingly // 1. byMatrix // 2. _center // 3. _distance //Glib::Mutex::Lock loc(m_gmutCurrentCameraPosition); //releaseed upon exiting this method // GET byMatrix m_mtxCamerasCurrentPos = ((CSceneMatrixManipulator *)m_osvPrimaryView->getCameraManipulator())->getMatrix(); // std::cout<< "CCompositeViewerGtkmmExt::on_run_step(): Getting camera Curr pos"<< std::endl; // for(int i=0; i<4; i++) // { // for (int j=0; j<4; j++) // std::cout << m_mtxCamerasCurrentPos(i,j) << " "; // std::cout<< std::endl; // } // std::cout<< std::endl; // GET _distance m_dCamearCurrentDistance = ((CSceneMatrixManipulator *)m_osvPrimaryView->getCameraManipulator())->m_dGetDistance(); // std::cout<< "\t_distance:= "<< // std::setiosflags(std::ios::fixed) << std::setprecision(3) << // m_dCamearCurrentDistance << std::endl; // GET _center osg::Vec3 ov3( ((CSceneMatrixManipulator *)m_osvPrimaryView->getCameraManipulator())->m_ov3GetCenter() ); m_ov3CameraCurrentCenter.set(ov3.x(), ov3.y(), ov3.z()); // std::cout<< "\t_center:= "<< // std::setiosflags(std::ios::fixed) << std::setprecision(4) << // m_ov3CameraCurrentCenter.x() <<","<< m_ov3CameraCurrentCenter.y() <<","<< m_ov3CameraCurrentCenter.z() << std::endl; // Step the frame this->frame(m_frame_step_rate); } // else-- run a frame return 1; } // on_run_step //---------------------------------------------------------------------------- -- GraphicsWindowGtkmm* CCompositeViewerGtkmmExt::pgwAddNewCameraToScene( double p_dWidth, double p_dHeight, int &p_iGwCamGrp, osg::Node *p_pognScene) { // Don't use auto_ptr for gw cuz don't want gw deallocated upon leaving // this method. Need to return a "VALID" pointer from this method. GraphicsWindowGtkmm* gw = new GraphicsWindowGtkmm ( p_dWidth, p_dHeight ); osg::ref_ptr<osgViewer::View> v= new osgViewer::View(); this->addView(v); v->setSceneData(p_pognScene); v->getCamera()->setViewport( new osg::Viewport(0,0, p_dWidth, p_dHeight) ); v->getCamera()->setProjectionMatrixAsPerspective ( 30.0f, static_cast<double> ( p_dWidth ) /static_cast<double> ( p_dHeight ), 1.0f, 10000.0f ); v->getCamera()->setGraphicsContext(gw); v->setCameraManipulator(new CSceneMatrixManipulator()); osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator(); statesetManipulator->setStateSet(v->getCamera()->getOrCreateStateSet()); v->addEventHandler(statesetManipulator.get()); gw->signal_expose_event().connect(sigc::mem_fun(*this, &CCompositeViewerGtkmmExt::on_graphics_window_expose_event)); int i_grp=0; CViewAndGraphicsWindowGroup *viewGrp = new CViewAndGraphicsWindowGroup(gw, v.get(), i_grp); m_vgwgViewGrpSets.push_back(viewGrp); return gw; } // pgwAddNewCameraToScene //---------------------------------------------------------------------------- -- bool CCompositeViewerGtkmmExt::vRemoveViewGwGroup(int p_iGroup) { if (!m_bAllocatedPrimaryView) return false; m_vgwgItr=m_vgwgViewGrpSets.begin(); for (unsigned ui=0; ui<m_vgwgViewGrpSets.size(); ui++, m_vgwgItr++) if (m_vgwgViewGrpSets[ui]->m_iGroup == p_iGroup) { this->removeView( m_vgwgViewGrpSets[ui]->m_posvView ); delete m_vgwgViewGrpSets[ui]; m_vgwgViewGrpSets.erase(m_vgwgItr); }// if found group return true; } // vRemoveViewGwGroup //---------------------------------------------------------------------------- -- void CCompositeViewerGtkmmExt::vRemoveAllViewGwGroups() { if (!m_bAllocatedPrimaryView) return; for (unsigned ui=0; ui<m_vgwgViewGrpSets.size(); ui++, m_vgwgItr++) { this->removeView(m_vgwgViewGrpSets[ui]->m_posvView); delete m_vgwgViewGrpSets[ui]; } } // vRemoveAllViewGwGroups //---------------------------------------------------------------------------- -- bool CCompositeViewerGtkmmExt::bSetPrimaryViewsSceneData( osg::Node *p_pognSceneData) { if (!m_bAllocatedPrimaryView) return false; m_osvPrimaryView->setSceneData(p_pognSceneData); m_bSceneDataSetForPrimaryView=true; return true; } // bSetPrimaryViewsSceneData //---------------------------------------------------------------------------- -- //---------------------------------------------------------------------------- -- //---------------------------------------------------------------------------- -- }; // ns osgViewer ************ Manipulator Camera ********* /** * **************************************************************************** * \file CSceneMatrixManipulator.h * \brief header for CSceneMatrixManipulator class * * **************************************************************************** */ #ifndef __CSceneMatrixManipulator__ #define __CSceneMatrixManipulator__ #include "osgHeaders.h" #include <osgGA/MatrixManipulator> #include <osgGA/TrackballManipulator> #include <osg/Quat> #include <iostream> #include <iomanip> #include <osgGA/GUIEventHandler> #include <osgGA/GUIEventAdapter> using namespace osg; using namespace osgGA; /** * **************************************************************************** * \class CSceneMatrixManipulator * \brief primary purpose is to allow the getting/setting of the _center and * _distance internal vars * * This class allows the positioning of the camera by setting the view matrix, * the _center and the _distance to "previously" known values or new ones. * **************************************************************************** */ class OSGGA_EXPORT CSceneMatrixManipulator : public osgGA::TrackballManipulator { // //////////////////////////////////////////////////////////////////// public:// Instance Methods // //////////////////////////////////////////////////////////////////// CSceneMatrixManipulator(); virtual void m_vSetDistance(double p_dDistance); virtual double m_dGetDistance(); virtual void m_vSetCenter(osg::Vec3 &p_ov3Center); virtual osg::Vec3 m_ov3GetCenter(); // ///////////////////////////////////////////////////////////////// protected:// Instance Methods // ///////////////////////////////////////////////////////////////// virtual ~CSceneMatrixManipulator(); virtual void computePosition(const osg::Vec3& eye,const osg::Vec3& center,const osg::Vec3& up); virtual bool calcMovement(); virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us); }; // class CSceneMatrixManipulator #endif //__CSceneMatrixManipulator__ ************ SOURCE ******************** /** * **************************************************************************** * \file CSceneMatrixManipulator.cpp * \brief definition for CSceneMatrixManipulator class * * **************************************************************************** */ #include "CSceneMatrixManipulator.h" #include <osgGA/TrackballManipulator> #include <osg/Quat> #include <osg/Notify> #include <osg/BoundsChecking> using namespace osg; using namespace osgGA; CSceneMatrixManipulator::CSceneMatrixManipulator() { } // ctor //------------------------------------------------------------------------------ CSceneMatrixManipulator::~CSceneMatrixManipulator() { std::cout<< "CSceneMatrixManipulator::~CSceneMatrixManipulator()" << std::endl; } // ctor //------------------------------------------------------------------------------ void CSceneMatrixManipulator::m_vSetDistance(double p_dDistance) { _distance = p_dDistance; } // m_vSetDistance //------------------------------------------------------------------------------ double CSceneMatrixManipulator::m_dGetDistance() { return _distance; } // m_dGetDistance //------------------------------------------------------------------------------ void CSceneMatrixManipulator::m_vSetCenter(osg::Vec3 &p_ov3Center) { _center.set (p_ov3Center.x(), p_ov3Center.y(), p_ov3Center.z()); } // m_vSetCenter //------------------------------------------------------------------------------ osg::Vec3 CSceneMatrixManipulator::m_ov3GetCenter() { return _center; } // m_ov3GetCenter //------------------------------------------------------------------------------ void CSceneMatrixManipulator::computePosition(const osg::Vec3& eye, const osg::Vec3& center,const osg::Vec3& up) { osg::Vec3 lv(center-eye); osg::Vec3 f(lv); f.normalize(); osg::Vec3 s(f^up); s.normalize(); osg::Vec3 u(s^f); u.normalize(); osg::Matrix rotation_matrix(s[0], u[0], -f[0], 0.0f, s[1], u[1], -f[1], 0.0f, s[2], u[2], -f[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f); _center = center; // std::cout<< "CSceneMatrixManipulator::computePosition*** ((***** computPos ***)) _center:= "<< // std::setiosflags(std::ios::fixed) << std::setprecision(4) << // _center.x() <<","<< _center.y() <<","<< _center.z() << std::endl; _distance = lv.length(); // std::cout<< "CSceneMatrixManipulator::computePosition*** _distance:= "<< // std::setiosflags(std::ios::fixed) << std::setprecision(3) << // _distance << std::endl; _rotation = rotation_matrix.getRotate().inverse(); } // computePosition //------------------------------------------------------------------------------ bool CSceneMatrixManipulator::calcMovement() { // mosue scroll is only a single event if (_ga_t0.get()==NULL) return false; float dx=0.0f; float dy=0.0f; unsigned int buttonMask=osgGA::GUIEventAdapter::NONE; if (_ga_t0->getEventType()==GUIEventAdapter::SCROLL) { dy = _ga_t0->getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_DOWN ? -0.1 : 0.1; buttonMask=GUIEventAdapter::SCROLL; } else { if (_ga_t1.get()==NULL) return false; dx = _ga_t0->getXnormalized()-_ga_t1->getXnormalized(); dy = _ga_t0->getYnormalized()-_ga_t1->getYnormalized(); float distance = sqrtf(dx*dx + dy*dy); // return if movement is too fast, indicating an error in event values // or change in screen. if (distance>0.5) { return false; } // return if there is no movement. if (distance==0.0f) { return false; } buttonMask = _ga_t1->getButtonMask(); } if (buttonMask==GUIEventAdapter::LEFT_MOUSE_BUTTON) { // rotate camera. osg::Vec3 axis; float angle; float px0 = _ga_t0->getXnormalized(); float py0 = _ga_t0->getYnormalized(); float px1 = _ga_t1->getXnormalized(); float py1 = _ga_t1->getYnormalized(); trackball(axis,angle,px1,py1,px0,py0); osg::Quat new_rotate; new_rotate.makeRotate(angle,axis); _rotation = _rotation*new_rotate; return true; } else if (buttonMask==GUIEventAdapter::MIDDLE_MOUSE_BUTTON || buttonMask== (GUIEventAdapter::LEFT_MOUSE_BUTTON|GUIEventAdapter::RIGHT_MOUSE_BUTTON)) { // pan model. float scale = -0.3f*_distance; osg::Matrix rotation_matrix; rotation_matrix.makeRotate(_rotation); osg::Vec3 dv(dx*scale,dy*scale,0.0f); _center += dv*rotation_matrix; // std::cout<< "CSceneMatrixManipulator::computePosition*** ((****PANNING****)) _center:= "<< // std::setiosflags(std::ios::fixed) << std::setprecision(4) << // _center.x() <<","<< _center.y() <<","<< _center.z() << std::endl; return true; } else if ((buttonMask==GUIEventAdapter::RIGHT_MOUSE_BUTTON) || (buttonMask==GUIEventAdapter::SCROLL)) { // zoom model. float fd = _distance; float scale = 1.0f+dy; if (fd*scale>_modelScale*_minimumZoomScale) { _distance *= scale; // std::cout<< "CSceneMatrixManipulator::computePosition*** ((****PANNING****)) _distance:= "<< // std::setiosflags(std::ios::fixed) << std::setprecision(4) << // _distance << std::endl; } else { // notify(DEBUG_INFO) << "Pushing forward"<<std::endl; // push the camera forward. float scale = -fd; osg::Matrix rotation_matrix(_rotation); osg::Vec3 dv = (osg::Vec3(0.0f,0.0f,-1.0f)*rotation_matrix)* (dy*scale); _center += dv; // std::cout<< "CSceneMatrixManipulator::computePosition*** ((***ZOOMING*** upd _center)) _center:= "<< // std::setiosflags(std::ios::fixed) << std::setprecision(4) << // _center.x() <<","<< _center.y() <<","<< _center.z() << std::endl; } // if fd*scale < return true; } // if bt msk == rt mouse or scroll return false; } // calcMovement //------------------------------------------------------------------------------ bool CSceneMatrixManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& us) { //std::cout<<"handle called"<< std::endl; switch(ea.getEventType()) { case(GUIEventAdapter::FRAME): if (_thrown) { if (calcMovement()) us.requestRedraw(); } return false; default: break; } if (ea.getHandled()) return false; switch(ea.getEventType()) { case(GUIEventAdapter::PUSH): { flushMouseEventStack(); addMouseEvent(ea); if (calcMovement()) us.requestRedraw(); us.requestContinuousUpdate(false); _thrown = false; return true; } case(GUIEventAdapter::RELEASE): { if (ea.getButtonMask()==0) { double timeSinceLastRecordEvent = _ga_t0.valid() ? (ea.getTime() - _ga_t0->getTime()) : DBL_MAX; if (timeSinceLastRecordEvent>0.02) flushMouseEventStack(); if (isMouseMoving()) { if (calcMovement()) { us.requestRedraw(); us.requestContinuousUpdate(true); _thrown = true; } } else { flushMouseEventStack(); addMouseEvent(ea); if (calcMovement()) us.requestRedraw(); us.requestContinuousUpdate(false); _thrown = false; } } else { flushMouseEventStack(); addMouseEvent(ea); if (calcMovement()) us.requestRedraw(); us.requestContinuousUpdate(false); _thrown = false; } return true; } case(GUIEventAdapter::DRAG): { addMouseEvent(ea); if (calcMovement()) us.requestRedraw(); us.requestContinuousUpdate(false); _thrown = false; return true; } case(GUIEventAdapter::MOVE): { return false; } case(GUIEventAdapter::KEYDOWN): if (ea.getKey()== GUIEventAdapter::KEY_Space) { flushMouseEventStack(); _thrown = false; home(ea,us); return true; } return false; case(GUIEventAdapter::FRAME): if (_thrown) { if (calcMovement()) us.requestRedraw(); } return false; default: return false; } return false; } // handle ______________________________________________________________________ You are receiving this email because you elected to monitor this forum. To stop monitoring this forum, login to SourceForge.net and visit: https://sourceforge.net/forum/unmonitor.php?forum_id=839184 |