2010-11-10 67 views
0

我一直很喜欢用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 
}; 

回答

3

首先,描述八个顶点,你需要八个纹理坐标。您提供的缓冲区只包含四个缓冲区,所以行为在技术上是未定义的(并且可能取决于您的编译器在数组之后放入内存中的内容)。

将一个常量作为纹理名称传递给glBindTexture也不是真的有效。你应该先通过任何glGenTextures给你回来。

即使这些问题得到解决,当您考虑这些问题时,如果您坚持只有八个顶点,边缘面上的纹理坐标将不会非常有用?这意味着您必须为每个连接的面都有意义的每个顶点找到纹理坐标。尝试在纸上画出草图 - 我不确定这是可能的。为了避免这种情况,您需要提供具有不同纹理坐标的重复顶点位置。

编辑:例如几何立方体,每个面都有一个完全独特的组顶点(直接在这里,未经测试的类型,使点):

// 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中顶点的描述是一个具有一个纹理坐标的位置。

+0

你好,感谢您的回复。我正在使用书籍等例子来创建我的代码。那么你将如何去构造一个立方体纹理。我真的很陌生,除了这个血腥的纹理之外,我得到了一切正常的工作(更多的代码) – Burf2000 2010-11-11 08:55:05

+0

作为第一步,总共使用24个顶点 - 每个面各有四个顶点,每个顶点都有自己的一组纹理坐标。因此,请保留最近添加的squareTextureCoords3,但展开cubeVertices数组并调整cubeVertexFaces。我现在不得不从我的办公桌冲下来,但如果问题仍然存在,我会在今晚稍后提供一个例子。 – Tommy 2010-11-11 18:18:00

0

尝试

const GLubyte cubeVertexFaces[] = { 
... 
    1, 5, 2, // Half of right face 
    6, 2, 5, // Other half of right face 
... 
}; 
+0

没有工作,我认为它与纹理坐标有关的问题 – Burf2000 2010-11-11 08:53:23