I'm trying to load a texture in OpenGL ES under iOS. This is the approach I'm taking:
- parsing an obj, from which I get position, normal and texcoord attributes.
- enabling (for this test) only position and texcoords to the shaders.
- converting an arbitrary 512x512 png to raw and loading that into GL, through sampler 0.
- in the vertex shader, merely returning gl_Position in canonical volume / clip space while passing the texcoords to the fragment shader.
- in the fragment shader, assigning to gl_FragColor the lookup from texture2d.
The texture file: t1.png:
The render result:
Clearly no texture mapped onto the cube. Though I'm getting some value out of texture2D() cause as you'll see, there's not lighting or color being passed or processed in the shaders... My guess is I'm somehow messing up how I think I'm converting the png to a raw buffer to pass in to the texture memory.
This is the obj file. A test cube:
# Blender v2.63 (sub 0) OBJ File: ''
# www.blender.org
mtllib cube2.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -0.000000 -0.000000 1.000000
vn -1.000000 -0.000000 -0.000000
vn 0.000000 0.000000 -1.000000
usemtl Material
s off
f 1/1/1 2/2/1 3/3/1
f 1/1/1 3/3/1 4/4/1
f 5/1/2 8/2/2 7/3/2
f 5/1/2 7/3/2 6/4/2
f 1/1/3 5/2/3 6/3/3
f 1/1/3 6/3/3 2/4/3
f 2/1/4 6/2/4 7/3/4
f 2/1/4 7/3/4 3/4/4
f 3/1/5 7/2/5 8/3/5
f 3/1/5 8/3/5 4/4/5
f 5/1/6 1/2/6 4/3/6
f 5/1/6 4/3/6 8/4/6
Then there's code which parses the obj and creates a data structure - the normals are being correctly parsed. Then, the relevant code snippets are:
Make sure position and texcoords are bind during linking:
// Attach vertex shader to program.
glAttachShader(_program, vertShader);
// Attach fragment shader to program.
glAttachShader(_program, fragShader);
// Bind attribute locations.
glBindAttribLocation(_program, 0, "position");
glBindAttribLocation(_program, 1, "tCoordinates");
The vertex shader (just passes the texcoords to the fragment shader):
uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;
attribute vec4 position;
attribute vec2 tCoordinates;
varying highp vec2 tCoordinatesVarying;
void main()
{
tCoordinatesVarying = tCoordinates;
gl_Position = modelViewProjectionMatrix * position;
}
The fragment shader, which "Should" be getting a vec4 from the loaded texture and merely passing that as the frag color:
uniform sampler2D s_texture;
varying highp vec2 tCoordinatesVarying;
void main()
{
gl_FragColor = texture2D(s_texture, tCoordinatesVarying);
}
And the setup code, pre-drawing, in the app which sets the vertex attributes and the texture and texture unit / sampler binding: (Ignore my not releasing of CG objects for now)
NSString* path = [[NSBundle mainBundle] pathForResource:@"t1.png" ofType:nil];
NSData* texData = [NSData dataWithContentsOfFile:path];
UIImage* image = [UIImage imageWithData:texData];
GLuint width = CGImageGetWidth(image.CGImage);
GLuint height = CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void* imageData = malloc(height*width*4);
CGContextRef context = CGBitmapContextCreate(imageData, width, height, 8, 4*width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Host);
CGContextClearRect(context, CGRectMake(0, 0, width, height));
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage);
CGContextRelease(context);
[EAGLContext setCurrentContext:self.context];
[self loadShaders];
glEnable(GL_DEPTH_TEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &_textureId);
glBindTexture(GL_TEXTURE_2D, _textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
int loc = glGetUniformLocation(_program, "s_texture");
glUniform1i(loc, 0);
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, vboData->vertexAttributesSize, vboData->vertexAttributes, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, vboData->vertexAttributesStride,vboData->vPositionOffset);
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, vboData->vertexAttributesStride,vboData->vTCoordinatesOffset);