简体   繁体   中英

OpenGL ES 2.0 texture turns out black

I'm trying to render a texture on a plain with OpenGL ES 2.0 for the first time. I must be doing something wrong because it's always black.

The texture comes from an image in resources.

This is the code of my render engine:

Initialization:

...//program creation and linking 


UIImage* image = [UIImage imageNamed:@"marilyn.jpg"];

GLubyte* textureData = (GLubyte *)malloc(image.size.width * image.size.height * 4);
CGContext* textureContext = CGBitmapContextCreate(textureData, image.size.width, image.size.height, 8, image.size.width * 4, CGImageGetColorSpace(image.CGImage), kCGImageAlphaPremultipliedLast);
CGContextDrawImage(textureContext, CGRectMake(0.0, 0.0, (CGFloat)image.size.width, (CGFloat)image.size.height), image.CGImage);
CGContextRelease(textureContext); 

glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);        

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.size.width, image.size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);

 ...//frame and render buffers...

render

glViewport(0, 0, backingWidth, backingHeight);

glClearColor(0.3f, 0.3f, 0.4f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(program);

glUniformMatrix4fv(u_modelView, 1, GL_FALSE, modelView.Pointer());
glUniformMatrix4fv(u_projection, 1, GL_FALSE, proj.Pointer()); 
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(u_sampler, 0);

const GLfloat squareVertices[] = {
    -200.0f, -200.0f,
    200.0f,-200.0f,
    -200.0f, 200.0f,        
    200.0f,200.0f,
};
const GLfloat squareTextureCoords[] = {
    1.0, 1.0,
    1.0, 0.0,
    0.0, 1.0,
    0.0, 0.0,
};

glVertexAttribPointer(p_texture.a_position, 2, GL_FLOAT, GL_FALSE, 0, squareVertices);
glEnableVertexAttribArray(p_texture.a_position);
glVertexAttribPointer(p_texture.a_textureCoord, 2, GL_FLOAT, GL_FALSE, 0, squareTextureCoords);
glEnableVertexAttribArray(p_texture.a_textureCoord);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

I'm sorry to put all that code, but I've been looking around for hours and I can't find what is wrong.

All I see when it renders is a black square instead of Marilyn's picture.

EDIT: I forgot to show the Shaders Vertex Shader:

attribute vec4 position;
attribute vec2 texture_coord;

varying vec2 textureCoord;

uniform mat4 Projection;
uniform mat4 ModelView;

void main(void)
{
    gl_Position = Projection * ModelView * position;
    textureCoord =  texture_coord;
}

Fragment Shader:

varying mediump vec2 textureCoord;

uniform sampler2D tex;

void main(void)
{
    gl_FragColor = texture2D(tex, textureCoord);
}

对不起,正如我在对cplusogl回答的评论中所说,问题是我没有使用方形纹理,边的大小是2的幂。

OGL/ES 2 can be picky about texture sizes. You are advised to check:

  • if the texture size doesn't exceed the maximum texture size of your implementation via glGetIntegerf(GL_MAX_TEXTURE_SIZE, < intptr >);
  • if either of the texture dimensions is npot (not a power of two), you also have to ask your hardware, if that is supported. The specs state: GL/ES 2.0 shall support NPOT Textures unless a repeating texture wrap mode is used. Your code doesn't set the wrap modes, so it's default: GL_REPEAT -> this may explain the black result
  • full NPOT support may be there - if the extension GL_ARB_texture_non_power_of_two is supported (just need to query it in advance)

How did you setup your textures? Some devices only supports GL_CLAMP_TO_EDGE attribute. In OGL es 2.0 the equivalent code looks like this:

GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
                       GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
                       GLES20.GL_CLAMP_TO_EDGE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM