本文介绍了是否需要glDisableClientState?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的用于渲染数组数据的每个示例都与以下代码相似,其中在绘图循环中,首先调用glEnableClientState以获取要使用的内容,并在完成后调用glDisableClientState:

Every example I've come across for rendering array data is similar to the following code, in which in your drawing loop you first call glEnableClientState for what you will be using and when you are done 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);
}

在我的程序中,我始终使用纹理坐标和顶点数组,因此我认为继续在每一帧启用和禁用它们是没有意义的.我将glEnableClientState移到循环外部,如下所示:

In my program I am always using texture coordinates and vertex arrays, so I thought it was pointless to keep enabling and disabling them every frame. I moved the glEnableClientState outside of the loop like so:

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);
}

似乎工作正常.我的问题是:

It seems to work fine. My question is:

我需要在某个地方调用glDisableClientState吗?也许当程序关闭时?.

Do I need to call glDisableClientState somewhere; perhaps when the program is closed?.

也可以这样做吗?因为其他所有人都启用和禁用了每个帧,所以我缺少什么了吗?

Also, is it ok to do it like this? Is there something I'm missing since everyone else enables and disables each frame?

推荐答案

设置了OpenGL状态后,它仍保持设置状态.您无需在每次绘制时都进行设置.

Once you have set some OpenGL state, it remains set. You don't need to set it each time you draw.

手动设置状态越少越容易出错-这可能就是为什么很多人不这样做的原因.

Manually setting the state as little as possible can be error-prone - this is probably why many people don't do it.

这篇关于是否需要glDisableClientState?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 07:09