Sign in to follow this  
  • entries
    94
  • comments
    127
  • views
    83135

raytracer part 7

Sign in to follow this  
Seriema

92 views

reflections!!! w00t!! colish! =D

I think I'll be updating my old journal posts to highlight the algorithm that added the said feature. I'll start now...
First, I had to refactor some code so that I could call my raytracing function recursivly (that's the way for reflections, pretty obvious when you think about it). And the actual reflection pseudo code is:

// this is part of the function Raytrace()
if( depth >= MAX_DEPTH ) // prevent endless reflection
return;

reflectionDir = ray.direction() - 2.0*Dot( ray.direction(), surfaceNormal ) * surfaceNormal;

reflectionColor = Color::NONE;
// the RAY_OFFSET is to prevent the ray from interecting with the object it came from.
reflectedRay = Ray(intersectionPoint + reflectionDir*RAY_OFFSET, reflectionDir);
call Raytrace( reflectedRay, depth+1, reflectionColor );
pixelColor += material.reflection() * reflColor * material.color(intersection);
}













There's something odd with my pic... there's a weird color difference. But reference pic nr 3 renders just fine (check my last journal entry).

Note, the black spheres are black because I don't have refraction yet.


EDIT: FIXED! The camera problem was that the up vector I got wasn't correct, have to recalculate it. So here's the "recalculate the up vector" function and the "calculate the view rectangle on the near plane"

cRectangle cCamera::getNear() const
{
const Real
// the up vector is normalized,
// thus upScale is the same as the opposite side
upScale = tan(Deg2Rad(_yFOV/2.0)) * _near,
rightScale = _whRatio * upScale;

const cVector3<> rightDir = Cross(_up, direction())*rightScale,
upDir = _up*upScale, centerNear = position() + _near*direction();

cRectangle nearRect(
centerNear-rightDir+upDir,
centerNear+rightDir+upDir,
centerNear-rightDir-upDir );

return nearRect;
}


void cCamera::recalcUp()
{
const cVector3<> front = direction();
const cVector3<> right = Cross(_up, front).normalize();
_up = Cross(front, right);
}









Kudos to uavfun for helping me figure out the problem!
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now