 # gray

Gray (Greiners Raytracer) is a hobby/research project of mine. It is a program that simulates light to produce computer generated images. The technique used is called ray tracing, where a large number of rays are cast against mathematical 3D models to simulate the interaction between light and materials. The result is beautiful computer generated images, like the ones we are used to see in special effects in movies.

## Parametric Surfaces

Programming Posted on Tue, February 02, 2010 16:32:13

Another way of creating 3D models is by using parametric surfaces. A parametric surface is described by a function of two variables u and v that evaluates to a x,y,z coordinate. The function f(u,v) can then, by sweeping u and v over a range, generate points that can be connected to a mesh of small triangles.

An example is a flat plane given by:
x = u
y = v
z = 0

for u = [0,1] and v=[0,1]

This yields a small flat square (rather dull to look at).

More interesting are parametric surfaces that sweep through all three coordinates, x,y,z.

This formula is a the formula for a torus:
x = (0.7+0.3*cos(v))*cos(u)
y = (0.7+0.3*cos(v))*sin(u)
z = 0.3*sin(v)

for u = [0, 2*pi] and v = [0, 2*pi]

And the resulting image (using a glass material): ## Lathe

Programming Posted on Tue, February 02, 2010 14:12:26

I developed a small tool for making 3D models for my test scenes in Gray. The tool is a simple “lathe”. A number of spline-curves can be drawn and when the curves are saved they can be rotated around the y-axis to generate a 3D mesh.

The tool is not very flexible but it served my need at the time.

Here’s a screenshot: ## Photon Mapping

Programming Posted on Tue, February 02, 2010 14:04:00

More work from the winter of 2009. Work with caustics using photon mapping. With the photon mapping technique (described by Henrik Wann Jensen) I implemented caustics in Gray. Caustics is the play of light seen when light passes through a transparent object and focuses into intricate patterns on other objects.

To produce these images, I start with with the light source and cast a large number of light rays into my 3D scene. If the rays hits a reflecting or refracting object the light bounces in new directions. When the light hits a diffuse non reflection object the position of the “photon” is stored into a photon map.

The next step is to gather these photons again, but this time starting from the camera. When the camera rays hits a diffuse object the number of photons in the neighbourhood of the hit point is computed. The density of photons (number of photons per area) gives an estimate of how much light has hit this point.

Here is an image. Notice the “shadow” cast by the pawn. ## Isosurfaces

Programming Posted on Tue, February 02, 2010 13:39:53

My work with isosurfaces actually started more than a year ago (christmas 2008), but I have yet not posted any images on my website of my work.

Isosurfaces are surfaces described with a formula rather than with thousands of small polygons. The benefit is that very complex models can be visualized using only little memory, the drawback is lack of artistic control and costly evaluation times of each isosurface formula.

So what does an isosurface formula look like? One of the simplest is the sphere given by f(x,y,z)=x*x+y*y+z*z-r*r . The isosurface is given by all the x,y,z triplets that evaulates to 0 when inserted into f(x,y,z). Iso-surface because, all evaluations give the same value 0 on the surface.

The way I ray trace isosurfaces is by defining a bounding box around my object. When a ray hits the bounding box, Gray evaluates the formula f. If f is positive, I move forward along the ray and evaluates f again. When f yields a negative value this iteration stops and the last position with a positive value is selected as a point on the surface.

The trick to optimize this progress is the find some upper bound on the derivative of the formula f (how fast does f go towards 0). This tells me how far I can move forward and still be sure that f is positive. The actual implementation in Gray iterates in smaller and smaller steps until it is within some accepted threshold.

Enough talking. Here is an image: This “rock” is a sphere, whose radius has been modulated with some fractal noise.

## First post

Programming Posted on Tue, February 02, 2010 13:09:37

Moving my old hand generated html homepage into the blog format. Hopefully with this format I will post news about my progress with Gray much more often than I did with the old homepage.

The old page can still be found at www.mathika.dk/grayold