在某些 Windows 系统上使用 OpenGL 和 DevIL 的白色纹理
我在尝试使用 DevIL 加载图像并在 OpenGL 中创建纹理时遇到一些问题。当我的一个朋友在他的 Windows 上测试我的程序时,所有应该包含纹理的矩形都是白色的,没有任何图像(纹理)。该问题似乎仅出现在 Window XP、Windows Vista 和 Windows 7 上,但并非在每台 Windows PC 上都会出现:我的 Windows XP 运行该程序没有问题。
也许缺少一些 DLL 或文件(不太可能),或者某些东西不允许加载图像或将其用作纹理。另一方面,该程序在 *UNIX 系统上运行良好。
这是我用来加载图像并生成纹理的代码:
void Image::load(const char* filename)
{
ILuint ilimg;
ilGenImages(1, &ilimg);
ilBindImage(ilimg);
if (!ilLoadImage(filename))
throw ImageLoadError;
glGenTextures(1, &image);
glBindTexture(GL_TEXTURE_2D, image);
bpp = ilGetInteger(IL_IMAGE_BPP);
width = ilGetInteger(IL_IMAGE_WIDTH);
height = ilGetInteger(IL_IMAGE_HEIGHT);
format = ilGetInteger(IL_IMAGE_FORMAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, bpp, width, height, 0, format,
GL_UNSIGNED_BYTE, ilGetData());
ilDeleteImages(1, &ilimg);
}
这是使用应用的纹理绘制矩形的代码:
void Rect::show()
{
glPushAttrib(GL_CURRENT_BIT);
glEnable(GL_TEXTURE_2D);
glColor4f(1.0, 1.0, 1.0, opacity);
glBindTexture(GL_TEXTURE_2D, texture->get_image());
glBegin(GL_POLYGON);
glTexCoord2i(0, 0); glVertex2f(x, y);
glTexCoord2i(1, 0); glVertex2f(x+width, y);
glTexCoord2i(1, 1); glVertex2f(x+width, y+height);
glTexCoord2i(0, 1); glVertex2f(x, y+height);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopAttrib();
}
如果您需要我没有提到的其他代码,请询问我,我会发布它。
I'm having some problems trying to load images with DevIL and creating textures in OpenGL. When a friend of mine tested my program on his Windows, all the rects, whom were supposed to contain textures, were white, without any image (texture). The problem seems to occur only on Window XP, Windows Vista and Windows 7, but not in every Windows PC: my Windows XP runs the program without problems.
Maybe there're some missing DLLs or files (improbable), or something that don't let the image to be loaded or used as a texture. By the other hand, the program runs fine on *UNIX systems.
This is the code I'm using to load an image and generate a texture:
void Image::load(const char* filename)
{
ILuint ilimg;
ilGenImages(1, &ilimg);
ilBindImage(ilimg);
if (!ilLoadImage(filename))
throw ImageLoadError;
glGenTextures(1, &image);
glBindTexture(GL_TEXTURE_2D, image);
bpp = ilGetInteger(IL_IMAGE_BPP);
width = ilGetInteger(IL_IMAGE_WIDTH);
height = ilGetInteger(IL_IMAGE_HEIGHT);
format = ilGetInteger(IL_IMAGE_FORMAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, bpp, width, height, 0, format,
GL_UNSIGNED_BYTE, ilGetData());
ilDeleteImages(1, &ilimg);
}
This is the code that draws a rect with the texture applied:
void Rect::show()
{
glPushAttrib(GL_CURRENT_BIT);
glEnable(GL_TEXTURE_2D);
glColor4f(1.0, 1.0, 1.0, opacity);
glBindTexture(GL_TEXTURE_2D, texture->get_image());
glBegin(GL_POLYGON);
glTexCoord2i(0, 0); glVertex2f(x, y);
glTexCoord2i(1, 0); glVertex2f(x+width, y);
glTexCoord2i(1, 1); glVertex2f(x+width, y+height);
glTexCoord2i(0, 1); glVertex2f(x, y+height);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopAttrib();
}
If you need some other code that I don't mentioned, ask me and i'll post it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
glTexImage 的内部格式参数不是每像素位数。实际上它的数值与格式完全无关。 OpenGL只定义了特定的值才有效,其中还有四个具有数字关系的值,但只是作为助记符:1(GL_LUMINANCE的简写)、2(GL_LUMINANCE_ALPHA)、3(GL_RGB)、4(GL_RGBA)。还有许多其他格式标记,但它们的数值是任意选择的。
图像数据的 bpp 还用于查找格式和类型参数。您需要为此实现 LUT 或 switch-case 结构。
The internal format parameter of glTexImage is not bits per pixel. Actually its numerical value is not related to the format at all. OpenGL defines only specific values to be valid, among them also four with numeric relation, but just as a mnemonic: 1 (shorthand for GL_LUMINANCE), 2 (GL_LUMINANCE_ALPHA), 3 (GL_RGB), 4 (GL_RGBA). There are also a number of other format tokens, but their numeric value is arbitrarily choosen.
Also the bpp for the image data is used to find the format and type parameters. You'll need to implement a LUT or switch-case structure for that.
您遇到的问题听起来像是实施版本问题。可能“正确”但不太“好”的实现并不会让您逃脱一些微妙的错误。或者另一方面,实现可能有一个微妙的错误,并且不接受您的有效代码。
我可能会尝试在 texcoord 之前交换
并
声明顶点。
除此之外,如果您还没有“红皮书”,请参阅 http://glprogramming.com/red/ Chapter09.html
The issue you are experiencing sounds like an implementation version issue. Probably a "correct" but less "nice" implementation isn't letting you get away with something subtly incorrect. Or on the other hand the implementation may have a subtle bug and isn't accepting your valid code.
I might try swapping
to
and declaring vertex before texcoord like.
Short of that if you do not already have "The Red Book" consult http://glprogramming.com/red/chapter09.html