【问题标题】:iPhone OpenGL : Texturing a cube issueiPhone OpenGL:纹理立方体问题
【发布时间】:2010-11-10 23:15:23
【问题描述】:

我真的很喜欢玩 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]);
}

所以正如我所说,除了立方体的一侧(看不到顶部和底部,所以不用担心),所有东西都会渲染,纹理变得很奇怪

谢谢

更新**

我现在尝试了这些坐标,但纹理不会出现在每一侧,它从前到后对其进行纹理处理,因此两侧就像纹理另一边的线条。我必须接近破解这个

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
};

【问题讨论】:

    标签: iphone opengl-es 3d


    【解决方案1】:

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

    将常量作为纹理名称传递给 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中顶点的描述是一个位置和一个纹理坐标。

    【讨论】:

    • 您好,感谢您的回复。我正在使用书籍等中的示例来创建我的代码。那么你将如何对立方体进行纹理处理。我对此真的很陌生,除了这个血腥的纹理之外,我得到了所有我想要的工作(更多的代码)
    • 第一步,只需使用总共 24 个顶点 — 每个面四个顶点,每个顶点都有自己的一组纹理坐标。所以保留您最近添加的 squareTextureCoords3,但扩展您的 cubeVertices 数组并调整您的 cubeVertexFaces。我现在必须离开办公桌,但如果问题仍然存在,我会在今晚晚些时候尝试提供一个示例。
    【解决方案2】:

    试试

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

    【讨论】:

    • 不行,我认为这是纹理坐标的问题
    猜你喜欢
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多