OSG osg::MatrixTransform
OSG osg::MatrixTransform
我试图在我的应用程序中绘制平视显示器(HUD)内的指南针。我已经成功地将场景中的指南针渲染为纹理几何体。问题是,当我尝试使用osg::MatrixTransfrom和osg::Matrix类旋转指南针时,它会将指南针转换到场景中的错误位置。下面是代码:
osg::ref_ptr<osg::Node> CustomHUD::DrawCompass(void)
{
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
vertices->push_back( osg::Vec3(CENTERX-150.0f,0.0f,0.0f));
vertices->push_back( osg::Vec3(CENTERX-150.0f,300.0f,0.0f));
vertices->push_back( osg::Vec3(CENTERX+150.0f,300.0f,0.0f));
vertices->push_back( osg::Vec3(CENTERX+150.0f,0.0f,0.0f));
osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
normals->push_back( osg::Vec3(0.0f,0.0f,-1.0f));
osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array;
texcoords->push_back(osg::Vec2(0.0f,0.0f));
texcoords->push_back(osg::Vec2(0.0f,1.0f));
texcoords->push_back(osg::Vec2(1.0f,1.0f));
texcoords->push_back(osg::Vec2(1.0f,0.0f));
osg::ref_ptr<osg::Geometry> quad = new osg::Geometry;
quad->setVertexArray(vertices.get());
quad->setNormalArray(normals.get());
quad->setNormalBinding(osg::Geometry::BIND_OVERALL);
quad->setTexCoordArray(0,texcoords.get());
quad->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
osg::ref_ptr<osg::Image> image = osgDB::readImageFile("/home/ttc/Pictures compassfinal2.png");
texture->setImage(image.get());
//texture->setTextureSize(500,500);
osg::ref_ptr<osg::Group> mainroot = new osg::Group;
osg::ref_ptr<osg::Geode> drawing2 = new osg::Geode;
drawing2->addDrawable(quad.get());
drawing2->getOrCreateStateSet()->setTextureAttributeAndModes(0,texture.get());
drawing2->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
drawing2->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::ON);
drawing2->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);
vtTransform *transnode = new vtTransform;
osg::ref_ptr<osg::MatrixTransform> transnodeosg = new osg::MatrixTransform;
transnodeosg->setMatrix(osg::Matrix::rotate(-0.9,osg::Vec3f(0.0,0.0,1.0)));
transnode->SetOsgTransform(transnodeosg);
transnode->addChild(drawing2);
mainroot->addChild(transnode);
return mainroot.get();
}
osg::Camera* CustomHUD::CreateHUD()
{
// create a camera to set up the projection and model view matrices, and thesubgraph to draw in the HUD
osg::Camera* camera = new osg::Camera;
// set the projection matrix
camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1280,0,1024));
// set the view matrix
camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
camera->setViewMatrix(osg::Matrix::identity());
// only clear the depth buffer
camera->setClearMask(GL_DEPTH_BUFFER_BIT);
// draw subgraph after main camera view.
camera->setRenderOrder(osg::Camera::POST_RENDER);
// we don't want the camera to grab event focus from the viewers main camera(s).
camera->setAllowEventFocus(false);
// add to this camera a subgraph to render
{
geode = new osg::Geode();
std::string timesFont("fonts/arial.ttf");
// turn lighting off for the text and disable depth test to ensure it's always ontop.
osg::StateSet* stateset = geode->getOrCreateStateSet();
osg::Vec3 position(0.0f,0.0f,0.0f);
osg::Vec3 delta(0.0f,-120.0f,0.0f);
{
osgText::Text* text = new osgText::Text;
geode->addDrawable( text );
text->setFont(timesFont);
text->setPosition(position);
text->setText("Egypt Airforces SVS");
position += delta;
}
DrawCenterSymbol();
camera->addChild(geode);
camera->addChild(DrawCompass());
}
我认为z轴旋转的中心是在屏幕左下角的ortho2D原点。
我想知道你是否试过这个:
在DrawCompass()中,push_back顶点以原点为中心。
vertices->push_back( osg::Vec3(-150.0f,-150.0f,0.0f));
vertices->push_back( osg::Vec3(-150.0f,+150.0f,0.0f));
vertices->push_back( osg::Vec3(+150.0f,+150.0f,0.0f));
vertices->push_back( osg::Vec3(+150.0f,-150.0f,0.0f));
然后,在使用旋转的setMatrix之前,将顶点平移到所需的位置,即(CENTERX, 150)。
osg::Matrix mat = osg::Matrix::rotate(-0.9,osg::Vec3f(0.0,0.0,1.0));
mat = mat * mat.translate(CENTERX, 150.0f, 0);
transnodeosg->setMatrix(mat);
我不确定它是否会工作,因为我没有尝试运行代码。
相关文章:
- OSG地球摄像机位置
- gluPartialDisk in osg/osgEarth
- 为什么将相机添加到osg中后,该相机不保持其他相机的子级?
- 如何在 osg::Geometry 上设置光泽度,而无需在 OpenSceneGraph 中设置材质
- 将 STL 模型转换为 OSG 格式
- 无法在 Windows 上将 OSG 与 GDAL 一起使用
- 如何使用OSG API在Osgearth上绘制三角形
- OSG窗口阻止OpenCV窗口的刷新
- 在OpenSceneGraph中创建球体(使用osg::Geometry)
- 如何从文件预加载 3D 模型并在 OSG 中多次使用它
- 是否有适用于 OpenSceneGraph osg ive 格式的 GDAL 驱动程序
- 图像在OSG中随着旋转而消失
- 来自osg::viewer的RGB图像
- osg::GraphicsContext::WindowingSystemInterface returns null
- OSG:使用AnimationPathManipulator的摄影机飞行
- 用OSG Cookbook中的draw实例渲染点云数据不工作
- 将OSG的osgViewerQt示例与Qt信号/插槽一起使用
- 如何从Python中使用c++ OSG对象
- 由于GDAL中的ODBC链接错误,无法使用Visual Studio 2013构建OSG Earth
- OSG osg::MatrixTransform