-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
298 lines (258 loc) · 8 KB
/
main.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
295
296
297
298
#include <maya/MFnPlugin.h>
#include <maya/MAnimControl.h>
#include <maya/MDGContextGuard.h>
#include <maya/MViewport2Renderer.h>
#include <maya/MSelectionList.h>
#include <maya/MFrameContext.h>
#include <maya/MPointArray.h>
#include <maya/MColorArray.h>
#include <maya/MAnimMessage.h>
#include <maya/MFnDagNode.h>
#include <maya/MPxCommand.h>
#include <maya/MArgDatabase.h>
#include <maya/MArgList.h>
#include <maya/MSyntax.h>
#include <maya/MDagPath.h>
#include <maya/M3dView.h>
#include <maya/MGlobal.h>
#include <vector>
#include <map>
using PointCache = std::map<int, std::pair<MPoint, MColor> >;
// render operation for tracking/drawing the trajectories
class SimpleRenderOperation : public MHWRender::MHUDRender
{
public:
static int frames;
static double pointSize;
static bool screenSpace;
static std::vector<std::pair<MDagPath, PointCache> > trackData;
public:
SimpleRenderOperation(const MString &name): MHUDRender(){}
virtual ~SimpleRenderOperation(){}
virtual MStatus execute(const MHWRender::MDrawContext& drawContext)
{
return MStatus::kSuccess;
}
virtual bool hasUIDrawables() const {return true;}
virtual void addUIDrawables(
MHWRender::MUIDrawManager& drawManager,
const MHWRender::MFrameContext& frameContext
)
{
if (trackData.size() == 0)
{
return;
}
MDagPath dag_cam = frameContext.getCurrentCameraPath();
MMatrix mat_cam = dag_cam.inclusiveMatrix();
std::map<int, MMatrix> M_ss;
int t_current = MAnimControl::currentTime().value();
int t_start = std::max(t_current - frames, int(MAnimControl::minTime().value()));
int t_end = std::min(t_current + frames + 1, int(MAnimControl::maxTime().value()));
MPointArray P;
MColorArray C;
for (auto& track : trackData)
{
auto& dag = track.first;
auto& cache = track.second;
// take draw override color from the object if set
MColor cc;
if (!MFnDagNode(track.first).drawOverrideColor(cc))
{
cc = MColor(1, 0.65, 0.25, 1);
}
for (int tt = t_start; tt < t_end; ++tt)
{
// cache the frame in its context if not available
if (cache.find(tt) == cache.end())
{
MDGContextGuard ctx(MDGContext(MTime(tt, MTime::uiUnit())));
auto m = track.first.inclusiveMatrix();
auto p = MPoint(m[3][0], m[3][1], m[3][2]);
if (screenSpace)
{
if (M_ss.find(tt) == M_ss.end())
{
M_ss[tt] = dag_cam.inclusiveMatrix().inverse() * mat_cam;
}
p *= M_ss[tt];
}
// project to viewport
MPoint q;
frameContext.worldToViewport(p, q.x, q.y);
cache[tt] = {q, cc};
}
P.append(cache[tt].first);
// apply smoothstep transparency so the trajectory fades out at both ends
double x = 1.0 - std::fabs(double(tt - t_current)) / frames;
x *= x;
C.append(MColor(cc.r, cc.g, cc.b, 3 * x - 2 * x));
}
}
// draw points on viewport
drawManager.beginDrawable();
auto radius = 0.5 * pointSize;
for (unsigned int ii = 0; ii < P.length(); ++ii)
{
drawManager.setColor(C[ii]);
drawManager.circle2d(P[ii], radius, true);
}
drawManager.endDrawable();
}
};
int SimpleRenderOperation::frames = 24;
double SimpleRenderOperation::pointSize = 5;
bool SimpleRenderOperation::screenSpace = true;
std::vector<std::pair<MDagPath, PointCache> > SimpleRenderOperation::trackData;
// render override to add the render operation
class SimpleRenderOverride : public MHWRender::MRenderOverride
{
protected:
SimpleRenderOperation* mSimpleRenderOp;
MString mUIName = "Simple Render Override";
public:
~SimpleRenderOverride() override {}
SimpleRenderOverride(const MString& name):
MHWRender::MRenderOverride(name)
{
MHWRender::MRenderer *theRenderer = MHWRender::MRenderer::theRenderer();
if (!theRenderer)
{
return;
}
// append the render op at the end of we draw tracks on top of the existing scene
MHWRender::MRenderer::theRenderer()->getStandardViewportOperations(mOperations);
mSimpleRenderOp = new SimpleRenderOperation("simpleRenderOp");
mOperations.insertAfter(MHWRender::MRenderOperation::kStandardSceneName, mSimpleRenderOp);
}
MHWRender::DrawAPI supportedDrawAPIs() const override {return MHWRender::kAllDevices;}
MString uiName() const override {return mUIName;}
static MString viewName;
};
MString SimpleRenderOverride::viewName = "simpleTrackView";
// simple command interface for setting up tracking objects
class simpleTrackCmd : public MPxCommand
{
public:
MStatus doIt(const MArgList& args) override;
static void* creator() {return new simpleTrackCmd;}
static MSyntax newSyntax()
{
MSyntax syntax;
syntax.useSelectionAsDefault(true);
syntax.setObjectType(MSyntax::kSelectionList);
syntax.addFlag("cl", "clear", MSyntax::kUnsigned);
syntax.addFlag("nf", "numOfFrames", MSyntax::kUnsigned);
syntax.addFlag("ps", "pointSize", MSyntax::kDouble);
syntax.addFlag("ss", "screenSpace", MSyntax::kUnsigned);
return syntax;
}
static void clearTracking()
{
SimpleRenderOperation::trackData.clear();
MMessage::removeCallback(cbidAnimEdited);
}
// could be smarter but for now just clear for all if any one is modified
static void OnAnimKeyframeEdited(MObjectArray& deltaObjs, void*)
{
for (auto& track : SimpleRenderOperation::trackData)
{
track.second.clear();
}
M3dView().refresh(true, true);
}
public:
static MCallbackId cbidAnimEdited;
};
MCallbackId simpleTrackCmd::cbidAnimEdited;
MStatus simpleTrackCmd::doIt(const MArgList& args)
{
MStatus status = MStatus::kSuccess;
MArgDatabase argData(syntax(), args, &status);
if (argData.isFlagSet("clear"))
{
clearTracking();
}
else // obtain the selected dags
{
clearTracking();
MSelectionList sl;
argData.getObjects(sl);
for (unsigned int ii = 0; ii < sl.length(); ++ii)
{
MDagPath dag;
if (MStatus::kSuccess == sl.getDagPath(ii, dag))
{
SimpleRenderOperation::trackData.push_back({dag, PointCache()});
}
}
if (SimpleRenderOperation::trackData.size())
{
cbidAnimEdited = MAnimMessage::addAnimKeyframeEditedCallback(OnAnimKeyframeEdited);
}
}
if (argData.isFlagSet("screenSpace"))
{
SimpleRenderOperation::screenSpace = argData.flagArgumentBool("screenSpace", 0);
}
if (argData.isFlagSet("numOfFrames"))
{
SimpleRenderOperation::frames = argData.flagArgumentInt("numOfFrames", 0);
}
if (argData.isFlagSet("pointSize"))
{
SimpleRenderOperation::pointSize = argData.flagArgumentDouble("pointSize", 0);
}
// force viewport to update
MGlobal::executeCommandOnIdle("refresh;");
return status;
}
// plugin registration/deregistration
MStatus initializePlugin(MObject obj)
{
MStatus status = MStatus::kSuccess;
MFnPlugin plugin(obj, "Simple Render Example", "Any", "Any");
MHWRender::MRenderer* renderer = MHWRender::MRenderer::theRenderer();
if (renderer)
{
SimpleRenderOverride *overridePtr =
new SimpleRenderOverride(SimpleRenderOverride::viewName);
if (overridePtr)
{
renderer->registerOverride(overridePtr);
}
}
status = plugin.registerCommand(
"simpleTrack",
simpleTrackCmd::creator,
simpleTrackCmd::newSyntax
);
if (!status)
{
status.perror("Failed to register simpleTrack");
}
return status;
}
MStatus uninitializePlugin(MObject obj)
{
MStatus status = MStatus::kSuccess;
MFnPlugin plugin(obj);
MHWRender::MRenderer* renderer = MHWRender::MRenderer::theRenderer();
if (renderer)
{
const MHWRender::MRenderOverride* overridePtr =
renderer->findRenderOverride(SimpleRenderOverride::viewName);
if (overridePtr)
{
renderer->deregisterOverride(overridePtr);
delete overridePtr;
}
}
simpleTrackCmd::clearTracking();
status = plugin.deregisterCommand("simpleTrack");
if (!status)
{
status.perror("Failed to deregister simpleTrack");
}
return status;
}