Sunday, August 2, 2009

Spherical Renders of Labyrinths

English    Spanish

Recently I was thinking about how one of the mazes that I programmed would it seen using a spherical lens. Spherical lens deform parts of the images that are away from center, so the things that are far away looks like further away indeed. So more things "fits" inside the image.
When the deformation is low the result is similar to a wide angle lens. When the deformation is high we get images more dificult to achieve in reality. When the deformation is very high the images are imposible to obtain in reality.
The code that generates a particular ray for a pixel can be coded like this:


Ray RayGenerator::GenerateRay(int pixel_x, int pixel_y)
{
Ray ray;
double u,v;

ConvertPixel_to_Unit(pixel_x,pixel_y,u,v);

ray.orig=From;
ray.dir=-Normalize3d(-i + u*tanFov*aspect*j + v*tanFov*k);

return ray;
}

This code generates rays that advance in the negative direction of the X axis

Slightly modifying the code we can obtain our new spherical camera:

Ray SphericalRayGenerator::GenerateRay(int pixel_x, int pixel_y)
{
Ray ray;
double u,v;

ConvertPixel_to_Unit(pixel_x,pixel_y,u,v);

u*=fishEyeFactor*aspect;
v*=fishEyeFactor;

Vector V;
double r=sqrt(u*u+v*v);

double a=atan2(v,u);
V.y=sin(a)*(1-cos(r));
V.z=-cos(a)*(1-cos(r));
V.x=sin(r);
V=Normalize3d(V);

Matrix matrix=Construct4d(i,j,k,VECTOR3D(0,0,0));
V=TransformVector3d(V,matrix);

ray.orig=From;
ray.dir=Normalize3d(V);

return ray;
}

This type of projection generates a pole (or singularity) in the positive direction of the X axis. This generates images wery similar to the ones that Escher made in Circle Limit. As we aproach to the horizon we go to infinity.
With the value 'fishEyeFactor' we replicate the plane several times over the sphere reaching infinity as concentrical circular shapes.

This image is a normal render of an infinite labyrinth:


As we increase the spherical factor we slightly deform the image:


If we increase too much the factor we reach infinity several times:


We can make a render looking "down" obtaining an image that remainds quite well to the ones of M.C.Escher:


Likewise it's possible to increase the spherical factor: