Advertisement

05.17.2008 at 11:47PM PDT, ID: 23411535
[x]
Attachment Details

How to implement refractions in recursive raytracer?

Asked by sph2105 in 3D Game Programming, Graphics & Game Programming, OpenGL Graphics & Game Programming

Tags: C++, Internet Explorer 7

I am writing a recursive raytracer and have tried to implement refractions for over 3 weeks now with no success. I am trying to use

float generateTransmissionRay(Ray ray, HitRecord hit, float n, Ray &temp)
{
      vec3 N;
      if(dot(ray.direction,hit.normal)<0)
            N = -hit.normal;
      else
            N = hit.normal;
      float nt = hit.intersection.obj.mat.refractivity;
      float discriminant = 1-(pow(n,2)*(1-pow(dot(ray.direction,N),2)))/pow(nt,2);
      if(discriminant<=0)
      {
            return false;
      }
      temp.origin = EPSILON*hit.intersection.point;
      temp.direction = ((n*(ray.direction-(dot(ray.direction,N))*N))/nt)-sqrt(discriminant)*N;
      return true;
}
 
In the code above, temp corresponds to the refraction ray. the origin of the refraction ray (temp.origin) being the intersection point of the original ray. hit is an object which contains all the information from the object that was intersected by the primary ray (material properties, normal at point of intersection. etc). The call to that function is in my rayColor function, which is the function I recursively call to generate reflection and refraction rays. I've attached the code snippet related to reflections and refractions calculations in my rayColor function. rayColor takes a ray, a hit object corresponding to that ray, a depth, and a refraction_index) When I initially call rayColor, I pass it an index refraction of 1.0. Then during the recursive calls, I pass it the refraction indices of the object intersected by the ray.

I'm sure there's a number of things I'm doing wrong. I just can't pinpoint where or what. if I don't scale the refraction color by the transmittance coefficient (which I'm not even sure that I'm calculating right because I dont know if the way I'm calculating absorbance, which is denoted as "a" in the code, is correct, my scene still turns white, and doesn't look any different.) Part of the issue is that the color components for the ray are becoming >1 when I add the refracted color, and I'm not entirely sure how to fix that. When doing reflections I had gotten around that by setting a color component to 1 everytime it went over. But I dont think I'm supposed to do that.

I atteched two images, one of my scene with refractions enabled, one with refractions disabled. Perhaps the images might provide some insight to someone wiser than I.

ANy help in this matter would be greatly appreciated. I've been stuck on this for weeks!Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
RGBA rayColor(Ray ray, HitRecord hit, int depth, float n1)
{
       ....*Code that Calculates diffuse and specular components of each ray for every light in the scene*.....
.......
.....
 
	//Use Beer's law to claculate absorbance  and transmittance coefficient
	float dist = distanceBetweenPoints(ray.origin,hit.intersection.point);
	RGBA a =(ray.color*EPSILON)*(-dist);
	RGBA k;
	k.r = exp(a.r)*hit.intersection.obj.mat.refractivity;
	k.b = exp(a.b)*hit.intersection.obj.mat.refractivity;
	k.g = exp(a.g)*hit.intersection.obj.mat.refractivity;
	k.a = exp(a.a)*hit.intersection.obj.mat.refractivity;
	//Calculate reflectance
	float R_0 = (n1-hit.intersection.obj.mat.refractivity)/(n1+hit.intersection.obj.mat.refractivity);
	R_0 = pow(R_0,2);
	float cosTheta = dot(eye,hit.normal);
	float RTheta = R_0 + (1.0 - R_0) * pow(1.0 - cosTheta, 5.0);
	
	if (hit.intersection.obj.mat.material_specular.r != 0 ||
		hit.intersection.obj.mat.material_specular.g != 0 ||
		hit.intersection.obj.mat.material_specular.b != 0 ||
		hit.intersection.obj.mat.material_specular.a != 0) {
			//Get intersectionpoint 
			vec3 intersect;
			intersect.x = hit.intersection.point.x;
			intersect.y = hit.intersection.point.y;
			intersect.z = hit.intersection.point.z;
			vec3 refDirection = (ray.direction - 2 * (dot(hit.normal, ray.direction) * hit.normal));
			normalize(refDirection);
			refRay = generateRay(intersect + EPSILON * refDirection,refDirection);
						
			shootRay(refRay,refRecord);
			float objreflectivity = hit.intersection.obj.mat.reflectivity;
			float objrefractivity = hit.intersection.obj.mat.refractivity;
			
			ray.color = ray.color + hit.intersection.obj.mat.material_specular*rayColor(refRay,refRecord,depth + 1,1.0);
	
			
			if(hit.intersection.obj.mat.refractivity!=0)
			{
				bool notTotallyReflective = generateTransmissionRay(ray, hit, n1,transray);
							
				if(notTotallyReflective)
				{
					shootRay(transray,transRecord);	
					//ray.color = ray.color + k*rayColor(transray,transRecord,depth + 1,hit.intersection.obj.mat.refractivity);							
				}
			
			}
	}
 
	if (ray.color.r > 1)
		ray.color.r = 1;
	if (ray.color.b > 1)
		ray.color.b = 1;
	if (ray.color.g > 1)
		ray.color.g = 1;
	
	return ray.color;
}
Attachments:
 
What my aytraced scene looks like with refracions disabled
What my aytraced scene looks like with refracions disabled
 
 
What my scene looks like with refractions enabled
What my scene looks like with refractions enabled
 
 
Loading Advertisement...
 
[+][-]05.19.2008 at 05:52PM PDT, ID: 21602737

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: 3D Game Programming, Graphics & Game Programming, OpenGL Graphics & Game Programming
Tags: C++, Internet Explorer 7
Sign Up Now!
Solution Provided By: JoseParrot
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.20.2008 at 09:34AM PDT, ID: 21607857

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.20.2008 at 10:26AM PDT, ID: 21608294

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.20.2008 at 11:56AM PDT, ID: 21609106

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.20.2008 at 12:05PM PDT, ID: 21609214

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628