VBO不能与多个对象一起正常工作

时间:2012-05-03 21:34:30

标签: opengl vbo

我的程序的目的是显示一个纹理平面的立方体。如果我分开加载一个或另一个对象,一切都可以。当我在立方体之前加载平面时,显示效果也很好。但是当我在飞机前加载立方体时,平面显示是正常的,但立方体是不正确的。没有纹理映射的问题是相同的,似乎来自初始化部分。我试图找到一个徒劳的解决方案。我觉得有些东西会被记忆覆盖,但如果是这样的话,我不知道为什么。我不知道为什么装载顺序很重要。

这是我的代码:

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

float angle = 0.0f;

float verticesBox[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 indicesBox[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 texturesBox[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
};

GLfloat verticesQuad[12] =
{
    -1.0f, -1.0f, 1.0f,//V0
    1.0f, -1.0f, 1.0f,//V1
    -1.0f, -1.0f, -1.0f,//V2
    1.0f, -1.0f, -1.0f,//V3

};

GLubyte indicesQuad[6] =
{
    0,1,2, 1,2,3
};

//Textures buffer

GLfloat texturesQuad[8] =
{
    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, texQuad;
    GLuint VBOCube[3], VBOQuad[3];

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

    texCube = loadTexture("caisse.jpg");
    texQuad = loadTexture("caisse.jpg");

    //Init Cube

    glGenBuffers(3, VBOCube);

    glBindBuffer(GL_ARRAY_BUFFER, VBOCube[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verticesBox),  verticesBox, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBOCube[1]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicesBox), indicesBox, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, VBOCube[2]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(texturesBox),  texturesBox, GL_STATIC_DRAW);

    //Init Quad

    glGenBuffers(3, VBOQuad);

    glBindBuffer(GL_ARRAY_BUFFER, VBOQuad[0]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(verticesQuad), verticesQuad, GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBOQuad[1]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicesQuad), indicesQuad, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, VBOQuad[2]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(texturesQuad), texturesQuad, GL_STATIC_DRAW);

    //Main loop

    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, 6.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, VBOCube[0]);
        glVertexPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));
        glBindBuffer(GL_ARRAY_BUFFER, VBOCube[2]);
        glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

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

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);

        glPopMatrix();

        //Draw Quad ---------------------------------------

        glPushMatrix();
        glRotatef(angle, 1.0f, 1.0f, 0.0f);
        glTranslatef(0.0f, 0.0f, -4.0f);

        glBindTexture(GL_TEXTURE_2D, texQuad);

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

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

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

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);

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

        angle += 1.0f;

        glPopMatrix();

        glFlush();
        SDL_GL_SwapBuffers();
    }

    glDeleteBuffers(3, VBOCube);
    glDeleteBuffers(3, VBOQuad);

    glDisable(GL_TEXTURE_2D);
    SDL_Quit();

    return 0;
}

0 个答案:

没有答案