(Dumb Question) glBegin & glEnd

I would like to apologize in advance for this rather dumb (newbie) question…

I understand the fundamentals on how a 3D graphics system works, but I am unfamiliar with OpenGL. The sample programs that I’ve been able to understand were all very simple, they just displayed 2D images by Declaring Polygons between glBegin and glEnd, then calling glFlush to draw them into the buffer and then onto the screen (Through GLUT).

This seems fine, but I’m looking at more advanced programs and I’m confused as to what actually happens between glBegin and glEnd. If I wanted to model an object, say something simple like a cube, what do I have to do with OpenGL? Do I have to form the polygons of the cube (post Transformations) through calls to glBegin and glEnd during every run of the display function? Or is the information stored somewhere from the first modeling of the cube? If it is stored there, then how can I access that information so that I have something to perform transformations on?

Finally, what exactly happens (on the data level) between glBegin and glEnd? Does this simply draw primitives into the frame buffer, or does it actually update part of the OpenGL state machine?

my understanding of this is that you would store the vertices of the cube when it was at some initial location (most often centered around the origin) and use these vertices in between glBegin/glEnd…

in order to move the cube around, before you start the glBegin/glEnd pair, you would set the current matrix mode to modelview by calling :
glMatrixMode(GL_MODELVIEW);

then call the appropriate transformation functions… for example, to move the cube forward 10 units on the z axis you would call :
glTranslatef(0.0f, 0.0f, -10.0f);

to rotate the cube 45 degrees on the y-axis, you would call :
glRotatef(45.0f, 0.0f, 1.0f, 0.0f);

etc…etc… make sure to remember that order of transformations counts (rotating then translating is different from translating then rotating…)

if you are familiar with matrix math, then you can calculate the matrix for any series of transformations and just multiply that matrix by the current modelview matrix also.

check the red book online for more good opengl info : http://ask.ii.uib.no/ebt-bin/nph-dweb/dynaweb/SGI_Developer/OpenGL_PG/

hope this answered your question
–brian

Ok, thanks for the info, but I’m still a little confused…

Imagine that you had a setting like this. You have a table, with two objects on it. One if them is a Cube, and the other is a sphere. You know what the transformations would be to roll the Sphere around, and make the cube spin around. So what would happen if you wanted to process both of these things in one frame? You could brute force it by clearing the screen and then redrawing it every frame by calculating the new vertices for the cube and sphere and then declaring the new sphere and cube between their own glBegin and glEnd pairs, but I’m almost positive that you aren’t supposed to do that.

What I’m guessing is that OpenGL saves these primitives somewhere when you initialize them between glBegin and glEnd. So if you have a world with multiple objects, you should be able to locate the object that you want to translate in memory, then perform the transformation on it, then move to the next object, etc, until you where ready to redraw the screen.

I understand that the ModelView matrix has something to do with all this, but could you clarify a little more?

-Thanks once again!

alright, i might have been a bit off in my first post…

you can (and oftentimes have to) call the glTranslatef/glRotatef functions inside the glBegin/glEnd pair…

for your example, you should probably know a bit about the matrix stack… ok, you set the matrixmode to modelview…

start your glBegin/glEnd pair, and call glPushMatrix(); (what this does is stores the current modelview matrix on an internal matrix stack, for later retrieval) do any transformations necessary to get the table where you want it… call glPopMatrix(); to get the matrix back to the state it was in before you drew the table…

now glPushMatrix(); again, do the transformations neccessary to move the cube where you want it to be… draw the cube… glPopMatrix();

glPushMatrix(); do the transformations for the sphere… draw the sphere… glPopMatrix(); glEnd();

basically there’s a simple order here for drawing objects wherever you want them…
set matrix mode…
begin drawing…
pushmatrix…
transform something…
draw something…
popmatrix…
rinse…
repeat…
end drawing…
swap buffers…

that’s all there is to it… i hope i explained it well…

—brian

I suggest that you read the OpenGL Programming Guide, in order to understand how opengl works. In short, I can tell you this:
-OpenGL is a state machine: you set some states (color, material, normal, etc), and subsequent commands (such as glVertex) render primitives using the last current states.
-OpenGL is a low-level api: it only knows of vertices, triangles, and the like. No objects or higher level structures are defined in OpenGL.
-A tipical OpenGL program whould do to render 1 frame of animation (pseudo code):

–glClear(color and depth buffer)
–Set virtual camera params (modelview and projection matrices)
–set lights
–set scene states
–glMatrixMode(GL_MODELVIEW);
–for all my objects
----Set material properties with gl States
----glPushMatrix()
----Set the obj’s position for the frame (glTranslatef,etc).
----Send Object Vertices
----glPopMatrix()
–endfor
–SwapBuffers

Again, please read the OpenGL Programming Guide to fully understand how it works.

Hell! This is the second time I write something simultaneouslly with other guy!.
Well… hope it helps anyway.

simultaneouslly?

Also, just to clarify. You don’t necessarily have to draw a single frame of your entire scene within a single glBegin/glEnd. (And often times it would be inefficient to do so.)

What I usually do is something like so…

for each object
glPushMatrix();
DoTransformations
DoMaterialTextureEtcIfNecessary
glBegin(primitiveType);
DrawObject
glEnd();
glPopMatrix();
next object

I typically try to avoid transformations inside glBegin/glEnd.

>>I typically try to avoid transformations inside glBegin/glEnd.

Now that is very wise of you

To be serious, you aren’t even allowed to do transformations within a glBegin/glEnd-pair. It will result in a GL_INVALID_OPERATION-error. So, don’t even try to call glRotatef/glTranslatef/glPushMatrix/glPopMatrix or any other function to manipulate the matrix stack within a glBegin/glEnd-pair.

hmm… can’t transform in between glBegin/glEnd… doh! i suppose it would be good if i knew what i was talking about before i tried to explain things to people

—brian

Originally posted by Bob:
[b]Now that is very wise of you

To be serious, you aren’t even allowed to do transformations within a glBegin/glEnd-pair. It will result in a GL_INVALID_OPERATION-error. So, don’t even try to call glRotatef/glTranslatef/glPushMatrix/glPopMatrix or any other function to manipulate the matrix stack within a glBegin/glEnd-pair.[/b]

That’s probably why I try to avoid them. Seriously, though, I think I knew this already, but plucky’s post that they could be done kind of threw me off and I didn’t bother to look it up to be sure. (Shame on you plucky!) I just knew his suggestion to use transformations inside begin/end seemed kind of ugly.