Black and white(grayscale) rendering

Hello everyone,



I'm currently working on a project for school. It is my first experience with openGL, but I've had and am having a blast trying to learn my way through the different aspects of openGL and what jMonkey has too offer. However I'm trying to get everything in black and white and I've been wrestling with it for a week now.



I've tried using the grayscale shader provided by sceneworker but all it does it make everything gray. It doesn't just give them a gray shader, it makes everything compleltly grey including the skybox etc.



vertex shader:


void main() {
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
   vec3 N = normalize(gl_NormalMatrix * gl_Normal);
   vec4 V = gl_ModelViewMatrix * gl_Vertex;
   vec3 L = normalize(gl_LightSource[0].position.xyz - V.xyz);
   vec3 H = normalize(L + vec3(0.0, 0.0, 1.0));

   const float specExp = 128.0;

   float NdotL = max(0.0, dot(N, L));
   vec4 diffuse = gl_Color * vec4(NdotL);

   // specular lighting
   float NdotH = max(0.0, dot(N, H));
   vec4 spec = vec4(0.0);

   if(NdotL > 0.0) {
      spec = vec4(pow(NdotH, specExp));
   }

   gl_FrontColor = diffuse + spec;
}




fragment shader:


void main() {
   float grey = dot(gl_Color.rgb, vec3(0.299, 0.587, 0.114));
   gl_FragColor = vec4(grey, grey, grey, 0);
}



Is the shader I'm currently using wrong? Will this work in combination with the engine (I've read when you do things in shaders, everything should be done in shaders or else it won't work) ? Or can this effect by achieved by using the engine and not using shaders all together? Maybe I can adjust the color-index so that it only contains grayscales? According to the web, GLUT should be able to adjust this, but I don't think the Java implementation supports this.

Thanks in advance.

Both shaders seems to be fine. But they both operating by vertex colors, not fragment colors and this mean that in reality all your object will be gray, because normally you do not change vertex colors and so they stay in default and the same color everywhere.



I guess that you want to see your texture in gray color. Use something like following

Vertex shader:


void main() {
   gl_TexCoord[0] = gl_MultiTexCoord0;
   gl_Position = ftransform();
}



Fragment shader:


uniform sampler2D tex;
   
void main()
{
   vec4 color = texture2D(tex, gl_TexCoord[0].st);
   float grey = dot(color.rgb, vec3(0.299, 0.587, 0.114));
   color = vec4(grey, grey, grey, 0);
      gl_FragColor = color;
}

That was it, the perfect shader!



Thanks alot :)…it's working perfectly now!

that greyscale shader in scene worker is just a copy and paste from open gl super bible…

never really used it for anything so apologies if it didn't do what it said on the tin…

can this shader be used for timed saturation/desaturation controller, i.e transition over time from black and white to color and vice-versa, can someone explain what needs to be done to accomplish this and secondly can parts of a model's texure be actively excluded from the effect somehow.


mcbeth said:

can this shader be used for timed saturation/desaturation controller, i.e transition over time from black and white to color and vice-versa, can someone explain what needs to be done to accomplish this and secondly can parts of a model's texure be actively excluded from the effect somehow.

All you said is possible but the 2nd part is not simple. Fastest way is to use a stencil buffer and draw the parts that are to be excluded, if that is not possible you may need to use offscreen buffers..

uniform sampler2D tex;
uniform float blend;
   
void main()
{
   vec4 color = texture2D(tex, gl_TexCoord[0].st);
   float lum = dot(color.rgb, vec3(0.299, 0.587, 0.114));
        vec4 gray = vec4(lum, lum, lum, color.a);
      gl_FragColor = mix(color, gray, blend);
}


Here's the modified code that does the job, "blend" is a float parameter between 0 and 1, where 0 is full color and 1 is black & white.

thank you very much momoko_fan :smiley: greatly appreciated will give this a go soon thanks again :slight_smile: