History

Better AnimDebugDraw rendering

Anthony J. Thibault committed 11 years ago

Commit b7a9b541 parent c1333e18 files · +168 −33Browse files

Showing the changes to libraries/render-utils/src/AnimDebugDraw.cpp (1 of 8 files). Show all

Unified · Split

libraries/render-utils/src/AnimDebugDraw.cpp+109−28
@@ -10,10 +10,11 @@ · 9 unmodified lines
1010 #include "animdebugdraw_vert.h"
1111 #include "animdebugdraw_frag.h"
1212 #include <gpu/Batch.h>
13−
14−#include "AnimDebugDraw.h"
1513 #include "AbstractViewStateInterface.h"
1614 #include "RenderUtilsLogging.h"
15+#include "GLMHelpers.h"
16+
17+#include "AnimDebugDraw.h"
1718
1819 struct Vertex {
1920 glm::vec3 pos;
@@ -165,45 +166,120 @@ static const uint32_t green = toRGBA(0, 255, 0, 255); · 145 unmodified lines
165166 static const uint32_t blue = toRGBA(0, 0, 255, 255);
166167 static const uint32_t cyan = toRGBA(0, 128, 128, 255);
167168
168−static void addWireframeSphereWithAxes(const AnimPose& rootPose, const AnimPose& pose, float radius, Vertex*& v) {
169+const int NUM_CIRCLE_SLICES = 24;
169170
171+static void addBone(const AnimPose& rootPose, const AnimPose& pose, float radius, Vertex*& v) {
172+
173+ AnimPose finalPose = rootPose * pose;
170174 glm::vec3 base = rootPose * pose.trans;
171175
176+ glm::vec3 xRing[NUM_CIRCLE_SLICES + 1]; // one extra for last index.
177+ glm::vec3 yRing[NUM_CIRCLE_SLICES + 1];
178+ glm::vec3 zRing[NUM_CIRCLE_SLICES + 1];
179+ const float dTheta = (2.0f * (float)M_PI) / NUM_CIRCLE_SLICES;
180+ for (int i = 0; i < NUM_CIRCLE_SLICES + 1; i++) {
181+ float rCosTheta = radius * cos(dTheta * i);
182+ float rSinTheta = radius * sin(dTheta * i);
183+ xRing[i] = finalPose * glm::vec3(0.0f, rCosTheta, rSinTheta);
184+ yRing[i] = finalPose * glm::vec3(rCosTheta, 0.0f, rSinTheta);
185+ zRing[i] = finalPose *glm::vec3(rCosTheta, rSinTheta, 0.0f);
186+ }
187+
172188 // x-axis
173189 v->pos = base;
174190 v->rgba = red;
175191 v++;
176− v->pos = rootPose * (pose.trans + pose.rot * glm::vec3(radius, 0.0f, 0.0f));
192+ v->pos = finalPose * glm::vec3(radius * 2.0f, 0.0f, 0.0f);
177193 v->rgba = red;
178194 v++;
179195
196+ // x-ring
197+ for (int i = 0; i < NUM_CIRCLE_SLICES; i++) {
198+ v->pos = xRing[i];
199+ v->rgba = red;
200+ v++;
201+ v->pos = xRing[i + 1];
202+ v->rgba = red;
203+ v++;
204+ }
205+
180206 // y-axis
181207 v->pos = base;
182208 v->rgba = green;
183209 v++;
184− v->pos = rootPose * (pose.trans + pose.rot * glm::vec3(0.0f, radius, 0.0f));
210+ v->pos = finalPose * glm::vec3(0.0f, radius * 2.0f, 0.0f);
185211 v->rgba = green;
186212 v++;
187213
214+ // y-ring
215+ for (int i = 0; i < NUM_CIRCLE_SLICES; i++) {
216+ v->pos = yRing[i];
217+ v->rgba = green;
218+ v++;
219+ v->pos = yRing[i + 1];
220+ v->rgba = green;
221+ v++;
222+ }
223+
188224 // z-axis
189225 v->pos = base;
190226 v->rgba = blue;
191227 v++;
192− v->pos = rootPose * (pose.trans + pose.rot * glm::vec3(0.0f, 0.0f, radius));
228+ v->pos = finalPose * glm::vec3(0.0f, 0.0f, radius * 2.0f);
193229 v->rgba = blue;
194230 v++;
195−}
196231
197−static void addWireframeBoneAxis(const AnimPose& rootPose, const AnimPose& pose,
198− const AnimPose& parentPose, float radius, Vertex*& v) {
199− v->pos = rootPose * pose.trans;
200− v->rgba = cyan;
201− v++;
202− v->pos = rootPose * parentPose.trans;
203− v->rgba = cyan;
204− v++;
232+ // z-ring
233+ for (int i = 0; i < NUM_CIRCLE_SLICES; i++) {
234+ v->pos = zRing[i];
235+ v->rgba = blue;
236+ v++;
237+ v->pos = zRing[i + 1];
238+ v->rgba = blue;
239+ v++;
240+ }
205241 }
206242
243+static void addLink(const AnimPose& rootPose, const AnimPose& pose,
244+ const AnimPose& parentPose, float radius, Vertex*& v) {
245+
246+ AnimPose pose0 = rootPose * parentPose;
247+ AnimPose pose1 = rootPose * pose;
248+
249+ glm::vec3 boneAxisWorld = glm::normalize(pose1.trans - pose0.trans);
250+ glm::vec3 boneAxis0 = glm::normalize(pose0.inverse().xformVector(boneAxisWorld));
251+ glm::vec3 boneAxis1 = glm::normalize(pose1.inverse().xformVector(boneAxisWorld));
252+
253+ glm::vec3 uAxis, vAxis, wAxis;
254+ generateBasisVectors(boneAxis0, glm::vec3(1, 0, 0), uAxis, vAxis, wAxis);
255+
256+ const int NUM_BASE_CORNERS = 4;
257+ glm::vec3 boneBaseCorners[NUM_BASE_CORNERS];
258+ boneBaseCorners[0] = pose0 * ((uAxis * radius) + (vAxis * radius) + (wAxis * radius));
259+ boneBaseCorners[1] = pose0 * ((uAxis * radius) - (vAxis * radius) + (wAxis * radius));
260+ boneBaseCorners[2] = pose0 * ((uAxis * radius) - (vAxis * radius) - (wAxis * radius));
261+ boneBaseCorners[3] = pose0 * ((uAxis * radius) + (vAxis * radius) - (wAxis * radius));
262+
263+ glm::vec3 boneTip = pose1 * (boneAxis1 * -radius);
264+
265+ for (int i = 0; i < NUM_BASE_CORNERS; i++) {
266+ v->pos = boneBaseCorners[i];
267+ v->rgba = cyan;
268+ v++;
269+ v->pos = boneBaseCorners[(i + 1) % NUM_BASE_CORNERS];
270+ v->rgba = cyan;
271+ v++;
272+ }
273+
274+ for (int i = 0; i < NUM_BASE_CORNERS; i++) {
275+ v->pos = boneBaseCorners[i];
276+ v->rgba = cyan;
277+ v++;
278+ v->pos = boneTip;
279+ v->rgba = cyan;
280+ v++;
281+ }
282+}
207283
208284 void AnimDebugDraw::update() {
209285
@@ -215,15 +291,20 @@ void AnimDebugDraw::update() { · 5 unmodified lines
215291 render::PendingChanges pendingChanges;
216292 pendingChanges.updateItem<AnimDebugDrawData>(_itemID, [&](AnimDebugDrawData& data) {
217293
294+ const size_t VERTICES_PER_BONE = (6 + (NUM_CIRCLE_SLICES * 2) * 3);
295+ const size_t VERTICES_PER_LINK = 8 * 2;
296+
297+ const float BONE_RADIUS = 0.0075f;
298+
218299 // figure out how many verts we will need.
219300 int numVerts = 0;
220301 for (auto&& iter : _skeletons) {
221302 AnimSkeleton::Pointer& skeleton = iter.second.first;
222− numVerts += skeleton->getNumJoints() * 6;
303+ numVerts += skeleton->getNumJoints() * VERTICES_PER_BONE;
223304 for (int i = 0; i < skeleton->getNumJoints(); i++) {
224305 auto parentIndex = skeleton->getParentIndex(i);
225306 if (parentIndex >= 0) {
226− numVerts += 2;
307+ numVerts += VERTICES_PER_LINK;
227308 }
228309 }
229310 }
@@ -231,12 +312,12 @@ void AnimDebugDraw::update() { · 1 unmodified line
231312 for (auto&& iter : _animNodes) {
232313 AnimNode::Pointer& animNode = iter.second.first;
233314 auto poses = animNode->getPosesInternal();
234− numVerts += poses.size() * 6;
315+ numVerts += poses.size() * VERTICES_PER_BONE;
235316 auto skeleton = animNode->getSkeleton();
236317 for (size_t i = 0; i < poses.size(); i++) {
237318 auto parentIndex = skeleton->getParentIndex(i);
238319 if (parentIndex >= 0) {
239− numVerts += 2;
320+ numVerts += VERTICES_PER_LINK;
240321 }
241322 }
242323 }
@@ -251,16 +332,17 @@ void AnimDebugDraw::update() { · 8 unmodified lines
251332 for (int i = 0; i < skeleton->getNumJoints(); i++) {
252333 AnimPose pose = skeleton->getAbsoluteBindPose(i);
253334
254− // draw axes
255− const float radius = 0.1f;
256− addWireframeSphereWithAxes(rootPose, pose, radius, v);
335+ const float radius = BONE_RADIUS / (pose.scale.x * rootPose.scale.x);
336+
337+ // draw bone
338+ addBone(rootPose, pose, radius, v);
257339
258− // line to parent.
340+ // draw link to parent
259341 auto parentIndex = skeleton->getParentIndex(i);
260342 if (parentIndex >= 0) {
261343 assert(parentIndex < skeleton->getNumJoints());
262344 AnimPose parentPose = skeleton->getAbsoluteBindPose(parentIndex);
263− addWireframeBoneAxis(rootPose, pose, parentPose, radius, v);
345+ addLink(rootPose, pose, parentPose, radius, v);
264346 }
265347 }
266348 }
@@ -284,14 +366,13 @@ void AnimDebugDraw::update() { · 17 unmodified lines
284366 absAnimPose[i] = poses[i];
285367 }
286368
287− // draw axes
288− const float radius = 0.1f;
289− addWireframeSphereWithAxes(rootPose, absAnimPose[i], radius, v);
369+ const float radius = BONE_RADIUS / (absAnimPose[i].scale.x * rootPose.scale.x);
370+ addBone(rootPose, absAnimPose[i], radius, v);
290371
291372 if (parentIndex >= 0) {
292373 assert((size_t)parentIndex < poses.size());
293374 // draw line to parent
294− addWireframeBoneAxis(rootPose, absAnimPose[i], absAnimPose[parentIndex], radius, v);
375+ addLink(rootPose, absAnimPose[i], absAnimPose[parentIndex], radius, v);
295376 }
296377 }
297378 }