2007-05-11 16:19:46 +08:00
|
|
|
// MFC_OSG.cpp : implementation of the cOSG class
|
|
|
|
//
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "MFC_OSG.h"
|
|
|
|
|
|
|
|
|
|
|
|
cOSG::cOSG(HWND hWnd) :
|
2014-11-04 18:46:59 +08:00
|
|
|
m_hWnd(hWnd)
|
2007-05-11 16:19:46 +08:00
|
|
|
{
|
2007-07-24 04:37:49 +08:00
|
|
|
}
|
2007-05-11 16:19:46 +08:00
|
|
|
|
2007-07-24 04:37:49 +08:00
|
|
|
cOSG::~cOSG()
|
|
|
|
{
|
|
|
|
mViewer->setDone(true);
|
|
|
|
Sleep(1000);
|
|
|
|
mViewer->stopThreading();
|
2007-05-11 16:19:46 +08:00
|
|
|
|
2007-07-24 04:37:49 +08:00
|
|
|
delete mViewer;
|
2007-05-11 16:19:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void cOSG::InitOSG(std::string modelname)
|
|
|
|
{
|
|
|
|
// Store the name of the model to load
|
|
|
|
m_ModelName = modelname;
|
|
|
|
|
|
|
|
// Init different parts of OSG
|
|
|
|
InitManipulators();
|
|
|
|
InitSceneGraph();
|
|
|
|
InitCameraConfig();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cOSG::InitManipulators(void)
|
|
|
|
{
|
|
|
|
// Create a trackball manipulator
|
|
|
|
trackball = new osgGA::TrackballManipulator();
|
|
|
|
|
|
|
|
// Create a Manipulator Switcher
|
|
|
|
keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
|
|
|
|
|
|
|
|
// Add our trackball manipulator to the switcher
|
|
|
|
keyswitchManipulator->addMatrixManipulator( '1', "Trackball", trackball.get());
|
|
|
|
|
|
|
|
// Init the switcher to the first manipulator (in this case the only manipulator)
|
|
|
|
keyswitchManipulator->selectMatrixManipulator(0); // Zero based index Value
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cOSG::InitSceneGraph(void)
|
|
|
|
{
|
|
|
|
// Init the main Root Node/Group
|
|
|
|
mRoot = new osg::Group;
|
|
|
|
|
|
|
|
// Load the Model from the model name
|
|
|
|
mModel = osgDB::readNodeFile(m_ModelName);
|
2011-09-12 18:47:59 +08:00
|
|
|
if (!mModel) return;
|
2007-05-11 16:19:46 +08:00
|
|
|
|
|
|
|
// Optimize the model
|
|
|
|
osgUtil::Optimizer optimizer;
|
|
|
|
optimizer.optimize(mModel.get());
|
|
|
|
optimizer.reset();
|
|
|
|
|
|
|
|
// Add the model to the scene
|
|
|
|
mRoot->addChild(mModel.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void cOSG::InitCameraConfig(void)
|
|
|
|
{
|
|
|
|
// Local Variable to hold window size data
|
|
|
|
RECT rect;
|
|
|
|
|
|
|
|
// Create the viewer for this window
|
2007-07-24 04:37:49 +08:00
|
|
|
mViewer = new osgViewer::Viewer();
|
2007-05-11 16:19:46 +08:00
|
|
|
|
|
|
|
// Add a Stats Handler to the viewer
|
|
|
|
mViewer->addEventHandler(new osgViewer::StatsHandler);
|
2014-11-04 18:46:59 +08:00
|
|
|
|
2007-05-11 16:19:46 +08:00
|
|
|
// Get the current window size
|
|
|
|
::GetWindowRect(m_hWnd, &rect);
|
|
|
|
|
|
|
|
// Init the GraphicsContext Traits
|
|
|
|
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
|
|
|
|
|
|
|
|
// Init the Windata Variable that holds the handle for the Window to display OSG in.
|
|
|
|
osg::ref_ptr<osg::Referenced> windata = new osgViewer::GraphicsWindowWin32::WindowData(m_hWnd);
|
|
|
|
|
|
|
|
// Setup the traits parameters
|
|
|
|
traits->x = 0;
|
|
|
|
traits->y = 0;
|
|
|
|
traits->width = rect.right - rect.left;
|
|
|
|
traits->height = rect.bottom - rect.top;
|
|
|
|
traits->windowDecoration = false;
|
|
|
|
traits->doubleBuffer = true;
|
|
|
|
traits->sharedContext = 0;
|
2007-07-24 04:37:49 +08:00
|
|
|
traits->setInheritedWindowPixelFormat = true;
|
2007-05-11 16:19:46 +08:00
|
|
|
traits->inheritedWindowData = windata;
|
|
|
|
|
|
|
|
// Create the Graphics Context
|
|
|
|
osg::GraphicsContext* gc = osg::GraphicsContext::createGraphicsContext(traits.get());
|
|
|
|
|
2013-09-09 20:37:52 +08:00
|
|
|
// Init Master Camera for this View
|
|
|
|
osg::ref_ptr<osg::Camera> camera = mViewer->getCamera();
|
2007-05-11 16:19:46 +08:00
|
|
|
|
|
|
|
// Assign Graphics Context to the Camera
|
|
|
|
camera->setGraphicsContext(gc);
|
|
|
|
|
|
|
|
// Set the viewport for the Camera
|
2018-06-19 00:33:03 +08:00
|
|
|
camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));
|
2007-05-11 16:19:46 +08:00
|
|
|
|
2018-03-07 00:54:57 +08:00
|
|
|
// set the draw and read buffers up for a double buffered window with rendering going to back buffer
|
|
|
|
camera->setDrawBuffer(GL_BACK);
|
|
|
|
camera->setReadBuffer(GL_BACK);
|
|
|
|
|
2010-06-14 23:25:05 +08:00
|
|
|
// Set projection matrix and camera attribtues
|
|
|
|
camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
|
|
|
camera->setClearColor(osg::Vec4f(0.2f, 0.2f, 0.4f, 1.0f));
|
|
|
|
camera->setProjectionMatrixAsPerspective(
|
|
|
|
30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0, 1000.0);
|
|
|
|
|
2007-05-11 16:19:46 +08:00
|
|
|
// Add the Camera to the Viewer
|
2010-06-14 23:25:05 +08:00
|
|
|
//mViewer->addSlave(camera.get());
|
|
|
|
mViewer->setCamera(camera.get());
|
2007-05-11 16:19:46 +08:00
|
|
|
|
|
|
|
// Add the Camera Manipulator to the Viewer
|
|
|
|
mViewer->setCameraManipulator(keyswitchManipulator.get());
|
|
|
|
|
|
|
|
// Set the Scene Data
|
|
|
|
mViewer->setSceneData(mRoot.get());
|
|
|
|
|
|
|
|
// Realize the Viewer
|
|
|
|
mViewer->realize();
|
2009-04-08 22:08:16 +08:00
|
|
|
|
|
|
|
// Correct aspect ratio
|
2010-06-14 23:25:05 +08:00
|
|
|
/*double fovy,aspectRatio,z1,z2;
|
2009-04-08 22:08:16 +08:00
|
|
|
mViewer->getCamera()->getProjectionMatrixAsPerspective(fovy,aspectRatio,z1,z2);
|
|
|
|
aspectRatio=double(traits->width)/double(traits->height);
|
2010-06-14 23:25:05 +08:00
|
|
|
mViewer->getCamera()->setProjectionMatrixAsPerspective(fovy,aspectRatio,z1,z2);*/
|
2007-05-11 16:19:46 +08:00
|
|
|
}
|
|
|
|
|
2007-05-17 19:04:57 +08:00
|
|
|
void cOSG::PreFrameUpdate()
|
|
|
|
{
|
|
|
|
// Due any preframe updates in this routine
|
|
|
|
}
|
|
|
|
|
|
|
|
void cOSG::PostFrameUpdate()
|
|
|
|
{
|
|
|
|
// Due any postframe updates in this routine
|
|
|
|
}
|
|
|
|
|
2011-09-12 18:47:59 +08:00
|
|
|
/*void cOSG::Render(void* ptr)
|
2007-05-11 16:19:46 +08:00
|
|
|
{
|
|
|
|
cOSG* osg = (cOSG*)ptr;
|
|
|
|
|
|
|
|
osgViewer::Viewer* viewer = osg->getViewer();
|
|
|
|
|
2007-05-17 19:04:57 +08:00
|
|
|
// You have two options for the main viewer loop
|
|
|
|
// viewer->run() or
|
|
|
|
// while(!viewer->done()) { viewer->frame(); }
|
|
|
|
|
|
|
|
//viewer->run();
|
|
|
|
while(!viewer->done())
|
|
|
|
{
|
|
|
|
osg->PreFrameUpdate();
|
|
|
|
viewer->frame();
|
|
|
|
osg->PostFrameUpdate();
|
|
|
|
//Sleep(10); // Use this command if you need to allow other processes to have cpu time
|
|
|
|
}
|
2007-05-11 16:19:46 +08:00
|
|
|
|
2014-11-04 18:46:59 +08:00
|
|
|
// For some reason this has to be here to avoid issue:
|
|
|
|
// if you have multiple OSG windows up
|
2007-05-11 16:19:46 +08:00
|
|
|
// and you exit one then all stop rendering
|
|
|
|
AfxMessageBox("Exit Rendering Thread");
|
2007-06-02 05:28:18 +08:00
|
|
|
|
2007-07-24 04:37:49 +08:00
|
|
|
_endthread();
|
2011-09-12 18:47:59 +08:00
|
|
|
}*/
|
|
|
|
|
|
|
|
CRenderingThread::CRenderingThread( cOSG* ptr )
|
|
|
|
: OpenThreads::Thread(), _ptr(ptr), _done(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CRenderingThread::~CRenderingThread()
|
|
|
|
{
|
|
|
|
_done = true;
|
2014-11-04 18:46:59 +08:00
|
|
|
if (isRunning())
|
|
|
|
{
|
|
|
|
cancel();
|
|
|
|
join();
|
|
|
|
}
|
2011-09-12 18:47:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
2007-05-17 19:04:57 +08:00
|
|
|
}
|