Monday, March 21, 2011

Building a Ray Tracer



The final project for "Introduction to Graphics" was to make a ray tracer, a program that renders a 3D scene by tracing the path of rays of light as they strike objects, reflect off of them, and cast shadows.

The image you see above was not drawn using OpenGL or any graphics utilities; its colors were rendered from scratch, pixel by pixel, by mathematically tracing rays.

For the assignment, our programs were not allowed to know ahead of time what the scene would include. Instead, an input text file would tell it about factors such as how the virtual camera lens is shaped, where the lights are and how they're colored, where the spheres are and how they're stretched, how reflective they are, and how chalky and how shiny their materials are.  Based on only that input text file, the program outputs the correct image data into a picture file.

As it happens, this is not the first ray tracer I've made (or else this project would have been as difficult as we were warned it would be); I also made one during high school, while attempting to make my own lifelike physics engine.  Having not yet learned about more common ways to display 3D graphics, I made my own ray tracer to render it (and also discovered a few other well-known graphics concepts by accident).  Soon I will dig up that engine and make a post about it on here, because it used a neat voxel subdivision approach that I'll be revisiting in the future. 

As for this tracer, it was created in a two day timespan and used 350 lines of C++ code, almost all of which were related to file input/output or lighting calculations.






Above: Some pictures from early development (click for bigger). The colors indicate things like a point's normal direction, its reflection direction, or one of the assorted vector dot products that were needed.




Above: Some pictures used for testing (click for bigger).

Here is a more aesthetically pleasing scene I put together, shown with different levels of reflectiveness for the spheres:


Here are two animations using the previous set of images:

Animation 1 (Smooth)

Animation 2 (Slow sequence)

Sunday, March 20, 2011

Animation Project: Fast 3D Sculpter


Here's a simple tool I've made for whipping up complex 3D structures quickly.  Right now it's best at making things like fractals and spirals, but you can make anything with it (houses, cars, animals, etc.) with relatively few commands.

The video below mainly just shows how it works; fast forward to the end to watch it produce some complicated formations.  Later on I'll record myself building a more visually realistic scene.

The user interface I made for this sculpter is extremely simple and has just a few buttons, with the clever "copy/paste" buttons doing most of the work.  These let you duplicate entire branches of the scene graph of all your objects, allowing you to exploit symmetries and patterns in the objects you're trying to make so that they're done in fewer clicks.

Speed, not visual accuracy, is the goal.  The idea is to quickly crank out prototypes with this, and then to use some more precise modeling tools later that will use the prototype's shape as a guideline.

The video:


Something like this will eventually be integrated into my long-term virtual environment project as the main way to construct things.  Specifically, when you're done whipping up shapes with this tool, your finished structure will be used as a bounding volume to be filled with particles of physical materials (such as cloth, fluids, sand, metal, etc.); the structure will then exist in the environment as if it were made of those materials, by using soft body dynamics and other techniques.

This was made in 2 weeks for "Introduction to Graphics" class and is my first OpenGL creation.  The class assignment was to animate something to show that we had learned how to do basic 3D stuff. I decided to make more of a construction sandbox than an animation; this "animation" won first place out of 49 others in a class vote, where its eligibility was borderline since I kind of went wild and did my own thing, but the results show the class at least seemed to approve. This sculpting tool was all done using C++ code; single matrix transforms (see examples) are used to simultaneously set the location, orientation, and shape of each object, with some more math thrown in when needed to make things bounce around.





Above:  Pictures from early development (Click for bigger).


Above:  Some shapes that were defined recursively, by doing copy/pastes within the scene graph.


Above:  The same shape is in both pictures, but in the second a scale transform is applied to the graph's root node along one axis.



Above:  Only one side of the house and ground needed to be drawn by hand.  The other three sides were automatically generated by simply hitting the paste button three times, after having copied that part of the scene graph in a way that also included a rotation transform.  Also shown is a tree, drawn by recursively replicating its own parts in a similar manner.  The second drawing shows the whole first drawing replicated with a transform, which causes the scene to mutate in interesting ways.


Saturday, March 12, 2011

A few bits and pieces from last quarter

Here is an animation of the physics system project from MS270 (Scientific Computing).  There are some strings / hairs and they fall.
The blue string is unstable and wobbles everywhere because it uses explicit Euler integration to do the physics, which lets inaccuracies accumulate over time; if I had tried to stiffen the string any more those inaccuracies would increase off to infinity.  The red and green strings however use the complicated implicit Euler method, and so they will behave fine at any stiffness. (To do that, I had to build a big matrix full of physics equations and then solve it with a matrix solver.)

Here's another one with both ends of a string held:
Here's part of a fluid dynamics system.  It shows the velocity of moving air over time at each location.  Normally wind slows down and dissipates as it leaves the high-pressure region, but if pressure is forcibly held constant, the wind accelerates.

No air particles are simulated; instead, math is used to deduce the speed of the hypothetical air at grid points.
The math included the incompressible Navier-Stokes equations respecting advection, which we were given some pretty baffling approximation formulas for.

Here's an example of Runge's Phenomenon from Numerical Methods, plotted using Newton's divided difference method.

Tuesday, March 8, 2011

Test / First Post!

I'm creating this blog to organize the images, animations, and research papers from my graphics projects into one place, and to show the progress of my graphics related stuff over time.

Each of the concepts that my graphics projects are hitting upon will all eventually be incorporated into one virtual environment / game later on.  I am planning on making a super-realistic virtual world for AI inhabitants to physically interact with.