• Advertisement
Sign in to follow this  

Beginners mistakes in opengl?

This topic is 3495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

After looking through some books and tutorials and gamedev and the mention of beginner mistakes and beginner concepts needing to be relearned;I have a question with this, can you list some that need to be either relearned or common bad code that would be used? I want to know some common pitfalls and the bad code that would be used? Do you have any examples? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Well, you should skip immediate mode and go directly to Vertex Buffers or Vertex Arrays, you know you're using immediate mode if your code has calls to glBegin and glEnd instead of glDrawElements or glDrawArrays.

Share this post


Link to post
Share on other sites
The single most common pitfall is people forgetting that OpenGL is a state machine. They'll turn on texturing to draw an object, and then wonder why the next thing they draw without textures is the color of the first pixel in the last texture they used. They'll set a color and then wonder why the next thing they draw after that is tinted green.

It's tinted green because your last call to glColor(...) was green! If you want to draw something in a different color, or reset it back to white for texturing, you have to set the state again with another call to glColor(...).

OpenGL is a state machine. Like a gadget that has a 100 little dials and levers on it. You have to set them all to the state you want them in before anything you draw.

Your rendering will end up being

Set Up States
Draw
Set Up States
Draw
...
...

For efficiency, it's best to sort your rendering by similar states, and change states on things as few times as possible.

Share this post


Link to post
Share on other sites
Quote:
Original post by Kwizatz
Well, you should skip immediate mode and go directly to Vertex Buffers or Vertex Arrays, you know you're using immediate mode if your code has calls to glBegin and glEnd instead of glDrawElements or glDrawArrays.


I am definitely one to use glBegin and such, as im following beginning opengl game programming. I also have the follower more opengl, which I have yet to touch. Are vertex buffers and functions like glDrawElements just faster/better?

Share this post


Link to post
Share on other sites
Quote:
Original post by agm_ultimatex
Quote:
Original post by Kwizatz
Well, you should skip immediate mode and go directly to Vertex Buffers or Vertex Arrays, you know you're using immediate mode if your code has calls to glBegin and glEnd instead of glDrawElements or glDrawArrays.
I am definitely one to use glBegin and such, as im following beginning opengl game programming. I also have the follower more opengl, which I have yet to touch. Are vertex buffers and functions like glDrawElements just faster/better?
You can get by just find with Begin and End. It can even be a good idea to use them when just starting a program.

But they are slow and outdated methods. They send every thing to the card one by one and also eat up function call time. To draw a model with 3000 triangles,that's over 18,000 function calls!

Each triangle gets 3 calls for vertex data, 3 calls for normal data, and 3 calls for texture data. Possibly more if you have more than one texture layer. Possibly more if you have vertex colors, etc...

The arrays store all that data in a big list and draw it in 1 function call.

Imagine you have a level that uses 25 texture maps and you sorted all your data by texture maps. For every texture you have an array of triangles to draw. You loop through all 25 textures, and draw the triangles that use them. That's 75 calls.

Change Texture
Change Array
Draw Array

That array can be stored on the card itself, and you might not even have to send over any data.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement