[Bf-blender-cvs] [17804e4ecb1] functions: experiment with an emitter abstraction
Jacques Lucke
noreply at git.blender.org
Fri Jun 7 23:28:22 CEST 2019
Commit: 17804e4ecb16bf04b2ace72cd1deb30bea1c967d
Author: Jacques Lucke
Date: Fri Jun 7 23:12:35 2019 +0200
Branches: functions
https://developer.blender.org/rB17804e4ecb16bf04b2ace72cd1deb30bea1c967d
experiment with an emitter abstraction
===================================================================
M source/blender/simulations/bparticles/c_wrapper.cpp
M source/blender/simulations/bparticles/core.cpp
M source/blender/simulations/bparticles/core.hpp
M source/blender/simulations/bparticles/playground_solver.cpp
===================================================================
diff --git a/source/blender/simulations/bparticles/c_wrapper.cpp b/source/blender/simulations/bparticles/c_wrapper.cpp
index 2ec8841dfa1..19c036449a9 100644
--- a/source/blender/simulations/bparticles/c_wrapper.cpp
+++ b/source/blender/simulations/bparticles/c_wrapper.cpp
@@ -1,5 +1,6 @@
#include "BParticles.h"
#include "core.hpp"
+#include "particles_container.hpp"
#include "playground_solver.hpp"
#define WRAPPERS(T1, T2) \
@@ -14,6 +15,7 @@
using BParticles::Description;
using BParticles::NamedBuffersRef;
+using BParticles::ParticlesBlock;
using BParticles::Solver;
using BParticles::StateBase;
using BParticles::WrappedState;
@@ -43,9 +45,24 @@ class TestForce : public BParticles::Force {
};
};
+class TestEmitter : public BParticles::Emitter {
+ public:
+ void emit(std::function<ParticlesBlock *()> request_block) override
+ {
+ ParticlesBlock *block = request_block();
+
+ uint index = block->next_inactive_index();
+ block->vec3_buffer("Position")[index] = {(float)(rand() % 100) / 30.0f, 0, 1};
+ block->vec3_buffer("Velocity")[index] = {0, 0.1, 0};
+ block->float_buffer("Age")[index] = 0;
+ block->active_amount()++;
+ }
+};
+
BParticlesDescription BParticles_playground_description(float control1, float control2)
{
- Description *description = new Description({new TestForce(control1, control2)});
+ Description *description = new Description({new TestForce(control1, control2)},
+ {new TestEmitter()});
return wrap(description);
}
void BParticles_description_free(BParticlesDescription description_c)
diff --git a/source/blender/simulations/bparticles/core.cpp b/source/blender/simulations/bparticles/core.cpp
index 8a61345ec08..a0d0457df8b 100644
--- a/source/blender/simulations/bparticles/core.cpp
+++ b/source/blender/simulations/bparticles/core.cpp
@@ -4,6 +4,12 @@ namespace BParticles {
Description::~Description()
{
+ for (Force *force : m_forces) {
+ delete force;
+ }
+ for (Emitter *emitter : m_emitters) {
+ delete emitter;
+ }
}
Solver::~Solver()
diff --git a/source/blender/simulations/bparticles/core.hpp b/source/blender/simulations/bparticles/core.hpp
index 3bab636f6e4..46749fd5a66 100644
--- a/source/blender/simulations/bparticles/core.hpp
+++ b/source/blender/simulations/bparticles/core.hpp
@@ -1,6 +1,7 @@
#pragma once
#include <memory>
+#include <functional>
#include "BLI_array_ref.hpp"
#include "BLI_math.hpp"
@@ -30,12 +31,21 @@ class Force {
virtual void add_force(NamedBuffersRef &buffers, ArrayRef<Vec3> dst) = 0;
};
+class ParticlesBlock;
+
+class Emitter {
+ public:
+ virtual void emit(std::function<ParticlesBlock *()> request_block) = 0;
+};
+
class Description {
private:
SmallVector<Force *> m_forces;
+ SmallVector<Emitter *> m_emitters;
public:
- Description(ArrayRef<Force *> forces) : m_forces(forces.to_small_vector())
+ Description(ArrayRef<Force *> forces, ArrayRef<Emitter *> emitters)
+ : m_forces(forces.to_small_vector()), m_emitters(emitters.to_small_vector())
{
}
@@ -44,6 +54,11 @@ class Description {
return m_forces;
}
+ ArrayRef<Emitter *> emitters()
+ {
+ return m_emitters;
+ }
+
virtual ~Description();
};
diff --git a/source/blender/simulations/bparticles/playground_solver.cpp b/source/blender/simulations/bparticles/playground_solver.cpp
index dba1776934e..ab7ec61b433 100644
--- a/source/blender/simulations/bparticles/playground_solver.cpp
+++ b/source/blender/simulations/bparticles/playground_solver.cpp
@@ -92,23 +92,13 @@ class SimpleSolver : public Solver {
void emit_new_particles(ParticlesContainer &particles)
{
- ParticlesBlock *non_full_block = nullptr;
- for (ParticlesBlock *block : particles.active_blocks()) {
- if (!block->is_full()) {
- non_full_block = block;
- break;
- }
- }
+ std::function<ParticlesBlock *()> request_non_full_block = [&particles]() -> ParticlesBlock * {
+ return particles.new_block();
+ };
- if (non_full_block == nullptr) {
- non_full_block = particles.new_block();
+ for (Emitter *emitter : m_description.emitters()) {
+ emitter->emit(request_non_full_block);
}
-
- uint index = non_full_block->next_inactive_index();
- non_full_block->vec3_buffer("Position")[index] = {(float)(rand() % 100) / 100.0f, 0, 1};
- non_full_block->vec3_buffer("Velocity")[index] = {0, 0.1, 0};
- non_full_block->float_buffer("Age")[index] = 0;
- non_full_block->active_amount()++;
}
void compress_all_blocks(ParticlesContainer &particles)
More information about the Bf-blender-cvs
mailing list