iDevGames Forums
Wonky specular in reflections - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Wonky specular in reflections (/thread-2619.html)

Wonky specular in reflections - TomorrowPlusX - May 23, 2008 05:24 AM

I've got simple planar reflection rendering, and by and large it looks pretty good. The reflection rendering is done by setting a householder planar-reflection transform after setting the camera transform and before setting lights and rendering scene geometry.

Diffuse lighting in reflections looks great, but specular is completely wrong. Here's a few screenshots:

Specular goes incorrect as the camera's distance from the reflection plane increases:

[Image: basicphysicstest-2008-05-23-00.png]

And specular approaches correctness as camera approaches the reflection plane:
[Image: basicphysicstest-2008-05-23-02.png]

Now, my guess is that what's wrong is that the camera's position is not reflected. This is screwing up my specular.

So, the way I see it, my only option is to take this into account in my shaders somehow. What's my best option?

Should I pass the planar reflection matrix if I'm rendering a reflection and an identity matrix if not? I'm not really certain what's the best way to fix this.

Wonky specular in reflections - DoG - May 24, 2008 02:37 AM

I suspect this has to do with a right vs. left handed coordinate system for the reflections, which messes with the normal calculations, but that doesn't really explain why things get better as you approach the reflection plane.

Wonky specular in reflections - PowerMacX - May 24, 2008 07:47 AM

Perhaps you are swapping the X & Y axes?

Wonky specular in reflections - TomorrowPlusX - May 28, 2008 04:11 AM

Sorry to have posted and run -- I've been busy. Holidays, birthdays, etc.

My gut feeling is that the reflection transform is, like DoG says, changing handedness. But for the life of me I can't come up with a rational solution. I've experimented a bit, but I'm going to post below the vanilla shader code, before I started experimenting.

The following are two lit non-textured simple shaders. I do multipass lighting, so this doesn't take into account emission or global ambient. And I also specialize on light type, so this is just directional lighting, not positional or spot ( though I see the same specular weirdness with those shaders too ).

varying vec3 N, V;
varying vec4 diffuse, ambient, specular;
varying float fog;

void main(void)
    V = (gl_ModelViewMatrix * gl_Vertex).xyz;
    N = normalize(gl_NormalMatrix * gl_Normal);

    gl_FogFragCoord = length( );
    fog = clamp( ( gl_Fog.end - gl_FogFragCoord ) * gl_Fog.scale, 0.0, 1.0 );

    diffuse = vec4( (gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse).rgb, gl_FrontMaterial.diffuse.w );
    ambient = vec4( (gl_FrontMaterial.ambient * gl_LightSource[0].ambient).rgb, gl_FrontMaterial.ambient.w );
    specular = vec4( (gl_FrontMaterial.specular * gl_LightSource[0].specular).rgb, gl_FrontMaterial.specular.w );

    gl_Position = ftransform();

varying vec3 N, V;
varying vec4 diffuse, ambient, specular;
varying float fog;

void main()
    vec3 L = gl_LightSource[0];
    vec3 E = normalize(-V); // we are in Eye Coordinates, so EyePos is (0,0,0)
    vec3 R = normalize(-reflect(L,N));

    //calculate Diffuse Term:
    vec4 Idiff = diffuse * max( dot(N,L), 0.0 );

    // calculate Specular Term:
    vec4 Ispec = specular * pow( max(dot(R,E),0.0), gl_FrontMaterial.shininess );

    // sum color
    vec4 color = ambient + Idiff + Ispec;

    gl_FragColor = vec4( mix( gl_Fog.color, color, fog ).rgb, specular.w );

Does anything jump out at you guys?

I'm beginning to despair of a solution here. I know there's a way to make this work, but my mind's drawing a blank. I suspect I've spent too long working on geometry and physics, and the part of my brain that handles GLSL has gone dormant Rasp

Wonky specular in reflections - Weebull - Jun 1, 2008 05:55 AM

I doubt it's anything to do with your shader as such, but more to do with the values being passed into it.

My gut instinct is the the normal is getting messed up somehow (or maybe not modified at all). I'd be debugging by setting the fragment colour to the normal, the eye vector, the reflection vector, etc, etc