我一直很喜歡用OpenGL玩和遵循一些例子等iPhone的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]);
}
所以,正如我所說的,一切都呈現但是立方體的一側(看不到頂和底部所以不擔心)紋理變得奇怪
由於
UPDATE **
我現在嘗試了這些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
};
你好,感謝您的回覆。我正在使用書籍等例子來創建我的代碼。那麼你將如何去構造一個立方體紋理。我真的很陌生,除了這個血腥的紋理之外,我得到了一切正常的工作(更多的代碼) – Burf2000 2010-11-11 08:55:05
作爲第一步,總共使用24個頂點 - 每個面各有四個頂點,每個頂點都有自己的一組紋理座標。因此,請保留最近添加的squareTextureCoords3,但展開cubeVertices數組並調整cubeVertexFaces。我現在不得不從我的辦公桌衝下來,但如果問題仍然存在,我會在今晚稍後提供一個例子。 – Tommy 2010-11-11 18:18:00