[Bf-blender-cvs] [c5850f6ef1e] functions: use runtime data in modifier

Jacques Lucke noreply at git.blender.org
Thu Jun 6 11:21:43 CEST 2019


Commit: c5850f6ef1ec99b379dea2f1f4d2ad556b7b6dfc
Author: Jacques Lucke
Date:   Thu Jun 6 11:21:31 2019 +0200
Branches: functions
https://developer.blender.org/rBc5850f6ef1ec99b379dea2f1f4d2ad556b7b6dfc

use runtime data in modifier

===================================================================

M	source/blender/modifiers/intern/MOD_nodeparticles.c
M	source/blender/simulations/SIM_particles.h
M	source/blender/simulations/playground.cpp

===================================================================

diff --git a/source/blender/modifiers/intern/MOD_nodeparticles.c b/source/blender/modifiers/intern/MOD_nodeparticles.c
index cd4992dfcaf..f47a900d784 100644
--- a/source/blender/modifiers/intern/MOD_nodeparticles.c
+++ b/source/blender/modifiers/intern/MOD_nodeparticles.c
@@ -42,16 +42,60 @@
 
 #include "SIM_particles.h"
 
-static Mesh *applyModifier(ModifierData *UNUSED(md),
+typedef struct RuntimeData {
+  ParticleSystemRef particle_system;
+  ParticlesStateRef state;
+} RuntimeData;
+
+static RuntimeData *get_runtime_data(NodeParticlesModifierData *npmd)
+{
+  RuntimeData *data = npmd->modifier.runtime;
+  BLI_assert(data);
+  return data;
+}
+
+static void ensure_runtime_data(NodeParticlesModifierData *npmd)
+{
+  if (npmd->modifier.runtime != NULL) {
+    return;
+  }
+
+  RuntimeData *data = MEM_callocN(sizeof(RuntimeData), __func__);
+  data->particle_system = SIM_particle_system_new();
+  data->state = SIM_particles_state_new(data->particle_system);
+  npmd->modifier.runtime = data;
+}
+
+static void free_runtime_data(RuntimeData *npmd)
+{
+  SIM_particles_state_free(npmd->state);
+  SIM_particle_system_free(npmd->particle_system);
+  MEM_freeN(npmd);
+}
+
+static void clear_runtime_data(NodeParticlesModifierData *npmd)
+{
+  if (npmd->modifier.runtime != NULL) {
+    free_runtime_data(npmd->modifier.runtime);
+    npmd->modifier.runtime = NULL;
+  }
+}
+
+static Mesh *applyModifier(ModifierData *md,
                            const struct ModifierEvalContext *UNUSED(ctx),
                            Mesh *UNUSED(mesh))
 {
-  ParticleSystemRef particle_system = NULL;
-  uint point_amount = SIM_particles_count(particle_system);
+  NodeParticlesModifierData *npmd = (NodeParticlesModifierData *)md;
+  ensure_runtime_data(npmd);
+  RuntimeData *runtime = get_runtime_data(npmd);
+
+  SIM_particle_system_step(runtime->state);
+
+  uint point_amount = SIM_particles_count(runtime->state);
   Mesh *mesh = BKE_mesh_new_nomain(point_amount, 0, 0, 0, 0);
 
   float(*positions)[3] = MEM_malloc_arrayN(point_amount, sizeof(float[3]), __func__);
-  SIM_particles_get_positions(particle_system, positions);
+  SIM_particles_get_positions(runtime->state, positions);
 
   for (uint i = 0; i < point_amount; i++) {
     copy_v3_v3(mesh->mvert[i].co, positions[i]);
@@ -66,9 +110,24 @@ static void initData(ModifierData *UNUSED(md))
 {
 }
 
+static void freeData(ModifierData *md)
+{
+  NodeParticlesModifierData *npmd = (NodeParticlesModifierData *)md;
+  clear_runtime_data(npmd);
+}
+
+static void freeRuntimeData(void *runtime_data_v)
+{
+  if (runtime_data_v == NULL) {
+    return;
+  }
+  RuntimeData *data = (RuntimeData *)runtime_data_v;
+  free_runtime_data(data);
+}
+
 static bool dependsOnTime(ModifierData *UNUSED(md))
 {
-  return false;
+  return true;
 }
 
 static void updateDepsgraph(ModifierData *UNUSED(md),
@@ -99,7 +158,7 @@ ModifierTypeInfo modifierType_NodeParticles = {
 
     /* initData */ initData,
     /* requiredDataMask */ NULL,
-    /* freeData */ NULL,
+    /* freeData */ freeData,
     /* isDisabled */ NULL,
     /* updateDepsgraph */ updateDepsgraph,
     /* dependsOnTime */ dependsOnTime,
@@ -107,5 +166,5 @@ ModifierTypeInfo modifierType_NodeParticles = {
     /* foreachObjectLink */ NULL,
     /* foreachIDLink */ foreachIDLink,
     /* foreachTexLink */ NULL,
-    /* freeRuntimeData */ NULL,
+    /* freeRuntimeData */ freeRuntimeData,
 };
diff --git a/source/blender/simulations/SIM_particles.h b/source/blender/simulations/SIM_particles.h
index 82bb035b901..965b50ae373 100644
--- a/source/blender/simulations/SIM_particles.h
+++ b/source/blender/simulations/SIM_particles.h
@@ -9,9 +9,17 @@ extern "C" {
 #endif
 
 typedef struct OpaqueParticleSystem *ParticleSystemRef;
+typedef struct OpaqueParticlesState *ParticlesStateRef;
 
-uint SIM_particles_count(ParticleSystemRef particle_system);
-void SIM_particles_get_positions(ParticleSystemRef particle_system, float (*dst)[3]);
+ParticleSystemRef SIM_particle_system_new(void);
+void SIM_particle_system_free(ParticleSystemRef particle_system);
+
+ParticlesStateRef SIM_particles_state_new(ParticleSystemRef particle_system);
+void SIM_particles_state_free(ParticlesStateRef state);
+void SIM_particle_system_step(ParticlesStateRef state);
+
+uint SIM_particles_count(ParticlesStateRef state);
+void SIM_particles_get_positions(ParticlesStateRef state, float (*dst)[3]);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/simulations/playground.cpp b/source/blender/simulations/playground.cpp
index aa785ca5269..42ebc03006b 100644
--- a/source/blender/simulations/playground.cpp
+++ b/source/blender/simulations/playground.cpp
@@ -1,7 +1,10 @@
 #include "BLI_math.h"
+#include "BLI_small_vector.hpp"
 
 #include "SIM_particles.h"
 
+using BLI::SmallVector;
+
 #define WRAPPERS(T1, T2) \
   inline T1 unwrap(T2 value) \
   { \
@@ -12,20 +15,59 @@
     return (T2)value; \
   }
 
-WRAPPERS(void *, ParticleSystemRef);
+struct Vector {
+  float x, y, z;
+};
+
+class ParticleSystem {
+};
+
+class ParticlesState {
+ public:
+  SmallVector<Vector> m_positions;
+};
+
+WRAPPERS(ParticleSystem *, ParticleSystemRef);
+WRAPPERS(ParticlesState *, ParticlesStateRef);
+
+ParticleSystemRef SIM_particle_system_new()
+{
+  return wrap(new ParticleSystem());
+}
+
+void SIM_particle_system_free(ParticleSystemRef particle_system)
+{
+  delete unwrap(particle_system);
+}
 
-uint SIM_particles_count(ParticleSystemRef UNUSED(particle_system))
+ParticlesStateRef SIM_particles_state_new(ParticleSystemRef UNUSED(particle_system))
 {
-  return 5;
+  ParticlesState *state = new ParticlesState();
+  return wrap(state);
 }
 
-void SIM_particles_get_positions(ParticleSystemRef UNUSED(particle_system), float (*dst)[3])
+void SIM_particles_state_free(ParticlesStateRef state)
 {
-  for (uint i = 0; i < 5; i++) {
-    float vec[3];
-    vec[0] = i;
-    vec[1] = i * 0.3f;
-    vec[2] = 1.0f;
-    copy_v3_v3(dst[i], vec);
+  delete unwrap(state);
+}
+
+void SIM_particle_system_step(ParticlesStateRef state_)
+{
+  ParticlesState *state = unwrap(state_);
+  for (Vector &position : state->m_positions) {
+    position.x += 0.1f;
   }
+  state->m_positions.append({0, 0, 1});
+}
+
+uint SIM_particles_count(ParticlesStateRef state_)
+{
+  ParticlesState *state = unwrap(state_);
+  return state->m_positions.size();
+}
+
+void SIM_particles_get_positions(ParticlesStateRef state_, float (*dst)[3])
+{
+  ParticlesState *state = unwrap(state_);
+  memcpy(dst, state->m_positions.begin(), state->m_positions.size() * sizeof(Vector));
 }



More information about the Bf-blender-cvs mailing list