|
大家好,
我有个问题,麻烦高人帮我看看。
我在Viewer里面用一个图像作为背景,然后加载一个IVE模型,想让模型显示在背景之前的。代码如下:- -(osg::Node *) create2DBGTexture
- {
- unsigned int w = 480;
- unsigned int h = 320;
-
- float vx = (float)w;
- float vy = (float)h;
-
- osg::ref_ptr<osg::Geometry> rectBG_geom = new osg::Geometry;
-
- osg::Vec3Array * vertices = new osg::Vec3Array;
- vertices->push_back(osg::Vec3(0.0, 0.0, -1.0));
- vertices->push_back(osg::Vec3(0.0, vy, -1.0));
- vertices->push_back(osg::Vec3(vx, vy, -1.0));
- vertices->push_back(osg::Vec3(vx, 0.0, -1.0));
- rectBG_geom->setVertexArray(vertices);
-
- osg::Vec2Array * texcoords = new osg::Vec2Array;
- texcoords->push_back(osg::Vec2(0.0, 0.0625));
- texcoords->push_back(osg::Vec2(0.0, 0.9375));
- texcoords->push_back(osg::Vec2(1.0, 0.9375));
- texcoords->push_back(osg::Vec2(1.0, 0.0625));
- rectBG_geom->setTexCoordArray(0,texcoords);
-
- osg::Vec3Array * normals = new osg::Vec3Array;
- normals->push_back(osg::Vec3(0.0f,0.0f,1.0f));
- rectBG_geom->setNormalArray(normals);
- rectBG_geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
- rectBG_geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,4));
-
- BG_image = new osg::Image;
- BG_image->setImage(AVWidth, AVHeight, 1, GL_RGBA, GL_BGRA, GL_UNSIGNED_BYTE, pAVImage, osg::Image::NO_DELETE);
-
- BG_texture = new osg::Texture2D;
- BG_texture->setDataVariance(osg::Object::DYNAMIC);
- BG_texture->setImage(BG_image);
- BG_texture->setResizeNonPowerOfTwoHint(false);
- BG_texture->setFilter(osg::Texture2D::MIN_FILTER, osg::Texture2D::NEAREST);
- BG_texture->setFilter(osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST);
- osg::StateSet* stateset = rectBG_geom->getOrCreateStateSet();
- stateset->setTextureAttributeAndModes(0,BG_texture,osg::StateAttribute::ON);
-
- osg::Geode * BG_geode = new osg::Geode;
- BG_geode->addDrawable(rectBG_geom);
-
- return BG_geode;
- }
- -(int) initOsgTree
- {
- int dwError = 0;
-
- osg::setNotifyLevel(osg::DEBUG_INFO);
-
- // camera
- unsigned int w = 480;
- unsigned int h = 320;
-
- //create and attach ortho camera for hud text
- osg::ref_ptr<osg::Camera> hudCamera = new osg::Camera;
- hudCamera->setProjectionMatrix(osg::Matrix::ortho2D(0,w,0,h));
- hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
- hudCamera->setViewMatrix(osg::Matrix::identity());
- hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
- hudCamera->setRenderOrder(osg::Camera::POST_RENDER);
-
- ModelMatrix = new osg::MatrixTransform();
- osg::ref_ptr<osg::Node> model = (osgDB::readNodeFile("myModel.IVE"));
- ModelMatrix->addChild(model);
- osg::Matrix m;
- m.makeTranslate(osg::Vec3(0.0, 0.0, 10.0));
- ModelMatrix->setMatrix(m);
-
- osg::ref_ptr<osg::Group> MARRoot = new osg::Group;
- MARRoot->getOrCreateStateSet()->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
- hudCamera->addChild([self create2DBGTexture]);
- MARRoot->addChild(hudCamera.get());
- MARRoot->addChild(ModelMatrix.get());
-
- // viewer
- MARViewer = new osgViewer::Viewer();
- MARViewer->setSceneData(MARRoot.get());
- MARViewer->setCameraManipulator(new osgGA::MultiTouchTrackballManipulator);
- MARViewer->realize();
- MARViewer->frame();
-
- return dwError;
- }
复制代码 很奇怪的是,程序运行起来后,竟然看不到模型,但如果把背景注释掉,能看到模型被正确加载。
1。 请帮忙看看怎么才能正确地显示模型和背景啊?我的Viewer是X-Y平面的,背景的Z坐标是-1,模型Z坐标是10,这样应该能让我的模型在前显示吧?
2。 MARViewer->setCameraManipulator(new osgGA::MultiTouchTrackballManipulator);这一句如果被注释掉,连模型也没有了,这句有必要么?我看好多例子里面也没有这句啊?
请各位不吝赐教,谢谢了!!
唐 |
|