Visual Update for computeparticles

Instead of using a small amount of large textured particles, use a large
amount of small monochrome particles.
Which uses a new vulkanexamplebase functionality of creating and
updating a only device visible buffer via a temporary staging buffer.
This commit is contained in:
Voultapher 2016-04-21 11:21:48 +02:00
parent eb428db92d
commit 5d7014b221
9 changed files with 203 additions and 209 deletions

View file

@ -5,9 +5,8 @@
struct Particle
{
vec4 pos;
vec4 col;
vec4 vel;
vec2 pos;
vec2 vel;
};
// Binding 0 : Position storage buffer
@ -26,14 +25,21 @@ layout (binding = 1) uniform UBO
int particleCount;
} ubo;
vec3 attraction(vec3 pos, vec3 attractPos)
vec2 attraction(vec2 pos, vec2 attractPos)
{
vec3 delta = attractPos - pos;
vec2 delta = attractPos - pos;
const float damp = 0.5;
float dDampedDot = dot(delta, delta) + damp;
float invDist = 1.0f / sqrt(dDampedDot);
float invDistCubed = invDist*invDist*invDist;
return delta * invDistCubed * 0.00035;
return delta * invDistCubed * 0.0035;
}
vec2 repulsion(vec2 pos, vec2 attractPos)
{
vec2 delta = attractPos - pos;
float targetDistance = sqrt(dot(delta, delta));
return delta * (1.0 / (targetDistance * targetDistance * targetDistance)) * -0.000035;
}
void main()
@ -45,22 +51,25 @@ void main()
return;
// Read position and velocity
vec3 vPos = particles[index].pos.xyz;
vec3 vVel = particles[index].vel.xyz;
vec3 destPos = vec3(ubo.destX, ubo.destY, 0.0);
vec2 vVel = particles[index].vel.xy;
vec2 vPos = particles[index].pos.xy;
vec2 destPos = vec2(ubo.destX, ubo.destY);
vec2 delta = destPos - vPos;
float targetDistance = sqrt(dot(delta, delta));
vVel += repulsion(vPos, destPos.xy) * 0.05;
// Calculate new velocity depending on attraction point
vVel += attraction(vPos, destPos.xyz);
// Move by velocity
vPos += vVel * ubo.deltaT;
if ((vPos.x < -1.0) || (vPos.x > 1.0))
vVel.x -= vVel.x;
// collide with boundary
if ((vPos.x < -1.0) || (vPos.x > 1.0) || (vPos.y < -1.0) || (vPos.y > 1.0))
vVel = (-vVel * 0.1) + attraction(vPos, destPos) * 12;
else
particles[index].pos.xy = vPos;
// Write back
particles[index].pos.xyz = vPos;
particles[index].vel.xyz = vVel;
particles[index].vel.xy = vVel;
}

View file

@ -3,13 +3,11 @@
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (binding = 0) uniform sampler2D sColorMap;
layout (location = 0) in vec4 inColor;
layout (location = 0) out vec4 outFragColor;
void main ()
{
outFragColor = texture(sColorMap, gl_PointCoord) * inColor;
outFragColor = inColor;
}

View file

@ -4,13 +4,12 @@
#extension GL_ARB_shading_language_420pack : enable
layout (location = 0) in vec4 inPos;
layout (location = 1) in vec4 inColor;
layout (location = 0) out vec4 outColor;
void main ()
{
gl_PointSize = 32.0;
outColor = inColor;
gl_Position = vec4(inPos.xyz, 1.0);
gl_PointSize = 1.0;
outColor = vec4(0.035);
gl_Position = vec4(inPos.xy, 1.0, 1.0);
}