主要思想是,创建GL_RGBA32F 纹理两个,用来储存粒子的位置;通过乒乓方法更新纹理的内容。
优点: 比cpu 计算更快;
缺点:对硬件有要求,需要OpenGL3.2点两个特征: 浮点纹理和顶点着色器纹理采用。
主要代码如下:
顶点着色器
#version 150
uniform sampler2D u_positionTexture;
uniform float u_positionTextureWidth;
uniform float u_time;
in vec2 a_vertex;
out vec4 v_particle;
// Pseudo random function
float rand(vec2 seed)
{
return fract(sin(dot(seed.xy, vec2(12.9898, 78.233))) * 43758.5453);
}
void main(void)
{
float position_speed = 0.2*u_time;
float lifetime_speed = 0.04*u_time;
// Get particle data from last texture
v_particle = texture(u_positionTexture, a_vertex);
// Each particle has the same translation vector. Pseudo random function is used to randomly distribute the particles over the screen
v_particle.x += (rand(a_vertex*123.456)*2.0-1.0)*position_speed;
v_particle.y += (rand(a_vertex*234.567)*2.0-1.0)