Reputation: 4254
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);
}
Upvotes: 3
Views: 2841
Reputation: 4254
I'm sorry, as I said on a comment to cplusogl answer, the problem was I wasn't using a square texture with sides' size being a power of 2.
Upvotes: 2
Reputation: 1633
OGL/ES 2 can be picky about texture sizes. You are advised to check:
Upvotes: 1
Reputation: 1775
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);
Upvotes: 1