Implementing 2D metaballs
In this recipe, we will learn how we can implement organic looking objects called metaballs.
Getting ready
In this recipe, we are going to use the code base from the Applying repulsion and attraction forces recipe in Chapter 5, Building Particle Systems.
How to do it…
We will implement the metaballs' rendering using a shader program. Perform the following steps to do so:
Create a file inside the
assets
folder with a name,passThru_vert.glsl
, and put the following code snippet inside it:void main() { gl_Position = ftransform(); gl_TexCoord[0] = gl_MultiTexCoord0; gl_FrontColor = gl_Color; }
Create a file inside the
assets
folder with a name,mb_frag.glsl
, and put the following code snippet inside it:#version 120 uniform vec2 size; uniform int num; uniform vec2 positions[100]; uniform float radius[100]; void main(void) { // Get coordinates vec 2 texCoord = gl_TexCoord[0].st; vec4 color = vec4(1.0,1.0,1.0, 0.0); float a = 0.0; int i; for(i ...