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?
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.
I don't think it's possible without using shaders or an extra buffer.
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.
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.
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.
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.
If you don't care about runtime speed, you can also use things like glReadPixels()
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
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
I always hoped that nVidia could implement something like this for their anaglyph stereo driver to prevent the red color flickering.
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.
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:
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...
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 :)
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.
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).