views:

30

answers:

2

This is a pretty beginner question, but given that I render a complex shape in OpenGL which requires a lot of calculations, is there anyway to export that to a state like a model that can be opened again later given you wont edit it again?

I can't create them outside the program (with something like Blender) since the shape is computed when the program starts. The render then gets translated, rotated, drawn over, etc. Note, that I don't need to edit the shape though, but I do want it in a 3D state so saving an image isn't an option.

I'm using OpenTK 1.0, VB.NET (.NET 4)

+2  A: 

The obvious possibility would be to write your vertices to a file, and then re-load them when needed. Unless you have specific reasons to do otherwise, it's probably best to use one of the many existing file formats for this, such as Wavefront OBJ, Ogre3d mesh XML files, etc.

Jerry Coffin
ah, that never hit me. thanks for the formats as well!
jay
A: 

THis sounds like an ideal situation for OpenGL display lists. The way you use them is to first ask OpenGL for a display list id to use:

GLuint draw_id = glGenLists(1);

(You can ask for several consecutive ids at a time, but in this case we are only asking for 1) Then, first time through your draw routine, call glNewList:

glNewList(draw_id, GL_COMPILE);
<do compute intensive drawing here>
glEndList();

Note that this will not actually render your shape (you can use GL_COMPILE_AND_EXECUTE in glNewList, which will render your shape, but that is generally discouraged)

Now, whenever you need to draw your object:

< set up your OpenGL matrices >
glCallList(draw_id);

And finally, when you are done with it, you can use glDeleteLists.

Display lists can actually speed up rendering, as some optimizations can be done, and all function call overhead is removed.

Now, technically, OpenGL display lists have been deprecated (not removed, however!) in OpenGL 3.0 and above (along with glTranslate, glVertex glBegin... grrr), but they should still work for the foreseeable future. To be future proof, you should use vertex arrays, but that is a fair bit more complicated.

-matt

Matthew Hall
why on earth do they keep deprecating everything? I'll try your method and see where it goes, but out of curiosity what's the alternative to display lists in OGL3?
jay
Well, from what I can tell, in OpenGL 3.0 and above, all vertex data is supposed to be placed in vertex buffer objects, and texture data in pixel buffer objects. All transformations (such as glRotate) are to be replaced by vertex shader programs. The reasoning seems to be that all high-performance OpenGL programs already do (most) of this anyway, but I do think it severely raises the bar for beginners, or for writing simple graphical apps. As a graphics programmer for a visualization lab, I have no immediate plans to give up OpenGL 2.1, though some day I may be forced to.
Matthew Hall