How to let OpenGL keep objects that already drawn and render new ones?

Started by
7 comments, last by mikeman 19 years ago
How to let OpenGL keep the objects that already drawn and add new objects at the same moment. I meant to draw a polyhedron, and when I request, I want a new object(cube, or what...)drawn on the screen, but the old one would not lose and keep running on the program window. Because I am using MFC, and everytime I clicked my "generate" button (I used INVALIDATE so as to refresh the view and get the things I wanted), so I have this problem to keep the old one not to lose in the screen) Could anybody help me? :<
Advertisement
You're almost certainly going to have to totally erase the scene and redraw everything.

This means you'll have to store the position of every object in your scene to be able to redraw it correctly.

If you're using double-buffering, it should not flicker when you do this however.

Mark
Thank you, but is that the only way?B ecause it will be a lot of troubles to me to save the positions of each vertexes of polyhedron.
if the objects are static then you could probably render the static objects to a seperate texture which u could composite with updating objects.....

but otherwise - yup - u are just gonna have to redraw all that sucker
Quote:Original post by littlejedi
Thank you, but is that the only way?B ecause it will be a lot of troubles to me
to save the positions of each vertexes of polyhedron.




You should store all of the vertices of the object in an array anyway.
this way you can use vertex arrays or VBOs to quickly render the object.
all transformations should be done using the OpenGL matrices.

I take it you're currently rendering with glBegin/end sandwiches? -- not too
good.

All you would have to save then is the values of your transforms for translate,
scale and rotation and apply these same values on the next render.
You're going to want to clear the screen each frame. There really isn't another way to draw. If you didn't clear the color buffer each frame, every time you moved anything it's previous position would remain on the screen and overlapped by the new position.

So let's say this little ASCII thing is our OpenGL object:

---@@@

If we were to shift it right a space, it would now look like this:

----@@@

because the original drawing (position and all) would remain in tact, and would then be overlapped by rendering the new position (and so only that first '-' would remain).

You are most definitely going to be saving the vertex positions of each object you draw. I mean, you're going to want to manipulate those values eventually, and you definitely don't want to hardcode them into the code. The only way to move anything like that would be to move EVERYTHING, which is kind of like what a camera does, but you'll still be storing the rotational/translational vectors of the camera as well.

Eventually, not only will you be storing a list of your objects' geometry, but you'll be storing the objects themselves so that you may manipulate them in other ways as well. So for example you'll make like a cube class and a cube will have a position vector, a velocity vector, and a texture ID. It'll also have a draw function and an update function. Each frame you'll call the update function (to change the position based on the velocity and the velocity based on friction or whatever) and then call the draw function ,which will draw based on the now-updated positions, or even better draws the shape in a default position and translates based on the position (allowing you to use display lists). You'll have a linked list or something to hold all of these objects and just loop through this list each frame, calling both of those functions.

I hope this gives you a better appreciation for keeping things a little more object oriented. If it's a little above your head, it won't be for long. Just play around a little bit longer and come back. It'll make sense soon.

Also, you might want to read this. I read that about 2 weeks into when I was first learning OpenGL. As soon as I read it, clouds parted and the holy gossimer light of understanding shone through. Armed with the knowledge it presented me, I had a rhudementory 3D FPS (First Person Shooter) up and running in a matter of weeks. Er...but do as I say and not as I do, in that I highly recommend a smaller project before trying to tackle something like that. Honestly I learned a bunch about OpenGL doing the FPS, but it was in writing a simple breakout/pong variant that I learned a bunch about actualy game programming. Start small and build.

Hope this helps! Feel free to ask about anything that isn't clear.

Good luck!
Without order nothing can exist - without chaos nothing can evolve.
Probably the main point is that yes, you need to store the vertices of your objects. It is better to do this in a file instead of hard coded. It is better to use either display lists or vertex arrays to draw objects instead of immediate mode(glBegin()...glEnd());. Either way, get used to using arrays to store the verts because that will be necesary, also will be drawing once each frame. Just use double buffering.


There is an extension called WGL_ARB_buffer_region that can be used to preserve the buffer, so everytime the screen needs a refresh, you call wglRestoreBufferRegionARB

Not every card supports it.

The other way is to use RECT textures or 2D texture.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Quote:Original post by littlejedi
Thank you, but is that the only way?B ecause it will be a lot of troubles to me to save the positions of each vertexes of polyhedron.


Normally, the best way is to save them in an array or use VAR/VBO. But since I don't think you're going after performance here, you could take the easy way out and use display lists as a quick solution. In your GL initialization function, you can compile the list:

GLuint polyhedron_list;polyhedron_list=glGenLists(1);glNewList(polyhedron_list,GL_COMPILE);//Draw the object as normalglEndList();


You need to do this only once. The object is not rendered, but "saved", and you can draw it any time by calling glCallList(polyhedron_list);

-EDIT: D'oh, kburkhart84 already mentioned display lists...

This topic is closed to new replies.

Advertisement