4

我无法加载 aBMP texture并将其显示在立方体的面上。
我正在研究。Windows 7我的编译器是Visual Studio 2010 Express。我FreeImage用来加载 bmp。我的图像位深度是 24。我的图像大小是 256 * 256。我SDL用来创建窗口和处理事件。
当我编译并运行程序时,我的立方体仍然是白色的,它的表面上什么也没有显示。我正在将纹理加载到一个GLuint变量中。当我打印该变量时,它只打印“1”。我的一个朋友告诉我,我的显卡与这个版本的不兼容OpenGL。所以我在虚拟机上编译并运行程序,Ubuntu它工作正常。立方体纹理成功。请你帮我运行它我的Windows正确吗?
如果你需要我的显卡是NVIDIA GT240 DDR5.

编辑

这是我的 initgl 函数:

//OpenGL initialization.
int initGL(void)
{
    glEnable(GL_TEXTURE_2D); //Enable texture mapping.
    if(!loadGLTextures("bmp_24.bmp"))
        return false;

    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f , 0.0f , 0.0f , 0.5f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    //Nice perspective.
    glHint(GL_PERSPECTIVE_CORRECTION_HINT , GL_NICEST);
    return true;
}

这是加载图像的函数。GLuint 纹理[1] 之前已定义:

//This function will load a bitmap image.
bool loadGLTextures(const char* file)
{
    FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;

    fif = FreeImage_GetFileType(file , 0);
    if(fif == FIF_UNKNOWN)
        fif = FreeImage_GetFIFFromFilename(file);
    if(fif == FIF_UNKNOWN)
        return false;

    FIBITMAP* dib = FreeImage_Load(fif , file);

    if(!dib)
        return false;
    
    //Create the texture.
    glGenTextures(1 , &texture[0]);

    //Typical texture generation using data from the bitmap.
    glBindTexture(GL_TEXTURE_2D , texture[0]);

    //Generate the texture.
    glTexImage2D(GL_TEXTURE_2D , 0 , GL_RGB , FreeImage_GetWidth(dib) , 
        FreeImage_GetHeight(dib) , 0 , GL_BGR_EXT , GL_UNSIGNED_BYTE , 
        FreeImage_GetBits(dib));



    //Linear filtering.
    glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR);

    printf("%d" , texture[0]);

    FreeImage_Unload(dib);

    return true;

}

这是我的渲染功能:

//All of the drawing goes through this.
int drawGLScene(void)
{
    static float xrot = 0 , yrot = 0 , zrot = 0;
    //Clear screen and depth buffer.
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glTranslatef(0.0f , 0.0f , -5.0f);
    glRotatef(xrot , 1.0f , 0.0f , 0.0f);
    glRotatef(yrot , 0.0f , 1.0f , 0.0f);
    glRotatef(zrot , 0.0f , 0.0f  ,1.0f);

    //Select the texture.
    glBindTexture(GL_TEXTURE_2D , texture[0]);

    glBegin(GL_QUADS);
    //Front:
    //Bottom left of the texture and quad.
    glTexCoord2f(0.0f , 0.0f); glVertex3f(-1.0f , -1.0f , 1.0f);
    //Bottom right fo the texture and quad.
    glTexCoord2f(1.0f , 0.0f); glVertex3f(1.0f , -1.0f , 1.0f);
    //Top right of the texture and quad.
    glTexCoord2f(1.0f , 1.0f); glVertex3f(1.0f , 1.0f , 1.0f);
    //Top left of the texture and quad.
    glTexCoord2f(0.0f , 1.0f); glVertex3f(-1.0f , 1.0f , 1.0f);

    //Back:
    //Bottom left of the texture and quad.
    glTexCoord2f(0.0f , 0.0f); glVertex3f(1.0f , -1.0f , -1.0f);
    //Bottom right of the texture and quad.
    glTexCoord2f(1.0f , 0.0f); glVertex3f(-1.0f , -1.0f , -1.0f);
    //Top right of the texture and the quad.
    glTexCoord2f(1.0f , 1.0f); glVertex3f(-1.0f , 1.0f , -1.0f);
    //Top left of the texture and the quad.
    glTexCoord2f(0.0f , 1.0f); glVertex3f(1.0f , 1.0f , -1.0f);

    //Top:
    //Top right of the texture and quad.
    glTexCoord2f(1.0f , 1.0f); glVertex3f(1.0f , 1.0f , -1.0f);
    //Top left of the texture and quad.
    glTexCoord2f(0.0f , 1.0f); glVertex3f(-1.0f , 1.0f , -1.0f);
    //Bottom left of the texture and quad.
    glTexCoord2f(0.0f , 0.0f); glVertex3f(-1.0f , 1.0f , 1.0f);
    //Bottom right of the texture and quad.
    glTexCoord2f(0.0f , 1.0f); glVertex3f(1.0f , 1.0f , 1.0f);

    //Bottom:
    //Top left of the texture and quad.
    glTexCoord2f(0.0f , 1.0f); glVertex3f(-1.0f , -1.0f , 1.0f);
    //Bottom left of the texture and quad.
    glTexCoord2f(0.0f , 0.0f); glVertex3f(-1.0f , -1.0f , -1.0f);
    //Bottom right of the texture and quad.
    glTexCoord2f(1.0f , 0.0f); glVertex3f(1.0f , -1.0f , -1.0f);
    //Top right of the texture and quad.
    glTexCoord2f(1.0f , 1.0f); glVertex3f(1.0f , -1.0f , 1.0f);

    //Right:
    //Bottom right of the texture and quad.
    glTexCoord2f(1.0f , 0.0f); glVertex3f(1.0f , -1.0f , -1.0f);
    //Top right of the texture and quad.
    glTexCoord2f(1.0f , 1.0f); glVertex3f(1.0f , 1.0f , -1.0f);
    //Top left of the texture and quad.
    glTexCoord2f(0.0f , 1.0f); glVertex3f(1.0f , 1.0f , 1.0f);
    //Bottom left of the texture and quad.
    glTexCoord2f(0.0f , 0.0f); glVertex3f(1.0f , -1.0f , 1.0f);

    //Left:
    //Bottom left of the texture and quad.
    glTexCoord2f(0.0f , 0.0f); glVertex3f(-1.0f , -1.0f , -1.0f);
    //Bottom right of the texture and quad.
    glTexCoord2f(1.0f , 0.0f); glVertex3f(-1.0f , -1.0f , 1.0f);
    //Top right of the texture and quad.
    glTexCoord2f(1.0f , 1.0f); glVertex3f(-1.0f , 1.0f , 1.0f);
    //Top left of the texture and quad.
    glTexCoord2f(0.0f , 1.0f); glVertex3f(-1.0f , 1.0f , -1.0f);
    glEnd();

    SDL_GL_SwapBuffers();

    xrot += 0.1;
    yrot += 0.1;
    zrot += 0.1;

    return true;

}
4

3 回答 3

6

您的代码中有许多可能出错的地方。

我的怀疑是,你initGL()过早地打电话,即没有可用的上下文。您没有显示有问题的代码,所以我只能对此进行猜测。

启用纹理单元仅对绘图很重要。您可以在禁用纹理单元的情况下上传纹理数据。我强烈建议你把它放在glEnable(GL_TEXTURE_2D);你的四边形绘图代码之前而不是其他任何地方(实际上你应该只调用 glEnable 和 glDisable 到绘图代码中,原因有几个)。

加载纹理时您无法执行若干健全性检查。此外,您忘记正确设置像素存储参数,但这会使您的纹理看起来失真。此外,从您的加载函数返回纹理 ID,而不是布尔值并将 ID 放入全局变量中也是有意义的。纹理 ID 0 是保留的,因此您可以使用它来指示错误。

不过,您设置了过滤器模式,因此这不是未定义 mipmap 级别的问题。

于 2012-06-17T16:32:49.287 回答
0

GL_TEXTURES_2D需要在创建纹理之前启用,所以需要在调用glEnable(GL_TEXTURE_2D);之前调用glGenTextures(1 , &texture[0]);

此外,要启用透明度,您需要调用 `glEnable(GL_BLEND);`

但这并不能解释为什么它可以在 Ubuntu 上运行,但不能在 Windows 上运行......我会说试一试,看看它是否有帮助。

编辑:现在您已经发布了渲染功能,我注意到的一件事是您没有设置渲染颜色。试着打电话

glColor4f(r,g,b,a); // r, g, b and a are float between [0,1], specifying your desired color

介于glLoadIdentity();你的第一个glVertex3f电话之间

于 2012-06-17T10:54:46.820 回答
0

您的纹理尺寸可能不是 2 的幂,即 1、2、4、8、16、32、64 等等。

这也可能在某些硬件上导致这样的问题,通常是在较旧的硬件上。

干杯;)

于 2013-05-22T10:00:24.327 回答