I was trying to render a cube and apply a single texture on all faces. as well as using as little vertices as i can by passing up indexes of the vertices of the face. example:
Vertices:
static final float FACE_VERTEX[] = {
// front
0.0f, 1.0f, 1.0f, //0
0.0f, 0.0f, 1.0f, //1
1.0f, 0.0f, 1.0f, //2
1.0f, 1.0f, 1.0f, //3
//back
1.0f, 1.0f, 0.0f, //4 - 3
1.0f, 0.0f, 0.0f, //5 - 2
0.0f, 0.0f, 0.0f, //6 - 1
0.0f, 1.0f, 0.0f, //7 - 0
};
Indexes:
static final int FACE_INDEX[] = {
//front
0,1,2, 0,2,3,
//back
4,5,6, 4,6,7,
//left
7,6,1, 7,1,0,
//right
3,2,5, 3,5,4,
//top
4,7,0, 4,0,3,
//bottom
1,6,5, 1,5,2
};
texture mapping data:
final int textureCoordinateData[] =
{
// Front face
0,0, 0,1, 1,1, 1,0,
//back
0,0, 0,1, 1,1, 1,0,
//left
0,0, 0,1, 1,1, 1,0,
//right
0,0, 0,1, 1,1, 1,0,
//top
0,0, 0,1, 1,1, 1,0,
//bottom
0,0, 0,1, 1,1, 1,0,
//top
0,0, 0,1, 1,1, 1,0,
//bottom
0,0, 0,1, 1,1, 1,0,
};
The texture is rendered on all sides of the cube, except top and bottom. only the first row of the pixels of the front face are rendered along the whole top and bottom faces (see screenshot):
I am using VBOs to store the vertex/index/texture data in the GPU, and rendering with
glDrawElements(GL_TRIANGLES, indexLength, GL_UNSIGNED_INT, 0);
However this issue is because the texture data should be mapped to the passed vertex data (which is kinda annoying for a cube model) and is not calculated by the index.
My questions will be:
- Is there any way to keep the vertices data as low as possible and map the texture to the index data?
- If I create 36 vertices (some are repeated) to solve the texture mapping issue, but I created the correct indexes to render the cube, would it still be faster than using glDrawArrays
? or shall i go with glDrawArrays
and trash the indexing data anyway?
related question (that didn't answer my question):
OpenGL ES - texture map all faces of an 8 vertex cube?
If you read the first comment on the answer:
What do you mean you have to use 24 vertexes? If you have to duplicate the vertexes what is the point of using an index buffer then if you are still sending repeat data to the GPU?