Hi! I'm Nicolas and I'm interested in information visualization, JavaScript and web standards.
I currently work as a Data Visualization Scientist at Twitter. I wrote PhiloGL, the JavaScript InfoVis Toolkit and V8-GL.
Using OCaml to visualize Radiohead's HoC music video (part 2)
Posted in:
ocaml
, visualization
, opengl
This post is about performing advanced camera movement in OpenGL.
We'll use the same Radiohead's HoC dataset we used in the previous post.
Once again, the quality of the youtube video is pretty lame. You can right click here and save link as... to download a high quality version of the video (~100MB).
I strongly recommend you to see the high quality video :)
Camera Instructions
Camera movement is made of Translations and/or Rotations.
We want to provide our camera model with instructions of the type:
[ Translate fromto ]
[ Rotate fromtorotation_axis ]
[ Translate ...; Rotate ... ]
As the last example shows, multiple transformations can be done at the same time (translations and rotations).
The definition for a transformation type is:
A camera instruction is a list of these operations (camera_op) and a number specifying the number of frames this transformation should take (i.e the duration of the transformation).
So, for example, this instruction:
( [ Translate( (100., 100., 100.), (0., 0., 0.) ) ], 300. )
translates the camera from (100, 100, 100) to (0, 0, 0) in 300 frames, that is in 10 seconds (at 30 frames per second).
Translation is done by simple interpolation. The interpolation formula for translating from A to B is something like this:
A + (B - A) * delta with delta in (0, 1).
Transitions
It would be nice if camera movement, besides being linear, could also perform other advanced transitions, like the ones used in Fx.Transitions by Mootools.
Some of these transitions are: Quadratic, EaseIn, EaseOut, EaseInOut, Back, Sine, etc...
These effects are achieved by applying functions to the delta value, changing the way it increases or descreases its value.
A possible interface for a Transition module is:
By using Transition.get_animation Quad EaseInOut delta we can change the timing of our animation from this:
into this:
Our camera instructions are then defined as:
The camera_model instance variables contain the destructured camera_op_list type elements: animations, total_frames and transition.
We also provide individual methods for handling translations and rotations. These methods simply compute a delta value, apply the interpolation and then call GlMat.translate3 or GlMat.rotate3.
The 40 line implementation looks like this:
Now that we have our camera model, we need a "timeline" object that can pass intructions to the camera at different stages of the animation.
We define a class-less object timeline that holds a list of camera transformations to be executed at a specific frame of the animation:
This is all I've done to handle camera movement.
I'm not an advanced OpenGL/OCaml developer, so any comment/suggestion about my understanding of OCaml/OpenGL is very welcome.
You can download the source here.
You can compile the source with: