OpenGL ES:在立方体上映射纹理

时间:2012-09-19 12:29:05

标签: opengl-es textures texture-mapping

我正在制作一个天空盒(在它的6个面上各有不同纹理的立方体)这就是我的所作所为:

我定义了我的顶点:

const float vertices[8u][3u] = {
    {-1.0f, -1.0f, -1.0f},
    {-1.0f, -1.0f, 1.0f},
    {-1.0f, 1.0f, -1.0f},
    {-1.0f, 1.0f, 1.0f},
    {1.0f, -1.0f, -1.0f},
    {1.0f, -1.0f, 1.0f},
    {1.0f, 1.0f, -1.0f},
    {1.0f, 1.0f, 1.0f}
};

我根据顶点数组定义了我的三角形(四元组在ES版本中不可用):

const unsigned short elements[8u][3u] = {
    {4u, 6u, 2u},
    {4u, 0u, 2u},
    {6u, 3u, 2u},
    {6u, 3u, 7u},
    {7u, 1u, 5u},
    {7u, 1u, 3u},
    {5u, 0u, 4u},
    {5u, 0u, 1u}
};

据我所知,我现在需要定义我的纹理坐标,我尝试了很多不同的东西,现在它看起来像这样:

float texCoords[4u][2u] = {
    {0.0f, 0.0f},
    {1.0f, 0.0f},
    {1.0f, 1.0f},
    {0.0f, 1.0f}
};

编辑:天空盒绝对不显示自己,我只看到一个二维平面图像,其中我的纹理以一种奇怪的方式重复。绝对没有立方体

绘图部分正在工作,我认为我的问题是基于纹理坐标,我是对的吗?我做错了什么?

2 个答案:

答案 0 :(得分:0)

  

我是对的吗?我做错了什么?

凝视着我的水晶球......不,我看不出你的问题是什么,你没有告诉我们。

无论如何,你的纹理坐标确实是错误的,你的数组只包含4.但你有一个8个顶点的立方体。然而,为了成为天空盒,你需要一个24个顶点的立方体,因为纹理坐标是顶点的一部分,你不能在由6个纹理组成的天空盒的某些顶点上共享纹理坐标。

但是我建议你使用立方体贴图纹理,这有很大的好处,你可以使用顶点坐标,就像你已经拥有它们一样,也可以用于纹理坐标。

答案 1 :(得分:-1)

对不起,我的问题不明确。 我真的不明白纹理坐标的概念是什么。现在我想我做了: 告诉你多少你希望每个脸都被你的纹理所充实。我的例子是完全填充。 这是我的新(看起来像)工作代码,以防它可以帮助某人:

//The 24 vertex of the 3d cube (the skybox)
static const float vertices[24u][3u] = {
        {-1.0f, -1.0f, -1.0f}, //face 1 (front)
        {-1.0f, 1.0f, -1.0f},
        {1.0f, -1.0f, -1.0f},
        {1.0f, 1.0f, -1.0f},

        {1.0f, 1.0f, 1.0f}, //face 2 (back)
        {1.0f, -1.0f, 1.0f},
        {-1.0f, 1.0f, 1.0f},
        {-1.0f, -1.0f, 1.0f},

        {-1.0f, -1.0f, -1.0f}, //face 3 (left)
        {-1.0f, -1.0f, 1.0f},
        {-1.0f, 1.0f, -1.0f},
        {-1.0f, 1.0f, 1.0f}, 

        {1.0f, 1.0f, 1.0f}, //face 4 (right)
        {1.0f, 1.0f, -1.0f},
        {1.0f, -1.0f, 1.0f},
        {1.0f, -1.0f, -1.0f},

        {-1.0f, 1.0f, -1.0f}, // face 5 (up)
        {-1.0f, 1.0f, 1.0f},
        {1.0f, 1.0f, 1.0f},
        {1.0f, 1.0f, -1.0f},

        {1.0f, -1.0f, 1.0f}, //face 6 (bot)
        {1.0f, -1.0f, -1.0f},
        {-1.0f, -1.0f, -1.0f},
        {-1.0f, -1.0f, 1.0f},
};

//Coordinate of the textures
float texCoords[24u][2u] = {
        {0.0f, 0.0f},
        {0.0f, 1.0f},
        {1.0f, 1.0f},
        {1.0f, 0.0f},

        {0.0f, 0.0f},
        {0.0f, 1.0f},
        {1.0f, 1.0f},
        {1.0f, 0.0f},

        {0.0f, 0.0f},
        {0.0f, 1.0f},
        {1.0f, 1.0f},
        {1.0f, 0.0f},

        {0.0f, 0.0f},
        {0.0f, 1.0f},
        {1.0f, 1.0f},
        {1.0f, 0.0f},

        {0.0f, 0.0f},
        {0.0f, 1.0f},
        {1.0f, 1.0f},
        {1.0f, 0.0f},

        {0.0f, 0.0f},
        {0.0f, 1.0f},
        {1.0f, 1.0f},
        {1.0f, 0.0f},        
};

//Defining triangles based on the vertice array indexes
const unsigned short elements[12u][3u] = {
        {0, 2, 1},
        {2, 1, 3},

        {4, 5, 6},
        {5, 6, 7},

        {8, 9, 10},
        {9, 10, 11},

        {12, 13, 14},
        {13, 14, 15},

        {16, 17, 18},
        {17, 18, 19},

        {20, 21, 22},
        {21, 22, 23}
};