opengl - 在 openGL 中纹理立方体的最佳方法

标签 opengl textures vertex-array

哪个是纹理立方体的最佳方式(最低内存,最快速度)?过了一会儿,我找到了这个解决方案:

数据结构:

GLfloat Cube::vertices[] =
 {-0.5f, 0.0f, 0.5f,   0.5f, 0.0f, 0.5f,   0.5f, 1.0f, 0.5f,  -0.5f, 1.0f, 0.5f,
  -0.5f, 1.0f, -0.5f,  0.5f, 1.0f, -0.5f,  0.5f, 0.0f, -0.5f, -0.5f, 0.0f, -0.5f,
  0.5f, 0.0f, 0.5f,   0.5f, 0.0f, -0.5f,  0.5f, 1.0f, -0.5f,  0.5f, 1.0f, 0.5f,
  -0.5f, 0.0f, -0.5f,  -0.5f, 0.0f, 0.5f,  -0.5f, 1.0f, 0.5f, -0.5f, 1.0f, -0.5f
  };

 GLfloat Cube::texcoords[] = { 0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0,
                               0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0,
                               0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0,
                               0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0
                             };

 GLubyte Cube::cubeIndices[24] = {0,1,2,3, 4,5,6,7, 3,2,5,4, 7,6,1,0,
                                  8,9,10,11, 12,13,14,15};

绘制功能:
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, texture);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glColor3f(1.0f, 1.0f, 1.0f);

        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glEnableClientState(GL_VERTEX_ARRAY);

        glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
        glVertexPointer(3, GL_FLOAT, 0, vertices);

        glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);
        glDisableClientState(GL_VERTEX_ARRAY);
        //glDisableClientState(GL_COLOR_ARRAY);
        glDisable(GL_TEXTURE_2D);

如您所见,结果是正确的:
cube

但要获得这个结果,我必须重新定义一些顶点(vertices 数组中有 16 个 3D 点),否则纹理无法在 DrawElement 函数中映射。

有人知道在顶点数组中纹理立方体的更好方法吗?

最佳答案

我在使用 opengl es 时遇到了同样的问题。我的研究表明没有其他方法可以做到这一点,因为每个顶点只能有一个与之关联的纹理坐标。

关于opengl - 在 openGL 中纹理立方体的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14505969/

相关文章:

c++ - 纹理坐标不对应于 cv::Mat 坐标

c++ - GLFW 洋红色导致意外行为

android - `glTexEnv`、 `glColor4x` 和 `glBlendFunc` 在 OpenGL ES 1.0 中有何关联?

Three.js 高效地将 Uvs 映射到平面

c++ - xtgmath.h 和 cmath 构建错误

c++ - 无法将纹理绑定(bind)到 GL_QUADS

iphone - 将顶点数组和面索引加载到 OpenGL-ES 中的最快方法?

c++ - 如何在 VertexArray 中翻转纹理?

ios - OpenGL ES : Do vertex structs need x, y 和 z?

opengl - 如果使用gluPerspective(),如何知道平截头体的边界?