An alternative to OpenGL for instant texture painting?

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!

+4
source share
1 answer

You are using the immediate mode, which today is not used (if at all) in serious 3D-graphics programming. In fact, on mobile devices with OpenGL ES (1.1 and 2.0) it is not even available.

At the very least, you should use vertex arrays that are configured 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 using vertex buffer objects (VBOs) via glGenBuffer , glBindBuffer and glBufferData . Here you can find an example here and here .

+7
source

Source: https://habr.com/ru/post/1341568/


All Articles