From 15be258b2729bbba1749677e8d24c1680f579c1a Mon Sep 17 00:00:00 2001
From: Tobias Frisch <tfrisch@uni-koblenz.de>
Date: Thu, 3 Feb 2022 05:24:34 +0100
Subject: [PATCH] Remove inverse matrix and precalculate impulse

Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de>
---
 .../shaders/transform_particles_to_grid.comp   | 18 +++++++++++-------
 .../shaders/update_particle_velocities.comp    |  7 +++----
 2 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/projects/wobble_bobble/shaders/transform_particles_to_grid.comp b/projects/wobble_bobble/shaders/transform_particles_to_grid.comp
index 69154658..990c5141 100644
--- a/projects/wobble_bobble/shaders/transform_particles_to_grid.comp
+++ b/projects/wobble_bobble/shaders/transform_particles_to_grid.comp
@@ -28,16 +28,21 @@ void main()	{
         if (localOffset < particles.length()) {
             shared_particles[gl_LocalInvocationIndex] = particles[localOffset].minimal;
 
-            vec3 distance = (position - shared_particles[gl_LocalInvocationIndex].position);
-
-            shared_particles[gl_LocalInvocationIndex].velocity += (
-                mat3(particles[localOffset].mls) * distance
+            shared_particles[gl_LocalInvocationIndex].pad = (
+                mat3(particles[localOffset].mls) *
+                (position - shared_particles[gl_LocalInvocationIndex].position)
+            ) + (
+                shared_particles[gl_LocalInvocationIndex].velocity *
+                shared_particles[gl_LocalInvocationIndex].mass
             );
         } else {
             shared_particles[gl_LocalInvocationIndex].position = vec3(0.0f);
             shared_particles[gl_LocalInvocationIndex].size = 0.0f;
             shared_particles[gl_LocalInvocationIndex].velocity = vec3(0.0f);
             shared_particles[gl_LocalInvocationIndex].mass = 0.0f;
+
+            shared_particles[gl_LocalInvocationIndex].pad = vec3(0.0f);
+            shared_particles[gl_LocalInvocationIndex].weight_sum = 1.0f;
         }
 
         barrier();
@@ -45,11 +50,10 @@ void main()	{
 
         for (uint i = 0; i < SHARED_PARTICLES_BATCH_SIZE; 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
+                shared_particles[i].pad * weight,
+                shared_particles[i].mass * weight
             );
         }
 
diff --git a/projects/wobble_bobble/shaders/update_particle_velocities.comp b/projects/wobble_bobble/shaders/update_particle_velocities.comp
index e8d671e5..f52224a7 100644
--- a/projects/wobble_bobble/shaders/update_particle_velocities.comp
+++ b/projects/wobble_bobble/shaders/update_particle_velocities.comp
@@ -81,15 +81,14 @@ void main()	{
 
             if ((J > 0.0f) && (mass > 0.0f)) {
                 mat3 F_T = transpose(F);
-                mat3 F_invT = inverse(F_T);
 
-                mat3 delta = lame2 * (F - F_invT) + lame1 * log(J) * F_invT;
+                mat3 delta = lame2 * (F * F_T - mat3(1.0f)) + lame1 * log(J);
 
-                //mls_Q += beta * dt * volume * particle.minimal.weight_sum * delta * F_T * D_inv / mass;
+                mls_Q += beta * dt * volume * delta * D_inv;
             }
 
             affine_C = affine_B * D_inv;
-            mls_Q += beta * affine_C;
+            mls_Q += beta * affine_C * mass;
         }
 
         F = (mat3(1.0f) + dt * affine_C) * F;
-- 
GitLab