Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL Alternative for Drawing Textures with Immediate Mode?

I'm writing a simple graphics engine that draws textures on the screen using OpenGL and C++. The way that I draw the textures is using the source code below--the drawing is done in a method contained in a "Sprite" class that I wrote, which is called by the main scene's game loop.

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, m_textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);

glTexCoord2f(0.0f, 0.0f);
glVertex2f(m_pos.x, m_pos.y);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(m_pos.x + m_size.x, m_pos.y);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(m_pos.x + m_size.x, m_pos.y + m_size.y);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(m_pos.x, m_pos.y + m_size.y);

glEnd();

glDisable(GL_TEXTURE_2D);

m_textureID is the id of the texture that was already loaded with OpenGL, and m_pos is a vector that stores the x and y position of the sprite, and m_size stores the size of the sprite.

The reason why I'm posting this is because I heard from somebody who is familiar with OpenGL that this is the wrong way to draw many different textures on the screen. Apparently, using glBegin(GL_QUADS) and glEnd() around calls to glVertex can be slow if there are many graphics on the screen at once, and that the correct way to do it is using vertex pointers.

Can anyone give me any pointers (no pun intended) on how to speed up the performance of this technique, using my implementation that I described above? Is there anything else I may be doing wrong? (I'm relatively new to OpenGL and graphics programming.)

Thanks in advance!

like image 561
dkaranovich Avatar asked Feb 28 '11 00:02

dkaranovich


1 Answers

You are using immediate mode, which isn't used much (if at all) in serious 3D graphics programming these days. In fact, on mobile devices with OpenGL ES (1.1 and 2.0) it isn't even available.

At the very least, you should use vertex arrays, which are set up using glVertexPointer, glNormalPointer, glColorPointer and glTexCoordPointer, and then drawn using glDrawArrays or glDrawElements.

The next step is to push your vertex data into GPU memory by using vertex buffer objects (VBOs) via glGenBuffer, glBindBuffer and glBufferData. You can find example code here and here.

like image 55
Marcelo Cantos Avatar answered Oct 28 '22 17:10

Marcelo Cantos