In my first Android OpenGL Application I want to render a simple cube with a texture to the screen. I load my object from an obj File and if I render it with a color it works great but with a texture I have some problems. I have debuged my code and looked at all arrays and found out that all is read fine from the file. Here is my obj file:
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 -1.918677 2.918677
vt -1.918677 -1.918677
vt 2.918677 -1.918677
vt 2.918677 2.918677
f 2/1 3/2 4/3
f 8/1 7/2 6/3
f 1/4 5/1 6/2
f 2/4 6/1 7/2
f 7/1 8/2 4/3
f 1/1 4/2 8/3
f 1/4 2/1 4/3
f 5/4 8/1 6/3
f 2/3 1/4 6/2
f 3/3 2/4 7/2
f 3/4 7/1 4/3
f 5/4 1/1 8/3
To draw the object on the screen I have these arrays (later as Buffer) and these OpenGL methods:
float vertices[] = {1f, -1f, -1f, 1f, ... , -1f, 1f, -1f };
short order[] = { 1, 2, 3, 7, ... , 4, 0, 7 }; // f o/t | o - 1
float textures[] = { -1.918677, 2.918677, ... , 2.918677 2.918677 };
Matrix.multiplyMM(mvpMatrix, 0, vpMatrix, 0, modelMatrix, 0);
GLES20.glUniformMatrix4fv(shaderMatrix, 1, false, mvpMatrix, 0);
GLES20.glEnableVertexAttribArray(shaderPosition);
GLES20.glVertexAttribPointer(shaderPosition, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer);
GLES20.glEnableVertexAttribArray(shaderTexCoordinate);
GLES20.glVertexAttribPointer(shaderTexCoordinate, 2, GLES20.GL_FLOAT, false, 0, textureBuffer);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, orderBuffer.capacity(), GLES20.GL_UNSIGNED_SHORT, orderBuffer);
GLES20.glDisableVertexAttribArray(shaderPosition);
But this does not work - the texture is not displayed correctly. I have the presumption the OpenGL does not know which triangle it has to draw with witch texture coordinate. So how can I tell this OpenGL? Do I have to create an array/buffer for the texture coordinates with the same size of the vertices so OpenGL use the first the vertices to render the triangle and the first three texturecoodinates for this triangle? Or how do I have to do it?
So in conclusion: Can I set something like the drawOrder funktion but for textureCoordinates?