Advertisement Jump to content
  • Advertisement
Sign in to follow this  
  • entries
  • comments
  • views

raytracer part 7

Sign in to follow this  


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

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 );

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  


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
  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!