|
楼主 |
发表于 2009-2-25 16:21:51
|
显示全部楼层
这是osgFrame.h
#pragma once
#include <osgProducer/Viewer>
#include <osgDB/ReadFile>
#include <CString>
#include <string>
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/MatrixTransform>
#include "TravelManipulator.h"
//此函数用于OSG与MFC进行交互
//MFC中不会涉及OSG的变量
class OSGFrame
{
public:
OSGFrame(void);
public:
~OSGFrame(void);
public:
//设置场景数据
void setSceneDat(CString);
void FirstOpenSetNode(void);
osg::Node* SetGeometry();
//渲染
bool renderFrame();
//对OSG进行初始化,需要一个句柄,来决定父窗口
void Initialized(HWND);
void setUpViewer(const std::string &);
//是否已经初始化
bool m_bInitialization;
bool m_bFirstOpen;
osg::ref_ptr<osgProducer::Viewer> m_pViewer;
};
这是cpp
#include "StdAfx.h"
#include "OSGFrame.h"
#include<osgText/Font>
#include<osgText/Text>
#include <osgProducer/Viewer>
#include "TravelManipulator.h"
OSGFrame::OSGFrame(void)
{
m_pViewer=new osgProducer::Viewer();
m_bInitialization=false;
m_bFirstOpen=true;
}
OSGFrame::~OSGFrame(void)
{//如果已经初始化且可用则析构
if (m_pViewer.valid()&&m_bInitialization)
{
m_pViewer->sync();
}
}
//初始化OSG设备
void OSGFrame::Initialized(HWND hwnd)
{
if (!m_bInitialization)
{//进行viewer的标设置,取销当按下ESC键程序退出功能
m_pViewer->setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS&~osgProducer::Viewer::ESCAPE_SETS_DONE);
m_pViewer->getCamera(0)->getRenderSurface()->fullScreen(false);
//设置viewer所使用的窗口
m_pViewer->getCamera(0)->getRenderSurface()->setWindow((const Producer::Window)hwnd);
//首先设置一个组的空间,如果是第一次需要打开一个默认的模型,则可以取消下面几行的注释添加FirstOpenSetNode函数
if (m_bFirstOpen)
{
FirstOpenSetNode();
}
else
{
m_pViewer->setSceneData(new osg::Group());
}
m_pViewer->realize(Producer::CameraGroup::SingleThreaded);
m_bInitialization=true;
}
renderFrame();
}
void OSGFrame::FirstOpenSetNode(void)
{
osg::Group* root=new osg::Group();
root->addChild(SetGeometry());
m_pViewer->setSceneData(root);
m_bFirstOpen=false;
}
osg::Node* OSGFrame::SetGeometry()
{
osg::Geode* geode = new osg::Geode;
float characterSize=100.0f;
osg::Vec3 pos(osg::Vec3(0,0,0));
osgText::Text* text=new osgText::Text;
text->setFont("simkai.ttf");
text->setCharacterSize(characterSize,1.0);
text->setPosition(pos);
text->setBackdropColor(osg::Vec4(0.0,1,0,1));
//text->setBackdropOffset(0.05);
text->setBackdropType(osgText::Text:ROP_SHADOW_BOTTOM_RIGHT);
text->setAxisAlignment(osgText::Text::SCREEN);
text->setAlignment(osgText::Text::CENTER_BOTTOM);
text->setCharacterSizeMode(osgText::Text::SCREEN_COORDS);
text->setText(L"虚拟演播室系统");
geode->addDrawable(text);
osg::Group* rootNode= new osg::Group;
rootNode->addChild(geode);
return (osg::Node*) rootNode;
}
void OSGFrame::setSceneDat(CString pathFileName)
{
osg::Group* root=new osg::Group();
osg::Node* pNode;
//从文件中读取Model,在代码后,将详解各字符类型间的转换,下面只是一种方法W TO A
USES_CONVERSION;
std::string str= W2A(pathFileName);
pNode = osgDB::readNodeFile (str);
//加入到根结点
root->addChild(pNode);
m_pViewer->sync();
if (!root==NULL)
{
m_pViewer->setSceneData(root);
m_pViewer->update();
m_pViewer->frame();
//选择像机,往往都会选择0(TraceBall)或者3(Driver)
//m_pViewer->selectCameraManipulator(0);
//m_pViewer->getKeySwitchMatrixManipulator()->getMatrixManipulatorWithIndex(0)->home(0.0);
}
}
//更新
bool OSGFrame::renderFrame()
{//渲染每一帧
if (m_bInitialization && m_pViewer.valid() && (m_pViewer->getSceneData() !=NULL))
{
TravelManipulator * travel = TravelManipulator::TravelScene (m_pViewer) ;
travel ->Active ();
m_pViewer->realize();
m_pViewer->sync();
m_pViewer->update();
m_pViewer->frame();
return true;
}
else
return false;
} |
|