September 03, 2014

Independent Study - Rendering

I am experimenting with Independent Study with my professor of probably one of my favorite classes I've ever taken (Rendering).  Unfortunately my school isn't offering the next class in the sequence but I'll be pursuing my studies all the same.

I really like Inigo Quilez's work, as I've said before, and if I could reach half of his shader writing proficiency I would be happy.  I'm interested in how mathematical shapes are visualized, namely fractals.  I had implemented the geometry for a torus knot last semester and I'd like to now move away from 3D meshes and create everything in the fragment shader, like they do on shadertoy.  To visualize this I'll need to learn how to implement a ray tracer, so that's my first project.

When starting off projects I like to start with very small, very dumb steps so I feel smarter each time I finish a small milestone.  By next week I would like to implement a ray tracer that accurately visualizes a sphere.

I've started by drawing a rectangle and creating a "camera".  I calculate the distance from the camera to each pixel for each y component, and use that to color the rectangle.  The camera can move up and down its axis so we can see the differences in shading.

Here is the code:
vec3 camera = vec3(0.0, iMouse.y / iResolution.y, 0.0);

bool is_in_x_range(float x, float left, float right)
{
    return (x > left && x < right);
}

bool is_in_y_range(float y, float bottom, float top)
{
    return (y > bottom && y < top);
}

bool is_in_box(vec2 uv) {
    return (is_in_x_range(uv.x, 0.3, 0.7) &&
        is_in_y_range(uv.y, 0.3, 0.7));
}

void main( void )
{
    vec2 uv = gl_FragCoord.xy / iResolution.xy;
 
    float x_col = 1.0, y_col = 1.0, z_col = 1.0;
 
    if (is_in_box(uv)) {
        y_col = distance(camera.y, uv.y);
    }

    gl_FragColor = vec4(x_col, y_col, z_col, 1.0);
}

And here it is in action:
(gif would be better here, I'll download some new software)

Very simple but it's a start..  Next thing to do would be to add a light, make it 3D (this is where a sphere would be easier), etc. etc.