我无法加载BMP texture
并将其显示在立方体的表面上.
我正在研究Windows 7
。我的编译器是Visual Studio 2010 Express
.我使用FreeImage
来加载 bmp。我的图像位深度是 24.My 图像大小是 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 texture[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;
}
代码中可能出现许多错误。
我的怀疑是,你过早地打电话给initGL()
,即还没有可用的上下文。您没有显示有问题的代码,所以我只能对此进行猜测。
启用纹理单元仅对绘图很重要。您可以在禁用纹理单元的情况下上传纹理数据。我强烈建议您将该glEnable(GL_TEXTURE_2D);
放在四元绘图代码之前,而不是其他任何地方(实际上您应该仅将 glEnable 和 glDisable 调用放入绘图代码中,原因有几个)。
加载纹理时,您无法执行几次健全性检查。此外,您忘记正确设置像素存储参数,但这会使您的纹理看起来失真。此外,从加载函数返回纹理 ID 而不是布尔值并将 ID 放入全局变量中也是有意义的。Texuture ID 0 是保留的,因此您可以使用它来指示错误。
不过,您设置了过滤模式,因此这不是未定义 mipmap 级别的问题。
GL_TEXTURES_2D
需要在 创建纹理之前启用,因此您需要在调用glGenTextures(1 , &texture[0]);
之前调用glEnable(GL_TEXTURE_2D);
此外,要启用透明度,您需要调用"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
通话之间
纹理尺寸可能不是 2 的幂,即 1、2、4、8、16、32、64 等。
这也可能导致某些硬件(通常在较旧的硬件中)出现此类问题。
干杯;)