Texture has only blue hue when using glTexImage2D

2019-07-19 09:15发布

I'm trying to show image using glTexImage2D, I work with MFC, Visual C 6.0.
Here the code:

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

/* enable 2D texture mapping */
glEnable( GL_TEXTURE_2D );

glTexImage2D(
    GL_TEXTURE_2D, 0, GL_RGB,
    800, 600,
    0, GL_RGB, GL_UNSIGNED_BYTE, BitmapBits);

static float v0[3] = { 0.0, 0.0, 0.0 };
static float v1[3] = { 0.0, 1.0, 0.0 };
static float v2[3] = { 1.0, 1.0, 0.0 };
static float v3[3] = { 1.0, 0.0, 0.0 };

static float t0[2] = { 0.0, 0.0 };
static float t1[2] = { 0.0, 1.0 };
static float t2[2] = { 1.0, 1.0 };
static float t3[2] = { 1.0, 0.0 };

glBegin( GL_QUADS );
    glTexCoord2f(0.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0);
    glTexCoord2f(1.0, 0.0);
    glVertex3f(1.0, 0.0, 0.0);
    glTexCoord2f(1.0, 1.0);
    glVertex3f(1.0, 1.0, 0.0);
    glTexCoord2f(0.0, 1.0);
    glVertex3f(0.0, 1.0, 0.0);
glEnd();

glDisable( GL_TEXTURE_2D );

I see only blue hue of the image. The loaded image BitmapBits is OK.
What can be the problem?

1条回答
我想做一个坏孩纸
2楼-- · 2019-07-19 09:53

You probably want to use GL_BGR_EXT/GL_BGR for format, not GL_RGB:

glTexImage2D(
    GL_TEXTURE_2D, 0, GL_RGB,
    800, 600,
    0, GL_BGR, GL_UNSIGNED_BYTE, BitmapBits);

Or swap the bytes yourself before uploading.

Since you're using NPOT textures make sure to check for GL_ARB_texture_non_power_of_two or GL version >= 2.0.

EDIT: Make sure you have glPixelStorei(GL_UNPACK_ALIGNMENT, 1) before upload.

查看更多
登录 后发表回答