From 8cd4d7eb358adc6091193508803f1e3b6ffeb7bb Mon Sep 17 00:00:00 2001
From: Tobias Frisch <tfrisch@uni-koblenz.de>
Date: Mon, 31 Jan 2022 15:31:10 +0100
Subject: [PATCH] Corrected velocity transform but there is still energy
 increase

Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de>
---
 .../shaders/transform_particles_to_grid.comp     | 14 +++++++++++---
 .../shaders/update_particle_deformation.comp     |  5 +++--
 .../shaders/update_particle_velocities.comp      |  6 +++++-
 projects/wobble_bobble/src/main.cpp              | 16 ++++++++++++----
 4 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/projects/wobble_bobble/shaders/transform_particles_to_grid.comp b/projects/wobble_bobble/shaders/transform_particles_to_grid.comp
index 6a15c5fd..ab5c30b2 100644
--- a/projects/wobble_bobble/shaders/transform_particles_to_grid.comp
+++ b/projects/wobble_bobble/shaders/transform_particles_to_grid.comp
@@ -20,6 +20,7 @@ void main()	{
     const vec3 position = (vec3(gl_GlobalInvocationID) + vec3(0.5f)) / imageSize(gridImage);
 
     vec4 gridValue = vec4(0.0f);
+
     uint offset = 0;
 
     memoryBarrierBuffer();
@@ -40,13 +41,20 @@ void main()	{
         memoryBarrierShared();
 
         for (uint i = 0; i < SHARED_PARTICLES_BATCH_SIZE; i++) {
-            gridValue += (
-                vec4(shared_particles[i].velocity, shared_particles[i].mass) *
-                voxel_particle_weight(position, shared_particles[i])
+            float weight = voxel_particle_weight(position, shared_particles[i]);
+            float mass = shared_particles[i].mass * weight;
+
+            gridValue += vec4(
+                shared_particles[i].velocity * mass,
+                mass
             );
         }
     }
 
+    if (gridValue.w > 0.0f) {
+        gridValue.xyz /= gridValue.w;
+    }
+
     imageStore(
         gridImage,
         ivec3(gl_GlobalInvocationID),
diff --git a/projects/wobble_bobble/shaders/update_particle_deformation.comp b/projects/wobble_bobble/shaders/update_particle_deformation.comp
index 8cd2aabe..d6b585d7 100644
--- a/projects/wobble_bobble/shaders/update_particle_deformation.comp
+++ b/projects/wobble_bobble/shaders/update_particle_deformation.comp
@@ -36,10 +36,11 @@ void main()	{
                     if (length(offset) < minimal.size * 2.0f) {
                         vec4 gridSample = texture(sampler3D(gridImage, gridSampler), voxel);
 
-                        velocity_gradient += outerProduct(
+                        // TODO: needs to be activated once everything else is stable!
+                        /*velocity_gradient += outerProduct(
                             gridSample.xyz,
                             voxel_particle_grad_weight(voxel, minimal)
-                        );
+                        );*/
                     }
                 }
             }
diff --git a/projects/wobble_bobble/shaders/update_particle_velocities.comp b/projects/wobble_bobble/shaders/update_particle_velocities.comp
index 63f519a3..53b2af96 100644
--- a/projects/wobble_bobble/shaders/update_particle_velocities.comp
+++ b/projects/wobble_bobble/shaders/update_particle_velocities.comp
@@ -14,6 +14,10 @@ layout(set=0, binding=1) uniform texture3D gridImage;
 layout(set=0, binding=2) uniform texture3D gridOldImage;
 layout(set=0, binding=3) uniform sampler gridSampler;
 
+layout( push_constant ) uniform constants {
+    float alpha;
+};
+
 void main()	{
     memoryBarrierBuffer();
     memoryBarrierImage();
@@ -48,7 +52,7 @@ void main()	{
             }
         }
 
-        particles[gl_GlobalInvocationID.x].minimal.velocity = mix(velocity_pic, velocity_flip, 0.95f);
+        particles[gl_GlobalInvocationID.x].minimal.velocity = mix(velocity_pic, velocity_flip, alpha);
     }
 
     memoryBarrierBuffer();
diff --git a/projects/wobble_bobble/src/main.cpp b/projects/wobble_bobble/src/main.cpp
index ee5dd9cd..ae846a90 100644
--- a/projects/wobble_bobble/src/main.cpp
+++ b/projects/wobble_bobble/src/main.cpp
@@ -21,7 +21,8 @@ float randomFloat(float min, float max) {
 	return min + (max - min) * dist(random_dev) / static_cast<float>(RAND_MAX);
 }
 
-void distributeParticles(Particle *particles, size_t count, const glm::vec3& center, float radius, float mass) {
+void distributeParticles(Particle *particles, size_t count, const glm::vec3& center, float radius,
+						 float mass, const glm::vec3& velocity) {
 	float volume = 0.0f;
 	
 	for (size_t i = 0; i < count; i++) {
@@ -40,7 +41,7 @@ void distributeParticles(Particle *particles, size_t count, const glm::vec3& cen
 		
 		particles[i].position = center + offset;
 		particles[i].size = size;
-		particles[i].velocity = glm::vec3(0.0f);
+		particles[i].velocity = velocity;
 		
 		volume += size;
 	}
@@ -137,7 +138,8 @@ int main(int argc, const char **argv) {
 			particles_vec.size(),
 			glm::vec3(0.5f),
 			0.05f,
-			0.27f
+			0.27f,
+			glm::vec3(0.0f, 0.1f, 0.0f)
 	);
 	
 	particles.fill(particles_vec);
@@ -519,6 +521,7 @@ int main(int argc, const char **argv) {
 	
 	bool initializedParticleVolumes = false;
 	bool renderGrid = true;
+	float alpha = 0.95f;
 	
 	auto start = std::chrono::system_clock::now();
 	auto current = start;
@@ -559,6 +562,9 @@ int main(int argc, const char **argv) {
 		timePushConstants.appendDrawcall(static_cast<float>(t));
 		timePushConstants.appendDrawcall(static_cast<float>(dt));
 		
+		vkcv::PushConstants tweakPushConstants (sizeof(float));
+		tweakPushConstants.appendDrawcall(static_cast<float>(alpha));
+		
 		cameraManager.update(dt);
 
 		glm::mat4 mvp = cameraManager.getActiveCamera().getMVP();
@@ -645,7 +651,7 @@ int main(int argc, const char **argv) {
 				{ vkcv::DescriptorSetUsage(
 						0, core.getDescriptorSet(updateParticleVelocitiesSets[0]).vulkanHandle
 				) },
-				vkcv::PushConstants(0)
+				tweakPushConstants
 		);
 		
 		core.recordBufferMemoryBarrier(cmdStream, particles.getHandle());
@@ -720,7 +726,9 @@ int main(int argc, const char **argv) {
 		gui.beginGUI();
 		
 		ImGui::Begin("Settings");
+		
 		ImGui::Checkbox("Render Grid", &renderGrid);
+		ImGui::SliderFloat("Alpha (PIC -> FLIP)", &alpha, 0.0f, 1.0f);
 		
 		ImGui::End();
 		
-- 
GitLab