Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL quad rendering optimization

I'm drawing quads in openGL. My question is, is there any additional performance gain from this:

// Method #1

glBegin(GL_QUADS);
// Define vertices for 10 quads
glEnd();

... over doing this for each of the 10 quads:

// Method #2

glBegin(GL_QUADS);
// Define vertices for first quad
glEnd();

glBegin(GL_QUADS);
// Define vertices for second quad
glEnd();

//etc...

All of the quads use the same texture in this case.

like image 264
Kevin Laity Avatar asked Dec 01 '22 07:12

Kevin Laity


2 Answers

Yes, the first is faster, because each call to glBegin or glEnd changes the OpenGL state.

Even better, however, than one call to glBegin and glEnd (if you have a significant number of vertices), is to pass all of your vertices with glVertexPointer (and friends), and then make one call to glDrawArrays or glDrawElements. This will send all your vertices to the GPU in one fell swoop, instead of incrementally by calling glVertex3f repeatedly.

like image 59
Jesse Beder Avatar answered Feb 17 '23 08:02

Jesse Beder


From a function call overhead perspective the second approach is more expensive. If instead of ten quads we used ten thousand. Then glBegin/glEnd would be called ten thousand times per frame instead of once.

More importantly glBegin/glEnd have been deprecated as of OpenGL 3.0, and are not supported by OpenGL ES.

Instead vertices are uploaded as vertex arrays using calls such as glDrawArrays. Tutorial and much more in depth information can be found on the NeHe site.

like image 35
Adaptation Avatar answered Feb 17 '23 09:02

Adaptation