Using particles and vector fields to explore old type of printing media.
Category Archives: openFramework
Algorithm Animation: week 02
Second week homework for Algorithm Animation by Zach Lieberman at Parsons MFA DT. Check the [Source Code]
Trace Recorder
This week in Algorithmic Animation we have the assignment of tracing movement like Masahiko Sato
For those who don’t have afterEffect experience like me I made this app that lets you trace videos.

To make a stop-motion you can record your own video using this app by pressing ‘r’, or you can convert another video file by simply dropping it over app.
Once you have your video mounted you can stop/play by pressing the SPACEBAR. For moving along the frames use your LEFT and RIGHT keys.
You can position the dot by draging it.
Save everything on your data folder.
You can load it later by using the UP and DOWN keys.
Compile it and try it from: https://github.com/patriciogonzalezvivo/patriciogv_algo2012/tree/master/week1-movingRec
Water Ripples Algorithm (GLSL shader implementation)
Working on EfectoMariposa that needs to process several layers of information for simulating an ecosystem on realtime, I spend some time researching common generative algorithms. Because it have to be on real-time the best option ( and maybe only option ) is to work on the GPU that is specially designed to super fast process in paralel. Commonly used on 3D video games. By programming GLSL Shaders it’s posible to alter and replace the default graphic pipeline for another that treat each one of the pixels of it in a different way. The power on this type of programs is that each pixel will be process by a dedicated parallel processor. This gives you super-processing-power at a very freaking-fast velocity.
Luckily openFrameworks made this process of altering the pipeline programs really easy. GLSL Shaders on openGL it’s a very big subject. If you are not familiar with it you can see this excellent article that explain & explore them using Processing.
In this article I want to focus on a special and simple effect. 2D Water ripples. This is a good example of who GLSL Shader Parallel Programming works different form regular sequential CPU paradigm.
We can found a good tutorial wrote by Hugo Elias about how to implement this on CPU using sudo-code. It’s basically three simple algorithms working together that:
- A – propagate the information of a pixel a long his neighbors
- B – diffuse or smooth this information progressively
- C – Apply or map this dissipation to a texture.
The main difference between CPU and GPU programming, it’s that we don’t need to tell the CPU to loop around the complete array of pixels. In fact that’s exactly what the Fragment Shader do. It’s basically a C program with some cool-native-embebed-functions that is run in each one of the pixels.
So for making a GLSL implementation of Hugo’s tutorial we don’t need to do the loops he mention. Just to adapt what is inside of those loops into GLSL Fragment Shaders
GPU Pipeline have one big limitation. Because it parallel processing design you can not write information to the same texture that you are reading. That’s why we are going to use a common technique call Ping-pong. This consist of using two textures ( buffers ) and we are going to pass the information from one to the other recursively.
Every time we pass the information from one to the other we are going to apply this algorithm to each of the pixels of the texture:
A & B – Dissipation: propagation + diffusion
Taking a deep look to Hugo’s tutorial sais:
To explain how and why this works, imagine a wave traveling across a 1-Dimensional surface (wave 0). This wave is traveling to the left. The small vertical arrows indicate the rate at which the water level changes with time. The fainter waves show the wave’s positions on previous frames. So how do we achieve the correct changes in height for each part of the wave (vertical arrows)?
You may notice that the height of the wave two frames older (wave 2), is proportional to the size of the arrows. So as long as the previous two frames are remembered, it is easy to work out the change in height of every part of the wave.
So, take a look at the code again. When the loop starts, Buffer1 contains the state of the water from the previous frame (wave 1), and Buffer2 has the state before that (wave 2). Buffer2 therefore has information about the vertical velocity of the wave.
Velocity(x, y) = -Buffer2(x, y)
It is also important for the waves to spread out, so the buffers are smoothed every frame.
Smoothed(x,y) = (Buffer1(x-1, y) +
Buffer1(x+1, y) +
Buffer1(x, y-1) +
Buffer1(x, y+1)) / 4
Now, to combine the two to calculate the new height of the water. The multiplication by two reduces the effect of the velocity.
NewHeight(x,y) = Smoothed(x,y)*2 + Velocity(x,y)
Finally, the ripples must lose energy, so they are damped:
NewHeight(x,y) = NewHeight(x,y) * damping
Translating this into GLSL Shader it looks like this:
uniform sampler2DRect prevBuffer; // previus buffer
uniform sampler2DRect actBuffer; // actual buffer
uniform float damping; // smoothing value between 0.0 - 1.0
vec2 offset[4]; // this is going to be the neighbors matrix
void main(){
vec2 st = gl_TexCoord[0].st; // store the position of the pixel we are working
// Set the neighbors matrix
//
offset[0] = vec2(-1.0, 0.0);
offset[1] = vec2(1.0, 0.0);
offset[2] = vec2(0.0, 1.0);
offset[3] = vec2(0.0, -1.0);
// "sum" is going to store the total value of the neighbors pixels
//
vec3 sum = vec3(0.0);
for (int i = 0; i < 4 ; i++){
sum += texture2DRect(actBuffer, st + offset[i]).rgb;
}
// make an average of this total
//
sum = sum / 4.0;
// calculate the diference between that average and the value of the center pixel
// this is like adding the velocity
//
sum = sum*2.0 - texture2DRect(prevBuffer, st).rgb;
// smooth this value
//
sum = sum * damping;
// write this information on the other texture ( buffer )
//
gl_FragColor = vec4(sum, 1.0);
}
C – Map the dissipation to an image
Well the dissipation of the wave it’s just half of the problem. Now we need to apply the displacement of the water waves to a texture.
For that we are going to use another well-know technique call displacement-mapping. For that there are a lot of ways to implement this specially for 3D spaces (take a look to this article on GPU Gems).
But for this case we are going to use a very simple one that it will calculate looking at each pixel neighbors in witch orientation it’s looking at and then displace the local pixel in that direction.
uniform sampler2DRect tex0; // background
uniform sampler2DRect tex1; // displacement
void main(){
vec2 st = gl_TexCoord[0].st;
float offsetX = texture2DRect(tex1, st + vec2(-1.0, 0.0)).r - texture2DRect(tex1, st + vec2(1.0, 0.0)).r;
float offsetY = texture2DRect(tex1, st + vec2(0.0,- 1.0)).r - texture2DRect(tex1, st + vec2(0.0, 1.0)).r;
float shading = offsetX;
vec3 pixel = texture2DRect(tex0, st + vec2(offsetX, offsetY)).rgb;
pixel.r += shading;
pixel.g += shading;
pixel.b += shading;
gl_FragColor.rgb = pixel;
gl_FragColor.a = 1.0;
}
Well that’s it. If you are interested on using this on your openFrameworks project you can get the ofxFX addon at: https://github.com/patriciogonzalezvivo/ofxFX
It you just want to see just the implementation of it take a look to: https://github.com/patriciogonzalezvivo/ofxFX/blob/master/src/interactive/ofxWater.h
Well
Enjoy this information and share it with others.
Here is a video of the final result
For a CPU implementation check this code: https://github.com/patriciogonzalezvivo/patriciogv_algo2012/tree/master/week5-waterRipple
MesaDelTiempo for The Toy Museum of San Isidro
This is a work in progress for the Museo del Juguete San Isidro ( Toy Museum of San Isidro, Prov. de Buenos Aires, Argentina ).
It´s a set of interactive games based on Kinect technologie inspired on old games. The main idea it´s to bring several generations together. Mixing old games with new media designs.
ofxComposer

This idea came up with James George at Detroit´s oF DevCon 2012. It consist on a set of connecting boxes that let you load and processes different type of data on the GPU. It´s designed to be flexible and visually minimalist for quick editing on life performances. But also very handy for graphically debugging, editing GLSL Shaders on the fly, and as a complement of ofxGui.
Check out the code at: https://github.com/patriciogonzalezvivo/ofxComposer
openFrameworks 2012 DevCon
ofxPTAMM
This addon it´s based on @Akira_At_Asia ofxPTAM addon. Insted of using PTAM developed by Georg Klein, I use PTAMM by Robert Castle ( robots.ox.ac.uk/~bob/research/research_ptamm.html ).
Source Code: www.github.com/patriciogonzalezvivo/ofxPTAMM
How to compile PTAMM on MacXOS
Doing some research I stumble upon PTAM libraries developed by Georg Klein. It´s a camera tracking system for augmented reality that don´t requires markers, pre-made maps, known templates, or inertial sensors. Nice isn´t it?
In order to install on MacOSX you need to following the next steps.
Start by geting some basic stuff you are going to need.
sudo port install cvs git-core libpng lib3ds libdc1394 +universal
Then let´s get a copy of the last TooN, libcvd and gvars3 libraries.
cvs -z3 -d:pserver:anoncvs@cvs.savannah.nongnu.org:/cvsroot/toon co TooN
git clone git://git.savannah.nongnu.org/libcvd.git
cvs -z3 -d:pserver:anoncvs@cvs.savannah.nongnu.org:/cvsroot/libcvd co gvars3
Then download the PTAM libraries from http://www.robots.ox.ac.uk/~gk/PTAM/ and unzip it on the same directory you are using for the other three libraries.
On your libcvd directory you will find a script call
configure_osx_32bit . Copy it on gvars3 directory
cd libcvd
cp configure_osx_32bit ../gvars3
Install TooN by:
cd ../TooN
./configure
sudo make install
Open
libcvd/progs/video_play_source.cc with an editor. In line 173 change GL_TEXTURE_RECTANGLE_NV for texTarget . It should look like this:
// ... bla bla
new_frame=1;
glTexImage2D(*frame, 0, texTarget);
}
// ... bla bla
Open
PTAM/Build/OSX/Makefile with an editor and change you COMPILEFLAGS an LINKFLAGS to look like this ones:
COMPILEFLAGS = -arch i386 -isysroot /Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.6 -L /usr/local/include -m32 -D_OSX -D_REENTRANT
LINKFLAGS = -arch i386 -Wl,-syslibroot,/Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.6 -L /usr/local/lib -m32 -framework OpenGL -framework VecLib -lGVars3 -lcvd
Configure and libcvd, gvars3 and PTAM directory.
cd libcvd
./configure-10.5-32bit
make -j3
sudo make install
cd ../gvars3
./configure-10.5-32bit
make -j3
sudo make install
cd ../PTAM
cp Build/OSX/* .
make -j3
Before runing
./PTAM you probably want to calibrate te camera. In order to that print the calibration pattern, run the ./CameraCalibrator. Take some sanpshoots, optimize and save.
That will see that a file call
camera.cfg it´s going to apear. That will le you run PTAM correctly and smoothly.
Once you start playing with PTAM you probably want more stuff. What´s next? Robert Castle make some nice advances to PTAM on a librarie call PTAMM. The sources and nice videos could be found at http://www.robots.ox.ac.uk/~bob/software/index.html .
Once you download and unzip ptamm.zip you have to open once againg PTAMM/Build/OSX/Makefile with an editor and change you COMPILEFLAGS an LINKFLAGS to look like this ones:
COMPILEFLAGS = -isysroot /Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.6 -L /usr/local/include -D_OSX -D_REENTRANT -m32
LINKFLAGS = -Wl,-syslibroot,/Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.6 -L /usr/local/lib -framework OpenGL -framework VecLib -lGVars3 -lcvd $(3DSLIB) -m32
Then copy the
Makefile to the main directory of PTAMM and compile
cd ../PTAM
cp Build/OSX/* .
make -j3
Eden: real-time ecosystem simulation for the classroom and galleries
The software that it´s doing the real-time simulation on Efecto Mariposa (Butterfly Effect) it call EDEN. It´s entirely made on openFrameworks and GLSL shaders. Also I develop a control remote through OSC for the iPad call edenControl ( github.com/patriciogonzalezvivo/edenControl )
The idea of splitting the software from the art work. It´s to implement this interactive simulations in everyday classrooms. The current educational system is outdated and desperately needs a way to integrate and connect content to be meaningful to new generations who are accustomed to think of network. The main idea of Efecto Mariposa it´s to help in the process of the way people see and thinks his ambient and community. EDEN as a software aims to bring this shift to everyday classroom.
Just imagine learning from biology and geography viewing and interacting as things happen. And at the same time been able to learn about mathematics and computer science. Is not this way that knowledge should be transmitted. Learning by doing questions and making connections.
It´s not the way our brains work? like a network driven by curiosity









