根据我的搭建经验,总结出一点非常有用的经验:
1.借鉴osg库自身携带的example例子中MFC的搭建例子。
然后对该框架进行一个个人的总结,希望有问题大家可以指出。

如图1,在创建好MFC单文档框架的前提下,新建一个.h文件并且在该.h文件中新建一个类,该类代表OSG类,在osg类中,我们要实现场景的初始化功能以及渲染功能。具体的表现形式如下。
#pragma once
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgViewer/api/win32/GraphicsWindowWin32>
#include <osgGA/TrackballManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgDB/DatabasePager>
#include <osgDB/Registry>
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include"UP_KXB.h"
#include <string>
class cOSG
{
public:
cOSG(HWND hWnd);
~cOSG();
void InitOSG(std::string filename);
void InitManipulators(void);
void InitSceneGraph(void);
void InitCameraConfig(void);
void SetupWindow(void);
void SetupCamera(void);
void PreFrameUpdate(void);
void PostFrameUpdate(void);
void Done(bool value) { mDone = value; }
bool Done(void) { return mDone; }
static void Render(void* ptr);
osgViewer::Viewer* getViewer() { return mViewer; }
//关于空心板的相关绘制函数
private:
bool mDone;
std::string m_ModelName;
HWND m_hWnd;
osgViewer::Viewer* mViewer;
osg::ref_ptr<osg::Group> mRoot;
osg::ref_ptr<osg::Node> mModel;
osg::ref_ptr<osgGA::TrackballManipulator> trackball;
osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator;
};在完成osg类的各个功能后,我们要修改MFC框架下的东西。
1.在view中,添加osg对象,并且在view构造时,实现osg对象的创建。这里有一个注意点就是:osg需要开启另外的线程。所以在view中添加对象如下:
cOSG* mOSG;
HANDLE mThreadHandle;2.在view的OnInitialUpdate()函数中,我们是实现osg场景的初始化以及开启另外的线程如下:
void CosgframeView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// Get Filename from DocumentOpen Dialog
CString csFileName = GetDocument()->GetFileName();
std::string ss = CW2A(csFileName.GetString());
// Init the osg class
mOSG->InitOSG(ss);
// Start the thread to do OSG Rendering
mThreadHandle = (HANDLE)_beginthread(&cOSG::Render, 0, mOSG);
}3.此时不要忘记在view的销毁函数中添加析构osg对象的功能。这样就能的保证一个对象从创建到销毁的完整生命周期。防止内存泄漏。
if (mOSG != 0) delete mOSG;
WaitForSingleObject(mThreadHandle, 1000);到此,一个osg+MFC的框架就搭建好了,但这只是一个简陋的框架,在这个框架内绘制什么模型,怎么调整这个显示窗口都是后续需要继续深入了解的。
761

被折叠的 条评论
为什么被折叠?



