PeriDyno 1.0.0
Loading...
Searching...
No Matches
GltfLoader.cu
Go to the documentation of this file.
1#include "GltfLoader.h"
2#include <GLPhotorealisticRender.h>
3
4#define NULL_TIME (-9599.99)
5#define NULL_POSITION (-959959.9956)
6#define TINYGLTF_IMPLEMENTATION
7
8#include "tinygltf/tiny_gltf.h"
9
10#include "GltfFunc.h"
11#include "ImageLoader.h"
12
13namespace dyno
14{
15 template<typename uint>
16 __global__ void C_Shape_PointCounter(
17 DArray<int> counter,
18 DArray<uint> point_ShapeIds,
19 uint target
20 );
21 IMPLEMENT_CLASS(BoundingBoxOfTextureMesh);
22
23 BoundingBoxOfTextureMesh::BoundingBoxOfTextureMesh()
24 : ComputeModule()
25 {
26 auto callback = std::make_shared<FCallBackFunc>(std::bind(&BoundingBoxOfTextureMesh::shapeIdChanged, this));
27
28 this->varShapeId()->attach(callback);
29 }
30
31 void BoundingBoxOfTextureMesh::compute()
32 {
33 shapeIdChanged();
34 }
35
36 void BoundingBoxOfTextureMesh::shapeIdChanged()
37 {
38 uint shapeId = this->varShapeId()->getValue();
39 auto mesh = this->inTextureMesh()->constDataPtr();
40
41 if (mesh == nullptr)
42 {
43 return;
44 }
45
46 if (this->outBoundingBox()->isEmpty())
47 {
48 this->outBoundingBox()->allocate();
49 }
50
51 auto& es = this->outBoundingBox()->getData();
52
53 if (shapeId < mesh->shapes().size())
54 {
55 auto bb = mesh->shapes()[shapeId]->boundingBox;
56
57 this->varCenter()->setValue((bb.v0 + bb.v1) / 2);
58 this->varLowerBound()->setValue(bb.v0);
59 this->varUpperBound()->setValue(bb.v1);
60
61 std::vector<Vec3f> vertices;
62
63 Vec3f v0 = bb.v0;
64 Vec3f v1 = Vec3f(bb.v0.x, bb.v0.y, bb.v1.z);
65 Vec3f v2 = Vec3f(bb.v1.x, bb.v0.y, bb.v1.z);
66 Vec3f v3 = Vec3f(bb.v1.x, bb.v0.y, bb.v0.z);
67
68 Vec3f v4 = Vec3f(bb.v0.x, bb.v1.y, bb.v0.z);
69 Vec3f v5 = Vec3f(bb.v0.x, bb.v1.y, bb.v1.z);
70 Vec3f v6 = bb.v1;
71 Vec3f v7 = Vec3f(bb.v1.x, bb.v1.y, bb.v0.z);
72
73 vertices.push_back(v0);
74 vertices.push_back(v1);
75 vertices.push_back(v2);
76 vertices.push_back(v3);
77 vertices.push_back(v4);
78 vertices.push_back(v5);
79 vertices.push_back(v6);
80 vertices.push_back(v7);
81
82 std::vector<TopologyModule::Edge> edges;
83 edges.push_back(TopologyModule::Edge(0, 1));
84 edges.push_back(TopologyModule::Edge(1, 2));
85 edges.push_back(TopologyModule::Edge(2, 3));
86 edges.push_back(TopologyModule::Edge(3, 0));
87
88 edges.push_back(TopologyModule::Edge(4, 5));
89 edges.push_back(TopologyModule::Edge(5, 6));
90 edges.push_back(TopologyModule::Edge(6, 7));
91 edges.push_back(TopologyModule::Edge(7, 4));
92
93 edges.push_back(TopologyModule::Edge(0, 4));
94 edges.push_back(TopologyModule::Edge(1, 5));
95 edges.push_back(TopologyModule::Edge(2, 6));
96 edges.push_back(TopologyModule::Edge(3, 7));
97
98 es.setPoints(vertices);
99 es.setEdges(edges);
100
101 es.update();
102 }
103 else
104 es.clear();
105 }
106
107 template<typename TDataType>
108 GltfLoader<TDataType>::GltfLoader()
109 {
110 auto callback = std::make_shared<FCallBackFunc>(std::bind(&GltfLoader<TDataType>::varChanged, this));
111 auto animationCallback = std::make_shared<FCallBackFunc>(std::bind(&GltfLoader<TDataType>::varAnimation, this));
112
113 this->stateJointSet()->setDataPtr(std::make_shared<EdgeSet<DataType3f>>());
114 this->stateShapeCenter()->setDataPtr(std::make_shared<PointSet<DataType3f>>());
115
116 this->varImportAnimation()->attach(callback);
117 this->varImportAnimation()->attach(animationCallback);
118 this->varFileName()->attach(callback);
119 this->varAnimationSpeed()->setRange(0.02,10);
120
121 auto callbackTransform = std::make_shared<FCallBackFunc>(std::bind(&GltfLoader<TDataType>::updateTransform, this));
122
123 this->varLocation()->attach(callbackTransform);
124 this->varScale()->attach(callbackTransform);
125 this->varRotation()->attach(callbackTransform);
126 this->varUseInstanceTransform()->attach(callbackTransform);
127
128
129 this->stateTextureMesh()->setDataPtr(std::make_shared<TextureMesh>());
130
131 auto render = std::make_shared<GLPhotorealisticRender>();
132 this->stateTextureMesh()->connect(render->inTextureMesh());
133 this->graphicsPipeline()->pushModule(render);
134
135 //Joint Render
136 auto callbackRender = std::make_shared<FCallBackFunc>(std::bind(&GltfLoader<TDataType>::varRenderChanged, this));
137 this->varJointRadius()->attach(callbackRender);
138
139 jointPointRender = std::make_shared<GLPointVisualModule>();
140 jointPointRender->setColor(Color(1.0f, 0.0f, 0.0f));
141 jointPointRender->varPointSize()->setValue(this->varJointRadius()->getValue());
142 jointPointRender->setVisible(true);
143 this->stateJointSet()->connect(jointPointRender->inPointSet());
144 this->graphicsPipeline()->pushModule(jointPointRender);
145
146 jointLineRender = std::make_shared<GLWireframeVisualModule>();
147 jointLineRender->varBaseColor()->setValue(Color(0, 1, 0));
148 jointLineRender->setVisible(true);
149 jointLineRender->varRadius()->setValue(this->varJointRadius()->getValue() / 3);
150 jointLineRender->varRenderMode()->setCurrentKey(GLWireframeVisualModule::EEdgeMode::CYLINDER);
151 this->stateJointSet()->connect(jointLineRender->inEdgeSet());
152 this->graphicsPipeline()->pushModule(jointLineRender);
153
154 this->stateAnimation()->setDataPtr(std::make_shared<JointAnimationInfo>());
155 this->stateAnimation()->promoteOuput();
156
157 auto glShapeCenter = std::make_shared<GLPointVisualModule>();
158 glShapeCenter->setColor(Color(1.0f, 1.0f, 0.0f));
159 glShapeCenter->varPointSize()->setValue(this->varJointRadius()->getValue() * 2);
160 glShapeCenter->setVisible(true);
161 this->stateShapeCenter()->connect(glShapeCenter->inPointSet());
162 this->graphicsPipeline()->pushModule(glShapeCenter);
163
164 auto showBoundingBox = std::make_shared<BoundingBoxOfTextureMesh>();
165 this->stateTextureMesh()->connect(showBoundingBox->inTextureMesh());
166 this->graphicsPipeline()->pushModule(showBoundingBox);
167
168 auto bbRender = std::make_shared<GLWireframeVisualModule>();
169 showBoundingBox->outBoundingBox()->connect(bbRender->inEdgeSet());
170 this->graphicsPipeline()->pushModule(bbRender);
171
172 this->stateSkin()->setDataPtr(std::make_shared<SkinInfo>());
173 this->stateJointsData()->setDataPtr(std::make_shared<JointInfo>());
174
175 this->stateTextureMesh()->promoteOuput();
176
177 this->setForceUpdate(false);
178
179 }
180
181 template<typename TDataType>
182 void GltfLoader<TDataType>::varAnimation()
183 {
184 auto importAnimation = this->varImportAnimation()->getValue();
185 if(importAnimation)
186 this->setForceUpdate(true);
187 else
188 this->setForceUpdate(false);
189
190
191
192 }
193
194
195 template<typename TDataType>
196 void GltfLoader<TDataType>::varChanged()
197 {
198 if (this->varFileName()->isEmpty())
199 return;
200
201 this->updateTransformState();
202
203 printf("!!!!!!!!!!!!!!!!! Import GLTF !!!!!!!!!!!!!!!!!!!!!!!!\n\n\n");
204
205 this->InitializationData();
206
207
208 using namespace tinygltf;
209
210 auto newModel = new Model;
211
212
213 TinyGLTF loader;
214 std::string err;
215 std::string warn;
216 std::string filename = this->varFileName()->getValue().string();
217
218
219 bool ret = loader.LoadASCIIFromFile(newModel, &err, &warn, filename);
220
221 if (!warn.empty()) {
222 printf("Warn: %s\n", warn.c_str());
223 }
224
225 if (!err.empty()) {
226 printf("Err: %s\n", err.c_str());
227 }
228
229 if (!ret) {
230 printf("Failed to parse glTF\n");
231 return;
232 }
233
234 ////import Animation
235 std::map<joint, std::vector<Real>> joint_T_Time;
236 std::map<joint, std::vector<Real>> joint_S_Time;
237 std::map<joint, std::vector<Real>> joint_R_Time;
238 std::map<joint, std::vector<Vec3f>> joint_T_f_anim;
239 std::map<joint, std::vector<Quat<float>>> joint_R_f_anim;
240 std::map<joint, std::vector<Vec3f>> joint_S_f_anim;
241
242 importAnimation(*newModel, joint_output, joint_input, joint_T_f_anim, joint_T_Time, joint_S_f_anim, joint_S_Time, joint_R_f_anim, joint_R_Time);
243
244 for (int i = 0; i< newModel->nodes.size();i++)
245 {
246 node_Name[i] = newModel->nodes[i].name;
247 }
248
249 // import Scenes:
250
251 std::map<scene, std::vector<int>> Scene_Nodes;
252 for (size_t i = 0; i < newModel->scenes.size(); i++)
253 {
254 std::vector<int> vecS_Roots;
255 vecS_Roots = newModel->scenes[i].nodes;
256 Scene_Nodes[i] = vecS_Roots;
257 }
258 std::vector<int> all_Nodes;
259 std::map<joint, std::vector<int>> nodeId_Dir;
260 int jointNum = -1;
261 int meshNum = -1;
262 std::vector<int> all_Meshs;
263 std::map<int, std::vector<int>> meshId_Dir;
264
265 getNodesAndHierarchy(*newModel, Scene_Nodes, all_Nodes, nodeId_Dir); //jointId_joint_Dir //update private: std::map<joint, std::vector<int>> jointId_joint_Dir;
266
267 updateJoint_Mesh_Camera_Dir(*newModel, jointNum, meshNum, jointId_joint_Dir, all_Joints, all_Nodes, nodeId_Dir, meshId_Dir, all_Meshs, maxJointId);
268
269 std::vector<std::vector<int>> joint_child; //build edgeset;
270
271 //get Local Transform T S R M
272 getJointsTransformData(all_Nodes, joint_child, joint_rotation, joint_scale, joint_translation, joint_matrix, *newModel);
273
274
275 d_joints.assign(all_Joints);
276
277 //get InverseBindMatrix (Global)
278 this->buildInverseBindMatrices(all_Joints);
279
280 std::vector<Mat4f> localMatrix;
281 localMatrix.resize(maxJointId + 1);
282
283 for (auto jId : all_Joints)
284 {
285 localMatrix[jId] = joint_matrix[jId];
286 }
287
288 this->stateJointLocalMatrix()->assign(localMatrix);
289
290
291 // get joint World Location
292 printf("************ Set Joint ************\n");
293 {
294 std::vector<Coord> jointVertices;
295 std::map<int, int> jointId_VId;
296
297 for (size_t j = 0; j < jointNum; j++)
298 {
299 joint jId = all_Joints[j];
300 jointId_VId[jId] = jointVertices.size();
301
302 jointVertices.push_back(getVertexLocationWithJointTransform(jId, Vec3f(0, 0, 0), joint_matrix));
303
304 }
305
306 //
307 this->stateJointSet()->getDataPtr()->setPoints(jointVertices);
308 std::vector<TopologyModule::Edge> edges;
309
310 for (size_t i = 0; i < jointNum; i++)
311 {
312 for (auto childId : joint_child[all_Joints[i]])
313 {
314 edges.push_back(TopologyModule::Edge(i, jointId_VId[childId]));
315 }
316 }
317 this->stateJointSet()->getDataPtr()->setEdges(edges);
318
319 jointVertices.clear();
320 }
321
322 auto texMesh = this->stateTextureMesh()->getDataPtr();
323 //materials
324 loadGLTFMaterial(*newModel, texMesh, filename);
325
326
327 loadGLTFShape(*newModel, texMesh, filename, &initialPosition,&initialNormal, &d_mesh_Matrix,&d_shape_meshId, this->stateSkin()->getDataPtr());
328
329
330 this->updateTransform();
331
332 this->stateSkin()->getDataPtr()->mesh = texMesh;
333
334 this->stateSkin()->getDataPtr()->initialPosition = initialPosition;
335
336 this->stateSkin()->getDataPtr()->initialNormal = initialNormal;
337
338
339
340 this->stateJointsData()->getDataPtr()->UpdateJointInfo(
341 this->stateJointInverseBindMatrix()->getData(),
342 this->stateJointLocalMatrix()->getData(),
343 this->stateJointWorldMatrix()->getData(),
344 all_Joints,
345 jointId_joint_Dir,
346 joint_translation,
347 joint_scale,
348 joint_rotation
349 );
350
351 this->stateJointsData()->getDataPtr()->setJointName(joint_Name);
352
353 this->stateAnimation()->getDataPtr()->setAnimationData(
354 joint_T_f_anim,
355 joint_T_Time,
356 joint_S_f_anim,
357 joint_S_Time,
358 joint_R_f_anim,
359 joint_R_Time,
360 this->stateJointsData()->getDataPtr()
361 );
362
363 this->stateAnimation()->getDataPtr()->setLoop(false);
364
365 this->updateAnimation(0);
366
367 delete newModel;
368 }
369
370 // ***************************** function *************************** //
371
372 template<typename TDataType>
373 void GltfLoader<TDataType>::updateTransform()
374 {
375 //updateModelTransformMatrix
376 this->updateTransformState();
377 auto animation = this->stateAnimation()->getDataPtr();
378 if (all_Joints.size()) //Animation
379 {
380 if (varImportAnimation()->getValue() && (!animation->mJoint_Index_Translation.empty() && !animation->mJoint_Index_Rotation.empty() && !animation->mJoint_Index_Scale.empty()))
381 updateAnimation(this->stateFrameNumber()->getValue());
382
383 }
384
385 if (this->stateTextureMesh()->getDataPtr()->vertices().size())
386 {
387 //Move by Dir
388 if (true)
389 {
390 cuExecute(this->stateTextureMesh()->getDataPtr()->vertices().size(),
391 ShapeTransform,
392 initialPosition,
393 this->stateTextureMesh()->getDataPtr()->vertices(),
394 initialNormal,
395 this->stateTextureMesh()->getDataPtr()->normals(),
396 d_mesh_Matrix,
397 this->stateTextureMesh()->getDataPtr()->shapeIds(),
398 d_shape_meshId
399 );
400 }
401
402 //Move by VarTransform
403 }
404
405
406
407 //update BoundingBox
408
409 auto shapeNum = this->stateTextureMesh()->getDataPtr()->shapes().size();
410
411 CArray<Coord> c_shapeCenter;
412 c_shapeCenter.resize(shapeNum);
413 //counter
414 for (uint i = 0; i < shapeNum; i++)
415 {
416 DArray<int> counter;
417 counter.resize(this->stateTextureMesh()->getDataPtr()->vertices().size());
418
419
420 cuExecute(this->stateTextureMesh()->getDataPtr()->vertices().size(),
421 C_Shape_PointCounter,
422 counter,
423 this->stateTextureMesh()->getDataPtr()->shapeIds(),
424 i
425 );
426
427 Reduction<int> reduce;
428 int num = reduce.accumulate(counter.begin(), counter.size());
429
430 DArray<Coord> targetPoints;
431 targetPoints.resize(num);
432
433 Scan<int> scan;
434 scan.exclusive(counter.begin(), counter.size());
435
436 cuExecute(this->stateTextureMesh()->getDataPtr()->vertices().size(),
437 C_SetupPoints,
438 targetPoints,
439 this->stateTextureMesh()->getDataPtr()->vertices(),
440 counter
441 );
442
443
444 Reduction<Coord> reduceBounding;
445
446 auto& bounding = this->stateTextureMesh()->getDataPtr()->shapes()[i]->boundingBox;
447 Coord lo = reduceBounding.minimum(targetPoints.begin(), targetPoints.size());
448 Coord hi = reduceBounding.maximum(targetPoints.begin(), targetPoints.size());
449
450 bounding.v0 = lo;
451 bounding.v1 = hi;
452 this->stateTextureMesh()->getDataPtr()->shapes()[i]->boundingTransform.translation() = (lo + hi) / 2;
453
454 c_shapeCenter[i] = (lo + hi) / 2;
455
456 targetPoints.clear();
457
458 counter.clear();
459 }
460
461 d_ShapeCenter.assign(c_shapeCenter); // Used to "ToCenter"
462 unCenterPosition.assign(this->stateTextureMesh()->getDataPtr()->vertices());
463
464 //ToCenter
465 if (varUseInstanceTransform()->getValue())
466 {
467 cuExecute(this->stateTextureMesh()->getDataPtr()->vertices().size(),
468 ShapeToCenter,
469 unCenterPosition,
470 this->stateTextureMesh()->getDataPtr()->vertices(),
471 this->stateTextureMesh()->getDataPtr()->shapeIds(),
472 d_ShapeCenter
473 );
474
475 auto& reShapes = this->stateTextureMesh()->getDataPtr()->shapes();
476
477 for (size_t i = 0; i < shapeNum; i++)
478 {
479 reShapes[i]->boundingTransform.translation() = reShapes[i]->boundingTransform.translation() + this->varLocation()->getValue();
480 }
481 }
482 else
483 {
484 auto& reShapes = this->stateTextureMesh()->getDataPtr()->shapes();
485
486 for (size_t i = 0; i < shapeNum; i++)
487 {
488 reShapes[i]->boundingTransform.translation() = Vec3f(0);
489 }
490 }
491
492 this->stateShapeCenter()->getDataPtr()->setPoints(d_ShapeCenter);
493
494 }
495
496 template<typename TDataType>
497 void GltfLoader<TDataType>::updateStates()
498 {
499 ParametricModel<TDataType>::updateStates();
500 auto animation = this->stateAnimation()->getDataPtr();
501
502 if (joint_output.empty() || !this->varImportAnimation()->getValue() || (animation->mJoint_Index_Rotation.empty()&& animation->mJoint_Index_Translation.empty()&&animation->mJoint_Index_Scale.empty()))
503 return;
504
505 updateAnimation(this->stateFrameNumber()->getValue());
506 auto jointInfo = this->stateJointsData()->getDataPtr();
507
508 this->stateJointsData()->getDataPtr()->UpdateJointInfo(
509 this->stateJointInverseBindMatrix()->getData(),
510 this->stateJointLocalMatrix()->getData(),
511 this->stateJointWorldMatrix()->getData(),
512 all_Joints,
513 jointId_joint_Dir,
514 joint_translation,
515 joint_scale,
516 joint_rotation
517 );
518 };
519
520
521 template<typename TDataType>
522 void GltfLoader<TDataType>::updateAnimation(int frameNumber)
523 {
524 if (joint_output.empty() || all_Joints.empty() || joint_matrix.empty())
525 return;
526
527 auto mesh = this->stateTextureMesh()->getDataPtr();
528
529
530 this->stateAnimation()->getDataPtr()->updateAnimationPose(this->stateElapsedTime()->getValue()* this->varAnimationSpeed()->getValue());
531
532
533 //update Joints
534 cuExecute(all_Joints.size(),
535 jointAnimation,
536 this->stateJointSet()->getDataPtr()->getPoints(),
537 this->stateJointsData()->getDataPtr()->mJointWorldMatrix,
538 d_joints,
539 this->stateTransform()->getValue()
540 );
541
542
543 //update Points
544
545 auto& skinInfo = this->stateSkin()->getData();
546
547
548 for (size_t i = 0; i < skinInfo.size(); i++)//
549 {
550 auto& bindJoint0 = skinInfo.V_jointID_0[i];
551 auto& bindJoint1 = skinInfo.V_jointID_1[i];
552
553 auto& bindWeight0 = skinInfo.V_jointWeight_0[i];
554 auto& bindWeight1 = skinInfo.V_jointWeight_1[i];
555
556 for (size_t j = 0; j < skinInfo.skin_VerticeRange[i].size(); j++)
557 {
558 //
559 Vec2u& range = skinInfo.skin_VerticeRange[i][j];
560
561 skinAnimation(initialPosition,
562 mesh->vertices(),
563 this->stateJointInverseBindMatrix()->getData(),
564 this->stateJointsData()->getDataPtr()->mJointWorldMatrix,
565
566 bindJoint0,
567 bindJoint1,
568 bindWeight0,
569 bindWeight1,
570 this->stateTransform()->getValue(),
571 false,
572 range
573 );
574
575 //update Normals
576
577 skinAnimation(
578 initialNormal,
579 mesh->normals(),
580 this->stateJointInverseBindMatrix()->getData(),
581 this->stateJointsData()->getDataPtr()->mJointWorldMatrix,
582
583 bindJoint0,
584 bindJoint1,
585 bindWeight0,
586 bindWeight1,
587 this->stateTransform()->getValue(),
588 true,
589 range
590 );
591
592 }
593 }
594
595
596 };
597
598
599
600 template<typename TDataType>
601 Vec3f GltfLoader<TDataType>::getVertexLocationWithJointTransform(joint jointId, Vec3f inPoint, std::map<joint, Mat4f> jMatrix)
602 {
603
604 Vec3f result = Vec3f(0);
605
606 const std::vector<int>& jD = getJointDirByJointIndex(jointId, jointId_joint_Dir);
607
608 Mat4f tempMatrix = Mat4f::identityMatrix();
609
610 //
611 for (int k = jD.size() - 1; k >= 0; k--)
612 {
613 joint select = jD[k];
614 tempMatrix *= jMatrix[select]; //
615
616 }
617
618 Vec4f jointLocation = tempMatrix * Vec4f(inPoint[0], inPoint[1], inPoint[2], 1);
619 result = Coord(jointLocation[0], jointLocation[1], jointLocation[2]);
620
621 return result;
622 };
623
624
625 template<typename TDataType>
626 void GltfLoader<TDataType>::buildInverseBindMatrices(const std::vector<joint>& all_Joints)
627 {
628
629 std::map<joint, Mat4f> tempJointMatrix = joint_matrix;
630 std::vector<Mat4f> temp;
631
632 temp.resize(maxJointId + 1);
633
634 for (size_t i = 0; i < maxJointId + 1; i++)
635 {
636 //temp.push_back(Mat4f::identityMatrix());
637 temp[i] = Mat4f::identityMatrix();
638
639 }
640
641
642
643 for (size_t i = 0; i < all_Joints.size(); i++)
644 {
645 joint jointId = all_Joints[i];
646
647 const std::vector<int>& jD = getJointDirByJointIndex(jointId, jointId_joint_Dir);
648
649 Mat4f tempMatrix = Mat4f::identityMatrix();
650
651
652 for (int k = 0; k < jD.size(); k++)
653 {
654 joint select = jD[k];
655
656 Vec3f tempVT = Vec3f(0, 0, 0);
657 Vec3f tempVS = Vec3f(1, 1, 1);
658 Quat<float> tempQR = Quat<float>(Mat3f::identityMatrix());
659
660 if (joint_input.find(select) != joint_input.end())
661 {
662 tempQR = joint_rotation[select];
663
664 tempVT = joint_translation[select];
665
666 tempVS = joint_scale[select];
667
668 Mat4f mT = Mat4f(1, 0, 0, tempVT[0], 0, 1, 0, tempVT[1], 0, 0, 1, tempVT[2], 0, 0, 0, 1);
669 Mat4f mS = Mat4f(tempVS[0], 0, 0, 0, 0, tempVS[1], 0, 0, 0, 0, tempVS[2], 0, 0, 0, 0, 1);
670 Mat4f mR = tempQR.toMatrix4x4();
671 //
672
673 tempJointMatrix[select] = mT * mS * mR;
674 }
675
676 tempMatrix *= tempJointMatrix[select].inverse();
677
678 }
679
680 joint_inverseBindMatrix[jointId] = (tempMatrix);
681
682
683
684 temp[jointId] = tempMatrix;
685
686 }
687
688 this->stateJointInverseBindMatrix()->assign(temp);
689
690 };
691
692
693 template<typename TDataType>
694 Vec3f GltfLoader<TDataType>::getmeshPointDeformByJoint(joint jointId, Coord worldPosition, std::map<joint, Mat4f> jMatrix)
695 {
696 Vec3f offest = worldPosition;
697
698 Vec4f v_bone_space = joint_inverseBindMatrix[jointId] * Vec4f(offest[0], offest[1], offest[2], 1);//
699
700 auto v_world_space = getVertexLocationWithJointTransform(jointId, Vec3f(v_bone_space[0], v_bone_space[1], v_bone_space[2]), jMatrix);
701
702 return v_world_space;
703 }
704
705 template<typename TDataType>
706 void GltfLoader<TDataType>::updateTransformState()
707 {
708 Vec3f location = this->varLocation()->getValue();
709 Vec3f scale = this->varScale()->getValue();
710 Mat4f mT = Mat4f(1, 0, 0, location[0], 0, 1, 0, location[1], 0, 0, 1, location[2], 0, 0, 0, 1);
711 Mat4f mS = Mat4f(scale[0], 0, 0, 0, 0, scale[1], 0, 0, 0, 0, scale[2], 0, 0, 0, 0, 1);
712 Mat4f mR = this->computeQuaternion().toMatrix4x4();
713 Mat4f transform = mT * mS * mR;
714
715 this->stateTransform()->setValue(transform);
716 }
717
718
719
720
721 template< typename Coord, typename Mat4f>
722 __global__ void StaticMeshTransform(
723 DArray<Coord> initialPosition,
724 DArray<Coord> Position,
725 Mat4f transform
726 )
727 {
728 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
729 if (pId >= Position.size()) return;
730
731 Vec4f temp = transform * Vec4f(initialPosition[pId][0], initialPosition[pId][1], initialPosition[pId][2], 1);
732 Position[pId] = Vec3f(temp[0], temp[1], temp[2]);
733
734 }
735
736
737 template< typename Coord, typename Mat4f, typename Vec3f >
738 __global__ void ShapeTransform(
739 DArray<Coord> intialPosition,
740 DArray<Vec3f> worldPosition,
741 DArray<Coord> intialNormal,
742 DArray<Vec3f> Normal,
743 DArray<Mat4f> WorldMatrix,
744 DArray<uint> vertexId_shape,
745 DArray<int> shapeId_MeshId
746 )
747 {
748 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
749 if (pId >= intialPosition.size()) return;
750
751 int shape = vertexId_shape[pId];
752
753 int MeshId = shapeId_MeshId[shape];
754
755 Vec4f tempV = Vec4f(intialPosition[pId][0], intialPosition[pId][1], intialPosition[pId][2], 1);
756 Vec4f tempN = Vec4f(intialNormal[pId][0], intialNormal[pId][1], intialNormal[pId][2], 0);
757 if (pId == 1)
758 {
759 auto iP = intialPosition[pId];
760 }
761
762 tempV = WorldMatrix[MeshId] * tempV;
763 tempN = WorldMatrix[MeshId] * tempN;
764
765 worldPosition[pId] = Coord(tempV[0], tempV[1], tempV[2]);
766 Normal[pId] = Coord(tempN[0], tempN[1], tempN[2]);
767 if (pId == 1)
768 {
769 auto iP = worldPosition[pId];
770 }
771
772 }
773
774
775
776
777
778
779 template< typename Coord, typename Vec4f, typename Mat4f ,typename Vec2u>
780 __global__ void PointsAnimation(
781 DArray<Coord> intialPosition,
782 DArray<Coord> worldPosition,
783 DArray<Mat4f> joint_inverseBindMatrix,
784 DArray<Mat4f> WorldMatrix,
785
786 DArray<Vec4f> bind_joints_0,
787 DArray<Vec4f> bind_joints_1,
788 DArray<Vec4f> weights_0,
789 DArray<Vec4f> weights_1,
790
791 Mat4f transform,
792 bool isNormal,
793
794 Vec2u range
795 )
796 {
797 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
798 if (pId >= worldPosition.size()) return;
799
800 if (pId<range[0] || pId>range[1])
801 return;
802
803 Vec4f result = Vec4f(0, 0, 0, float(!isNormal));
804
805 int skinInfoVertexId = pId - range[0];
806
807 Coord offest;
808
809 bool j0 = bind_joints_0.size();
810 bool j1 = bind_joints_1.size();
811
812 if (j0)
813 {
814 for (unsigned int i = 0; i < 4; i++)
815 {
816 int jointId = int(bind_joints_0[skinInfoVertexId][i]);
817 Real weight = weights_0[skinInfoVertexId][i];
818
819 offest = intialPosition[pId];
820 Vec4f v_bone_space = joint_inverseBindMatrix[jointId] * Vec4f(offest[0], offest[1], offest[2], float(!isNormal));//
821
822 result += (transform * WorldMatrix[jointId] * v_bone_space) * weight;
823 }
824 }
825 if (j1)
826 {
827 for (unsigned int i = 0; i < 4; i++)
828 {
829 int jointId = int(bind_joints_1[skinInfoVertexId][i]);
830 Real weight = weights_1[skinInfoVertexId][i];
831
832 offest = intialPosition[pId];
833 Vec4f v_bone_space = joint_inverseBindMatrix[jointId] * Vec4f(offest[0], offest[1], offest[2], float(!isNormal));//
834
835 result += (WorldMatrix[jointId] * v_bone_space) * weight;
836 }
837 }
838
839 //result = transform * result;
840
841 if (j0 | j1)
842 {
843 worldPosition[pId][0] = result[0];
844 worldPosition[pId][1] = result[1];
845 worldPosition[pId][2] = result[2];
846 }
847
848 if (isNormal)
849 worldPosition[pId] = worldPosition[pId].normalize();
850
851 }
852
853 template< typename Coord, typename Mat4f>
854 __global__ void jointAnimation(
855 DArray<Coord> worldPosition,
856 DArray<Mat4f> WorldMatrix,
857 DArray<int> joints,
858 Mat4f transform
859 )
860 {
861 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
862 if (pId >= joints.size()) return;
863
864 Vec4f result = Vec4f(0, 0, 0, 1);
865 Coord offest;
866 int jointId = joints[pId];
867 result = transform * WorldMatrix[jointId] * result;
868
869 //result = transform * result;
870
871 worldPosition[pId][0] = result[0];
872 worldPosition[pId][1] = result[1];
873 worldPosition[pId][2] = result[2];
874
875 }
876
877
878
879 template<typename TDataType>
880 void GltfLoader<TDataType>::InitializationData()
881 {
882 joint_rotation.clear();
883 joint_scale.clear();
884 joint_translation.clear();
885 joint_matrix.clear();
886 jointId_joint_Dir.clear();
887
888 Scene_Name.clear();
889 joint_output.clear();
890 joint_input.clear();
891 joint_inverseBindMatrix.clear();
892 joint_AnimaMatrix.clear();
893 Scene_Name.clear();
894 all_Joints.clear();
895
896 initialPosition.clear();
897 d_joints.clear();
898 initialNormal.clear();
899
900
901 meshId_Dir.clear();
902 node_matrix.clear();
903
904 d_mesh_Matrix.clear();
905 d_shape_meshId.clear();
906 unCenterPosition.clear();
907 node_Name.clear();
908
909
910 maxJointId = -1;
911
912
913
914 this->stateInitialMatrix()->clear();
915 this->stateJointInverseBindMatrix()->clear();
916 this->stateJointLocalMatrix()->clear();
917
918 this->stateJointWorldMatrix()->clear();
919 this->stateTexCoord_0()->clear();
920 this->stateTexCoord_1()->clear();
921 this->stateTextureMesh()->getDataPtr()->clear();
922
923 }
924
925
926
927 template<typename TDataType>
928 GltfLoader<TDataType>::~GltfLoader()
929 {
930 InitializationData();
931
932 initialPosition.clear();
933 initialNormal.clear();
934 d_joints.clear();
935 }
936
937 template<typename TDataType>
938 void GltfLoader<TDataType>::varRenderChanged()
939 {
940 jointLineRender->varRadius()->setValue(this->varJointRadius()->getValue() / 2);
941 jointPointRender->varPointSize()->setValue(this->varJointRadius()->getValue());
942 }
943
944
945
946 template< typename Coord, typename uint>
947 __global__ void ShapeToCenter(
948 DArray<Coord> iniPos,
949 DArray<Coord> finalPos,
950 DArray<uint> shapeId,
951 DArray<Coord> t
952 )
953 {
954 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
955 if (pId >= iniPos.size()) return;
956
957 finalPos[pId] = iniPos[pId] - t[shapeId[pId]];
958 Vec4f P = Vec4f(finalPos[pId][0], finalPos[pId][1], finalPos[pId][2], 1);
959
960 finalPos[pId] = Coord(P[0], P[1], P[2]);
961
962 }
963
964
965
966 template< typename Coord, typename uint>
967 __global__ void initialCenterCoord(
968 DArray<Coord> Pos,
969 DArray<uint> shapeId,
970 DArray<int> pointId,
971 DArray<Coord> iniPoint,
972 int shapeNum
973 )
974 {
975 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
976 if (pId >= Pos.size()) return;
977
978 for (int i = 0; i < shapeNum; i++)
979 {
980 if (shapeId[pId] == i && pointId[i] == -1)
981 {
982 pointId[i] = i;
983 iniPoint[i] = Pos[pId];
984 break;
985 }
986 }
987 }
988
989
990 template<typename uint>
991 __global__ void C_Shape_PointCounter(
992 DArray<int> counter,
993 DArray<uint> point_ShapeIds,
994 uint target
995 )
996 {
997 uint tId = threadIdx.x + blockDim.x * blockIdx.x;
998 if (tId >= point_ShapeIds.size()) return;
999
1000 counter[tId] = (point_ShapeIds[tId]== target) ? 1 : 0;
1001 }
1002
1003
1004 template<typename Coord>
1005 __global__ void C_SetupPoints(
1006 DArray<Coord> newPos,
1007 DArray<Coord> pos,
1008 DArray<int> radix
1009 )
1010 {
1011 uint tId = threadIdx.x + blockDim.x * blockIdx.x;
1012 if (tId >= pos.size()) return;
1013
1014 if (tId < pos.size() - 1 && radix[tId] != radix[tId + 1])
1015 {
1016 newPos[radix[tId]] = pos[tId];
1017 }
1018 else if (tId == pos.size() - 1 && pos.size() > 2)
1019 {
1020 if(radix[tId] != radix[tId - 1])
1021 newPos[radix[tId]] = pos[tId];
1022 }
1023
1024 }
1025
1026
1027 DEFINE_CLASS(GltfLoader);
1028}