OpenGL texture is all black when rendered with shader
I have a very simple OpenGL application that renders only one textured quad. This is my code, which works just fine (the textured quad appears just fine):
// Bind the test texture
glBindTexture(GL_TEXTURE_2D, mTestTexture);
// Draw the quad
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(x, y + (float)height, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(x + (float)width, y + (float)height, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(x + (float)width, y, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(x, y, 0.0f);
glEnd();
Then I wanted to intoduce a simple shader. So I modified my code a little:
// Use shader and point it to the right texture
auto texLocation = glGetUniformLocation(mProgram, "tex");
glUseProgram(mProgram);
glUniform1i(texLocation, mTestTexture);
// Draw the quad
// Same drawing code as before...
Vertex shader:
void main(void)
{
gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
}
Fragment shader:
uniform sampler2D tex;
void main()
{
vec4 color = texture2D(tex, gl_TexCoord[0].st);
gl_FragColor = color;
}
Now all I get is a black quad :-(
I already tried and tested a lot of things:
Does anyone have an idea why I won't see my texture when using the shader?
glUniform1i(texLocation, mTestTexture);
^^^^^^^^^^^^ texture object
Texture unit indexes are bound to samplers, not texture objects.
Use texture unit zero instead:
glUniform1i(texLocation, 0);
链接地址: http://www.djcxy.com/p/33994.html