OpenGL 纹理单元 纹理目标 理解 glActiveTexture glBindTexture


可以这样简单的理解为:显卡中有N个纹理单元(具体数目依赖你的显卡能力),每个纹理单元(GL_TEXTURE0、GL_TEXTURE1等)都有GL_TEXTURE_1D、GL_TEXTURE_2D等,如下代码:

struct TextureUnit
{
    GLuint targetTexture1D;
    GLuint targetTexture2D;
    GLuint targetTexture3D;
    GLuint targetTextureCube;
    ...
};
 
TextureUnit textureUnits[GL_MAX_TEXTURE_IMAGE_UNITS]
GLuint currentTextureUnit = 0;

默认情况下当前活跃的纹理单元为0.

 

void glActiveTexture(GLenum textureUnit)
{
    currentTextureUnit = textureUnit  - GL_TEXTURE0 ;
}


glActiveTextue 并不是激活纹理单元,而是选择当前活跃的纹理单元。


void glBindTexture(GLenum textureTarget, GLuint textureObject)
{
    TextureUnit *texUnit = &textureUnits[currentTextureUnit];
    switch(textureTarget)
    {
    case GL_TEXTURE_1D: texUnit->targetTexture1D = textureObject; break;
    case GL_TEXTURE_2D: texUnit->targetTexture2D = textureObject; break;
    case GL_TEXTURE_3D: texUnit->targetTexture3D = textureObject; break;
    case GL_TEXTURE_CUBEMAP: texUnit->targetTextureCube = textureObject; break;
    }
}

从示例代码中可以看到:当绑定纹理目标时,所作用的是当前活跃的纹理单元。

你可能感兴趣的:(OpenGL)