|
本帖最后由 nie 于 2014-4-3 14:35 编辑
想做一个飞机飞行时显示偏航角的刻度盘,先画好了表盘和表针,利用更新回调获取
飞机当前方向和目标方向,然后实现表盘的偏转,代码如下:
//创建表盘
osg::Geode* CreateClockFace ()
{
osg::Geode* clockGeode = new osg::Geode();
osg::Geometry* NumPoint = new osg::Geometry () ;
osg::ref_ptr <osg::Vec3Array > Num = new osg::Vec3Array ;
osg::Vec4Array* colors2 = new osg::Vec4Array ;
for (int j = -6; j <=6 ; j++)
{
double i = j*myPI/18;
colors2 ->push_back (osg::Vec4f (sin(i), cos(i), 0.5, 1.0)) ;
colors2 ->push_back (osg::Vec4f (cos(i), sin(i), 0.5, 1.0)) ;
if (j%3==0)
{
Num ->push_back (osg::Vec3 (180*sin(i) , 180*cos(i),-0.0)) ;
Num ->push_back (osg::Vec3 (220*sin(i) , 220*cos(i),-0.0)) ;
}
else
{
Num ->push_back (osg::Vec3 (180*sin(i) , 180*cos(i),-0.0)) ;
Num ->push_back (osg::Vec3 (200*sin(i) , 200*cos(i),-0.0)) ;
}
}
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array(1);
(*normals)[0].set( 0.0f,-1.0f, 0.0f );
NumPoint ->setVertexArray (Num) ;
NumPoint->addPrimitiveSet(new osg:rawArrays(osg:rimitiveSet:INES,0,26));
NumPoint->setNormalArray( normals.get() );
NumPoint->setNormalBinding( osg::Geometry::BIND_OVERALL );
NumPoint ->setColorArray(colors2);
NumPoint->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
NumPoint->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF|osg::StateAttribute::OVERRIDE);
clockGeode->addDrawable (NumPoint) ;
return clockGeode;
}
//实现表盘的更新回调
class SpeedShipCallback:public osg::NodeCallback
{
public:
SpeedShipCallback(osg::MatrixTransform* cessnaNode):m_cessnaNode(cessnaNode)
{
//osg::MatrixTransform* cessnaNode
}
virtual void operator()(osg::Node* node,osg::NodeVisitor* nv)
{
osg::ref_ptr<osg::MatrixTransform>mtCow = dynamic_cast<osg::MatrixTransform*>(node);
osg::Vec3 tarDir;//目标方向
osg::Vec3 curDir;//表盘当前方向
osg::Matrix mat;//姿态
osg::Vec3 tarPos;//目标位置
osg::Vec3 curPos;//飞机当前位置
tarPos = osg::Vec3(0.0f, 160.0f, 0.0f);
osg::Matrixd *pmatrix0 = getWorldCoords(m_cessnaNode);
curPos = pmatrix0->getTrans();
tarDir = tarPos - curPos;
curDir = mtCow->getMatrix().getRotate()*osg::Y_AXIS;
mat = osg::Matrix::rotate(curDir,tarDir);
mat.setTrans(mtCow->getMatrix().getTrans());
mtCow->setMatrix(mat);
//traverse(node,nv);
}
public:
osg::MatrixTransform* m_cessnaNode;//飞机节点
};
int main()
{
//添加飞机节点和运动路径
//将表盘加入场景,并添加跟新回调
//渲染场景
}
在运行时,表盘能够动起来,但是线条一直有重影,暂停时却没有了。
不知道什么原因,求大神指点。 |
|