• Create Account

# gs2912

Member Since 05 Feb 2013
Offline Last Active Apr 05 2013 06:05 AM

### #5033715Screen space reflections: Issues

Posted by on 18 February 2013 - 05:55 AM

Sorry for the late response, but here your are.

I split the algorithm into two functions and remove some debugging stuff, I had there (e.g. a counter in the raytracing while loop for manually breaking the loop):

```vec4 SSR()
{
vec3 reflectedColor = vec3(0.0f);

vec3 normal = normalize( texture(deferredNormalTex, vert_UV) ).xyz;

// Depth at current fragment
float currDepth = linearizeDepth( texture(deferredDepthTex, vert_UV).z );

// Eye position, camera is at (0, 0, 0), we look along negative z, add near plane to correct parallax
vec3 eyePosition = normalize( vec3(0, 0, Camera.NearPlane) );
vec4 reflectionVector = ProjectionMatrix * reflect( vec4(-eyePosition, 0), vec4(normal, 0) ) ;

// Call raytrace to get reflected color
reflectedColor = raytrace(reflectionVector.xyz, currDepth);

return vec4(reflectedColor, 1.0f);
}```
```/*
* Raytracing to get reflected color
*/
vec3 raytrace(in vec3 reflectionVector, in float startDepth)
{
vec3 color = vec3(0.0f);
float stepSize = rayStepSize;

float size = length(reflectionVector.xy);
reflectionVector = normalize(reflectionVector/size);
reflectionVector = reflectionVector * stepSize;

// Current sampling position is at current fragment
vec2 sampledPosition = vert_UV;
// Current depth at current fragment
float currentDepth = startDepth;
// The sampled depth at the current sampling position
float sampledDepth = linearizeDepth( texture(deferredDepthTex, sampledPosition).z );

// Raytrace as long as in texture space of depth buffer (between 0 and 1)
while(sampledPosition.x <= 1.0 && sampledPosition.x >= 0.0 &&
sampledPosition.y <= 1.0 && sampledPosition.y >= 0.0)
{
// Update sampling position by adding reflection vector's xy and y components
sampledPosition = sampledPosition + reflectionVector.xy;
// Updating depth values
currentDepth = currentDepth + reflectionVector.z * startDepth;
float sampledDepth = linearizeDepth( texture(deferredDepthTex, sampledPosition).z );

// If current depth is greater than sampled depth of depth buffer, intersection is found
if(currentDepth > sampledDepth)
{
// Delta is for stop the raytracing after the first intersection is found
// Not using delta will create "repeating artifacts"
float delta = (currentDepth - sampledDepth);
if(delta < 0.003f )
{
color = texture(deferredDiffuseTex, sampledPosition).rgb;
break;
}
}
}

return color;
}
```

If you have some questions or comments, give it to me . I'd like to get more into that whole screen space stuff and understand it more.

### #5029946Screen space reflections: Issues

Posted by on 07 February 2013 - 08:48 PM

Okay, I got it working  , had' some space conversion problems. And adding the camera's near-plane value to the view vector helped very much:

PARTNERS