加载BMP成OpenGL纹理切换红色和蓝色。 (C ++ / Windows中)

问题描述:

我想一个位图加载到一个OpenGL纹理,并显示到屏幕上,但是当我这样做,红色和蓝色的价值观似乎切换(例如:一个蓝色的形象出现橙色,绿色的图像保持不变,等等..)。此问题只装载位图时存在,我可以加载.pngs相对没有错误。

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.

这是code我使用加载位图和设置纹理。我使用的是魔鬼,但我不知道怎么的相关是,当我用不同的系统(我不太记得是什么,它​​是在window.h中的函数,我相信)作为中存在的问题:

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):

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()过glEnable(GL_TEXTURE_2D)都较早在程序中调用,其他不相关的功能之间。任何帮助查找原因(希望修复)问题会更AP preciated。

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.

你有你的RGB和BGR倒退。

You've got your RGB and BGR backwards.

glTexImage2D(GL_TEXTURE_2D, 0, 3, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), \
    0, GL_RGB, GL_UNSIGNED_BYTE, ilGetData());

有一个 GL_BGR 您可以在第二行中指定,而不是 GL_RGB 的?这应该修复它。

Is there a GL_BGR you can specify on the second line, instead of GL_RGB? That should fix it.

的原因一个PNG使得图像翻转是因为一个BMP是如何存储:骨形态发生蛋白存储自下而上,该文件中的第一像素的图像的底部行

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.