[Bf-blender-cvs] [01196be6619] functions: initial particle system api test
Jacques Lucke
noreply at git.blender.org
Fri Jun 7 12:34:32 CEST 2019
Commit: 01196be661967721cf0ea10a3afb7216818ba241
Author: Jacques Lucke
Date: Fri Jun 7 11:04:01 2019 +0200
Branches: functions
https://developer.blender.org/rB01196be661967721cf0ea10a3afb7216818ba241
initial particle system api test
Still not happy with the way solver-dependent state is handled.
===================================================================
M source/blender/modifiers/intern/MOD_nodeparticles.c
A source/blender/simulations/BParticles.h
D 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 1d3b72ea679..07cf80c0a03 100644
--- a/source/blender/modifiers/intern/MOD_nodeparticles.c
+++ b/source/blender/modifiers/intern/MOD_nodeparticles.c
@@ -45,11 +45,12 @@
#include "DEG_depsgraph.h"
#include "DEG_depsgraph_query.h"
-#include "SIM_particles.h"
+#include "BParticles.h"
typedef struct RuntimeData {
- ParticleSystemRef particle_system;
- ParticlesStateRef state;
+ BParticlesDescription description;
+ BParticlesSolver solver;
+ BParticlesState state;
float last_simulated_frame;
} RuntimeData;
@@ -66,19 +67,21 @@ static void ensure_runtime_data(NodeParticlesModifierData *npmd)
return;
}
- RuntimeData *data = MEM_callocN(sizeof(RuntimeData), __func__);
- data->particle_system = SIM_particle_system_new();
- data->state = SIM_particles_state_new(data->particle_system);
- data->last_simulated_frame = 0.0f;
+ RuntimeData *runtime = MEM_callocN(sizeof(RuntimeData), __func__);
+ runtime->description = BParticles_playground_description();
+ runtime->solver = BParticles_solver_build(runtime->description);
+ runtime->state = BParticles_state_init(runtime->solver);
+ runtime->last_simulated_frame = 0.0f;
- npmd->modifier.runtime = data;
+ npmd->modifier.runtime = runtime;
}
-static void free_runtime_data(RuntimeData *npmd)
+static void free_runtime_data(RuntimeData *runtime)
{
- SIM_particles_state_free(npmd->state);
- SIM_particle_system_free(npmd->particle_system);
- MEM_freeN(npmd);
+ BParticles_state_free(runtime->state);
+ BParticles_solver_free(runtime->solver);
+ BParticles_description_free(runtime->description);
+ MEM_freeN(runtime);
}
static void clear_runtime_data(NodeParticlesModifierData *npmd)
@@ -100,21 +103,35 @@ static Mesh *applyModifier(ModifierData *md,
Scene *scene = DEG_get_evaluated_scene(ctx->depsgraph);
float current_frame = BKE_scene_frame_get(scene);
+ BParticlesDescription new_description = BParticles_playground_description();
+ BParticlesSolver new_solver = BParticles_solver_build(new_description);
+
if (current_frame == runtime->last_simulated_frame + 1) {
- SIM_particle_system_step(runtime->state);
- runtime->last_simulated_frame = current_frame;
+ BParticles_state_adapt(new_solver, &runtime->state);
+
+ BParticles_solver_free(runtime->solver);
+ BParticles_description_free(runtime->description);
+ runtime->description = new_description;
+ runtime->solver = new_solver;
+
+ BParticles_state_step(runtime->state);
}
else {
- SIM_particles_state_free(runtime->state);
- runtime->state = SIM_particles_state_new(runtime->particle_system);
- runtime->last_simulated_frame = current_frame;
+ BParticles_state_free(runtime->state);
+ BParticles_solver_free(runtime->solver);
+ BParticles_description_free(runtime->description);
+
+ runtime->description = new_description;
+ runtime->solver = new_solver;
+ runtime->state = BParticles_state_init(new_solver);
}
+ runtime->last_simulated_frame = current_frame;
- uint point_amount = SIM_particles_count(runtime->state);
+ uint point_amount = BParticles_state_particle_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(runtime->state, positions);
+ BParticles_state_get_positions(runtime->state, positions);
for (uint i = 0; i < point_amount; i++) {
copy_v3_v3(mesh->mvert[i].co, positions[i]);
diff --git a/source/blender/simulations/BParticles.h b/source/blender/simulations/BParticles.h
new file mode 100644
index 00000000000..ff69983d502
--- /dev/null
+++ b/source/blender/simulations/BParticles.h
@@ -0,0 +1,33 @@
+
+#ifndef __SIM_PARTICLES_C_H__
+#define __SIM_PARTICLES_C_H__
+
+#include "BLI_utildefines.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct OpaqueBParticlesDescription *BParticlesDescription;
+typedef struct OpaqueBParticlesSolver *BParticlesSolver;
+typedef struct OpaqueBParticlesState *BParticlesState;
+
+BParticlesDescription BParticles_playground_description(void);
+void BParticles_description_free(BParticlesDescription description);
+
+BParticlesSolver BParticles_solver_build(BParticlesDescription description);
+void BParticles_solver_free(BParticlesSolver solver);
+
+BParticlesState BParticles_state_init(BParticlesSolver solver);
+void BParticles_state_adapt(BParticlesSolver new_solver, BParticlesState *state_to_adapt);
+void BParticles_state_step(BParticlesState state);
+void BParticles_state_free(BParticlesState state);
+
+uint BParticles_state_particle_count(BParticlesState state);
+void BParticles_state_get_positions(BParticlesState state, float (*dst)[3]);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SIM_PARTICLES_C_H__ */
diff --git a/source/blender/simulations/SIM_particles.h b/source/blender/simulations/SIM_particles.h
deleted file mode 100644
index 965b50ae373..00000000000
--- a/source/blender/simulations/SIM_particles.h
+++ /dev/null
@@ -1,28 +0,0 @@
-
-#ifndef __SIM_PARTICLES_C_H__
-#define __SIM_PARTICLES_C_H__
-
-#include "BLI_utildefines.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-typedef struct OpaqueParticleSystem *ParticleSystemRef;
-typedef struct OpaqueParticlesState *ParticlesStateRef;
-
-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
-}
-#endif
-
-#endif /* __SIM_PARTICLES_C_H__ */
diff --git a/source/blender/simulations/playground.cpp b/source/blender/simulations/playground.cpp
index 2284f9be2e0..4f99e448d39 100644
--- a/source/blender/simulations/playground.cpp
+++ b/source/blender/simulations/playground.cpp
@@ -1,7 +1,7 @@
#include "BLI_math.h"
#include "BLI_small_vector.hpp"
-#include "SIM_particles.h"
+#include "BParticles.h"
using BLI::SmallVector;
@@ -15,59 +15,141 @@ using BLI::SmallVector;
return (T2)value; \
}
-struct Vector {
- float x, y, z;
-};
+namespace BParticles {
+
+class Description;
+class Solver;
+class State;
-class ParticleSystem {
+class Description {
+ public:
+ virtual ~Description();
};
-class ParticlesState {
+class Solver {
public:
- SmallVector<Vector> m_positions;
+ virtual ~Solver();
+
+ virtual void step(State *state) const = 0;
};
-WRAPPERS(ParticleSystem *, ParticleSystemRef);
-WRAPPERS(ParticlesState *, ParticlesStateRef);
+class State {
+ public:
+ virtual ~State();
+
+ virtual Solver *solver() const = 0;
+};
-ParticleSystemRef SIM_particle_system_new()
+Description::~Description()
{
- return wrap(new ParticleSystem());
}
-
-void SIM_particle_system_free(ParticleSystemRef particle_system_c)
+Solver::~Solver()
{
- delete unwrap(particle_system_c);
}
-
-ParticlesStateRef SIM_particles_state_new(ParticleSystemRef UNUSED(particle_system_c))
+State::~State()
{
- ParticlesState *state = new ParticlesState();
- return wrap(state);
}
-void SIM_particles_state_free(ParticlesStateRef state_c)
+} // namespace BParticles
+
+struct Vector {
+ float x, y, z;
+};
+
+using BParticles::Description;
+using BParticles::Solver;
+using BParticles::State;
+
+WRAPPERS(BParticles::Description *, BParticlesDescription);
+WRAPPERS(BParticles::Solver *, BParticlesSolver);
+WRAPPERS(BParticles::State *, BParticlesState);
+
+BParticlesDescription BParticles_playground_description()
{
- delete unwrap(state_c);
+ return wrap(new Description());
}
-
-void SIM_particle_system_step(ParticlesStateRef state_c)
+void BParticles_description_free(BParticlesDescription description_c)
{
- ParticlesState *state = unwrap(state_c);
- for (Vector &position : state->m_positions) {
- position.x += 0.1f;
+ delete unwrap(description_c);
+}
+
+class SimpleState : public State {
+ private:
+ SmallVector<Vector> m_positions;
+
+ public:
+ Solver *m_solver;
+
+ SimpleState(Solver *solver) : m_solver(solver)
+ {
+ }
+
+ Solver *solver() const override
+ {
+ return m_solver;
}
- state->m_positions.append({0, 0, 1});
+
+ SmallVector<Vector> &positions()
+ {
+ return m_positions;
+ }
+};
+
+class SimpleSolver : public Solver {
+ private:
+ Description *m_description;
+
+ public:
+ SimpleSolver(Description *description) : m_description(description)
+ {
+ }
+
+ void step(State *state_) const override
+ {
+ SimpleState *state = (SimpleState *)state_;
+ for (Vector &position : state->positions()) {
+ position.x += 0.1f;
+ }
+ state->positions().append({0, 0, 1});
+ }
+};
+
+BParticlesSolver BParticles_solver_build(BParticlesDescription description_c)
+{
+ return wrap(new SimpleSolver(unwrap(description_c)));
+}
+void BParticles_solver_free(BParticlesSolver solver_c)
+{
+ delete unwrap(solver_c);
}
-uint SIM_particles_count(ParticlesStateRef state_c)
+BParticlesState BParticles_state_init(BParticlesSolver solver_c)
{
- ParticlesState *state = unwrap(state_c);
- return state->m_positions.size();
+ return wrap(new SimpleState(unwrap(solver_c)));
+}
+void BParticles_state_adapt(BParticlesSolver new_solver_c, BParticlesState *state_to_adapt_c)
+{
+ SimpleState *state = (SimpleState *)unwrap(*state_to_adapt_c);
+ state->m_solver = unwrap(new_solver_c);
+}
+void BParticles_state_step(BParticlesState state_c)
+{
+ State *state = unwrap(state_c);
+ Solver *solver = state->solver();
+ solver->step(state);
+}
+void BParticles_state_free(BParticlesState state_c)
+{
+ delete unwrap(state_c);
}
-void SIM_particles_get_positions(ParticlesStateRef state_c, float (*dst)[3])
+uint BParticles_state_particle_count(BParticlesState state_c)
+{
+ SimpleState *state = (SimpleState *)unwrap(state_c);
+ return state->positions().size();
+}
+void BParticles_state_get_positions(BParticlesState state_c, float (*dst)[3])
{
- Particl
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list