Sunday, March 14, 2010

MkPerfMon

MkPerfMon showing CPU idle time.
MkPerfMon is a test program for the PDH API in Windows.

Tuesday, March 2, 2010

Raytracing - Part 1


A raytracer is a program which creates images tracing rays from the camera to the scene. It can handle easily things that are described with mathematical methods, it's capable of creating photorealistic images. Unfortunately, it's (often) a slow method. Examples of raytracing include Pixar movies, POV-Ray...

The algorithm is simple: you have to trace a ray for each pixel on the screen. If a ray touches a shape, then calculate lighting for that point and plot it. It's simple, but you need to code intersection testing code for each shape you want to render (spheres, planes...). Sphere-ray intersection testing code is one of the easiest to code. So, the main loop for a raytracer is:

For each point:
Trace ray and test intersection.
If it exists, calculate lighting and plot point.


You can use orthographic projection for the ray: set the ray's origin to the point, and set the direction as an unit vector pointing forward.

The formula for testing line-sphere intersection is:

d: distance
l: direction of the ray (normalized)
o: start position of the ray
c: sphere center
r: sphere radius

d = (l * c) +- sqrt( (l * c)2 - c2 + r2 )
If the value under the root is less than zero, the ray doesn't intersect the sphere.
If the value is greater than zero, there are two intersection points (using +-):
p = o + d*l


This formula is easy to code. I'll handle lighting in part 2.