[英]iPhone OpenGL : Texturing a cube issue
我真的很喜欢与OpenGl一起玩,并遵循了一些示例等
我的问题是构造一个多维数据集。 大多数侧面纹理都很好(前,后,左),但右是一团糟。
我的立方体
// 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
};
我的纹理贴图
const GLshort squareTextureCoords2[] = {
0, 5, // top left
0, 0, // bottom left
15, 0, //bottom right
15, 5 //top right
};
我的多维数据集代码
//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]);
}
因此,正如我所说,所有内容都会渲染,但立方体的一侧(不能看到顶部和底部,所以不用担心)纹理变得很奇怪
谢谢
更新**
我现在尝试了这些Coords,并且纹理没有出现在从正面到背面的每一侧,所以侧面就像是从纹理另一边缘开始的线。 我快要破解了
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
};
首先,要描述八个顶点,您需要八个纹理坐标。 您提供的缓冲区仅包含四个,因此行为在技术上是未定义的(并且可能取决于编译器在数组之后放入内存的内容,如果有的话)。
将常量作为纹理名称传递给glBindTexture也是无效的。 您应该传递glGenTextures早些时候给您的任何内容。
即使这些问题已解决,但是如果您严格地只保留8个顶点,那么当您考虑它时,肯定会想到边缘面上的纹理坐标不是很有用吗? 这意味着您必须找到每个顶点的纹理坐标,这对于每个连接的面都有意义。 尝试在纸上画草图-我不确定是否有可能。 为避免这种情况,您需要提供具有不同纹理坐标的重复顶点位置。
编辑:示例几何体的多维数据集,其中每个面都有一组完全唯一的顶点(直接在此处输入,未经测试,即可得出结论):
// 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
};
其他代码保持不变。 关键在于,在OpenGL中,您无法单独索引顶点位置和纹理坐标。 忽略颜色,法线等的可能性,OpenGL中顶点的描述是一个具有一个纹理坐标的位置。
尝试
const GLubyte cubeVertexFaces[] = {
...
1, 5, 2, // Half of right face
6, 2, 5, // Other half of right face
...
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.