Saturday, May 28, 2011

Particle System with Point Sprites

It's been almost 10 days I stuck with particle system. Still I have few doubts/problems/queries. But still I'll post my progress
I have asked my question regarding particle system updation on stackoverflow, waiting for answers J

Creating particle system with point sprites is very exciting for me.
Here is the screen shot:


Let's dig in.
Code for this post can be found at Google Code
Particle Structure
First let's look at properties of a Particle.
My particle structure as [x,y,z,     r,g,b,      dx,dy,dz, life,age]
                                       Position    Color         displacement
ParticleManager class

Setup function, for initiating the particle system with the position, random colors and displacement.
Draw function, is for drawing the particles, we here pass the particle related information to shaders
Update function, is called by Update Thread
Vertex Shader
String strVShader =

            "attribute vec4 a_Position;" +
            "attribute vec4 a_move;" +
            "uniform float a_time;" +
            "attribute vec3 a_color;" +
            "varying vec3 v_color;" +
            "void main()" +
            "{" +
                "v_color = a_color;" +
                "gl_PointSize = 10.0;" +
                "gl_Position = a_Position;" +               
                "gl_Position += (a_time * a_move * 0.5);" +
                "gl_Position.w = 1.0;" +
a_position represents position of each particle, a_move represents displacement/movement
a_color represents color of a particle,
In this vertex shader, we are setting the position of each particle based on the time and the displacement values.
Fragment Shader
String strFShader =
            "precision mediump float;" +
            "uniform sampler2D u_texture;" +
            "varying vec3 v_color;" +
            "void main()" +
            "{" +               
                "vec4 tex = texture2D(u_texture, gl_PointCoord);" +
                "gl_FragColor = vec4(v_color,0.5) * tex;" +

Fragment shader is not changed much, only I'm setting alpha to 0.5,
We have to take it from life and age value of particle, will do it later.
Drawing part is very similar to previous posts. Not many changes here, you can have look in the code.


  1. Add gravity and wind
  2. Change alpha value with particle life


More details in next post