|
#include <osg/NodeCallback>
#include <osg/PositionAttitudeTransform>
#include <osgViewer/Viewer>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgGA/TrackballManipulator>
int main()
{
osg::Node* groundNode = NULL;
osg::Node* tankNode = NULL;
osg::Group* root = new osg::Group();
osgViewer::Viewer viewer;
osg:ositionAttitudeTransform* tankXform ;
osg::Camera* manuallyPlaceCamera=new osg::Camera;
groundNode = osgDB::readNodeFile("\\Models\\JoeDirt\\JoeDirt.flt");
tankNode = osgDB::readNodeFile("\\Models\\T72-Tank\\T72-tank_des.flt");
// 创建绿色的天空布景。
osg::ClearNode* backdrop = new osg::ClearNode;
backdrop->setClearColor(osg::Vec4(0.0f,0.8f,0.0f,1.0f));
root->addChild(backdrop);
//添加地形模型
root->addChild(groundNode);
tankXform = new osg::PositionAttitudeTransform();
root->addChild(tankXform);
tankXform->addChild(tankNode);
tankXform->setPosition( osg::Vec3(10,10,8));
tankXform->setAttitude( osg:uat(osg:egreesToRadians(-45.0), osg::Vec3(0,0,1) ) );
osgGA::TrackballManipulator * Tman = new osgGA::TrackballManipulator();
viewer.setCameraManipulator(Tman);
osg::Matrixd myCameraMatrix;
osg::Matrixd cameraRotation;
osg::Matrixd cameraTrans;
cameraRotation.makeRotate(
osg::DegreesToRadians(-20.0), osg::Vec3(0,1,0), // 滚转角(Y轴)
osg::DegreesToRadians(-15.0), osg::Vec3(1,0,0) , // 俯仰角(X轴)
osg::DegreesToRadians( 10.0), osg::Vec3(0,0,1) ); // 航向角(Z轴)
// 相机位于坦克之后60个单元,之上7个单元。
cameraTrans.makeTranslate( 10,-50,15 );
myCameraMatrix = cameraRotation * cameraTrans;
viewer.setSceneData( root );
viewer.realize();
viewer.run();
while( !viewer.done() )
{
if (manuallyPlaceCamera)
{
osg::Matrixd i = myCameraMatrix.inverse(myCameraMatrix);
Tman->setByInverseMatrix( osg::Matrix(i.ptr() ) * osg::Matrix::rotate( -3.1415926/2.0, 1, 0, 0 ) );
}
viewer.frame();
}
}
帮忙看看,我想实现相机位于坦克之后60个单元,之上7个单元,但没什么效果,不知是哪里出错了 |
|