Question

I am trying to map a 256*256 texture onto a cube. When i do so, the front and rear faces are as expected, the left and right faces are inverted in the z direction and the top and bottom faces are "smudged" into a repeating blur. This issue im having has been covered previously, however the only solutions i can find are to use quads, which arent available in Androids implementation of OpenGL ES2.0, or to create a vertex list that contains the verticies for all faces (rather than just the 8 vertices) and then to reference them with indices 0-23 (rather than 0-7).

The code im using that gives the result above is as follows:

private float vertices[] = {

    //Front 
    -1.0f, -1.0f, 1.0f,  //0 Bottom Left
    1.0f, -1.0f, 1.0f,  //1 Bottom Right
    -1.0f, 1.0f, 1.0f,  //2 Top Left
    1.0f, 1.0f, 1.0f,  //3 Top Right

    //Back
    -1.0f, -1.0f, -1.0f, //4 Bottom Left
    1.0f, -1.0f, -1.0f, //5 Bottom Right
    -1.0f, 1.0f, -1.0f, //6 Top Left        
    1.0f, 1.0f, -1.0f //7 Top Right 
};

//Order to draw points in vertices array
private short indices[] = {
    //Faces definition
    0,1,2, 2,1,3, //Front
    5,4,7, 7,4,6, //Back
    1,5,3, 3,5,7, //Right 
    4,0,6, 6,0,2, //Left
    2,3,6, 6,3,7, //Top
    4,5,0, 0,5,1 //Bottom
};

private float textureVertices[] = {
    0.0f, 1.0f, //Front
    1.0f, 1.0f, 
    0.0f, 0.0f,
    1.0f, 0.0f,

    1.0f, 1.0f, //Back
    0.0f, 1.0f, 
    1.0f, 0.0f,
    0.0f, 0.0f,

    1.0f, 1.0f,
    0.0f, 1.0f, //Right 
    1.0f, 0.0f,
    0.0f, 0.0f,

    1.0f, 1.0f, //Left
    0.0f, 1.0f, 
    1.0f, 0.0f,
    0.0f, 0.0f,

    0.0f, 1.0f, //Top
    1.0f, 1.0f, 
    0.0f, 0.0f,
    1.0f, 0.0f,

    0.0f, 1.0f, //Bottom
    1.0f, 1.0f, 
    0.0f, 0.0f,
    1.0f, 0.0f

};

public void draw(GL10 gl){
    gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]);

    gl.glEnable(GL10.GL_CULL_FACE);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

    gl.glFrontFace(GL10.GL_CCW);
    gl.glCullFace(GL10.GL_BACK);

    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuffer); 
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer); 
    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,GL10.GL_UNSIGNED_SHORT,indicesBuffer);

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glDisable(GL10.GL_CULL_FACE);
}

I think the accepted answer in the post Mapping a square texture to a triangle nearly covers what im after, if it is indeed possible to index and texture in the way that i am expected to.

Was it helpful?

Solution

Your vertices array should match the layout of textureVertices array. That means that 8 vertices are not enought to render textured cube, beacuase the vertices at the same position might have different texture coordinates depending on what face they belong to, because indices are indices into both the texture and vertex array.

Consider a cube rotated so that an edge faces You. On the left side of the edge, the texture ends. On the right side, the texture starts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top