Wonky specular in reflections

Sage
Posts: 1,199
Joined: 2004.10
Post: #1
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.
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #2
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.
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #3
Perhaps you are swapping the X & Y axes?
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #4
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 ).

lit.vs
Code:
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( V.xyz );
    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();
}

lit_directional.fs
Code:
varying vec3 N, V;
varying vec4 diffuse, ambient, specular;
varying float fog;

void main()
{
    vec3 L = gl_LightSource[0].position.xyz;
    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
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2008.02
Post: #5
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
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  GLSL: specular for point lights? Fenris 3 4,504 Nov 16, 2006 04:27 PM
Last Post: martinsm
  achieving simulated reflections ghettotek 6 3,770 Feb 21, 2003 05:48 AM
Last Post: ghettotek