I am writing a simple graphics engine that draws textures on the screen using OpenGL and C ++. The way I draw textures uses the source code below - the picture is executed in the method contained in the "Sprite" class that I wrote, which is called by the main loop of the game.
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 identifier of the texture that has already been loaded by OpenGL, and m_pos is the vector that stores the position of the sprite x and y, and m_size preserves the size of the sprite.
The reason I am posting this is because I heard from someone familiar with OpenGL that this is the wrong way to draw a lot of different textures on the screen. Apparently, using glBegin (GL_QUADS) and glEnd () around glVertex calls can be slow if there are a lot of graphics on the screen, and that the right way to do this is to use vertex pointers.
Can someone give me any pointers (not a pun) on how to speed up this technique using my implementation described above? Is there anything else I can do wrong? (I'm relatively new to OpenGL and graphical programming.)
Thanks in advance!
source share