将 BMP 加载到 OpenGL 纹理中会切换红色和蓝色。(C++/Windows)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5123387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading a BMP into an OpenGL textures switches the red and blue colors. (C++/Windows)
提问by David Yaw
I'm trying to load a bitmap into an OpenGL texture and display it to the screen, but when I do so, the red and blue values seem to switch (e.g.: a blue image appears orange, green images remain unchanged, etc..). This problem only exists when loading bitmaps, I can load .pngs relatively error free.
我正在尝试将位图加载到 OpenGL 纹理中并将其显示到屏幕上,但是当我这样做时,红色和蓝色值似乎切换(例如:蓝色图像显示为橙色,绿色图像保持不变等。 )。这个问题只在加载位图时存在,我可以相对无误地加载.pngs。
This is the code I'm using to load the bitmaps and set the textures. I'm using DevIl, but I'm not sure how relevant that is, as the problem existed when I used a different system (I don't quite remember what, it was a function in window.h, I believe):
这是我用来加载位图和设置纹理的代码。我正在使用 DevIl,但我不确定它的相关性,因为当我使用不同的系统时存在问题(我不太记得是什么,它是 window.h 中的一个函数,我相信):
ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
ilEnable(IL_ORIGIN_SET);
ILuint image;
ilGenImages(1, &image);
ilBindImage(image);
ilLoad(IL_BMP, "Data/NeHe.bmp"); // Incidentally, loading a png, although it fixes the problem,
// rotates the image 180 degrees. Not sure if that's important or not,
// But it's why I added the first line of code
glGenTextures(3, &_texture[0]);
glBindTexture(GL_TEXTURE_2D, _texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), \
0, GL_RGB, GL_UNSIGNED_BYTE, ilGetData());
ilInit()
and glEnable(GL_TEXTURE_2D)
are both called earlier in the program, among other less relevant functions. Any help finding the cause of (and hopefully fixing) the problem would be much appreciated.
ilInit()
和glEnable(GL_TEXTURE_2D)
都在程序中较早地被调用,以及其他不太相关的函数。任何帮助找到(并希望解决)问题的原因将不胜感激。
回答by David Yaw
You've got your RGB and BGR backwards.
你的 RGB 和 BGR 倒退了。
glTexImage2D(GL_TEXTURE_2D, 0, 3, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), \
0, GL_RGB, GL_UNSIGNED_BYTE, ilGetData());
Is there a GL_BGR
you can specify on the second line, instead of GL_RGB
? That should fix it.
是否GL_BGR
可以在第二行指定一个,而不是GL_RGB
?那应该解决它。
The reason a PNG causes the image to flip is because of how a BMP is stored: BMPs are stored bottom up, the first pixels in the file are the bottom row of the image.
PNG 导致图像翻转的原因是 BMP 的存储方式:BMP 自下而上存储,文件中的第一个像素是图像的底行。
回答by magnavimaras
You can switch GL_RGB
to GL_BGR_EXT
.
In some cases GL_BGR
isn't recognized.
您可以切换GL_RGB
到GL_BGR_EXT
. 在某些情况下GL_BGR
无法识别。
回答by red
I had a similar issue a while back. Try setting GL_RBG
in glTexImage2D
to GL_BGR
.
不久前我遇到了类似的问题。尝试设置GL_RBG
在glTexImage2D
来GL_BGR
。
回答by Jerry Coffin
You'll normally have a call to glTexImage2D that specifies the external format of the pixels. From the sounds of things, you need to check that and switch from GL_RGB to GL_BGR.
您通常会调用 glTexImage2D 来指定像素的外部格式。从事物的声音来看,您需要检查并从 GL_RGB 切换到 GL_BGR。
回答by Shaotim
I know this is an old topic but I found the easiest way was to flip the z and x in the vec4 that texture2D returns in the fragment shader, not glTexture2d. It doesn't rely on extensions that may not exist. Solved it on an GLES 2 project im working on.
我知道这是一个古老的话题,但我发现最简单的方法是翻转 vec4 中的 z 和 x,texture2D 在片段着色器中返回,而不是 glTexture2d。它不依赖于可能不存在的扩展。在我正在处理的 GLES 2 项目上解决了这个问题。
"varying vec2 v_texCoord; "
"uniform sampler2d s_texture; "
"vec4 v_bgr; "
"void main() "
"{ "
" v_bgr = texture2D( s_texture, v_texCoord ); "
" gl_FragColor = v_bgr.zyxw; "
"} ";