Reflection issue

I am working on adding reflection to a grid, but I am having rather big problems understanding what is going wrong. The grid is a screen space grid made following this tutorial: Screen space projected grid with GLSL and WebGL - Jérémy BOUNY

I do like this to project the reflections:

    vec3 eye_w = gl_ModelViewMatrixInverse[3].xyz;
    vec3 incident = - eye_w;
    vec3 reflected = reflect(incident, gl_Vertex);
    refTexcoord =normalize(reflected);


    vec4 color = texture2D(mainTex, vec2(refTexcoord.xy));

But the result:

Any idea what is happening?

This is only correct if the object is a unit sphere. The second parameter to reflect() is the surface normal, which needs to be normalised.

Thanks for the answer!
I tried a different code instead, and I feel sooo close to getting it…
But something is still odd. It’s hard to say in the photo, so maybe I’ll record a video later.
Maybe you can still see what’s missing in the new code:

viewCoord = (gl_ProjectionMatrix * viewMatrix ) * screenPlaneWorldPosition;

vec4 projCoord = normalize(viewCoords / viewCoords.w); = ( * 0.5 + 0.5;
projCoord += bump; //normalmap
projCoord = clamp(projCoord, 0.001, 0.999);

    vec4 color = texture2D(reflectionTex, vec2(projCoord.x, -projCoord.y));

Thanks for the help!

Now I tried to skip the screen space grid and use a simple graph.
But still I cannot manage basic reflections.
The image refuses to center on the mesh.

Any ideas what is wrong?
vec3 vEyeNormal = gl_NormalMatrix * gl_Normal;

vec4 vVert4 = gl_ModelViewMatrix * gl_Vertex;
vec3 vEyeVertex = normalize( / vVert4.w);
vec4 vCoords = vec4(reflect(vEyeVertex, vEyeNormal), 0.0); =normalize(;

vec4 reflectionColor = texture2D(mainTex, refTexcoord.xy);



This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.