I am working on an application using OpenGL ES 2.0 for an embedded device.
This is my fragment shader:
varying vec2 v_texCoord;
uniform sampler2D s_texture;
void main() {
gl_FragColor = texture2D(s_texture, v_texCoord);
}
I set up the textures properly. For some reason, calling glTexImage2D
is not producing the result I'm looking for. The texture is entirely black, rather than being filled with the data I provide it.
This is how I create the texture:
GLuint textureId;
// 2x2 Image, 3 bytes per pixel (R, G, B)
GLubyte pixels[6 * 3] =
{
255, 0, 0, // Red
0, 255, 0, // Green
0, 0, 255, // Blue
255, 255, 0, // Yellow
0, 255, 255,
255, 0, 255
};
// Use tightly packed data
glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );
// Generate a texture object
glGenTextures ( 1, &textureId );
// Bind the texture object
glBindTexture ( GL_TEXTURE_2D, textureId );
// Load the texture
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, 3, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels );
// Set the filtering mode
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
Afterward, I bind the texture like so:
samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" );
glActiveTexture ( GL_TEXTURE0 );
glBindTexture ( GL_TEXTURE_2D, textureId );
// Set the sampler texture unit to 0
glUniform1i ( samplerLoc, 0 );
I confirmed that the vertex and texture coordinates are bound and passed to the shaders when debugging. So, it has to be an issue with s_texture
or the glTexImage2D
function itself.
GetError
? You may also be callingTexImage
andTexParameter
in the wrong order (TexParameter
typically comes beforeTexImage
), though I'm not sure if that affects anything. That said, it's very unlikely to be an issue withTexImage
, and is almost definitely a mistake on your part. – user457812