|
楼主 |
发表于 2012-12-28 17:25:01
|
显示全部楼层
本帖最后由 1162810317 于 2012-12-29 20:48 编辑
#include "stdafx.h"
#include "MFC_OSG.h"
cOSG::cOSG(HWND hWnd) :
m_hWnd(hWnd)
{
}
cOSG::~cOSG()
{
mViewer->setDone(true);
Sleep(1000);
mViewer->stopThreading();
delete mViewer;
}
void cOSG::InitOSG()//改写了,原来为void cOSG::InitOSG(str::string modelname; )//
{
// Store the name of the model to load
// m_ModelName = modelname;原来的注释掉
// Init different parts of OSG
InitManipulators();
InitSceneGraph(m);
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()
{
// Init the main Root Node/Group
mRoot = new osg::Group;
// Load the Model from the model name
// mModel = osgDB::readNodeFile(m_ModelName);原来的注释掉
mModel =SetGeometry();//直接用自己的模型代替 // 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
mViewer = new osgViewer::Viewer();
// Add a Stats Handler to the viewer
mViewer->addEventHandler(new osgViewer::StatsHandler);
// 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;
traits->setInheritedWindowPixelFormat = true;
traits->inheritedWindowData = windata;
// Create the Graphics Context
osg::GraphicsContext* gc = osg::GraphicsContext::createGraphicsContext(traits.get());
// Init a new Camera (Master for this View)
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
// Assign Graphics Context to the Camera
camera->setGraphicsContext(gc);
// Set the viewport for the Camera
camera->setViewport(new osg::Viewport(traits->x, traits->y, traits->width, traits->height));
// 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);
// Add the Camera to the Viewer
//mViewer->addSlave(camera.get());
mViewer->setCamera(camera.get());
// Add the Camera Manipulator to the Viewer
mViewer->setCameraManipulator(keyswitchManipulator.get());
// Set the Scene Data
mViewer->setSceneData(mRoot.get());
// Realize the Viewer
mViewer->realize();
// Correct aspect ratio
/*double fovy,aspectRatio,z1,z2;
mViewer->getCamera()->getProjectionMatrixAsPerspective(fovy,aspectRatio,z1,z2);
aspectRatio=double(traits->width)/double(traits->height);
mViewer->getCamera()->setProjectionMatrixAsPerspective(fovy,aspectRatio,z1,z2);*/
}
void cOSG::Render(void* ptr)
{
cOSG* osg = (cOSG*)ptr;
osgViewer::Viewer* viewer = osg->getViewer();
// You have two options for the main viewer loop
// viewer->run() or
// while(!viewer->done()) { viewer->frame(); }
//viewer->run();
while(!viewer->done())
{
osg->reFrameUpdate();
viewer->frame();
osg->PostFrameUpdate();
//Sleep(10); // Use this command if you need to allow other processes to have cpu time
}
_endthread();
}
osg::Node *cOSG::SetGeometry()
{
osg::Group * root=new osg::Group ();//geomery->geode->root
osg::Geode * trianglegeode= new osg::Geode ();
osg::Geometry * trianglegeometry = new osg::Geometry ();
trianglegeode->addDrawable (trianglegeometry);
root->addChild (trianglegeode);//把三角形加入根节点中
//建立3个顶点
osg::Vec3Array * trianglevertices=new osg::Vec3Array ();
trianglevertices->push_back (osg::Vec3 (0,0,0));
trianglevertices->push_back (osg::Vec3 (20,10,0));
trianglevertices->push_back (osg::Vec3 (30,0,15));
//把顶点关联到三角形中
trianglegeometry->setVertexArray (trianglevertices);
//决定节点的组织方式
osg:rawElementsUInt * trianglebase=
new osg::DrawElementsUInt (osg:rimitiveSet ::TRIANGLES ,0);
trianglebase->push_back (2);
trianglebase->push_back (1);
trianglebase->push_back (0);
//下面来设置颜色,用四元组来存放,把三个顶点关联三个颜色
osg::Vec4Array * colors =new osg::Vec4Array ();
colors->push_back (osg::Vec4 (1.0f,0.0f,0.0f,1.0f));//红
colors->push_back (osg::Vec4 (0.0f,1.0f,0.0f,1.0f));//绿
colors->push_back (osg::Vec4 (0.0f,0.0f,1.0f,1.0f));//蓝
//建立颜色结点索引,该类是从ARRAY继承而来
osg::TemplateIndexArray <unsigned int,osg::Array ::UIntArrayType ,3,3> *colorindexarray;
colorindexarray=new osg::TemplateIndexArray<unsigned int,osg::Array ::UIntArrayType ,3,3>;
colorindexarray->push_back (0);
colorindexarray->push_back (1);
colorindexarray->push_back (2);
//设置颜色组
trianglegeometry->setColorArray (colors);
//设置索引
trianglegeometry->setColorIndices (colorindexarray);
//设置绑定方式
trianglegeometry->setColorBinding (osg::Geometry ::BIND_PER_VERTEX );
osg::MatrixTransform *trans=new osg::MatrixTransform ;
trans->setMatrix (osg::Matrix ::rotate (osg::DegreesToRadians (125.0),1.0,0.0,0.0));
//把物体旋转125度,围绕X轴,再次加入到场景中
//移动后再次加入几何体到根结点中,此时加入两个三角形
trans->addChild (trianglegeode);
root->addChild (trans);
return (osg::Node *)root;
}
// osgmfc_geoView.cpp : Cosgmfc_geoView 类的实现
//
#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "osgmfc_geo.h"
#endif
#include "osgmfc_geoDoc.h"
#include "osgmfc_geoView.h"
#include "MFC_OSG.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// Cosgmfc_geoView
IMPLEMENT_DYNCREATE(Cosgmfc_geoView, CView)
BEGIN_MESSAGE_MAP(Cosgmfc_geoView, CView)
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &Cosgmfc_geoView::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
// ON_WM_NCCREATE()
ON_WM_CREATE()
END_MESSAGE_MAP()
// Cosgmfc_geoView 构造/析构
Cosgmfc_geoView::Cosgmfc_geoView()
{
// TODO: 在此处添加构造代码
}
Cosgmfc_geoView::~Cosgmfc_geoView()
{
}
BOOL Cosgmfc_geoView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
return CView::PreCreateWindow(cs);
}
// Cosgmfc_geoView 绘制
void Cosgmfc_geoView::OnDraw(CDC* /*pDC*/)
{
Cosgmfc_geoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
}
void Cosgmfc_geoView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void Cosgmfc_geoView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// Cosgmfc_geoView 诊断
#ifdef _DEBUG
void Cosgmfc_geoView::AssertValid() const
{
CView::AssertValid();
}
void Cosgmfc_geoView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
Cosgmfc_geoDoc* Cosgmfc_geoView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(Cosgmfc_geoDoc)));
return (Cosgmfc_geoDoc*)m_pDocument;
}
#endif //_DEBUG
// Cosgmfc_geoView 消息处理程序
//BOOL Cosgmfc_geoView::OnNcCreate(LPCREATESTRUCT lpCreateStruct)
//{
// if (!CView::OnNcCreate(lpCreateStruct))
// return FALSE;
//
// // TODO: Add your specialized creation code here
// mOSG = new cOSG(m_hWnd);
// return TRUE;
//}
void Cosgmfc_geoView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
mOSG->InitSceneGraph (); //原本为mOSG->InitSceneGraph ("cow.osg"); 可以运行的
mThreadHandle = (HANDLE)_beginthread(&cOSG::Render,0,mOSG);
}
int Cosgmfc_geoView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
mOSG = new cOSG(m_hWnd);
return 0;
}
顺便问下,在《step into Osg》中,说到实例化 m_pViewer->realize(Producer::CameraGroup::SingleThreaded); 什么意思啊??
|
|