我无法加载 BMP 纹理
并将其显示在立方体的面上。
我正在使用 Windows 7
。我的编译器是 Visual Studio 2010 Express
。我使用 FreeImage
加载 bmp。我的图像位深度是 24。我的图像大小是 256 * 256。我正在使用 SDL
创建窗口并处理事件。
当我编译并运行程序时,我的立方体仍然是白色的,它的表面没有任何显示。我正在将纹理加载到 GLuint
变量中。当我打印该变量时,它只打印“1”。我的一个 friend 告诉我,我的显卡与这个版本的 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;
}
最佳答案
您的代码中有很多地方可能会出错。
我的怀疑是,您过早地调用了 initGL()
,即没有可用的上下文。您没有显示有问题的代码,所以我只能对此进行猜测。
启用纹理单元仅对绘图很重要。您可以在禁用纹理单元的情况下上传纹理数据。我强烈建议您将 glEnable(GL_TEXTURE_2D);
放在您的四边形绘图代码之前,而不是其他地方(实际上,出于多种原因,您应该只调用 glEnable 和 glDisable 到绘图代码中)。
加载纹理时您未能执行多项健全性检查。此外,您忘记正确设置像素存储参数,但这会使您的纹理看起来失真。此外,从加载函数返回纹理 ID 而不是 bool 值并将 ID 放入全局变量也是有意义的。纹理 ID 0 已保留,因此您可以使用它来指示错误。
尽管您设置了过滤模式,所以这不是未定义 mipmap 级别的问题。
关于c++ - 纹理在 opengl 中显示为白色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11070476/