From 5162fcc1081228d10c58348e10f5be21f588c4e8 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 12 Sep 2011 10:47:59 +0000 Subject: [PATCH] From Wang Rui, "I've encountered a strange problem that osgviewerMFC doesn't work well with the StatsHandler. When the 's' key is pressed, the rendering window will be halted. I tried solving the problem by commenting a line in CMFC_OSG_MDIView::OnKeyDown() and it seems to work now. Another improvement here is to use a thread class derived from OpenThreads to replace the old _beginthread(). It helps a lot in keeping a high frame rate when you open more than one MDI child windows. And the application using OpenThreads in my opinion will be more compatible and portable." --- examples/osgviewerMFC/MFC_OSG.cpp | 32 ++++++++++++++++++++++- examples/osgviewerMFC/MFC_OSG.h | 15 ++++++++++- examples/osgviewerMFC/MFC_OSG_MDIView.cpp | 9 ++++--- examples/osgviewerMFC/MFC_OSG_MDIView.h | 3 ++- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/examples/osgviewerMFC/MFC_OSG.cpp b/examples/osgviewerMFC/MFC_OSG.cpp index 29fa7c561..56056b4f8 100644 --- a/examples/osgviewerMFC/MFC_OSG.cpp +++ b/examples/osgviewerMFC/MFC_OSG.cpp @@ -52,6 +52,7 @@ void cOSG::InitSceneGraph(void) // Load the Model from the model name mModel = osgDB::readNodeFile(m_ModelName); + if (!mModel) return; // Optimize the model osgUtil::Optimizer optimizer; @@ -141,7 +142,7 @@ void cOSG::PostFrameUpdate() // Due any postframe updates in this routine } -void cOSG::Render(void* ptr) +/*void cOSG::Render(void* ptr) { cOSG* osg = (cOSG*)ptr; @@ -166,4 +167,33 @@ void cOSG::Render(void* ptr) AfxMessageBox("Exit Rendering Thread"); _endthread(); +}*/ + +CRenderingThread::CRenderingThread( cOSG* ptr ) +: OpenThreads::Thread(), _ptr(ptr), _done(false) +{ +} + +CRenderingThread::~CRenderingThread() +{ + _done = true; + while( isRunning() ) + OpenThreads::Thread::YieldCurrentThread(); +} + +void CRenderingThread::run() +{ + if ( !_ptr ) + { + _done = true; + return; + } + + osgViewer::Viewer* viewer = _ptr->getViewer(); + do + { + _ptr->PreFrameUpdate(); + viewer->frame(); + _ptr->PostFrameUpdate(); + } while ( !testCancel() && !viewer->done() && !_done ); } diff --git a/examples/osgviewerMFC/MFC_OSG.h b/examples/osgviewerMFC/MFC_OSG.h index 011a2deba..933886d55 100644 --- a/examples/osgviewerMFC/MFC_OSG.h +++ b/examples/osgviewerMFC/MFC_OSG.h @@ -27,7 +27,7 @@ public: void PostFrameUpdate(void); void Done(bool value) { mDone = value; } bool Done(void) { return mDone; } - static void Render(void* ptr); + //static void Render(void* ptr); osgViewer::Viewer* getViewer() { return mViewer; } @@ -41,3 +41,16 @@ private: osg::ref_ptr trackball; osg::ref_ptr keyswitchManipulator; }; + +class CRenderingThread : public OpenThreads::Thread +{ +public: + CRenderingThread( cOSG* ptr ); + virtual ~CRenderingThread(); + + virtual void run(); + +protected: + cOSG* _ptr; + bool _done; +}; diff --git a/examples/osgviewerMFC/MFC_OSG_MDIView.cpp b/examples/osgviewerMFC/MFC_OSG_MDIView.cpp index 64be29736..e083e91e7 100644 --- a/examples/osgviewerMFC/MFC_OSG_MDIView.cpp +++ b/examples/osgviewerMFC/MFC_OSG_MDIView.cpp @@ -75,9 +75,10 @@ int CMFC_OSG_MDIView::OnCreate(LPCREATESTRUCT lpCreateStruct) void CMFC_OSG_MDIView::OnDestroy() { + delete mThreadHandle; if(mOSG != 0) delete mOSG; - WaitForSingleObject(mThreadHandle, 1000); + //WaitForSingleObject(mThreadHandle, 1000); CView::OnDestroy(); } @@ -93,13 +94,15 @@ void CMFC_OSG_MDIView::OnInitialUpdate() mOSG->InitOSG(csFileName.GetString()); // Start the thread to do OSG Rendering - mThreadHandle = (HANDLE)_beginthread(&cOSG::Render, 0, mOSG); + //mThreadHandle = (HANDLE)_beginthread(&cOSG::Render, 0, mOSG); + mThreadHandle = new CRenderingThread(mOSG); + mThreadHandle->start(); } void CMFC_OSG_MDIView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) { // Pass Key Presses into OSG - mOSG->getViewer()->getEventQueue()->keyPress(nChar); + //mOSG->getViewer()->getEventQueue()->keyPress(nChar); // Close Window on Escape Key if(nChar == VK_ESCAPE) diff --git a/examples/osgviewerMFC/MFC_OSG_MDIView.h b/examples/osgviewerMFC/MFC_OSG_MDIView.h index 2caf35d98..2f2fc1165 100644 --- a/examples/osgviewerMFC/MFC_OSG_MDIView.h +++ b/examples/osgviewerMFC/MFC_OSG_MDIView.h @@ -34,7 +34,8 @@ public: protected: cOSG* mOSG; - HANDLE mThreadHandle; + //HANDLE mThreadHandle; + CRenderingThread* mThreadHandle; // Generated message map functions protected: