# Problem with Reflections - Raytracing GLSL

4 replies to this topic

### #1Abhishek Bansal

New Member

• Members
• 5 posts

Posted 23 December 2011 - 06:55 PM

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 !!

http://glsl.heroku.com/e#808.11
Thanks and Regards
Abhishek Bansal

### #2Stainless

Member

• Members
• 581 posts
• LocationSouthampton

Posted 24 December 2011 - 10:41 AM

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.

### #3Abhishek Bansal

New Member

• Members
• 5 posts

Posted 24 December 2011 - 11:46 AM

Stainless, on 24 December 2011 - 10:41 AM, said:

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.....
Thanks and Regards
Abhishek Bansal

### #4Stainless

Member

• Members
• 581 posts
• LocationSouthampton

Posted 24 December 2011 - 04:06 PM

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.

### #5Abhishek Bansal

New Member

• Members
• 5 posts

Posted 24 December 2011 - 04:15 PM

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..
Thanks and Regards
Abhishek Bansal

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users