1

我正在用 OpenGL 制作一个立方体。通常我使用即时代码,如:

glNormal3f(0.0,1.0,0.0);
glTexCoord2f(0.0f,0.0f);
glVertex3f( 0.5f, 0.5f,-0.5f);
...

这有点过时了。现在我正在使用vertices[]andindices[]glDrawElements处理一个立方体:

static float vertices[] = 
        {
    -0.5000, -0.5000,  0.5000,
     0.5000, -0.5000,  0.5000,
    -0.5000,  0.5000,  0.5000,
    -0.5000,  0.5000,  0.5000,
     0.5000, -0.5000,  0.5000,
     0.5000,  0.5000,  0.5000, 

    -0.5000, -0.5000, -0.5000,
    -0.5000,  0.5000, -0.5000,
     0.5000, -0.5000, -0.5000,
     0.5000, -0.5000, -0.5000,
    -0.5000,  0.5000, -0.5000,
     0.5000,  0.5000, -0.5000,

     0.5000, -0.5000, -0.5000, 
     0.5000,  0.5000, -0.5000, 
     0.5000, -0.5000,  0.5000, 
     0.5000, -0.5000,  0.5000, 
     0.5000,  0.5000, -0.5000, 
     0.5000,  0.5000,  0.5000, 

    -0.5000, -0.5000, -0.5000, 
    -0.5000, -0.5000,  0.5000, 
    -0.5000,  0.5000, -0.5000, 
    -0.5000,  0.5000, -0.5000, 
    -0.5000, -0.5000,  0.5000, 
    -0.5000,  0.5000,  0.5000, 

    -0.5000, -0.5000, -0.5000,
     0.5000, -0.5000, -0.5000,
    -0.5000, -0.5000,  0.5000,
    -0.5000, -0.5000,  0.5000,
     0.5000, -0.5000, -0.5000,
     0.5000, -0.5000,  0.5000,

    -0.5000,  0.5000, -0.5000,
    -0.5000,  0.5000,  0.5000,
     0.5000,  0.5000, -0.5000,
     0.5000,  0.5000, -0.5000,
    -0.5000,  0.5000,  0.5000,
     0.5000,  0.5000,  0.5000,

};

static byte indices[] = 
{
0,  1,  2, 
3,  4,  5,

18, 19, 20,
21, 22, 23,

12, 13, 14,
15, 16, 17,

6,  7,  8,
9, 10, 11,

30, 31, 32,
33, 34, 35,

24, 25, 26,
27, 28, 29
};

问题是我不知道如何正确设置法线和纹理坐标,以便场景正确。如何用给定的数据计算它们?我试图这样做:

glNormalPointer( GL_FLOAT, 0, normals);
glTexCoordPointer(2, GL_FLOAT, 0, texcoords);

这个数据:

static float normals[] =
{
    -1.0000, -1.0000,  1.0000,
     1.0000, -1.0000,  1.0000,
    -1.0000,  1.0000,  1.0000,
    -1.0000,  1.0000,  1.0000,
     1.0000, -1.0000,  1.0000,
     1.0000,  1.0000,  1.0000, 
};

static float texcoords[] =
{
    1.0000, 0.0000, 0.0000,
    1.0000, 1.0000, 0.0000,
    0.0000, 1.0000, 0.0000,
    0.0000, 0.0000, 0.0000
};

但我设法从中得到的只是一个丑陋的混乱场景,灯光和纹理都被破坏了。

4

1 回答 1

2

您需要具有相同数量的位置、文本坐标和法线。数组中的索引indices指向三元组(pos、texcoord、normal)。因此必须复制一些 texcoords 或法线。

https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_05

您可以基于(尽管它不使用索引) http://www.opengl-tutorial.org/beginners-tutorials/tutorial-4-a-colored-cube/

于 2013-10-15T12:39:46.710 回答