Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: (Dumb Question) glBegin & glEnd

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2001
    Location
    Naperville, IL, USA
    Posts
    11

    (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?

  2. #2
    Junior Member Newbie
    Join Date
    Dec 2000
    Location
    Appleton, WI, USA
    Posts
    20

    Re: (Dumb Question) glBegin & glEnd

    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-dwe...per/OpenGL_PG/

    hope this answered your question
    --brian
    -brian ploeckelman
    plucky@lumiere.net

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2001
    Location
    Naperville, IL, USA
    Posts
    11

    Re: (Dumb Question) glBegin & glEnd

    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!

  4. #4
    Junior Member Newbie
    Join Date
    Dec 2000
    Location
    Appleton, WI, USA
    Posts
    20

    Re: (Dumb Question) glBegin & glEnd

    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
    -brian ploeckelman
    plucky@lumiere.net

  5. #5
    Junior Member Regular Contributor
    Join Date
    Feb 2000
    Location
    Caracas/Venezuela
    Posts
    182

    Re: (Dumb Question) glBegin & glEnd

    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.

  6. #6
    Junior Member Regular Contributor
    Join Date
    Feb 2000
    Location
    Caracas/Venezuela
    Posts
    182

    Re: (Dumb Question) glBegin & glEnd

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

  7. #7
    Senior Member OpenGL Pro
    Join Date
    May 2000
    Location
    Hannover, Germany
    Posts
    1,074

    Re: (Dumb Question) glBegin & glEnd

    simultaneouslly?

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Oct 2000
    Location
    Fargo, ND
    Posts
    1,755

    Re: (Dumb Question) glBegin & glEnd

    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..

    Code :
    for each object
      glPushMatrix();
      DoTransformations
      DoMaterialTextureEtcIfNecessary
      glBegin(primitiveType);
      DrawObject
      glEnd();
      glPopMatrix();
    next object
    I typically try to avoid transformations inside glBegin/glEnd.
    Deiussum
    Software Engineer and OpenGL enthusiast

  9. #9
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,982

    Re: (Dumb Question) 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.

  10. #10
    Junior Member Newbie
    Join Date
    Dec 2000
    Location
    Appleton, WI, USA
    Posts
    20

    Re: (Dumb Question) glBegin & glEnd

    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
    -brian ploeckelman
    plucky@lumiere.net

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •