简体   繁体   English

OpenSceneGraph - 如何添加墙或3

[英]OpenSceneGraph - How to add a Wall or 3

I need to add a wall to my scenegraph, and have it work so I can not go past the wall with my camera. 我需要在我的场景图中添加一个墙,并让它工作,所以我不能用相机穿过墙。 I am creating an laboratory scene, but I am new to 3d programming in general. 我正在创建一个实验室场景,但我不熟悉3d编程。 I have been working with the book OpensceneGraph 3.0 Beginner's guide, and so far, Ok. 我一直在使用OpensceneGraph 3.0初学者指南,到目前为止,好的。

I have a couple of furniture in my scene but what I would like to do is add a wall beyond which my camera should not go. 我的场景中有几个家具,但我想要做的是添加一个墙,超出该墙我的相机不应该去。 My code below, from the book, Openscenegraph beginner, does not seem to do anything(page 83). 我的下面的代码,从书,Openscenegraph初学者,似乎没有做任何事情(第83页)。 I add it and I don't see a wall and I am still able to move everywhere in the scene with my camera. 我添加它,我没有看到墙,我仍然能够用我的相机在场景中的任何地方移动。 How do I create a wall in my application. 如何在我的应用程序中创建墙。

osg::ref_ptr<osg::Group> root = new osg::Group();
    //adding walls to the lab to make it more room like -- 7/6/12
    osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
    vertices->push_back(osg::Vec3(0.0f, 0.0f, 0.0f));
    vertices->push_back(osg::Vec3(0.0f, 0.0f, 1.0f));
    vertices->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));
    vertices->push_back(osg::Vec3(1.0f, 0.0f, 1.5f));
    vertices->push_back(osg::Vec3(2.0f, 0.0f, 0.0f));
    vertices->push_back(osg::Vec3(2.0f, 0.0f, 1.0f));
    vertices->push_back(osg::Vec3(3.0f, 0.0f, 0.0f));
    vertices->push_back(osg::Vec3(3.0f, 0.0f, 1.5f));
    vertices->push_back(osg::Vec3(4.0f, 0.0f, 0.0f));
    vertices->push_back(osg::Vec3(4.0f, 0.0f, 1.0f));

    osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
    normals->push_back(osg::Vec3(0.0f, -1.0f, 0.0f));

    osg::ref_ptr<osg::Geometry>geom = new osg::Geometry;
    geom->setVertexArray(vertices.get());
    geom->setNormalArray((normals.get()));
    geom->setNormalBinding((osg::Geometry::BIND_OVERALL));
    geom->addPrimitiveSet((new osg::DrawArrays(GL_QUAD_STRIP,0,10)));
    osg::ref_ptr<osg::Geode> wall = new osg::Geode;
    wall->addDrawable(geom.get());
    root->addChild(wall);

osgViewer::Viewer viewer;
      viewer.setSceneData(root.get());
    viewer.run();

You are already drawing the "wall" as coded above - it looks a little more like a fence than a wall, but you can easily fix that by moving the 1.0 values all the way up to 1.5 to match the others. 你已经按照上面的编码绘制了“墙” - 它看起来更像栅栏而不是墙壁,但你可以通过将1.0值一直移动到1.5以匹配其他值来轻松解决这个问题。 You might not be seeing it with the rest of your scene because of differences of scale - if the dimensions of your furniture are in 100's, for example. 例如,如果您的家具尺寸为100,那么您可能无法在场景的其余部分看到它。 Replace your root->addChild(wall) with this code: 用以下代码替换root-> addChild(wall):

 // assumes your furniture is already added to root
 float scale=root->getBound().radius();
 osg::ref_ptr<osg::PositionAttitudeTransform> pax = new osg::PositionAttitudeTransform;
 pax->addChild(wall);
 pax->setScale(osg::Vec3d(scale,scale,scale));
 root->addChild(pax);

Then you will see your fence. 然后你会看到你的围栏。 Move the position/rotation of pax to place your wall. 移动pax的位置/旋转以放置墙壁。 As I mentioned in the comments, then you'll have to use some intersection code to tell the camera where to stop. 正如我在评论中提到的那样,你将不得不使用一些交叉代码来告诉相机停在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM