Problem with Reflections - Raytracing GLSL

1c97f53f11a6361827fa146b97758e65
0
Abhishek_Bansal 101 Dec 23, 2011 at 18:55 raytracing opengl shaders gpu

Hello people !!

I am trying develop a ray tracer in GLSL. I have already implemented shadows and phong. I am working with spheres and planes only. Now when i was implementing reflections i am having strange problem in which i get proper reflections of sphere in the plane, but when it comes to plane’s reflection in spheres and mutual reflection on spheres it only produces some kind of noise.

Interesting fact is that shape of this noise is exactly same as reflection’s should be. I cant figure out the problem despite of too much efforts in debugging….please help !!

link to GLSL sandbox containing my shader….

http://glsl.heroku.com/e#808.11

4 Replies

Please log in or register to post a reply.

B5262118b588a5a420230bfbef4a2cdf
0
Stainless 151 Dec 24, 2011 at 10:41

iPoint.x = iPoint.x + ref.x * t1;
iPoint.y = iPoint.y + ref.y * t1;
iPoint.z = iPoint.z + ref.z * t1;

This looks wrong to me.

Your intersection code returns 0 or 1 for t1, so that code does not move your sampling point to the intersection point, it just moves it along the vector towards your intersection point.

1c97f53f11a6361827fa146b97758e65
0
Abhishek_Bansal 101 Dec 24, 2011 at 11:46

@Stainless

iPoint.x = iPoint.x + ref.x * t1; iPoint.y = iPoint.y + ref.y * t1; iPoint.z = iPoint.z + ref.z * t1; This looks wrong to me. Your intersection code returns 0 or 1 for t1, so that code does not move your sampling point to the intersection point, it just moves it along the vector towards your intersection point.

Ok but t1 is an out variable while 0 or 1 is stored in variable named “id”. And that’s what i did for primary rays. Isn’t that supposed to be true for secondary rays also ??

I am stuck….. :(

B5262118b588a5a420230bfbef4a2cdf
0
Stainless 151 Dec 24, 2011 at 16:06

Looking at your code, what you are trying to do is shoot a ray from the point being sampled.

The ray is travelling along the reflection vector.

You then do a ray sphere intersect ( which returns 0 OR 1) this is correctly returning 1 to signal that the ray has intersected something.

However you are then using this value as if it was a distance along the vector, it isn’t it’s a flag.

Say the ray intersects a sphere 10 units away from the test point, a value of 1 will mean you sample empty air to find the reflected colour instead of the sphere.

What you want to do is form a vector from the centre of the sphere the ray intersected with towards the sample point on the first sphere.

Normalise this, multiply by the radius of the second sphere, add in the centre of the second sphere and you now have the point on the second sphere you want to sample for your reflected colour.

1c97f53f11a6361827fa146b97758e65
0
Abhishek_Bansal 101 Dec 24, 2011 at 16:15

u probably are not getting it..!!

I am using 0 OR 1 only to check that if there is an intersection or not…

int id = checkIntersection(t1,camera, rayDirection);   

in above code this 0 OR 1 is getting stored in “id” AND distance along that vector is getting stored in variable “t1” which i am using to travel along reflected vector.

anyways thank you for help !! i found solution to my problem the problem was in sphere intersection code.
correct code is..

int sphereIntersect(vec3 rayDir, vec3 rayOrigin, out float t1, in vec3 sCenter, in float sRadius)
{
    t1=1000000.0;
    rayDir = normalize(rayDir);
    float B = 2.0 *( ( rayDir.x * (rayOrigin.x - sCenter.x ) )+  ( rayDir.y * (rayOrigin.y - sCenter.y )) + ( rayDir.z * (rayOrigin.z - sCenter.z ) ));
    float C = pow((rayOrigin.x - sCenter.x),2.0) + pow((rayOrigin.y - sCenter.y),2.0) + pow((rayOrigin.z - sCenter.z),2.0) - pow(sRadius,2.0);
    
    float D = B*B - 4.0*C ;
    
    if(D>0.0)
    {
        t1= (-B - pow(D, .5)) / 2.0;
        if (t1 > 0.0)
        {
             return 1;  
            
        }
        
    }
    
        return 0; //since determinant of quadratic equation <0 so no solution and hence no intersection
}

previously i was using both roots of quadratic equation which was wrong..!!

Thank you once again.. :)