使用glTexImage2D时,纹理只有蓝色色调

时间:2012-02-15 15:47:25

标签: opengl mfc textures

我正在尝试使用glTexImage2D显示图像,我使用MFC,Visual C 6.0。
代码如下:

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

我只看到图像的蓝色色调。加载的图像BitmapBits没问题 可能是什么问题?

1 个答案:

答案 0 :(得分:6)

您可能希望将GL_BGR_EXT / GL_BGR用于format,而不是GL_RGB

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

或者在上传之前自己交换字节。

由于您使用的是NPOT纹理,请务必检查GL_ARB_texture_non_power_of_two或GL版本> = 2.0。

编辑:确保在上传之前有glPixelStorei(GL_UNPACK_ALIGNMENT, 1)