[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:09:18
|
Read and respond to this message at: https://sourceforge.net/forum/message.php?msg_id=7416679 By: allensaucier777 Fantastic! but I don't see a way to "attach" a file for you, so I'll post the code here in this message: ******** HEADER ************ /** * ************************************************************************* * \file CCompositeViewerGtkmm.h * \brief Header for the composite viewer gtkmm class * * * This class is based on Rick L. Vinyard, Jr. code for his osgGtk and * osgGtkmm classes. His classes use non-composite viewer. This class * is designed to use a composite viewer. * * ************************************************************************* */ #ifndef OSGVIEWER_CCOMPOSITE_CCompositeViewerGtkmm_H #define OSGVIEWER_CCOMPOSITE_CCompositeViewerGtkmm_H #include <iostream> #include <osgUtil/Optimizer> #include <osgDB/ReadFile> #include <osg/Material> #include <osg/Geode> #include <osg/BlendFunc> #include <osg/Depth> #include <osg/Projection> #include <osg/PolygonOffset> #include <osg/MatrixTransform> #include <osg/Camera> #include <osg/FrontFace> #include <osgText/Text> #include <osgGA/TrackballManipulator> #include <osgGA/FlightManipulator> #include <osgGA/StateSetManipulator> #include <osgViewer/ViewerEventHandlers> #include <osgViewer/CompositeViewer> #include <osgFX/Scribe> #include <osg/io_utils> #include <osgViewer/CompositeViewer> #include <osgGtkmm/GraphicsWindowGtkmm.h> /** * ***************************************************************************** * \namespace osgViewer * \brief namespace that is used to house all of the gtkmm Viewer classes * * ***************************************************************************** */ namespace osgViewer { /** * *************************************************************************** * \class CCompositeViewerGtkmm * \brief Extends OSG's Viewer with several Gtkmm specific aspects. * * In particular, the ability to establish this viewer in a gtkmm graphics * window with the convenience method setup_viewer_in_gtkmm_window(). * * Also reimplements run() to use the glibmm timer main loop * * author Rick L Vinyard Jr <rvi...@cs...> & Allen Saucier * * *************************************************************************** */ class CCompositeViewerGtkmm : public osgViewer::CompositeViewer { public: CCompositeViewerGtkmm(); ///< ctor 1 CCompositeViewerGtkmm ( osg::ArgumentParser& arguments ); ///< ctor 2 CCompositeViewerGtkmm ( const osgViewer::CCompositeViewerGtkmm& CCompositeViewerGtkmm, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY ); ///< cptor /** If running, stops the viewer */ virtual ~CCompositeViewerGtkmm(); ///< dtor virtual bool isSameKindAs(const osg::Object* object) const; virtual const char* libraryName() const; virtual const char* className() const; /** Sets up this viewer to run in a Gtkmm window of the specified width and height */ GraphicsWindowGtkmm* setup_viewer_in_gtkmm_window(int width, int height, osg::Node *p_pognScene); /** * Starts the viewer running at the currently set frame interval and priority * * Runs until stopped */ virtual int run(); /** Runs the viewer until the viewer's OSG frame stamp reaches @a frame_num */ int run_to_frame(int frame_num); /** Runs the viewer until the viewer's OSG frame stamp reaches the current frame stamp + @a frames */ int run_frames(int frames); /** Stops the viewer from running */ virtual void stop(); /** True if the viewer is running, false otherwise */ bool is_running(); /** Returns the frames-per-second rate based on the currently set frame interval */ double fps(); /** * Sets the frames per second update rate * * Since the update interval has a resolution of 1ms, the update interval * may not be exactly the same as the set rate. * * For example, if set_fps(60) is called the actual update rate will not * be 16.66667 ms (1000 ms / 60). Instead, the update rate will be the * floor of 1000/60 or 16ms. Thus, the actual frame rate will be 62.5 fps. */ void set_fps(double fps); /** Returns the frame interval in milliseconds that will be used when the viewer is running */ unsigned frame_interval_ms(); /** Sets the frame interval in milliseconds that will be used when the viewer is running */ void set_frame_interval_ms(unsigned interval); /** Returns the run priority that will be used when the viewer is running */ int run_priority(); /** Sets the run priority that will be used when the viewer is running */ void set_run_priority( int priority ); /** * Returns the frame step rate that will be used when the viewer is running. * This is the value that will be passed to osgViewer::Viewer::advance() and * controls the rate at which the viewer is advanced. */ double running_frame_step_rate(); /** Sets the frame step rate that will be used when the viewer is running. */ void set_running_frame_step_rate(double frame_rate); /** Returns the frame number that the viewer will run to */ int get_run_to_frame_number(); protected: bool m_is_running; unsigned m_frame_interval; int m_run_priority; double m_frame_step_rate; int m_run_to_frame_number; sigc::connection m_timer_connection;///< frame loop osg::ref_ptr<osgViewer::View> m_osvPrimaryView; ///< 1st view for composite viewer bool on_graphics_window_expose_event(GdkEventExpose* event); virtual int on_run_step(); void connect_timer(); virtual int run_implementation(); virtual void m_vInitialize(); private: /** Proxy callback point that passes the callback to the virtual on_run_step() */ int on_run_step_proxy(); }; } #endif ***************************** ********* SOURCE *********** /*************************************************************************** * Copyright (C) 2008 by Rick L. Vinyard, Jr. * * rvi...@cs... * * * * This file is part of the osgGTK library. * * * * The osgGTK library is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * version 3 as published by the Free Software Foundation. * * * * The osgGTK library is distributed in the hope that it will be * * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this software. If not see <http://www.gnu.org/licenses/>. * ***************************************************************************/ #include "CCompositeViewerGtkmm.h" #include <osgGA/TrackballManipulator> namespace osgViewer { CCompositeViewerGtkmm::CCompositeViewerGtkmm(): CompositeViewer(), m_is_running(false), m_frame_interval(30), m_run_priority(Glib::PRIORITY_DEFAULT), m_frame_step_rate(USE_REFERENCE_TIME), m_run_to_frame_number(0) { m_vInitialize(); } // ctor 1 CCompositeViewerGtkmm::CCompositeViewerGtkmm ( osg::ArgumentParser &arguments ): CompositeViewer ( arguments ), m_is_running(false), m_frame_interval(30), m_run_priority(Glib::PRIORITY_DEFAULT), m_frame_step_rate(USE_REFERENCE_TIME), m_run_to_frame_number(0) { m_vInitialize(); } // ctor 2 CCompositeViewerGtkmm::CCompositeViewerGtkmm ( const osgViewer::CCompositeViewerGtkmm & viewer, const osg::CopyOp & copyop ) : CompositeViewer ( viewer, copyop ), m_is_running(false), m_frame_interval(30), m_run_priority(Glib::PRIORITY_DEFAULT), m_frame_step_rate(USE_REFERENCE_TIME), m_run_to_frame_number(0) { } // cpctor CCompositeViewerGtkmm::~CCompositeViewerGtkmm() { if ( this->is_running() ) this->stop(); std::cout<< "CCompositeViewerGtkmm::~CCompositeViewerGtkmm()" << std::endl; } // dtor void CCompositeViewerGtkmm::m_vInitialize() { } // m_vInitialize bool CCompositeViewerGtkmm::isSameKindAs(const osg::Object* object) const { return dynamic_cast<const CCompositeViewerGtkmm*>(object)!=0; } const char* CCompositeViewerGtkmm::libraryName() const { return "osgGtkmm"; } const char* CCompositeViewerGtkmm::className() const { return "CCompositeViewerGtkmm"; } GraphicsWindowGtkmm * CCompositeViewerGtkmm::setup_viewer_in_gtkmm_window( int width, int height, osg::Node *p_pognScene ) { GraphicsWindowGtkmm* gw = new GraphicsWindowGtkmm ( width, height ); // this->setThreadingModel ( osgViewer::Viewer::SingleThreaded ); // this->getCamera()->setViewport ( 0, 0, width, height ); // this->getCamera()->setProjectionMatrixAsPerspective ( 30.0f, static_cast<double> ( width ) /static_cast<double> ( height ), 1.0f, 10000.0f ); // this->getCamera()->setGraphicsContext ( gw ); m_osvPrimaryView = new osgViewer::View(); this->addView(m_osvPrimaryView); m_osvPrimaryView->setSceneData(p_pognScene); 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 ); m_osvPrimaryView->getCamera()->setGraphicsContext(gw); m_osvPrimaryView->setCameraManipulator(new osgGA::TrackballManipulator()); osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator(); statesetManipulator->setStateSet(m_osvPrimaryView->getCamera()-> getOrCreateStateSet()); m_osvPrimaryView->addEventHandler(statesetManipulator.get()); gw->signal_expose_event().connect(sigc::mem_fun(*this, &CCompositeViewerGtkmm::on_graphics_window_expose_event)); return gw; } int CCompositeViewerGtkmm::run() { const char* str = getenv("OSG_RUN_FRAME_COUNT"); if (str) m_run_to_frame_number = atoi(str); else m_run_to_frame_number = 0; return this->run_implementation(); } int CCompositeViewerGtkmm::run_to_frame(int frame_num) { m_run_to_frame_number = frame_num; return this->run_implementation(); } int CCompositeViewerGtkmm::run_frames(int frames) { if (frames <= 0 ) return 0; m_run_to_frame_number = getViewerFrameStamp()->getFrameNumber() + frames; return this->run_implementation(); } void CCompositeViewerGtkmm::stop() { if ( m_timer_connection ) m_timer_connection.disconnect(); m_is_running = false; } bool CCompositeViewerGtkmm::is_running() { return m_is_running; } double CCompositeViewerGtkmm::fps() { return 1000.0 / m_frame_interval; } void CCompositeViewerGtkmm::set_fps(double fps) { if ( m_frame_interval == 1000.0 / fps ) return; m_frame_interval = 1000.0 / fps; if ( this->is_running() ) this->connect_timer(); } unsigned CCompositeViewerGtkmm::frame_interval_ms() { return m_frame_interval; } void CCompositeViewerGtkmm::set_frame_interval_ms(unsigned interval) { if ( m_frame_interval == interval ) return; m_frame_interval = interval; if ( this->is_running() ) this->connect_timer(); } int CCompositeViewerGtkmm::run_priority() { return m_run_priority; } void CCompositeViewerGtkmm::set_run_priority(int priority) { if ( m_run_priority == priority ) return; m_run_priority = priority; if ( this->is_running() ) this->connect_timer(); } double CCompositeViewerGtkmm::running_frame_step_rate() { return m_frame_step_rate; } void CCompositeViewerGtkmm::set_running_frame_step_rate(double frame_rate) { m_frame_step_rate = frame_rate; } int CCompositeViewerGtkmm::get_run_to_frame_number() { return m_run_to_frame_number; } bool CCompositeViewerGtkmm::on_graphics_window_expose_event(GdkEventExpose * event) { if (_done) return false; if (_firstFrame) { viewerInit(); if (!isRealized()) { realize(); } _firstFrame = false; } // This is the only piece that is different from ViewerBase::frame() // advance(simulationTime); eventTraversal(); updateTraversal(); renderingTraversals(); return false; } int CCompositeViewerGtkmm::on_run_step() { //std::cout<< "CCompositeViewerGtkmm::on_run_step()" << std::endl; // Should we stop or run one frame? if ( m_run_to_frame_number and getViewerFrameStamp()->getFrameNumber() >= m_run_to_frame_number) { this->stop(); return 0; } else { // Step the frame this->frame(m_frame_step_rate); } return 1; } int CCompositeViewerGtkmm::on_run_step_proxy() { return this->on_run_step(); } void CCompositeViewerGtkmm::connect_timer() { // Disconnect the old timer if ( m_timer_connection ) m_timer_connection.disconnect(); // Connect the new timer m_timer_connection = Glib::signal_timeout().connect(sigc::mem_fun( *this, &CCompositeViewerGtkmm::on_run_step_proxy), m_frame_interval, m_run_priority); } int CCompositeViewerGtkmm::run_implementation() { // Already have a camera manipulator for the Primary View //if (!getCameraManipulator() && getCamera()->getAllowEventFocus()) // setCameraManipulator(new osgGA::TrackballManipulator()); if (!isRealized()) realize(); m_is_running = true; this->connect_timer(); // If we didn't establish a connection to the timer, return // an error according to ViewerBase::run() semantics if ( not m_timer_connection ) return -1; return 0; } } ***************************** ______________________________________________________________________ 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 |