Question

I am making a cube in OpenGL. Normaly I used instant code like:

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

which is a bit deprecated. Now I am using vertices[] and indices[] and glDrawElements to handle a cube:

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

The problem is that I don't know how to set the normals and texture coords properly so the scene would be correct. How can I count them with the given data? I have tried to do so with:

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

and this data:

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

but all that I managed to get from this was an ugly messy scene with broken lighting and textures.

Était-ce utile?

La solution

You need to have the same number of positions, textcoords and normals. Index in your indices array points to a triplet (pos, texcoord, normal). Thus some texcoords or normals will have to be duplicated.

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

you can base on (though it does not use indices) http://www.opengl-tutorial.org/beginners-tutorials/tutorial-4-a-colored-cube/

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top