OpenGL纹理搞砸了

raa*_*aaj 0 opengl

这是我加载纹理的代码.我试图使用此示例加载文件; 它是一个gif文件.我可以问一下是否可以加载gif文件,还是只能加载原始文件?

void setUpTextures()
{

    printf("Set up Textures\n");
    //This is the array that will contain the image color information.
    // 3 represents red, green and blue color info.
    // 512 is the height and width of texture.
    unsigned char earth[512 * 512 * 3];

    // This opens your image file.
    FILE* f = fopen("/Users/Raaj/Desktop/earth.gif", "r");
    if (f){
        printf("file loaded\n");
    }else{
        printf("no load\n");
        fclose(f);
        return;
    }

    fread(earth, 512 * 512 * 3, 1, f);
    fclose(f);


    glEnable(GL_TEXTURE_2D);

    //Here 1 is the texture id
    //The texture id is different for each texture (duh?)
    glBindTexture(GL_TEXTURE_2D, 1);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    //In this line you only supply the last argument which is your color info array,
    //and the dimensions of the texture (512)
    glTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE,earth);

    glDisable(GL_TEXTURE_2D);
}


void Draw()
{

    glEnable(GL_TEXTURE_2D);
    // Here you specify WHICH texture you will bind to your coordinates.
    glBindTexture(GL_TEXTURE_2D,1);
    glColor3f(1,1,1);


    double n=6;
    glBegin(GL_QUADS);
    glTexCoord2d(0,50);     glVertex2f(n/2, n/2);
    glTexCoord2d(50,0);     glVertex2f(n/2, -n/2);
    glTexCoord2d(50,50);    glVertex2f(-n/2, -n/2);
    glTexCoord2d(0,50);     glVertex2f(-n/2, n/2);
    glEnd();
    // Do not forget this line, as then the rest of the colors in your
    // Program will get messed up!!!
    glDisable(GL_TEXTURE_2D);
}
Run Code Online (Sandbox Code Playgroud)

我得到的就是这个: 在此输入图像描述

我能知道为什么吗?

Jas*_*onD 5

基本上,不,你不能只给GL任意纹理格式 - 它只需要像素数据,而不是编码文件.

您发布的代码清楚地声明了一个24位RGB数据的数组,但随后您打开并尝试从GIF文件中读取那么多数据.GIF是一种压缩和palettised格式,包含标题信息等,因此永远不会起作用.

您需要使用图像加载器将文件解压缩为原始像素.

此外,您的纹理坐标看起来不正确.有四个顶点,但只使用了3个不同的坐标,2个相邻的坐标彼此对角相对.即使您的纹理正确加载,也不太可能是您想要的.