Grayscale Effect

BlitzMax Forums/OpenGL Module/Grayscale Effect

Ferminho(Posted 2005) [#1]
I'd like to draw part of the graphics in grayscale, without having to change them by hand (realtime or preprocessing). What I want to do is 'turn' grey some graphics when being drawn, something like a setblend (grayblend)

I think it can't be achieved with blend transformation... I've been looking and trying with glTexImage2D and GL_LUMINANCE but still cannot figure out how to do it, I got strange results (probably messed up the format?) Anyone can lend a hand?


Dreamora(Posted 2005) [#2]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/opengl/glfunc03_8m7n.asp

But I don't think this helps you much if you don't to do any preprocessing, at least as long as you don't want to draw it pixel by pixel.


You could do it using a fragment program or fragment shader thought, which just sums up the actual fragment color components, devides it by 3 and saves the result back to all 3 components.


xlsior(Posted 2005) [#3]
You could do it using a fragment program or fragment shader thought, which just sums up the actual fragment color components, devides it by 3 and saves the result back to all 3 components.


Except true greyscale isn't just adding up R+G+B and dividing by three -- there is a different formula that takes the luminosity (?) of each of the channels into consideration: greyscale = (red * 0.299) + (green * 0.587) + (blue * 0.114)

Simply adding and dividing by three will yield very unrealistic results.

...That said, I would be very interested in a greyscale blendmode as well (DirectX + openGL)

Anyway, in the mean time you can also check out this entry I put in the codearchives a long time ago: http://www.blitzbasic.com/codearcs/codearcs.php?code=1282
It can fade a color screen to black-and-white... But speed is somewhat limited since it has to do it pixel-by-pixel.


Ferminho(Posted 2005) [#4]
Thanks for the replies guys :)

@Dreamora : maybe I could do glreadpixels with GL_LUMINANCE and then gldrawpixels with the same format? would be like a grabpixmap+drawpixmap but faster, maybe? I'm not very much into opengl and so I'm not quite sure how it works
About the fragment shader... too hard for me and would raise the requirements too much, I think :(

@xlsior : thanks for your code, I had a look, and I did something like that before trying it through opengl; but it's too slow I'm afraid. I'm thinking about doing it 1/2 or 1/4 the size of the screen and rescaling... as I'd use this effect for some kind of 'flashback' distortion I could easily 'disguise' the low detail results... dunno, have to try

Ah, thanks for the formula too! I'll be using that one from now on :)


ImaginaryHuman(Posted 2006) [#5]
Use the GL_BIAS and GL_SCALE (i think) to automatically shrink the values to the correct amounts on a per component basis.