Scene Graphs and Instancing

By Judy Hartley (Intel) (11 posts) on September 9, 2009 at 8:24 am

This is the fourth blog I’ve written on the subject of porting a DirectX demo over to OpenGL. The first blog details what my project is about. The second and third blogs talk about some of the areas I have been working on and the troubles I’ve been having with the conversion of .x files into a format I can use for OpenGL. COLLADA files were suggested and I’ve spent quite a bit of time working on files in various mesh or graphic programs to get a consistent way to convert .x files. I think I’ve mentioned that I wrote a Perl script to convert .x files into .obj files. I also wrote a draw function for an .obj file loader I found on the web. The problem was that some of the obj files would render correctly and some wouldn’t. I was also dealing with having quite a few objects to load and I wanted an easier way to handle all of the objects.

When I explained my issues with one of the graphics gurus I’m very fortunate to work around, he said that there were two main ways to handle the objects: Scene Graphs and Instancing. We took a look at the DirectX code and discovered that the writers of this code used Instancing for the bugs and the castle blocks. In case you haven’t seen the Destroy the Castle demo, here is a screen shot:

The Original "Destroy the Castle" demo
Naturally, my first instinct was to use Instancing for my OpenGL program as well. It turns out, however, that there is no function for instancing in OpenGL without extensions. Yes, you can do it with glDrawElementsInstancedEXT if you have a high-end graphics card that supports the use of these functions. But since I am creating my code to be ported to smaller devices, it isn’t likely that these functions will work for my purposes. You can use a technique called GLSL Pseudo-Instancing which Nvidia demonstrated. I haven’t gotten into shaders as yet, and I could only find one example of this use, so I decided to look into Scene Graphs.

According to Wikipedia, A scene graph is a general data structure commonly used by vector-based graphics editing applications and modern computer games. They have links to several commonly used programs. I went to the Open Scene Graph (OSG) website. After reading about what they could do and seeing tutorials available to help, I decided to install it on my system and try it out. It was fairly easy to install in onto my system with the help of an article or two. I ended up changing some of the environment variables from ones using a reference to another environment variable to a direct reference to the file. But it compiled on the first try, and I think that is a major accomplishment all by itself! After using it for over a week, I’m impressed but also a little frustrated. Well, that’s par for the course, isn’t it? The great thing is that OSG was able to load and texture my .obj files with no trouble. All of my .obj files! So I think I can say that my Perl script works. In OSG, I have recognizable landscape and objects. See the picture below:
OpenGL "Castle Destruction" so far.

The lighting isn’t right yet, and I still don’t pivot the view with the cannon, but the castle is there and several bugs are stationed on the left side. Notice the “break” in the castle that you don’t see in the original? It actually exists there as well, but we don’t see it because the original angle of the cannon is set so it doesn’t show. Take a look at these side by side shots after I move the camera.

OpenGL program once the view is moved.

OpenGL program once the view is moved.

Original program showning break in castle.

Original program showning break in castle.

Another thing I’ve discovered is that OSG can be easily attached to your OpenGL/GLUT code. They have some example code to show you how to do it, and that makes incorporating OSG into your program fairly easy. The thing I do find frustrating is that the reference documentation list those functions that are available, but in many cases don’t even tell you what they should be used for. For instance, I was having a hard time changing the initial view in the window. The default view shows objects with the standard backdrop blue behind them. My sky object (a hemisphere with the sky material used on the inside ) displays as a black object against the blue background. See the picture below:

Original Camera Position for OpenGL program.

Original Camera Position for OpenGL program.

What happens is that you have to use the mouse to scroll INTO the hemisphere and there you can see the scene as rendered above. I will not tell you how long it took me to set the code so it would start inside the globe, but take it from me, it was a long time.
Here are a couple of lines of the original code:

Global:
osgGA::TrackballManipulator *Vman = new osgGA::TrackballManipulator();

Inside Main:
viewer = new osgViewer::Viewer;
window = viewer->setUpViewerAsEmbeddedInWindow(100,100,800,600);
viewer->setSceneData(root);
viewer->setCameraManipulator(Vman);

And here are the ammended lines :

Global:
osgGA::TrackballManipulator *Vman = new osgGA::TrackballManipulator();
Inside Main:
osgGA::TrackballManipulator *Vman = new osgGA::TrackballManipulator();

viewer = new osgViewer::Viewer;
window = viewer->setUpViewerAsEmbeddedInWindow(100,100,800,600);

Vman->setHomePosition(osg::Vec3d( -500, 12150, 380),osg::Vec3d(-500,0,0), osg::Vec3d( 0, 0, 1 ), false );

viewer->setSceneData(root);
viewer->setCameraManipulator(Vman);

The one new line tells the Camera Manipulator where to start. Notice, too that the “z” direction is “up” rather than the standard “y”. From what I understand, this is specific to OSG. It does kind of throw you for a loop at first, though.

All in all, I am finally beginning to see how to go about completing this project. My next job is to get the lighting right, and get the view pivoting about the camera. Keep your chin up, and I’ll let you know soon how it goes. Cheers!

Categories: Mobility, Open Source, Software Engineering, Uncategorized, Visual Computing

Comments (1)

September 10, 2009 5:51 AM PDT


rudi
The breake in the castle that you are trying to explain to show up in the OpenGL version
will never do so!
Have a closer look at the breakout in the wall in the original (DirectX) right of the middle axis building,
it is showing up on the left side of that building in the OpenGL-version and it is mirrored.
In fact the whole building is mirrored, seames your axis transformation needs a sign change on the
horizontal.

Trackbacks (8)


Leave a comment  

To obtain technical support, please go to Software Support.
Name (required)*

Email (required; will not be displayed on this page)*

Your URL (optional)


Comment*