iPhone OpenGL:纹理立方体问题

发布于 2024-10-01 07:27:18 字数 2724 浏览 1 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叫嚣ゝ 2024-10-08 07:27:18

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

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

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.

绳情 2024-10-08 07:27:18

尝试

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

Try

const GLubyte cubeVertexFaces[] = {
...
    1, 5, 2, // Half of right face  
    6, 2, 5, // Other half of right face 
...
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文