-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiquidFunCHOP.cpp
295 lines (240 loc) · 7.75 KB
/
LiquidFunCHOP.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "LiquidFunCHOP.h"
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <assert.h>
#include "Scenes.h"
// These functions are basic C function, which the DLL loader can find
// much easier than finding a C++ Class.
// The DLLEXPORT prefix is needed so the compile exports these functions from the .dll
// you are creating
extern "C" {
DLLEXPORT void FillCHOPPluginInfo(CHOP_PluginInfo* info) {
// Always set this to CHOPCPlusPlusAPIVersion.
info->apiVersion = CHOPCPlusPlusAPIVersion;
// The opType is the unique name for this CHOP. It must start with a
// capital A-Z character, and all the following characters must lower case
// or numbers (a-z, 0-9)
info->customOPInfo.opType->setString("Customsignal");
// The opLabel is the text that will show up in the OP Create Dialog
info->customOPInfo.opLabel->setString("Custom Signal");
// Information about the author of this OP
info->customOPInfo.authorName->setString("Author Name");
info->customOPInfo.authorEmail->setString("email@email.com");
// This CHOP can work with 0 inputs
info->customOPInfo.minInputs = 0;
// It can accept up to 1 input though, which changes it's behavior
info->customOPInfo.maxInputs = 1;
}
DLLEXPORT CHOP_CPlusPlusBase* CreateCHOPInstance(const OP_NodeInfo* info) {
// Return a new instance of your class every time this is called.
// It will be called once per CHOP that is using the .dll
return new LiquidFunCHOP(info);
}
DLLEXPORT void DestroyCHOPInstance(CHOP_CPlusPlusBase* instance) {
// Delete the instance here, this will be called when
// Touch is shutting down, when the CHOP using that instance is deleted, or
// if the CHOP loads a different DLL
delete (LiquidFunCHOP*)instance;
}
};
LiquidFunCHOP::LiquidFunCHOP(const OP_NodeInfo* info) : myNodeInfo(info) {
shared_ptr<SceneBase> damBreak(new DamBreak());
_scenes.push_back(damBreak);
shared_ptr<SceneBase> waveMachine(new WaveMachine());
_scenes.push_back(waveMachine);
}
LiquidFunCHOP::~LiquidFunCHOP() {
delete _world;
_world = NULL;
}
void LiquidFunCHOP::init(const OP_Inputs* inputs) {
// World
double gx = 0;
double gy = 0;
inputs->getParDouble2("Gravity", gx, gy);
b2Vec2 gravity;
gravity.Set(gx, gy);
_world = new b2World(gravity);
// Particle System
double radius = inputs->getParDouble("Particlesize");
//double gravityScale = inputs->getParDouble("Particlegravityscale");
//double density = inputs->getParDouble("Particledensity");
double damping = inputs->getParDouble("Particledamping");
const b2ParticleSystemDef particleSystemDef;
_particleSystem = _world->CreateParticleSystem(&particleSystemDef);
_particleSystem->SetGravityScale(0.4f);
_particleSystem->SetDensity(1.2f);
_particleSystem->SetRadius(radius);
_particleSystem->SetDamping(damping);
b2BodyDef bodyDef;
_groundBody = _world->CreateBody(&bodyDef);
int sceneIndex = inputs->getParInt("Sceneindex");
if (0 <= sceneIndex && sceneIndex < _scenes.size()) {
auto scene = _scenes[sceneIndex];
scene->setup(_world, _particleSystem, inputs);
_initialized = true;
_sceneIndex = sceneIndex;
}
}
void LiquidFunCHOP::restart() {
delete _world;
_initialized = false;
}
void LiquidFunCHOP::getGeneralInfo(CHOP_GeneralInfo* ginfo, const OP_Inputs* inputs, void* reserved1) {
// This will cause the node to cook every frame
ginfo->cookEveryFrameIfAsked = true;
// Note: To disable timeslicing you'll need to turn this off, as well as ensure that
// getOutputInfo() returns true, and likely also set the info->numSamples to how many
// samples you want to generate for this CHOP. Otherwise it'll take on length of the
// input CHOP, which may be timesliced.
ginfo->timeslice = false;
ginfo->inputMatchIndex = 0;
}
bool LiquidFunCHOP::getOutputInfo(CHOP_OutputInfo* info, const OP_Inputs* inputs, void* reserved1) {
info->numChannels = 2;
info->sampleRate = inputs->getParInt("Fps");
if (!_initialized) {
init(inputs);
}
info->numSamples = _particleSystem->GetParticleCount();
return true;
}
void
LiquidFunCHOP::getChannelName(int32_t index, OP_String* name, const OP_Inputs* inputs, void* reserved1) {
if (index == 0) {
name->setString("tx");
} else if (index == 1) {
name->setString("ty");
}
}
void LiquidFunCHOP::execute(CHOP_Output* output, const OP_Inputs* inputs, void* reserved) {
int velocityIter = inputs->getParInt("Velocityiterations");
int positionIter = inputs->getParInt("Positioniterations");
int fps = inputs->getParInt("Fps");
float dt = 1.0 / fps;
_world->Step(dt, velocityIter, positionIter);
if (0 <= _sceneIndex && _sceneIndex < _scenes.size()) {
auto scene = _scenes[_sceneIndex];
scene->update(dt);
}
b2Vec2* positions = _particleSystem->GetPositionBuffer();
int n = _particleSystem->GetParticleCount();
for (int i = 0; i < n; i++) {
output->channels[0][i] = positions[i].x;
output->channels[1][i] = positions[i].y;
}
}
int32_t LiquidFunCHOP::getNumInfoCHOPChans(void* reserved1) {
// We return the number of channel we want to output to any Info CHOP
// connected to the CHOP. In this example we are just going to send one channel.
return 2;
}
bool LiquidFunCHOP::getInfoDATSize(OP_InfoDATSize* infoSize, void* reserved1) {
return false;
}
void LiquidFunCHOP::getInfoDATEntries(int32_t index,
int32_t nEntries,
OP_InfoDATEntries* entries,
void* reserved1) {
}
void LiquidFunCHOP::setupParameters(OP_ParameterManager* manager, void* reserved1) {
// Start
{
OP_NumericParameter np;
np.name = "Restart";
np.label = "Restart";
OP_ParAppendResult res = manager->appendPulse(np);
assert(res == OP_ParAppendResult::Success);
}
// Scene
{
if (0 < _scenes.size()) {
OP_NumericParameter np;
np.name = "Sceneindex";
np.label = "Scene Index";
np.defaultValues[0] = 0;
np.minSliders[0] = 0;
np.maxSliders[0] = _scenes.size() - 1;
manager->appendInt(np);
}
}
// Particle
{
OP_StringParameter sp;
sp.name = "Particletype";
sp.label = "Particle Type";
sp.defaultValue = "Solid";
const char* names[] = { "Solid", "Rigid", "Elastic" };
const char* labels[] = { "Solid", "Rigid", "Elastic" };
OP_ParAppendResult res = manager->appendMenu(sp, 3, names, labels);
assert(res == OP_ParAppendResult::Success);
}
{
OP_NumericParameter np;
np.name = "Particlesize";
np.label = "Particle Size";
np.defaultValues[0] = 0.02;
np.minSliders[0] = 0.0;
np.maxSliders[0] = 0.1;
OP_ParAppendResult res = manager->appendFloat(np);
}
{
OP_NumericParameter np;
np.name = "Particledamping";
np.label = "Particle Damping";
np.defaultValues[0] = 0.2;
np.minSliders[0] = 0.0;
np.maxSliders[0] = 1.0;
OP_ParAppendResult res = manager->appendFloat(np);
}
// Gravity
{
OP_NumericParameter np;
np.name = "Gravity";
np.label = "Gravity";
np.defaultValues[0] = 0.0;
np.defaultValues[1] = -9.8;
np.minSliders[0] = -20.0;
np.maxSliders[0] = 20.0;
np.minSliders[1] = -20.0;
np.maxSliders[1] = 20.0;
OP_ParAppendResult res = manager->appendXY(np);
assert(res == OP_ParAppendResult::Success);
}
// Velocity iterations
{
OP_NumericParameter np;
np.name = "Velocityiterations";
np.label = "Velocity Iterations";
np.defaultValues[0] = 6;
np.minSliders[0] = 0;
np.maxSliders[0] = 10;
OP_ParAppendResult res = manager->appendInt(np);
}
// Position iterations
{
OP_NumericParameter np;
np.name = "Positioniterations";
np.label = "Position Iterations";
np.defaultValues[0] = 2;
np.minSliders[0] = 0;
np.maxSliders[0] = 10;
OP_ParAppendResult res = manager->appendInt(np);
}
// fps
{
OP_NumericParameter np;
np.name = "Fps";
np.label = "fps";
np.defaultValues[0] = 60;
np.minSliders[0] = 0;
np.maxSliders[0] = 240;
OP_ParAppendResult res = manager->appendInt(np);
}
}
void LiquidFunCHOP::pulsePressed(const char* name, void* reserved1) {
if (!strcmp(name, "Restart")) {
restart();
}
}