pouët.net

Convert color to grayscale in OpenGL

category: general [glöplog]
 
So I'm looking for an easy way to convert the current frame from color to grayscale. I found a nice sourcecode here, but it requires an extra buffer and it does seem to work properly here (yeah, must be my fault, but I don't get it :P Seems to work with GL_FRONT, but not with GL_BACK). So ist there an easy way to do this? VB6 code appreciated, but C will also do, I guess.
GLSL?
added on the 2008-03-30 16:37:28 by the_Ye-Ti the_Ye-Ti
GL_LUMINANCE?
I suppose you can't draw your stuff in tones of grey...
I don't think it's possible without using shaders or an extra buffer.
added on the 2008-03-30 16:42:26 by xernobyl xernobyl
GL_SETDATE(1978);
Maali: that would be glSetDate(1978);

Anyways, either what macaw suggested, or by simply making sure everything is grey from the beginning (unless you want it dynamic).

Easiest: (always): Fragment shader.
added on the 2008-03-30 17:02:13 by Hatikvah Hatikvah
how does fragment shaders work?

Some background information: The conversion doesn't have to be superfast, it has to be done one time in my game. I.e. I want to convert the game window from colors to grayscale.
Afair, the most basic way if you don't want to deal with shaders is something like glCopyTexImage2D() with GL_LUMINANCE but it's really not the fastest one.
added on the 2008-03-30 17:05:34 by keops keops
If you don't care about runtime speed, you can also use things like glReadPixels()
added on the 2008-03-30 17:07:56 by keops keops
Assuming you have everything in a 2D texture you could use an ARB fragment program like this:

Code: !!ARBfp1.0 PARAM luma = { 0.299, 0.587, 0.114, 1.0}; TEMP texfrag, gray; TEX texfrag, fragment.texcoord[0], texture[0], 2D; DP3 gray.r, texfrag, luma; SWZ result.color, gray, r,r,r,1; END
added on the 2008-03-30 17:30:04 by mic mic
or do it the real democoder way: duplicate all textures (or vertex colours) in grayscale in your data directory and just render those the way you would the coloured version
added on the 2008-03-30 17:40:09 by skrebbel skrebbel
I always hoped that nVidia could implement something like this for their anaglyph stereo driver to prevent the red color flickering.
added on the 2008-03-30 19:12:24 by Salinga Salinga
Code:void main() { // Convert to grayscale using NTSC conversion weights float gray = dot(gl_Color.rgb, vec3(0.299, 0.587, 0.114)); gl_FragColor = vec4(gray, gray, gray, gl_Color.a); }


GLSL code.
added on the 2008-04-01 20:19:21 by LiraNuna LiraNuna
I know the thread is already ages old but i didn't try to fix my code for a long time (actually, i fixied it while i was on vacation). so if anyone else is interested, here's the code:
Code: ReDim grayBuffer(lWidth & lHeight) As Byte glPixelTransferf pxtRedScale, 0.299 'Grayscale glPixelTransferf pxtGreenScale, 0.587 glPixelTransferf pxtBlueScale, 0.114 glPixelStoref pxsPackAlignment, 1 'Without any padding bytes glReadBuffer rbmBack 'select appropriate buffer 'copy pixels into custom buffer: lumincae buffer, format: 8bpp glReadPixels 0, 0, lWidth, lHeight, rpLuminance, pxlByte, grayBuffer(0) glPixelTransferf pxtRedScale, 1 'return to normal color scale glPixelTransferf pxtGreenScale, 1 glPixelTransferf pxtBlueScale, 1

now, you will probably have to make your picture square (but i guess you know that :P) and convert it into an opengl texture again...
Code: glTexImage2D glTexture2D, 0, 1, lWidth, lHeight, 0, tiLuminance, pxlByte, grayBuffer(0)


if there are people that are as strange as me and try to code 2D OpenGL games in Visual Basic 6, feel free to contact me, I have some other useful routines for this :)
whooops, it *did* actually work in my case, but the first line is of course wrong:
Code:ReDim grayBuffer(lWidth * lHeight) As Byte
gl in basic. you're brave.
added on the 2008-07-27 22:20:02 by skrebbel skrebbel
i didn't another choice since i started this project in 2003, already ported it from QBasic form Visual Basic and don't want to port it once again (especially since VB6 has such a nice GUI designer).

login