Is glDisableClientState required?

Each example I found to display array data is similar to the following code, in which in the drawing loop you first call glEnableClientState for what you will use, and when you finish, you call glDisableClientState:

void drawScene(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glBindTexture(GL_TEXTURE_2D, texturePointerA); glTexCoordPointer(2, GL_FLOAT, 0,textureCoordA); glVertexPointer(3, GL_FLOAT, 0, verticesA); glDrawElements(GL_QUADS, numPointsDrawnA, GL_UNSIGNED_BYTE, drawIndicesA); glBindTexture(GL_TEXTURE_2D, texturePointerB); glTexCoordPointer(2, GL_FLOAT, 0,textureCoordB); glVertexPointer(3, GL_FLOAT, 0, verticesB); glDrawElements(GL_QUADS, numPointsDrawnB, GL_UNSIGNED_BYTE, drawIndicesB); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); } 

In my program, I always use texture coordinates and vertex arrays, so I thought it was pointless to keep turning them on and off in every frame. I moved glEnableClientState outside of the loop like this:

 bool initGL(void) { //... glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); } void drawScene(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glBindTexture(GL_TEXTURE_2D, texturePointerA); glTexCoordPointer(2, GL_FLOAT, 0,textureCoordA); glVertexPointer(3, GL_FLOAT, 0, verticesA); glDrawElements(GL_QUADS, numPointsDrawnA, GL_UNSIGNED_BYTE, drawIndicesA); glBindTexture(GL_TEXTURE_2D, texturePointerB); glTexCoordPointer(2, GL_FLOAT, 0,textureCoordB); glVertexPointer(3, GL_FLOAT, 0, verticesB); glDrawElements(GL_QUADS, numPointsDrawnB, GL_UNSIGNED_BYTE, drawIndicesB); } 

Everything seems to be fine. My question is:

I need to call gldisableClientState somewhere; perhaps when the program is closed?

Also, is it good to do so? Is there something that I am missing as everyone else allows and disables each frame?

+2
source share
2 answers

Once you have installed some kind of OpenGL state, it remains installed. You do not need to install it every time you draw.

Manually setting the state as low as possible can be error prone - which is probably why many people do not.

+6
source

Good performance and good examples are not necessarily the same thing. Sample code exists to show you how to do something, to clearly explain how everything works. Good examples show how to minimize the chance of errors in code. And so on.

Performance often arises from what you are doing β€œrisky,” but since you are doing everything right, it works.

It’s good practice to establish any state (within reason) that you need at that time, and turn off this state as soon as you are done. This minimizes the chance of screwing. But this may be due to some performance.

Then again, for easy on / off, this may not be. Although, to be frank, the fact that you are pulling vertices from client memory instead of a buffer object and that you are not using glDrawRangeElements at the same time is probably more of a drag on performance than some additional on / off.

In short: don't worry about it.

+4
source

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


All Articles