Jump to content


Need some help with Ray Tracing Reflection.


  • You cannot reply to this topic
4 replies to this topic

#1 Psycho_Gamer

    New Member

  • Members
  • Pip
  • 8 posts

Posted 27 March 2007 - 02:34 AM

Hello,

So, my scene basically is composed of two spheres lying on a plane. Each of these objects are reflective. I am performing recursive ray shading as follows:


	//the colour to return...Start it off black.

	Colour col(0.0, 0.0, 0.0); 


	//end recursion

	if(curDepth==maxDepth){

		col = ray.col;

		return col;

	}

	traverseScene(_root, ray); 


    //did the ray hit anything?

	if (!ray.intersection.none) {

		computeShading(ray); 

		col = ray.col;  

	

		//the colour that reflection causes.

		Colour reflectionColour(0.0, 0.0, 0.0);


		//check for reflection.

		if(ray.intersection.mat->reflection>0){

			

			Vector3D normal= ray.intersection.normal;


			Vector3D reflectionMirror= ray.dir - ((2*(normal.dot(ray.dir)))*normal);


			//create the reflection ray, and shoot it out to the scene.


			Ray3D reflectRay;

			reflectRay.dir=reflectionMirror;

			reflectRay.origin=ray.intersection.point;

			

			reflectionColour=shadeRay(reflectRay, curDepth+1, maxDepth);

		}


		col = ray.col +(ray.intersection.mat->reflection * reflectionColour);


		ray.col=col;

			

		ray.col.clamp();

		

	}

	return col; 



My Ray3d struct is:


struct Ray3D {

	Ray3D() {

		intersection.none = true; 

	}

	Ray3D( Point3D p, Vector3D v ) : origin(p), dir(v) {

		intersection.none = true;

	}

	// The origin of the ray

	Point3D origin;


	//the direction that this ray is going in/

	Vector3D dir;

	// Intersection status, should be computed by the intersection

	// function.

	Intersection intersection;

	// Current colour of the ray, should be computed by the shading

	// function.

	Colour col;

};


Intersection Struct is:


struct Intersection {

	// Location of intersection.

	Point3D point;

	// Normal at the intersection.

	Vector3D normal;

	// Material at the intersection.

	Material* mat;

	// Position of the intersection point on your ray.

	// (i.e. point = ray.origin + t_value * ray.dir)

	// This is used when you need to intersect multiply objects and

	// only want to keep the nearest intersection.

	double t_value;	

	// Set to true when no intersection has occured.

	bool none;


	//where the light source is located for this ray.

	Vector3D lightSource;

};

Does anyone know why my reflection is not working? It basically changes all of the colours in my scene to funky colours. I have been on this for 4 hours,lol. I bet it is something really small too :(

Thanks

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 27 March 2007 - 04:15 AM

please use the code tags when you post code.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 geon

    Senior Member

  • Members
  • PipPipPipPip
  • 893 posts

Posted 27 March 2007 - 08:17 AM

The only strange thing I can see is that you clamp the color on each level of the recursion. You shouldn't do that until it's time to output a picture.

How about posting a screenie?

#4 Psycho_Gamer

    New Member

  • Members
  • Pip
  • 8 posts

Posted 27 March 2007 - 05:20 PM

geon said:

The only strange thing I can see is that you clamp the color on each level of the recursion. You shouldn't do that until it's time to output a picture.

How about posting a screenie?

I currently dont have a screen shot with me as I am at school and the picture is on my computer at home. Basically, I have a gold sphere on a green surface. Both have reflective coefficients set. I do get reflection of the sphere on the surface, but the reflection colour is purple for some reason.

EDIT: I also had to change the reflected ray to:

Vector3D reflectionMirror = -(2*(normal.dot(ray.dir)*normal)+ray.dir)


#5 Psycho_Gamer

    New Member

  • Members
  • Pip
  • 8 posts

Posted 27 March 2007 - 05:39 PM

geon said:

The only strange thing I can see is that you clamp the color on each level of the recursion. You shouldn't do that until it's time to output a picture.

How about posting a screenie?

Whoa! So I did what you suggested here, but could not understand why it was not working. Then I traced out the execution of my program and saw that I was not clamping in the location where the recursion ended (I thought I was, but it turned out to be wrong :P). Needless to say, it works now. Refraction should be a walk in the park :P.

Thanks geon :D





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users