3

I'll keep it simple: The code listed at the end of this post is in top-to-bottom order within my project. I have an OGLES2.0 framework in place which renders everything except textures perfectly. When rendering a single texture all I see is a black box of the correct dimensions.

Here's what I've verified:

  • Having output the texture bytes (it's format GL_ALPHA) it's plain to see there are zero and none-zero values, so the data looks correct (or at least not all black!).

  • The texture ids are correct, verified by using the following in the vertex shader: gl_FragColor=vec4(v_texCoord.xy,0.0,1.0); ..and observing the expected black->green->yellow->red colour flow moving from (0,0)->(0,1)->(1,1)->(1,0) is seen.

  • My texture has power of two dimensions: 256 x 64, correctly reflected in the data array.

I'd be tremendously grateful for help in determining what's wrong, as after several hours of googling and prodding I'm stumped!

glGenTextures(1, &_textureId); GLint savedId; glGetIntegerv(GL_TEXTURE_BINDING_2D, &savedId); glBindTexture(GL_TEXTURE_2D, _textureId); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); GLenum datatype = -1; GLenum format = -1; switch([self pixelFormat]) { case kGLTexturePixelFormat_RGBA8888: format=GL_RGBA; datatype=GL_UNSIGNED_BYTE; break; case kGLTexturePixelFormat_RGB565: format=GL_RGB; datatype=GL_UNSIGNED_SHORT_5_6_5; break; case kGLTexturePixelFormat_A8: // * This is current format, used for testing. format=GL_ALPHA; datatype=GL_UNSIGNED_BYTE; break; default: [NSException raise:NSInternalInconsistencyException format:@""]; } glTexImage2D(GL_TEXTURE_2D, 0, format, [self pixelsWide], [self pixelsHigh], 0, format, datatype, [self data]); glBindTexture(GL_TEXTURE_2D, savedId); //... GLint s_textureId = glGetUniformLocation(program, "s_texture"); //... glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, [_textureAtlas textureId]); glUniform1i(s_textureId, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // In vertex shader: attribute vec2 a_texCoord; varying vec2 v_texCoord; uniform sampler2D s_texture; void main() { // ... v_texCoord = a_texCoord; } // In fragment shader: varying vec2 v_texCoord; uniform sampler2D s_texture; void main() { // ... gl_FragColor = texture2D(s_texture, v_texCoord); } 

1 Answer 1

6

Your code looks fine, and I must say that I expect the output color to be black : as your texture is of GL_ALPHA format, the documentation of GL ES says :

GL_ALPHA Each element is a single alpha component. The GL converts it to floating point and assembles it into an RGBA element by attaching 0 for red, green, and blue.

So if you want to display your alpha value as a greyscale color, you'll need to instruct your fragment shader to do so :

gl_FragColor = texture2D(s_texture, v_texCoord).aaaa; 

This will duplicate the alpha value in all red, green, red and alpha channels.

If you want your output alpha channel to remain 1.0 (opaque), then you'll want :

gl_FragColor = vec4( texture2D(s_texture, v_texCoord).aaa, 1.0 ); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you SO much, you're exactly right! I modified to use .aaaa and I can see my text now :-) Very, very grateful indeed, have a beer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.