0

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. I have been working with the book OpensceneGraph 3.0 Beginner's guide, and so far, Ok.

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). 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();
user272671
  • 657
  • 4
  • 13
  • 26
  • How are you adding `root` to the scene graph with the rest of your items? – tmpearce Jul 06 '12 at 20:40
  • Have you tried writing a custom manipulator? There's some intersection code in the DriveManipulator that you could copy to the FirstPersonManipulator. – Ruan Caiman Jul 09 '12 at 21:00
  • How will a drive manipulator add a wall? – user272671 Jul 10 '12 at 16:18
  • Sorry, I thought you had figured out how to draw one, then the intersection would be helpful. It's not about adding a wall - it's about adding intersection code so you know when you've hit a wall. This code exists already in the drive manipulator and one or two of the other manipulators, but not in the first person one. So if you want the logic, look at how it's already been done, and apply the same technique to your application. – Ruan Caiman Jul 12 '12 at 16:07

1 Answers1

2

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. 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. Replace your root->addChild(wall) with this code:

 // 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. As I mentioned in the comments, then you'll have to use some intersection code to tell the camera where to stop.

Ruan Caiman
  • 881
  • 5
  • 5