Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL concept question

Tags:

c++

winapi

opengl

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

like image 456
the_source Avatar asked Dec 22 '22 10:12

the_source


1 Answers

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 streams of vertices to draw triangles to the screen.

Every frame drawing iteration follows some outline like this:

  • clear all buffers
  • for each window element (main scene, HUD, minimap, etc.):
    • set scissor and viewport
    • conditionally clear depth and/or stencil
    • set projection matrix
    • set modelview matrix for initial view
    • for each model
      • apply model transformation onto matrix stack
      • bind model data (textures, vertices, etc.)
      • issue model drawing commands
  • 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.

like image 103
datenwolf Avatar answered Dec 24 '22 01:12

datenwolf