OpenGL 用与原始图像不同的颜色渲染纹理?

发布于 2024-10-01 05:34:23 字数 3668 浏览 0 评论 0原文

为什么这些不显示相同的颜色?

原始图像:

alt text

以上述图像作为纹理的平面: alt text

这是怎么回事? 原始图像为 100x100 像素,用绘画制作并保存为 24 位位图。 这是我的 opengl 初始化代码:

    _hdc = GetDC(_hwnd);

PIXELFORMATDESCRIPTOR pfd;
ZeroMemory( &pfd, sizeof( pfd ) );
pfd.nSize = sizeof( pfd );
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
int iFormat = ChoosePixelFormat( _hdc, &pfd );
SetPixelFormat( _hdc, iFormat, &pfd );

_hrc = wglCreateContext(_hdc);
wglMakeCurrent(_hdc, _hrc);

GLHelper* helper = GLHelper::get();
helper->initialize(_hwnd, _hdc, _hrc);
changeScreenResolution(_settings.windowWidth, _settings.windowHeight,
    _settings.sceneWidth, _settings.sceneHeight);

// Initialize OpenGL Settings
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
glEnable(GL_TEXTURE_2D);
glDepthFunc(GL_LEQUAL); 
float globalAmbient[4] = {0, 0, 0, 1};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbient);

我使用 FreeImage 库,它看起来经过了很好的测试并且被广泛使用。

这是图像加载代码:

//image format
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
//pointer to the image, once loaded
FIBITMAP *dib(0);
//pointer to the image data
BYTE* bits(0);
//image width and height
unsigned int width(0), height(0);
//OpenGL's image ID to map to
GLuint gl_texID;

//check the file signature and deduce its format
fif = FreeImage_GetFileType(filename, 0);
//if still unknown, try to guess the file format from the file extension
if(fif == FIF_UNKNOWN) 
    fif = FreeImage_GetFIFFromFilename(filename);
//if still unkown, return failure
if(fif == FIF_UNKNOWN)
    return false;

//check that the plugin has reading capabilities and load the file
if(FreeImage_FIFSupportsReading(fif))
    dib = FreeImage_Load(fif, filename);
//if the image failed to load, return failure
if(!dib)
    return false;

//retrieve the image data
bits = FreeImage_GetBits(dib);
//get the image width and height
width = FreeImage_GetWidth(dib);
height = FreeImage_GetHeight(dib);
//if this somehow one of these failed (they shouldn't), return failure
if((bits == 0) || (width == 0) || (height == 0))
    return false;

//if this texture ID is in use, unload the current texture
if(m_texID.find(texID) != m_texID.end())
    glDeleteTextures(1, &(m_texID[texID]));

//generate an OpenGL texture ID for this texture
glGenTextures(1, &gl_texID);
//store the texture ID mapping
m_texID[texID] = gl_texID;
//bind to the new texture ID
glBindTexture(GL_TEXTURE_2D, gl_texID);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

//store the texture data for OpenGL use
glTexImage2D(GL_TEXTURE_2D, level, internal_format, width, height,
    border, image_format, GL_UNSIGNED_BYTE, bits);

//Free FreeImage's copy of the data
FreeImage_Unload(dib);

Why are these not displaying the same colors?

Original Image:

alt text

Plane with above Image as texture:
alt text

WTF is happening?
The original image is 100x100 pixels, made in paint and saved as a 24 bit bitmap.
Here is my opengl initialization code:

    _hdc = GetDC(_hwnd);

PIXELFORMATDESCRIPTOR pfd;
ZeroMemory( &pfd, sizeof( pfd ) );
pfd.nSize = sizeof( pfd );
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
int iFormat = ChoosePixelFormat( _hdc, &pfd );
SetPixelFormat( _hdc, iFormat, &pfd );

_hrc = wglCreateContext(_hdc);
wglMakeCurrent(_hdc, _hrc);

GLHelper* helper = GLHelper::get();
helper->initialize(_hwnd, _hdc, _hrc);
changeScreenResolution(_settings.windowWidth, _settings.windowHeight,
    _settings.sceneWidth, _settings.sceneHeight);

// Initialize OpenGL Settings
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
glEnable(GL_TEXTURE_2D);
glDepthFunc(GL_LEQUAL); 
float globalAmbient[4] = {0, 0, 0, 1};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbient);

I use the library FreeImage, which looks pretty well tested and widely used.

Here is the image loading code:

//image format
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
//pointer to the image, once loaded
FIBITMAP *dib(0);
//pointer to the image data
BYTE* bits(0);
//image width and height
unsigned int width(0), height(0);
//OpenGL's image ID to map to
GLuint gl_texID;

//check the file signature and deduce its format
fif = FreeImage_GetFileType(filename, 0);
//if still unknown, try to guess the file format from the file extension
if(fif == FIF_UNKNOWN) 
    fif = FreeImage_GetFIFFromFilename(filename);
//if still unkown, return failure
if(fif == FIF_UNKNOWN)
    return false;

//check that the plugin has reading capabilities and load the file
if(FreeImage_FIFSupportsReading(fif))
    dib = FreeImage_Load(fif, filename);
//if the image failed to load, return failure
if(!dib)
    return false;

//retrieve the image data
bits = FreeImage_GetBits(dib);
//get the image width and height
width = FreeImage_GetWidth(dib);
height = FreeImage_GetHeight(dib);
//if this somehow one of these failed (they shouldn't), return failure
if((bits == 0) || (width == 0) || (height == 0))
    return false;

//if this texture ID is in use, unload the current texture
if(m_texID.find(texID) != m_texID.end())
    glDeleteTextures(1, &(m_texID[texID]));

//generate an OpenGL texture ID for this texture
glGenTextures(1, &gl_texID);
//store the texture ID mapping
m_texID[texID] = gl_texID;
//bind to the new texture ID
glBindTexture(GL_TEXTURE_2D, gl_texID);

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

//store the texture data for OpenGL use
glTexImage2D(GL_TEXTURE_2D, level, internal_format, width, height,
    border, image_format, GL_UNSIGNED_BYTE, bits);

//Free FreeImage's copy of the data
FreeImage_Unload(dib);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

So要识趣 2024-10-08 05:34:23

位图存储为 BGR(蓝-绿-红)。您的加载代码将其加载为 RGB(红-绿-蓝)。这是翻转你的红色和蓝色通道。

使用GL_BGR作为glTexImage2Dformat参数。

Bitmaps are stored BGR (blue-green-red). Your loading code is loading it as RGB (red-green-blue). This is flipping your red and blue channel.

Use GL_BGR for the format parameter of glTexImage2D.

一世旳自豪 2024-10-08 05:34:23

我不知道你如何加载位图或告诉 OpenGL 使用什么像素格式,但你的红色和蓝色通道正在交换。 AFAIK 位图将像素存储为 BGR,因此请确保加载位图时为 BGR 而不是 RGB。

I don't know how you're loading your bitmap or what pixel format you're telling OpenGL to use, but you're red and blue channel are being swapped. AFAIK bitmaps store pixels as BGR, so make sure when you load the bitmap as BGR and not RGB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文