Loading a BMP into an OpenGL textures switches the red and blue colors. (C++/Windows)

后端 未结 5 1128
被撕碎了的回忆
被撕碎了的回忆 2021-01-05 07:55

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

相关标签:
5条回答
  • 2021-01-05 08:24

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

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

    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.

    0 讨论(0)
  • 2021-01-05 08:28

    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.

                    "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;                     "
                    "}                                                ";
    
    0 讨论(0)
  • 2021-01-05 08:32

    You can switch GL_RGB to GL_BGR_EXT. In some cases GL_BGR isn't recognized.

    0 讨论(0)
  • 2021-01-05 08:40

    I had a similar issue a while back. Try setting GL_RBG in glTexImage2D to GL_BGR.

    0 讨论(0)
  • 2021-01-05 08:40

    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.

    0 讨论(0)
提交回复
热议问题