|
// 创建一个追踪求操作器
trackball = new osgGA::TrackballManipulator();
//创建一个调度操作器
keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
// 添加追踪球操作器到调度操作器中
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
// Local Variable to hold window size data
RECT rect;
// 创建一个视图
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));
// Add the Camera to the Viewer
mViewer->addSlave(camera.get());
// Add the Camera Manipulator to the Viewer
mViewer->setCameraManipulator(keyswitchManipulator.get());
osg::ref_ptr<osg::Group> root = new osg::Group();
osgText::Text* updatetext = new osgText::Text();
CreateHUD *hudText= new CreateHUD();
root->addChild(hudText->createHUD(updatetext));
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//HUD node
osg::Camera* hudCamera = new osg::Camera;
hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
hudCamera->setProjectionMatrixAsOrtho2D(0,1280,0,1024);//(0,18,0,14)
hudCamera->setViewMatrix(osg::Matrix::identity());
hudCamera->setRenderOrder(osg::Camera:OST_RENDER);
hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
osg::ref_ptr<osg::Node> node;
osg::PositionAttitudeTransform* transform = new osg::PositionAttitudeTransform;
transform->addChild(node);
root->addChild(transform);
//hudCamera->addChild(transform);
transform->setScale(osg::Vec3(1.0,1.0,1.0));
transform->setPosition(osg::Vec3(0.0,0.0,0.0));
osg:uat rotation;
rotation.makeRotate(osg:egreesToRadians(0.0f), osg::Vec3(1.0f,0.0f,0.0f),
osg::DegreesToRadians(0.0f), osg::Vec3(0.0f, 1.0f, 0.0f),
osg::DegreesToRadians(0.0f), osg::Vec3(0.0f, 0.0f, 1.0f));
transform->setAttitude(rotation);
//HUD node
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
osgUtil::Optimizer optimizer ;
optimizer.optimize(root) ;
// Set the Scene Data
mViewer->setSceneData(root.get());
// Realize the Viewer
mViewer->realize();
}
|
|