Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Coord纹理阵列不适用于立方体_Arrays_Opengl_Coordinates_Textures - Fatal编程技术网

Arrays Coord纹理阵列不适用于立方体

Arrays Coord纹理阵列不适用于立方体,arrays,opengl,coordinates,textures,Arrays,Opengl,Coordinates,Textures,我的程序的目的是加载和显示一个简单的立方体,每个面上都有相同的纹理。问题是前两个面(前、后)良好。我在纹理数组中尝试了几种顶点组合,但都不起作用。我不知道是否需要在纹理数组中添加更多顶点,或更改顺序或更改索引数组 #define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes) float vertices[] = { -1.0f, -1.0f, 1.0f, // 0 1.0f, -1.0f, 1.0f, // 1 -1

我的程序的目的是加载和显示一个简单的立方体,每个面上都有相同的纹理。问题是前两个面(前、后)良好。我在纹理数组中尝试了几种顶点组合,但都不起作用。我不知道是否需要在纹理数组中添加更多顶点,或更改顺序或更改索引数组

#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)

float vertices[] =
{
    -1.0f, -1.0f, 1.0f, // 0 
    1.0f, -1.0f, 1.0f,  // 1 
    -1.0f, 1.0f, 1.0f,  // 2 
    1.0f, 1.0f, 1.0f,   // 3 

    -1.0f, -1.0f, -1.0f,// 4 
    1.0f, -1.0f, -1.0f, // 5 
    -1.0f, 1.0f, -1.0f, // 6 
    1.0f, 1.0f, -1.0f,  // 7 
}; 

GLubyte indices[] =
{
    0,1,2, 1,3,2, //front face
    6,7,5, 6,5,4, //rear face
    1,5,3, 5,7,3, //problem on the right face
    //2,3,6, 3,6,7,
    //2,4,0, 2,4,6
    0,5,1, 5,4,0
}; 

float textures[] =
{
    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3
}; 

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_WM_SetCaption("Texture Mapping",NULL);
    SDL_SetVideoMode(500, 500, 32, SDL_OPENGL);

    bool continuer = true;
    SDL_Event event;
    GLuint texCube;

    glClearDepth(1.0f);
    glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(70.0f, (float)500.0f / (float)500.0f, 1.0f, 3000.0f);
    glewInit();

    texCube = loadTexture("caisse.jpg");
    glBindTexture(GL_TEXTURE_2D, texCube);

    glEnable(GL_TEXTURE_2D);

    while (continuer)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = false;
        }

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

        //Draw Cube ---------------------------------------

        glPushMatrix();
        glRotatef(90.0f, 1.0f, 1.0f, 0.0f);

        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, 0, vertices);

        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glTexCoordPointer(2, GL_FLOAT, 0, textures);

        glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_BYTE, indices);

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);

        //-------------------------------------------------

        glPopMatrix();

        glFlush();
        SDL_GL_SwapBuffers();
    }

    glDisable(GL_TEXTURE_2D);
    SDL_Quit();

    return 0;
}

您的角点共享纹理坐标,这根本不起作用。
每个面应该有4个顶点,因为每个角点的纹理坐标会因面而异。

您的角点共享纹理坐标,这是行不通的。
每个面应该有4个顶点,因为每个角的纹理坐标因面而异。

我的问题的解决方案是以下代码:

#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)

float angle = 0.0f;

float vertices[72] =
{
    -1.0f, -1.0f, 1.0f,//VO - 0
    1.0f, -1.0f, 1.0f,//V1 - 1
    -1.0f, 1.0f, 1.0f,//V2 - 2
    1.0f, 1.0f, 1.0f,//V3 - 3

    -1.0f, -1.0f, -1.0f,//V4 - 4
    1.0f, -1.0f, -1.0f,//V5 - 5
    -1.0f, 1.0f, -1.0f,//V6 - 6
    1.0f, 1.0f, -1.0f,//V7 - 7

    -1.0f, 1.0f, 1.0f,//V2 - 8
    1.0f, 1.0f, 1.0f,//V3 - 9
    -1.0f, 1.0f, -1.0f,//V6 - 10
    1.0f, 1.0f, -1.0f,//V7 - 11

    -1.0f, -1.0f, 1.0f,//VO - 12
    1.0f, -1.0f, 1.0f,//V1 - 13
    -1.0f, -1.0f, -1.0f,//V4 - 14
    1.0f, -1.0f, -1.0f,//V5 - 15

    -1.0f, -1.0f, 1.0f,//VO - 16
    -1.0f, 1.0f, 1.0f,//V2 - 17
    -1.0f, -1.0f, -1.0f,//V4 - 18
    -1.0f, 1.0f, -1.0f,//V6 - 19

    1.0f, -1.0f, 1.0f,//V1 - 20
    1.0f, 1.0f, 1.0f,//V3 - 21
    1.0f, -1.0f, -1.0f,//V5 - 22
    1.0f, 1.0f, -1.0f,//V7 - 23
}; 

GLubyte indices[36] =
{
    0,1,2, 1,3,2,
    6,7,5, 6,5,4,
    8,9,10, 9,10,11,
    12,13,14, 13,14,15,
    17,16,18, 17,18,19,
    20,21,22, 21,22,23
}; 

float textures[48] =
{
    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3
}; 

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_WM_SetCaption("Texture Mapping",NULL);
    SDL_SetVideoMode(500, 500, 32, SDL_OPENGL);

    bool continuer = true;
    SDL_Event event;
    GLuint texCube;

    glClearDepth(1.0f);
    glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(70.0f, (float)500.0f / (float)500.0f, 1.0f, 3000.0f);
    glewInit();

    texCube = loadTexture("caisse.jpg");
    glBindTexture(GL_TEXTURE_2D, texCube);

    glEnable(GL_TEXTURE_2D);

    while (continuer)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = false;
        }

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

        //Draw Cube ---------------------------------------

        glPushMatrix();
        glRotatef(angle, 1.0f, 1.0f, 0.0f);

        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, 0, vertices);

        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glTexCoordPointer(2, GL_FLOAT, 0, textures);

        glDrawElements(GL_TRIANGLES, 48, GL_UNSIGNED_BYTE, indices);

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);

        //-------------------------------------------------

        angle += 1.0f;

        glPopMatrix();

        glFlush();
        SDL_GL_SwapBuffers();
    }

    glDisable(GL_TEXTURE_2D);
    SDL_Quit();

    return 0;
}

我的问题的解决方案是以下代码:

#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)

float angle = 0.0f;

float vertices[72] =
{
    -1.0f, -1.0f, 1.0f,//VO - 0
    1.0f, -1.0f, 1.0f,//V1 - 1
    -1.0f, 1.0f, 1.0f,//V2 - 2
    1.0f, 1.0f, 1.0f,//V3 - 3

    -1.0f, -1.0f, -1.0f,//V4 - 4
    1.0f, -1.0f, -1.0f,//V5 - 5
    -1.0f, 1.0f, -1.0f,//V6 - 6
    1.0f, 1.0f, -1.0f,//V7 - 7

    -1.0f, 1.0f, 1.0f,//V2 - 8
    1.0f, 1.0f, 1.0f,//V3 - 9
    -1.0f, 1.0f, -1.0f,//V6 - 10
    1.0f, 1.0f, -1.0f,//V7 - 11

    -1.0f, -1.0f, 1.0f,//VO - 12
    1.0f, -1.0f, 1.0f,//V1 - 13
    -1.0f, -1.0f, -1.0f,//V4 - 14
    1.0f, -1.0f, -1.0f,//V5 - 15

    -1.0f, -1.0f, 1.0f,//VO - 16
    -1.0f, 1.0f, 1.0f,//V2 - 17
    -1.0f, -1.0f, -1.0f,//V4 - 18
    -1.0f, 1.0f, -1.0f,//V6 - 19

    1.0f, -1.0f, 1.0f,//V1 - 20
    1.0f, 1.0f, 1.0f,//V3 - 21
    1.0f, -1.0f, -1.0f,//V5 - 22
    1.0f, 1.0f, -1.0f,//V7 - 23
}; 

GLubyte indices[36] =
{
    0,1,2, 1,3,2,
    6,7,5, 6,5,4,
    8,9,10, 9,10,11,
    12,13,14, 13,14,15,
    17,16,18, 17,18,19,
    20,21,22, 21,22,23
}; 

float textures[48] =
{
    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3
}; 

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_WM_SetCaption("Texture Mapping",NULL);
    SDL_SetVideoMode(500, 500, 32, SDL_OPENGL);

    bool continuer = true;
    SDL_Event event;
    GLuint texCube;

    glClearDepth(1.0f);
    glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(70.0f, (float)500.0f / (float)500.0f, 1.0f, 3000.0f);
    glewInit();

    texCube = loadTexture("caisse.jpg");
    glBindTexture(GL_TEXTURE_2D, texCube);

    glEnable(GL_TEXTURE_2D);

    while (continuer)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = false;
        }

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

        //Draw Cube ---------------------------------------

        glPushMatrix();
        glRotatef(angle, 1.0f, 1.0f, 0.0f);

        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, 0, vertices);

        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glTexCoordPointer(2, GL_FLOAT, 0, textures);

        glDrawElements(GL_TRIANGLES, 48, GL_UNSIGNED_BYTE, indices);

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);

        //-------------------------------------------------

        angle += 1.0f;

        glPopMatrix();

        glFlush();
        SDL_GL_SwapBuffers();
    }

    glDisable(GL_TEXTURE_2D);
    SDL_Quit();

    return 0;
}

以及使用VBO的解决方案:

#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)

float angle = 0.0f;

float vertices[72] =
{
    -1.0f, -1.0f, 1.0f,//VO - 0
    1.0f, -1.0f, 1.0f,//V1 - 1
    -1.0f, 1.0f, 1.0f,//V2 - 2
    1.0f, 1.0f, 1.0f,//V3 - 3

    -1.0f, -1.0f, -1.0f,//V4 - 4
    1.0f, -1.0f, -1.0f,//V5 - 5
    -1.0f, 1.0f, -1.0f,//V6 - 6
    1.0f, 1.0f, -1.0f,//V7 - 7

    -1.0f, 1.0f, 1.0f,//V2 - 8
    1.0f, 1.0f, 1.0f,//V3 - 9
    -1.0f, 1.0f, -1.0f,//V6 - 10
    1.0f, 1.0f, -1.0f,//V7 - 11

    -1.0f, -1.0f, 1.0f,//VO - 12
    1.0f, -1.0f, 1.0f,//V1 - 13
    -1.0f, -1.0f, -1.0f,//V4 - 14
    1.0f, -1.0f, -1.0f,//V5 - 15

    -1.0f, -1.0f, 1.0f,//VO - 16
    -1.0f, 1.0f, 1.0f,//V2 - 17
    -1.0f, -1.0f, -1.0f,//V4 - 18
    -1.0f, 1.0f, -1.0f,//V6 - 19

    1.0f, -1.0f, 1.0f,//V1 - 20
    1.0f, 1.0f, 1.0f,//V3 - 21
    1.0f, -1.0f, -1.0f,//V5 - 22
    1.0f, 1.0f, -1.0f,//V7 - 23
}; 

GLubyte indices[36] =
{
    0,1,2, 1,3,2,
    6,7,5, 6,5,4,
    8,9,10, 9,10,11,
    12,13,14, 13,14,15,
    17,16,18, 17,18,19,
    20,21,22, 21,22,23
}; 

float textures[48] =
{
    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3
}; 

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_WM_SetCaption("Texture Mapping",NULL);
    SDL_SetVideoMode(500, 500, 32, SDL_OPENGL);

    bool continuer = true;
    SDL_Event event;
    GLuint texCube;
    GLuint VBO[3];

    glClearDepth(1.0f);
    glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(70.0f, (float)500.0f / (float)500.0f, 1.0f, 3000.0f);
    glewInit();

    texCube = loadTexture("caisse.jpg");

    glGenBuffers(2, VBO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBO[1]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(textures), textures, GL_STATIC_DRAW);


    glEnable(GL_TEXTURE_2D);

    while (continuer)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = false;
        }

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

        //Draw Cube ---------------------------------------

        glPushMatrix();
        glRotatef(angle, 1.0f, 1.0f, 0.0f);

        glBindTexture(GL_TEXTURE_2D, texCube);

        glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
        glVertexPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));
        glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
        glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

        glDrawElements(GL_TRIANGLES, 48, GL_UNSIGNED_BYTE, OFFSET_BUFFER(0));

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);

        //-------------------------------------------------

        angle += 1.0f;

        glPopMatrix();

        glFlush();
        SDL_GL_SwapBuffers();
    }

    glDisable(GL_TEXTURE_2D);
    SDL_Quit();

    return 0;
}

以及使用VBO的解决方案:

#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)

float angle = 0.0f;

float vertices[72] =
{
    -1.0f, -1.0f, 1.0f,//VO - 0
    1.0f, -1.0f, 1.0f,//V1 - 1
    -1.0f, 1.0f, 1.0f,//V2 - 2
    1.0f, 1.0f, 1.0f,//V3 - 3

    -1.0f, -1.0f, -1.0f,//V4 - 4
    1.0f, -1.0f, -1.0f,//V5 - 5
    -1.0f, 1.0f, -1.0f,//V6 - 6
    1.0f, 1.0f, -1.0f,//V7 - 7

    -1.0f, 1.0f, 1.0f,//V2 - 8
    1.0f, 1.0f, 1.0f,//V3 - 9
    -1.0f, 1.0f, -1.0f,//V6 - 10
    1.0f, 1.0f, -1.0f,//V7 - 11

    -1.0f, -1.0f, 1.0f,//VO - 12
    1.0f, -1.0f, 1.0f,//V1 - 13
    -1.0f, -1.0f, -1.0f,//V4 - 14
    1.0f, -1.0f, -1.0f,//V5 - 15

    -1.0f, -1.0f, 1.0f,//VO - 16
    -1.0f, 1.0f, 1.0f,//V2 - 17
    -1.0f, -1.0f, -1.0f,//V4 - 18
    -1.0f, 1.0f, -1.0f,//V6 - 19

    1.0f, -1.0f, 1.0f,//V1 - 20
    1.0f, 1.0f, 1.0f,//V3 - 21
    1.0f, -1.0f, -1.0f,//V5 - 22
    1.0f, 1.0f, -1.0f,//V7 - 23
}; 

GLubyte indices[36] =
{
    0,1,2, 1,3,2,
    6,7,5, 6,5,4,
    8,9,10, 9,10,11,
    12,13,14, 13,14,15,
    17,16,18, 17,18,19,
    20,21,22, 21,22,23
}; 

float textures[48] =
{
    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3

    0.0f, 1.0f,//0
    1.0f, 1.0f,//1
    0.0f, 0.0f,//2
    1.0f, 0.0f,//3
}; 

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    SDL_WM_SetCaption("Texture Mapping",NULL);
    SDL_SetVideoMode(500, 500, 32, SDL_OPENGL);

    bool continuer = true;
    SDL_Event event;
    GLuint texCube;
    GLuint VBO[3];

    glClearDepth(1.0f);
    glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(70.0f, (float)500.0f / (float)500.0f, 1.0f, 3000.0f);
    glewInit();

    texCube = loadTexture("caisse.jpg");

    glGenBuffers(2, VBO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBO[1]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(textures), textures, GL_STATIC_DRAW);


    glEnable(GL_TEXTURE_2D);

    while (continuer)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                continuer = false;
        }

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

        //Draw Cube ---------------------------------------

        glPushMatrix();
        glRotatef(angle, 1.0f, 1.0f, 0.0f);

        glBindTexture(GL_TEXTURE_2D, texCube);

        glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
        glVertexPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));
        glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
        glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

        glDrawElements(GL_TRIANGLES, 48, GL_UNSIGNED_BYTE, OFFSET_BUFFER(0));

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);

        //-------------------------------------------------

        angle += 1.0f;

        glPopMatrix();

        glFlush();
        SDL_GL_SwapBuffers();
    }

    glDisable(GL_TEXTURE_2D);
    SDL_Quit();

    return 0;
}

如果确实想节省几个字节,仍然可以分解几个顶点(16个顶点而不是24个顶点)。你考虑你的立方体没有两个相反的面孔,并分别添加这两个面孔。以下是一个IQM模型,其功能如下:

# Inter-Quake Export 

mesh "Cube" 

material "cube.tga" 

vp 1 1 -1 
vn 1 1 -1 
vt 0 0 

vp 1 -1 -1 
vn 1 -1 -1 
vt 0 1 

vp -1 -1 -1 
vn -1 -1 -1 
vt 1 1 

vp -1 1 -1 
vn -1 1 -1 
vt 1 0 

vp 1 1 1 
vn 1 1 1 
vt 1 0 

vp -1 1 1 
vn -1 1 1 
vt 0 0 

vp -1 -1 1 
vn -1 -1 1 
vt 0 1 

vp 1 -1 1 
vn 1 -1 1 
vt 1 1 

vp 1 1 -1 
vn 1 1 -1 
vt 0 0 

vp -1 1 -1 
vn -1 1 -1 
vt 1 0 

vp -1 1 1 
vn -1 1 1 
vt 1 1 

vp 1 1 1 
vn 1 1 1 
vt 0 1 

vp 1 1 -1 
vn 1 1 -1 
vt 0 0 

vp -1 1 -1 
vn -1 1 -1 
vt 1 0 

vp -1 1 1 
vn -1 1 1 
vt 1 1 

vp 1 1 1 
vn 1 1 1 
vt 0 1 

fm 0 1 2 
fm 0 2 3 
fm 0 4 7 
fm 0 7 1 
fm 4 6 7 
fm 4 5 6 
fm 5 2 6 
fm 5 3 2 
fm 8 9 10 
fm 8 10 11 
fm 12 15 14 
fm 12 14 13

如果确实想节省几个字节,仍然可以分解几个顶点(16个顶点而不是24个顶点)。你考虑你的立方体没有两个相反的面孔,并分别添加这两个面孔。以下是一个IQM模型,其功能如下:

# Inter-Quake Export 

mesh "Cube" 

material "cube.tga" 

vp 1 1 -1 
vn 1 1 -1 
vt 0 0 

vp 1 -1 -1 
vn 1 -1 -1 
vt 0 1 

vp -1 -1 -1 
vn -1 -1 -1 
vt 1 1 

vp -1 1 -1 
vn -1 1 -1 
vt 1 0 

vp 1 1 1 
vn 1 1 1 
vt 1 0 

vp -1 1 1 
vn -1 1 1 
vt 0 0 

vp -1 -1 1 
vn -1 -1 1 
vt 0 1 

vp 1 -1 1 
vn 1 -1 1 
vt 1 1 

vp 1 1 -1 
vn 1 1 -1 
vt 0 0 

vp -1 1 -1 
vn -1 1 -1 
vt 1 0 

vp -1 1 1 
vn -1 1 1 
vt 1 1 

vp 1 1 1 
vn 1 1 1 
vt 0 1 

vp 1 1 -1 
vn 1 1 -1 
vt 0 0 

vp -1 1 -1 
vn -1 1 -1 
vt 1 0 

vp -1 1 1 
vn -1 1 1 
vt 1 1 

vp 1 1 1 
vn 1 1 1 
vt 0 1 

fm 0 1 2 
fm 0 2 3 
fm 0 4 7 
fm 0 7 1 
fm 4 6 7 
fm 4 5 6 
fm 5 2 6 
fm 5 3 2 
fm 8 9 10 
fm 8 10 11 
fm 12 15 14 
fm 12 14 13

可能的重复不是完全相同的问题,但我搜索了很长时间没有任何结果,我没有找到一个图图来帮助我。我两天以来一直在研究这个问题。谢谢你的帮助。可能的重复不是完全相同的问题,但我搜索了很长时间没有任何结果,我没有找到一个图图来帮助我。我两天以来一直在研究这个问题。谢谢你的帮助。谢谢你的回答。我尝试使用glDrawArrays,只使用了一个顶点数组和纹理数组,这个解决方案很有效(每个面有四个顶点(顶点[]和纹理[]))。但是想用infdices数组找到更好的解决方案来分解我的代码。您的意思是在本例中,我必须在顶点[]和纹理[]中添加顶点吗?是的,因为您指定的每个索引都引用位置和纹理数组中的相同顶点。尽管立方体中只有8个角,正方形中只有4个角,但立方体中的每个角的每一侧的纹理坐标并不相同。如果你试着把它画在纸上,你就会明白我的意思。是的,这就是我所看到的。但我认为它可能使用索引数组(因式分解)处理较少的顶点。因此,对于顶点数组,这种索引数组只有8个顶点的方法只适用于顶点数组。如果我想添加纹理层,我必须像你说的那样更改顶点缓冲区(所以是24个顶点),并创建一个包含24个顶点的纹理缓冲区。是这样吗?但在这种情况下,glDrawArrays函数就足够了。索引数组是无用的。再次感谢你的回答。也许还有别的办法,但据我所知,那是唯一的办法(我很想知道是否有办法)。许多更复杂的网格将具有具有相同位置和纹理坐标(例如角色或地形)的共享顶点,因此不会使用索引。我用您的解决方案解决了我的问题。事实上,它并不像你说的那样毫无用处。使用indice数组更好。更具可读性。所以非常感谢你的回答和对我的耐心。再见,谢谢你的回答。我尝试使用glDrawArrays,只使用了一个顶点数组和纹理数组,这个解决方案很有效(每个面有四个顶点(顶点[]和纹理[]))。但是想用infdices数组找到更好的解决方案来分解我的代码。您的意思是在本例中,我必须在顶点[]和纹理[]中添加顶点吗?是的,因为您指定的每个索引都引用位置和纹理数组中的相同顶点。尽管立方体中只有8个角,正方形中只有4个角,但立方体中的每个角的每一侧的纹理坐标并不相同。如果你试着把它画在纸上,你就会明白我的意思。是的,这就是我所看到的。但我认为它可能使用索引数组(因式分解)处理较少的顶点。因此,对于顶点数组,这种索引数组只有8个顶点的方法只适用于顶点数组。如果我想添加纹理层,我必须像你说的那样更改顶点缓冲区(所以是24个顶点),并创建一个包含24个顶点的纹理缓冲区。是这样吗?但在这种情况下,glDrawArrays函数就足够了。索引数组是无用的。再次感谢你的回答。也许还有别的办法,但据我所知,那是唯一的办法(我很想知道是否有办法)。许多更复杂的网格将具有具有相同位置和纹理坐标(例如角色或地形)的共享顶点,因此不会使用索引。我用您的解决方案解决了我的问题。事实上,它并不像你说的那样毫无用处。使用indice数组更好。更具可读性。所以非常感谢你的回答和对我的耐心。再见。