当我使用SOIL加载下一个图像时,它将替换我之前加载的所有图像

时间:2014-05-19 18:11:23

标签: opengl

这是我编码的一部分,一个简单的气球点击游戏。我有一个背景图像和另一个气球图像,代码似乎替换了我以前加载的所有图像。 Helppppp

    GLuint tex_2d;
    GLuint tex_bg;

    int main (int argc, char **argv)
    {

        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE);
        glutInitWindowSize(800, 800);
        glutInitWindowPosition(100, 100);
        glutCreateWindow("Lab Assignment");

        init();
        glutDisplayFunc(display);
        glutIdleFunc(display);

        glutMouseFunc(mouse);

        glutMainLoop();

        return EXIT_SUCCESS;
    }

这是我为图像加载编码的地方,tex_bg是background.bmp用于背景,tex_2d用于气球图像。为tex_2d加载的图像替换了比它早加载的背景图像。

    void init()
    {
        tex_bg = SOIL_load_OGL_texture
        (
            "background.bmp",
            SOIL_LOAD_AUTO,
            SOIL_CREATE_NEW_ID,
            SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
        );

        tex_2d = SOIL_load_OGL_texture
        (
            "balloonImage.bmp",
            SOIL_LOAD_AUTO,
            SOIL_CREATE_NEW_ID,
            SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
        );

        if (tex_2d == 0 || tex_bg == 0)
        {
            printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
            exit(0);
        }

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);

    }



    void drawQuadOutline()
    {
        glEnable(GL_TEXTURE_2D);

        glBegin(GL_QUADS);
            glBindTexture(GL_TEXTURE_2D, tex_bg);
            glNormal3f(0, 1, 0);
            glTexCoord2f(0.0f, 0.0f);   glVertex3f(-1.0f, -1.0f, 1.0f);
            glTexCoord2f(1.0f, 0.0f);   glVertex3f(1.0f, -1.0f, 1.0f);
            glTexCoord2f(1.0f, 1.0f);   glVertex3f(1.0f, 1.0f, 1.0f);
            glTexCoord2f(0.0f, 1.0f);   glVertex3f(-1.0f, 1.0f, 1.0f);
        glEnd();

        glDisable(GL_TEXTURE_2D);
    }

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    drawQuadOutline();

    drawCircleOutline(balloon[balloonBurst]);

    glutSwapBuffers();
}   
    void drawCircleOutline(Circle o)
    {

        float angle;

        glEnable(GL_TEXTURE_2D);

        glBegin(GL_POLYGON);
            glBindTexture(GL_TEXTURE_2D, tex_2d);
            for(angle=0.0f; angle<360.0f; angle+=2.0f)
            {        
                float radian = angle * (pi/180.0f);
                float xcos = (float)cos(radian);
                float ysin = (float)sin(radian);
                float x = xcos * o.r  + o.pos.x;
                float y = ysin * o.r  + o.pos.y;
                float tx = xcos * 0.5f + 0.5f;
                float ty = ysin * 0.5f + 0.5f;

                glTexCoord2f(tx, ty);
                glVertex2f(x, y);
            }

        glEnd();

        glDisable(GL_TEXTURE_2D);
    }

我的编码有问题吗?或滥用SOIL功能?

1 个答案:

答案 0 :(得分:1)

您的编码问题是您在glBindTexture(...)glBegin (...)之间致电glEnd (...)

这是无效的;如果您选中glGetError (...),就会知道这一点。

您需要重新编写此代码:

    glBegin(GL_POLYGON);
        glBindTexture(GL_TEXTURE_2D, tex_2d);

    ...

    glBegin(GL_QUADS);
        glBindTexture(GL_TEXTURE_2D, tex_bg);

对此:

    glBindTexture(GL_TEXTURE_2D, tex_2d);
    glBegin(GL_POLYGON);

    ...

    glBindTexture(GL_TEXTURE_2D, tex_bg);
    glBegin(GL_QUADS);