|
在viewer中渲染了一个图像作为窗口的纹理背景,想在背景前方显示一个ive模型,利用了两个camera来实现,代码如下:- -(osg::Node *) create2DBGTexture
- {
- // 图像材质
- unsigned int w = SCREEN_WIDTH;
- unsigned int h = SCREEN_HEIGHT;
-
- 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, 0.0));
- vertices->push_back(osg::Vec3(0.0, vy, 0.0));
- vertices->push_back(osg::Vec3(vx, vy, 0.0));
- vertices->push_back(osg::Vec3(vx, 0.0, 0.0));
- rectBG_geom->setVertexArray(vertices);
-
- // 材质坐标
- osg::Vec2Array * texcoords = new osg::Vec2Array;
- texcoords->push_back(osg::Vec2(1.0, 0.0625));
- texcoords->push_back(osg::Vec2(1.0, 0.9375));
- texcoords->push_back(osg::Vec2(0.0, 0.9375));
- texcoords->push_back(osg::Vec2(0.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));
-
- // 材质实例
- osg::ref_ptr<osg::Texture2D> BG_texture = new osg::Texture2D;
- BG_texture->setDataVariance(osg::Object::DYNAMIC);
- BG_texture->setImage(osgDB::readImageFile("BackGround.png"));
- 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::Geode * BG_geode = new osg::Geode;
- BG_geode->setDataVariance(osg::Object::DYNAMIC);
- BG_geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED);
- BG_geode->getOrCreateStateSet()->setTextureAttributeAndModes(0,BG_texture,osg::StateAttribute::ON);
- BG_geode->addDrawable(rectBG_geom);
- return BG_geode;
- }
- -(osg::Group*) createVideoBackground
- {
- osg::ref_ptr<osg::Camera> Camera = new osg::Camera;
- Camera->setProjectionMatrixAsOrtho2D(0,SCREEN_WIDTH,0,SCREEN_HEIGHT);
- Camera->setViewMatrix(osg::Matrix::identity());
- Camera->setRenderOrder(osg::Camera::NESTED_RENDER);
- Camera->setClearMask(GL_DEPTH_BUFFER_BIT);
- Camera->getOrCreateStateSet()->setMode(GL_LIGHTING, GL_FALSE);
- Camera->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, GL_FALSE);
- Camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
-
- Camera->addChild([self create2DBGTexture]);
-
- return Camera;
- }
- // 初始化osg组织
- -(int) initOsgTree
- {
- int dwError = 0;
-
- // 提示信息?
- osg::setNotifyLevel(osg::DEBUG_INFO);
-
- // 模型相关参数(模型数据、变换矩阵)
- osg::ref_ptr<osg:: MatrixTransform> ModelMatrix = new osg::MatrixTransform();
- ModelMatrix->setMatrix(osg::Matrix::identity());
- osg::ref_ptr<osg::Node> model = (osgDB::readNodeFile("myModel.IVE"));
- ModelMatrix->addChild(model);
- ModelMatrix->getOrCreateStateSet()->setRenderBinDetails(100, "RenderBin");
- // 设置背景
- osg::ref_ptr<osg::Group> videoBackground = [self createVideoBackground];
- videoBackground->getOrCreateStateSet()->setRenderBinDetails(-1, "RenderBin");
-
- // 设置camera
- osg::Matrix projectMatrix;
- projectMatrix.set(myProjectMateix);
- osg::ref_ptr<osg::Camera> cam = new osg::Camera();
- cam->setRenderOrder(osg::Camera::NESTED_RENDER);
- cam->setProjectionMatrix(projectMatrix);
- cam->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
- cam->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);
-
- cam->addChild(ModelMatrix.get()); // 模型
- cam->addChild(videoBackground.get()); // 背景
-
- // 根节点
- osg::ref_ptr<osg::Group> MARRoot = new osg::Group;
- MARRoot->addChild(cam.get());
-
- // viewer
- osg::ref_ptr<osg:: Viewer> MARViewer = new osgViewer::Viewer();
- MARViewer->setSceneData(MARRoot.get());
-
- MARViewer->realize();
- MARViewer->frame();
-
- osg::setNotifyLevel(osg::INFO);
-
- return dwError;
- }
复制代码 这段代码是参考osgART写的,感觉并无什么问题,可是一运行,就死机了,
我跟踪了一下代码,发现是折在“MARViewer->setSceneData(MARRoot.get())”,
跟踪进OSG的源码,最后追到view.cpp中的
// now make sure the scene graph is set up with the correct DataVariance to protect the dynamic elements of
// the scene graph from being run in parallel.
osgUtil::Optimizer::StaticObjectDetectionVisitor sodv;
getSceneData()->accept(sodv);
这块就跟不下去了,死机了。看注释,好像是我的参数设置的有问题。
能否高人帮我看看我的问题出在哪里,谢谢!! |
|