-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathFilterSOP.cpp
More file actions
274 lines (227 loc) · 6.5 KB
/
FilterSOP.cpp
File metadata and controls
274 lines (227 loc) · 6.5 KB
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
/* Shared Use License: This file is owned by Derivative Inc. (Derivative)
* and can only be used, and/or modified for use, in conjunction with
* Derivative's TouchDesigner software, and only if you are a licensee who has
* accepted Derivative's TouchDesigner license or assignment agreement
* (which also govern the use of this file). You may share or redistribute
* a modified version of this file provided the following conditions are met:
*
* 1. The shared file or redistribution must retain the information set out
* above and this list of conditions.
* 2. Derivative's name (Derivative Inc.) or its trademarks may not be used
* to endorse or promote products derived from this file without specific
* prior written permission from Derivative.
*/
#include "FilterSOP.h"
#include "Parameters.h"
#include <cassert>
using namespace TD;
// 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
FillSOPPluginInfo(SOP_PluginInfo *info)
{
// For more information on CHOP_PluginInfo see CHOP_CPlusPlusBase.h
// Always set this to CHOPCPlusPlusAPIVersion.
info->apiVersion = SOPCPlusPlusAPIVersion;
// For more information on OP_CustomOPInfo see CPlusPlus_Common.h
OP_CustomOPInfo& customInfo = info->customOPInfo;
// Unique name of the node which starts with an upper case letter, followed by lower case letters or numbers
customInfo.opType->setString("Filter");
// English readable name
customInfo.opLabel->setString("Filter");
// Information of the author of the node
customInfo.authorName->setString("Gabriel Robels");
customInfo.authorEmail->setString("support@derivative.ca");
// This CHOP takes one input
customInfo.minInputs = 1;
customInfo.maxInputs = 1;
}
DLLEXPORT
SOP_CPlusPlusBase*
CreateSOPInstance(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 FilterSOP(info);
}
DLLEXPORT
void
DestroySOPInstance(SOP_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 (FilterSOP*)instance;
}
};
FilterSOP::FilterSOP(const OP_NodeInfo*)
{
};
FilterSOP::~FilterSOP()
{
};
void
FilterSOP::getGeneralInfo(SOP_GeneralInfo* ginfo, const TD::OP_Inputs* inputs, void*)
{
// This will cause the node to cook every frame if the output is used
// We set it to true otherwise the sop does not update when input sop changes
ginfo->cookEveryFrameIfAsked = true;
// Since it is a filter we cannot load geometry directly to GPU
ginfo->directToGPU = false;
}
void
FilterSOP::execute(SOP_Output* output, const TD::OP_Inputs* inputs, void*)
{
const OP_SOPInput* sop = inputs->getInputSOP(0);
if (!sop)
return;
Vector t{};
const OP_CHOPInput* chop = myParms.evalTranslatechop(inputs);
if (!chop)
myWarningString = "Translate CHOP not set.";
else
t = getTranslate(chop);
copyPointsTranslated(output, sop, t);
copyAttributes(output, sop);
copyPrimitives(output, sop);
}
void
FilterSOP::executeVBO(SOP_VBOOutput* output, const TD::OP_Inputs* inputs, void*)
{
// Not Called since ginfo->directToGPU is false
}
void
FilterSOP::setupParameters(TD::OP_ParameterManager* manager, void*)
{
myParms.setup(manager);
}
void
FilterSOP::getWarningString(OP_String* warning, void*)
{
warning->setString(myWarningString.c_str());
// Reset string after reporting it.
myWarningString = "";
}
void
FilterSOP::copyPointsTranslated(SOP_Output* output, const OP_SOPInput* sop, const Vector& t) const
{
const Position* inPos = sop->getPointPositions();
for (int i = 0; i < sop->getNumPoints(); ++i)
{
TD::Position xlatedPos = TD::Position(inPos[i]) + t;
output->addPoint(xlatedPos);
}
}
void
FilterSOP::copyAttributes(SOP_Output* output, const OP_SOPInput* sop) const
{
copyNormals(output, sop);
copyColors(output, sop);
copyTextures(output, sop);
copyCustomAttributes(output, sop);
}
void
FilterSOP::copyNormals(SOP_Output* out, const OP_SOPInput* in) const
{
if (!in->hasNormals())
return;
const Vector* normals = in->getNormals()->normals;
int numPts = out->getNumPoints();
out->setNormals(normals, numPts, 0);
}
void
FilterSOP::copyColors(SOP_Output* out, const OP_SOPInput* in) const
{
if (!in->hasColors())
return;
const Color* colors = in->getColors()->colors;
int numPts = out->getNumPoints();
out->setColors(colors, numPts, 0);
}
void
FilterSOP::copyTextures(SOP_Output* out, const OP_SOPInput* in) const
{
const TexCoord* textures = in->getTextures()->textures;
int numLayers = in->getTextures()->numTextureLayers;
int numPts = out->getNumPoints();
out->setTexCoords(textures, numPts, numLayers, 0);
}
void
FilterSOP::copyCustomAttributes(SOP_Output* out, const OP_SOPInput* in) const
{
int numPts = out->getNumPoints();
for (int i = 0; i < in->getNumCustomAttributes(); ++i)
{
const SOP_CustomAttribData* customAttrib = in->getCustomAttribute(i);
out->setCustomAttribute(customAttrib, numPts);
}
}
void
FilterSOP::copyPrimitives(SOP_Output* out, const OP_SOPInput* in)
{
const SOP_PrimitiveInfo* prims = in->myPrimsInfo;
bool isTriangulated = true;
for (int i = 0; i < in->getNumPrimitives(); ++i)
{
int nVertices = prims[i].numVertices;
const int32_t* indices = prims[i].pointIndices;
if (prims[i].type != PrimitiveType::Polygon || nVertices > 3)
{
isTriangulated = false;
}
switch (nVertices)
{
case 1:
{
out->addParticleSystem(1, indices[0]);
break;
}
case 2:
{
out->addLine(indices, 2);
break;
}
case 3:
{
out->addTriangles(indices, 1);
}
default:
{
int32_t* tmp = new int32_t[nVertices + 1];
memcpy(tmp, indices, nVertices * sizeof(int32_t));
tmp[nVertices] = indices[0];
out->addLine(tmp, nVertices + 1);
delete[] tmp;
}
}
}
if (!isTriangulated)
{
myWarningString = "Input geometry is not a triangulated polygon.";
}
}
Vector
FilterSOP::getTranslate(const OP_CHOPInput* chop)
{
Vector ret{};
int lastSample = chop->numSamples - 1;
int numChan = chop->numChannels;
if (numChan > 2)
{
ret.z = chop->getChannelData(2)[lastSample];
}
else
{
myWarningString = "Translate CHOP should have at least 3 channels.";
}
if (numChan > 1)
ret.y = chop->getChannelData(1)[lastSample];
if (numChan > 0)
ret.x = chop->getChannelData(0)[lastSample];
return ret;
}