如果您将采样器制服设置为正确的纹理单元,那么我的印象是,如果当前绑定的纹理目标为0,则无关紧要。例如,
glActiveTexture(GL_TEXTURE1);
glGenTextures(1, &mytexture);
glBindTexture(GL_TEXTURE_2D, mytexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, my_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0); // This is the line I'm wondering about
稍后画画时......
glUniform1i(glGetUniformLocation(program, "mysampler"), 1);
//draw_stuff
不幸的是,除非我将GL_TEXTURE_2D绑定到mytexture,否则屏幕全黑。当GL_TEXTURE_2D绑定到0 ???
时,采样是否合法答案 0 :(得分:4)
确切地说,将GL_TEXTUREN视为多个纹理目标类型(GL_TEXTURE_2D,GL_TEXTURE_3D等)的插槽。在激活GL_TEXTURE1并将纹理绑定到GL_TEXTURE_2D时,您告诉驱动程序,插槽1中的2d纹理将设置为“mytexture”。
然后您还需要将此信息传递给着色器:
glUniform1i(glGetUniformLocation(program, "mysampler"), 1);
这只是告诉你的着色器中的sampler2D,它应该在插槽1中查找GL_TEXTURE_2D。如果取消绑定纹理,它将无需采样。