PDA

View Full Version : Need some help with Ray Tracing Reflection.


Psycho_Gamer
03-26-2007, 08:34 PM
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

Reedbeta
03-26-2007, 10:15 PM
please use the code tags when you post code.

geon
03-27-2007, 02: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?

Psycho_Gamer
03-27-2007, 11:20 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?

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)

Psycho_Gamer
03-27-2007, 11:39 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?

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