图像没有显示openGL

时间:2016-11-03 13:03:54

标签: c image opengl display soil

我正在尝试使用OpenGL和SOIL显示图像,但我无法创建它。当我调用此函数时,只显示空方块。

   void display_image(char* file_name)
{
    GLuint texture_id=loadIm(file_name);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture_id);
glLoadIdentity();
glColor3f(0.0, 0.0, 0.0);
glBindTexture(GL_TEXTURE_2D, texture_id);
glBegin(GL_QUADS);
glTexCoord2f(0.0,0.0); glVertex3f(0, 0.0, 0.0);
glTexCoord2f(0,1); glVertex3f(width, 0.0, 0.0);
glTexCoord2f(1.0,1.0); glVertex3f(width, height, 0.0);
glTexCoord2f(1,0); glVertex3f(0,height, 0.0);
glEnd();
glFlush();
glutSwapBuffers();    
}

如果你想在这里检查loadIm func,那就是:

GLuint loadIm(char *file_name)
{
   glEnable(GL_TEXTURE_2D);

    GLuint texture_id;
    glGenTextures(1, &texture_id);
width,height;

    glBindTexture(GL_TEXTURE_2D, texture_id);

    unsigned char *image = SOIL_load_image(file_name, &width, &height, 0, SOIL_LOAD_RGBA);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);

    SOIL_free_image_data(image);

    if (texture_id == -1) {
        return -1;
    }

    return texture_id;

}

我做错了什么?

0 个答案:

没有答案