Search code examples
c++openglglutopengl-compat

Is there a way to render single channel greyscale images using glDrawPixels?


I'm trying to create a program that handles images of all types by using oiio and OpenGL (simple functions, nothing too crazy).

I've got 4 channel and 3 channel images working, I'm just not sure how to render greyscale (1 channel) images.

if (channels == 4){
   glDrawPixels(W, H, GL_RGBA, GL_FLOAT, img);
}

I'm hoping the answer is something like the snippet above.


Solution

  • If you've to an image where each pixel is a single grayscale value, then you can use the format GL_LUMINANCE. See glDrawPixels.
    The luminance value converted and is set to the red, green, and blue color channel, the alpha channel will be set 1.

    If the type of each pixel is a 32 bit floating point value, the the format is GL_FLOAT:

    glDrawPixels(W, H, GL_LUMINANCE, GL_FLOAT, img);
    

    If the grayscale value is encoded in a byte, then you've to use the unsigned normalized floating point format GL_UNSIGNED_BYTE:

    glDrawPixels(W, H, GL_LUMINANCE, GL_UNSIGNED_BYTE, img);