主要思想是,创建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)*position_speed;
v_particle.z += (rand(a_vertex*345.678)*2.0-1.0)*position_speed
GPU粒子系统:乒乓法更新纹理内容

该博客介绍了使用OpenGL实现GPU粒子系统的方法,通过GL_RGBA32F纹理存储粒子位置并利用乒乓更新技术提高效率。文章提及该方法的优点是运算速度比CPU快,但需要OpenGL3.2及以上的浮点纹理和顶点着色器纹理支持。主要内容包含顶点着色器和片段着色器的代码展示。
最低0.47元/天 解锁文章
511

被折叠的 条评论
为什么被折叠?



