Reputation: 5193
I been really enjoying playing with OpenGl and have followed some examples etc
My issue is texturing a cube. Most side texture fine (front, back, left) but right is a massed up mess.
My cube
// cube
static const GLfloat cubeVertices[] = {
-5.0f, 15.0f, 5.0f,
5.0f, 15.0f, 5.0f,
5.0f,0.0f, 5.0f,
-5.0f,0.0f, 5.0f,
-5.0f, 15.0f,-5.0f,
5.0f, 15.0f,-5.0f,
5.0f,0.0f,-5.0f,
-5.0f,0.0f,-5.0f
};
static const GLubyte cubeNumberOfIndices = 36;
const GLubyte cubeVertexFaces[] = {
0, 1, 5, // Half of top face
0, 5, 4, // Other half of top face
4, 6, 5, // Half of front face
4, 6, 7, // Other half of front face
0, 1, 2, // Half of back face
0, 3, 2, // Other half of back face
1, 2, 5, // Half of right face
2, 5, 6, // Other half of right face
0, 3, 4, // Half of left face
7, 4, 3, // Other half of left face
3, 6, 2, // Half of bottom face
6, 7, 3, // Other half of bottom face
};
My Texture map
const GLshort squareTextureCoords2[] = {
0, 5, // top left
0, 0, // bottom left
15, 0, //bottom right
15, 5 //top right
};
My cube code
//tell GL about our texture
glBindTexture(GL_TEXTURE_2D, 1);
glTexCoordPointer(2, GL_SHORT, 0, squareTextureCoords2);
glVertexPointer(3, GL_FLOAT, 0, cubeVertices);
for(int i = 0; i < cubeNumberOfIndices; i += 3) {
//glColor4ub(cubeFaceColors[colorIndex], cubeFaceColors[colorIndex+1], cubeFaceColors[colorIndex+2], cubeFaceColors[colorIndex+3]);
int face = (i / 3.0);
if (face % 2 != 0.0) {
}
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, &cubeVertexFaces[i]);
}
So as I said, everything renders but one side of the cube (cant see top and bottom so not worried) the texture goes all weird
Thanks
UPDATE **
I now tried these Coords and the texture does not appear on each side it textures it from front to back and so the sides are like lines from the other edge of the texture. I got to be close to cracking this
const GLfloat squareTextureCoords3[] = {
// Front face
0, 5, // top left
0, 0, // bottom left
5, 0, // bottom right
5, 5, // top right
// Top face
0, 5, // top left
0, 0, // bottom left
5, 0, // bottom right
5, 5, // top right
// Rear face
0, 5, // top left
0, 0, // bottom left
5, 0, // bottom right
5, 5, // top right
// Bottom face
0, 5, // top left
0, 0, // bottom left
5, 0, // bottom right
5, 5, // top right
// Left face
5, 5, // top left
0, 5, // bottom left
0, 0, // bottom right
5, 0, // top right
// Right face
0, 5, // top left
0, 0, // bottom left
5, 0, // bottom right
5, 5, // top right
};
Upvotes: 0
Views: 571
Reputation: 100622
First of all, to describe eight vertices you need eight texture coordinates. You're supplying a buffer containing only four, so behaviour is technically undefined (and will probably depend on what, if anything, your compiler put in memory after the array).
It's also not really valid to pass a constant as the texture name to glBindTexture. You should pass in whatever glGenTextures gave you back earlier.
Even if those problems were fixed, surely when you think about it the texture coordinates on the edge faces won't be very useful if you're sticking rigidly to having just eight vertices? That means you have to find texture coordinates for each vertex that make sense for every connected face. Try sketching it out on paper - I'm not sure it's possible. To avoid that you need to supply duplicate vertex positions with different texture coordinates.
EDIT: example geometry for a cube where each face has a completely unique set of vertices (typed directly in here, untested, to make the point):
// this is unchanged from your original
static const GLfloat squareTextureCoords3[] = {
// Front face
0, 5, // top left
0, 0, // bottom left
5, 0, // bottom right
5, 5, // top right
// Top face
0, 5, // top left
0, 0, // bottom left
5, 0, // bottom right
5, 5, // top right
// Rear face
0, 5, // top left
0, 0, // bottom left
5, 0, // bottom right
5, 5, // top right
// Bottom face
0, 5, // top left
0, 0, // bottom left
5, 0, // bottom right
5, 5, // top right
// Left face
5, 5, // top left
0, 5, // bottom left
0, 0, // bottom right
5, 0, // top right
// Right face
0, 5, // top left
0, 0, // bottom left
5, 0, // bottom right
5, 5, // top right
};
// this is changed, to match the tex coord list
static const GLfloat cubeVertices[] = {
// front face
-5.0f, 15.0f, 5.0f,
5.0f, 15.0f, 5.0f,
5.0f, 0.0f, 5.0f,
-5.0f, 0.0f, 5.0f,
// top face
-5.0f, 15.0f, 5.0f,
5.0f, 15.0f, 5.0f,
5.0f, 15.0f,-5.0f,
-5.0f, 15.0f,-5.0f,
// rear face
-5.0f, 15.0f,-5.0f,
5.0f, 15.0f,-5.0f,
5.0f,0.0f,-5.0f,
-5.0f,0.0f,-5.0f
// bottom face
-5.0f, 0.0f, 5.0f,
5.0f, 0.0f, 5.0f,
5.0f, 0.0f,-5.0f,
-5.0f, 0.0f,-5.0f,
// left face
-5.0f, 0.0f, 5.0f,
-5.0f, 0.0f,-5.0f,
-5.0f, 15.0f,-5.0f,
-5.0f, 15.0f, 5.0f,
// right face
5.0f, 0.0f, 5.0f,
5.0f, 0.0f,-5.0f,
5.0f, 15.0f,-5.0f,
5.0f, 15.0f, 5.0f,
};
// this is changed also, to match the new arrays above
const GLubyte cubeVertexFaces[] = {
4, 5, 6, // Half of top face
4, 6, 7, // Other half of top face
0, 1, 2, // Half of front face
0, 2, 3, // Other half of front face
8, 9, 10, // Half of back face
8, 10, 11, // Other half of back face
20, 21, 22, // Half of right face
20, 22, 23, // Other half of right face
16, 17, 18, // Half of left face
16, 18, 19, // Other half of left face
12, 13, 14, // Half of bottom face
12, 14, 15, // Other half of bottom face
};
Other code remains the same. The key thing is just that in OpenGL you can't separately index vertex positions and texture coordinates. Ignoring the possibility of colours, normals, etc, the description of a vertex in OpenGL is one position with one texture coordinate.
Upvotes: 3
Reputation: 2672
Try
const GLubyte cubeVertexFaces[] = {
...
1, 5, 2, // Half of right face
6, 2, 5, // Other half of right face
...
};
Upvotes: 0