C++ - OpenGL Concept Question - Stack Overflow
C++ - OpenGL Concept Question - Stack Overflow
Asked 12 years, 8 months ago Modified 12 years, 8 months ago Viewed 367 times
I'm just starting OpenGL programming in Win32 C++ so don't be too hard on me :) I've been wandering
along the NeHe tutorials and 'the red book' a bit now, but I'm confused. So far I've been able to set up an
4 OpenGL window, draw some triangles etc, no problem. But now I want to build a model and view it from
different angles. So do we:
1. Load a model into memory (saving triangles/quads coordinates in structs on the heap) and in each
scene render we draw all stuff we have to the screen using glVertex3f and so on.
2. Load/draw the model once using glVertex3f etc and we can just change the viewing position in
each scene.
3. Other...?
It seems to me option 1 is most plausible from all I read so far, however it seems a bit ehh.. dumb! Do
we have to decide which objects are visible, and only draw those. Isn't that very slow? Option 2 might
seem more attractive :)
EDIT: Thanks for all the help, I've decided to do: read my model from file, then load it into the GPU
memory using glBufferData and then feed that data to the render function using glVertexPointer and
glDrawArrays .
Share Improve this question Follow edited Aug 8, 2011 at 14:24 asked Aug 8, 2011 at 12:39
genpfault the_source
51.6k 11 88 142 648 1 6 12
First you need to understand, that OpenGL actually doesn't understand the term "model", all what
OpenGL sees is a stream of vertices coming in and depending on the current mode it uses those
7 streams of vertices to draw triangles to the screen.
swap buffers
OpenGL does not remember what's happening up there. There was (is) some facility, called Display
Lists but they are not able to store all kinds of commands – also they got deprecated and removed from
recent OpenGL versions. The immediate mode commands glBegin, glEnd, glVertex, glNormal and
glTexCoord have been removed as well.
So the idea is to upload some data (textures, vertex arrays, etc.) into OpenGL buffer objects. However
only textures are directly understood by OpenGL as what they are (images). All other kinds of buffers
require you telling OpenGL how to deal with them. This is done by calls to
gl{Vertex,Color,TexCoord,Normal,Attrib}Pointer to set data access parameters and
glDraw{Arrays,Elements} to trigger OpenGL fetching a stream of vertices to be fed to the rasterizer.
Share Improve this answer Follow edited Aug 8, 2011 at 14:51 answered Aug 8, 2011 at 12:57
datenwolf
161k 13 188 303
Aaah ok, so the whole idea changed a bit over the years. Old tutorials still use those immediate mode commands. I
think I'll have look in the local bookstore for a recent one, all this is getting very complicated (and I considered
myself king after I mastered the windows GDI...) – the_source Aug 8, 2011 at 13:06
@the_source: The basic idea never changed, but what got streamlined is the way, data is sent to OpenGL. On
recent OpenGL implementations a glBegin … glEnd block is in-situ turned into a vertex array, but below the
OpenGL level (means that you don't magically get a usable VA from immediate mode calls). Every call to glVertex
is like pushing the next vertex attribute vector to the end of the vertex array, expanding it on the go. – datenwolf
Aug 8, 2011 at 13:31
@the_source Quite unrelated comment, but as you are talking about books. If you are really looking for a book
about modern GL without deprecated functionality, I would recommend the NEWEST edition of the SuperBible, see
this question for more info. The Red Book is definitely a bit outdated. But as datenwolf said, it doesn't do any harm
to learn OpenGL from the old style, as the underlying concepts won't change that much. – Christian Rau Aug 8,
2011 at 13:56
Thanks Christian, and that is a very helpful link! – the_source Aug 8, 2011 at 14:13
You should upload the data to the GPU memory once, and then draw each frame using as few
commands as possible.
4
Previously, this was done using display lists. Nowadays, it's all about vertex buffer objects (a.k.a. VBOs),
so look into those.
Here's a tutorial about VBOs, written before they were only an extension, and not a core part of
OpenGL.
Thanks, so it is actually option 1, but instead of on the heap we store the model in GPU memory? And we still have
to draw everything each frame? – the_source Aug 8, 2011 at 12:52
@the_source: Sort of. You won't be specifying each vertex (which was my main point), but you need to draw the
scene each frame still. It's just waaay faster to tell the GPU to render data it already has on local memory, than
having the CPU send each vertex one at a time each frame. – Macke Aug 8, 2011 at 13:45
Indeed yes, you don't have to send the data to the GPU each frame, but you do have to render it :) – jcoder Aug 8,
2011 at 13:47