how to implement grayscale rendering in OpenGL?

17,825

Solution 1

If you're working against a modern-enough OpenGL, I would say pixel shaders is a very suitable solution here. Either by hooking into each polygon's shading as they render, or by doing a single full-screen quad in a second pass that just reads each pixel, converts to grayscale, and writes it back. Unless your resolution, graphics hardware, and target framerate are somehow "extrem", that should be doable these days in most cases.

Solution 2

For most Desktops Render-To-Texture isn't that expensive anymore, all of compiz, aero, etc and effects like bloom or depth of field seen in recent titles depend on it.

Actually you don't convert the screen texture per se to grayscale, you would want to draw a scree-sized quad with the texture and a fragment shader transforming the valures to grayscale.

Another option is to have two sets of fragment shaders for your triangles, one just copying the gl_FrontColor attribute as the fixed function pieline would, and another that writes grayscale values to the screen buffer.

A third option might be indexed color modes, if you set uüp a grayscale palette, but that mode might be deprecated and poorly supported by now; plus you lose a lot of functionality like blending, if I remember correctly.

Share:
17,825
zyndor
Author by

zyndor

Software engineering consultant, games developer and independent gaming enthusiast. Mobile, cross platform. SDL, Unity etc. Developer of the open source, cross platform C++ game development library XRhodes.

Updated on June 11, 2022

Comments

  • zyndor
    zyndor about 2 years

    When rendering a scene of textured polygons, I'd like to be able to switch between rendering in the original colors and a "grayscale" mode. I've been trying to achieve this using blending and color matrix operations; none of it worked (with blending I couldn't find a glBlendFunc() that achieved something remotely resembling to what I wanted, and color matrix operations ...are discussed here).

    A solution that comes to mind (but also is rather expensive) is to capture the screen every frame and convert the resulting texture to a grayscale one and display that instead... (Where I said grayscale I actually meant anything with a low saturation, but I'm guessing for most of the possible solutions it won't differ all that much from grayscale).

    What other options do I have?

  • zyndor
    zyndor about 15 years
    yup, no blending with colorindex mode. what do you mean "all of compiz, aero, etc"?
  • Triang3l
    Triang3l almost 11 years
    He means that desktop window renderers use rendering to texture often nowadays (he's likely a window manager programmer).