-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCreatureModule.cpp
1935 lines (1595 loc) · 57.7 KB
/
CreatureModule.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* Creature Runtimes License
*
* Copyright (c) 2015, Kestrel Moon Studios
* All rights reserved.
*
* Preamble: This Agreement governs the relationship between Licensee and Kestrel Moon Studios(Hereinafter: Licensor).
* This Agreement sets the terms, rights, restrictions and obligations on using [Creature Runtimes] (hereinafter: The Software) created and owned by Licensor,
* as detailed herein:
* License Grant: Licensor hereby grants Licensee a Sublicensable, Non-assignable & non-transferable, Commercial, Royalty free,
* Including the rights to create but not distribute derivative works, Non-exclusive license, all with accordance with the terms set forth and
* other legal restrictions set forth in 3rd party software used while running Software.
* Limited: Licensee may use Software for the purpose of:
* Running Software on Licensee’s Website[s] and Server[s];
* Allowing 3rd Parties to run Software on Licensee’s Website[s] and Server[s];
* Publishing Software’s output to Licensee and 3rd Parties;
* Distribute verbatim copies of Software’s output (including compiled binaries);
* Modify Software to suit Licensee’s needs and specifications.
* Binary Restricted: Licensee may sublicense Software as a part of a larger work containing more than Software,
* distributed solely in Object or Binary form under a personal, non-sublicensable, limited license. Such redistribution shall be limited to unlimited codebases.
* Non Assignable & Non-Transferable: Licensee may not assign or transfer his rights and duties under this license.
* Commercial, Royalty Free: Licensee may use Software for any purpose, including paid-services, without any royalties
* Including the Right to Create Derivative Works: Licensee may create derivative works based on Software,
* including amending Software’s source code, modifying it, integrating it into a larger work or removing portions of Software,
* as long as no distribution of the derivative works is made
*
* THE RUNTIMES IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE RUNTIMES OR THE USE OR OTHER DEALINGS IN THE
* RUNTIMES.
*****************************************************************************/
#include "CreatureModule.h"
#include <algorithm>
template <typename T>
static T clipNum(const T& n, const T& lower, const T& upper) {
return std::max(lower, std::min(n, upper));
}
static JsonNode * GetJSONLevelNodeFromKey(JsonNode& json_obj,
const std::string& key)
{
JsonNode * ret_node = NULL;
if(std::string(json_obj.key) == key) {
ret_node = &json_obj;
}
else {
JsonNode * cur_node = &json_obj;
while(true) {
if(std::string(cur_node->key) == key) {
ret_node = cur_node;
break;
}
JsonNode * next_node = cur_node->next;
if(next_node == NULL) {
break;
}
cur_node = next_node;
}
}
return ret_node;
}
static JsonNode * GetJSONNodeFromKey(JsonNode& json_obj,
const std::string& key)
{
JsonNode * ret_node = NULL;
for(JsonIterator it = JsonBegin(json_obj.value);
it != JsonEnd(json_obj.value); ++it)
{
if( std::string((*it)->key) == key) {
ret_node = *it;
break;
}
}
return ret_node;
}
static std::vector<std::string> GetJSONKeysFromNode(JsonNode& json_obj)
{
std::vector<std::string> ret_keys;
for(JsonIterator it = JsonBegin(json_obj.value);
it != JsonEnd(json_obj.value); ++it)
{
ret_keys.push_back(std::string((*it)->key));
}
return ret_keys;
}
static std::vector<float> GetJSONNodeFloatArray(JsonNode& json_obj)
{
std::vector<float> ret_vals;
for(JsonIterator it = JsonBegin(json_obj.value);
it != JsonEnd(json_obj.value); ++it)
{
JsonNode * cur_node = *it;
ret_vals.push_back((float)cur_node->value.toNumber());
}
return ret_vals;
}
static std::vector<int> GetJSONNodeIntArray(JsonNode& json_obj)
{
std::vector<int> ret_vals;
for(JsonIterator it = JsonBegin(json_obj.value);
it != JsonEnd(json_obj.value); ++it)
{
JsonNode * cur_node = *it;
ret_vals.push_back((int)cur_node->value.toNumber());
}
return ret_vals;
}
static glm::float32 * ReadJSONPoints3D(JsonNode& json_obj,
const std::string& key,
int& num_pts)
{
std::vector<float> pts_array = GetJSONNodeFloatArray(*GetJSONNodeFromKey(json_obj, key));
num_pts = (int)pts_array.size() / 2; // Since we are storing them as x,y pairs
glm::float32 * ret_pts = new glm::float32[num_pts * 3];
int cur_index = 0;
for(size_t i = 0; i < pts_array.size(); i+=2)
{
ret_pts[cur_index] = pts_array[i];
ret_pts[cur_index + 1] = pts_array[i + 1];
ret_pts[cur_index + 2] = 0;
cur_index += 3;
}
return ret_pts;
}
static glm::float32 * ReadJSONPoints2D(JsonNode& json_obj,
const std::string& key,
int& num_pts)
{
std::vector<float> pts_array = GetJSONNodeFloatArray(*GetJSONNodeFromKey(json_obj, key));
glm::float32 * ret_pts = new glm::float32[pts_array.size()];
for(size_t i = 0; i < pts_array.size(); i++)
{
ret_pts[i] = pts_array[i];
}
num_pts = (int)pts_array.size() / 2; // Since we are storing them as x,y pairs
return ret_pts;
}
static std::vector<glm::vec2> ReadJSONPoints2DVector(JsonNode& json_obj,
const std::string& key)
{
std::vector<float> pts_array = GetJSONNodeFloatArray(*GetJSONNodeFromKey(json_obj, key));
std::vector<glm::vec2> ret_pts(pts_array.size() / 2);
int pt_index = 0;
for(size_t i = 0; i < pts_array.size(); i+=2)
{
ret_pts[pt_index].x = pts_array[i];
ret_pts[pt_index].y = pts_array[i + 1];
pt_index++;
}
return ret_pts;
}
static glm::uint32 * ReadJSONUints(JsonNode& json_obj,
const std::string& key,
int& num_ints)
{
std::vector<int> ints_array = GetJSONNodeIntArray(*GetJSONNodeFromKey(json_obj, key));
glm::uint32 * ret_pts = new glm::uint32[ints_array.size()];
for(size_t i = 0; i < ints_array.size(); i++)
{
ret_pts[i] = (glm::uint32)ints_array[i];
}
num_ints = (int)ints_array.size();
return ret_pts;
}
static glm::vec2 ReadJSONVec2(JsonNode& json_obj,
const std::string& key)
{
std::vector<float> read_array = GetJSONNodeFloatArray(*GetJSONNodeFromKey(json_obj, key));
return glm::vec2(read_array[0], read_array[1]);
}
static glm::vec4 ReadJSONVec4_2(JsonNode& json_obj,
const std::string& key)
{
std::vector<float> read_array = GetJSONNodeFloatArray(*GetJSONNodeFromKey(json_obj, key));
return glm::vec4(read_array[0], read_array[1], 0, 1.0f);
}
static glm::mat4 ReadJSONMat4(JsonNode& json_obj,
const std::string& key)
{
std::vector<float> read_array = GetJSONNodeFloatArray(*GetJSONNodeFromKey(json_obj, key));
float mat_vals[16];
for(int i = 0; i < 16; i++) {
mat_vals[i] = read_array[i];
}
return glm::make_mat4(mat_vals);
}
static std::vector<int> ReadIntArray(JsonNode& json_obj,
const std::string& key)
{
std::vector<int> read_array = GetJSONNodeIntArray(*GetJSONNodeFromKey(json_obj, key));;
std::vector<int> ret_array(read_array.size());
for(int i = 0; i < read_array.size(); i++)
{
ret_array[i] = read_array[i];
}
return ret_array;
}
static std::vector<float> ReadFloatArray(JsonNode& json_obj,
const std::string& key)
{
std::vector<float> read_array = GetJSONNodeFloatArray(*GetJSONNodeFromKey(json_obj, key));
std::vector<float> ret_array(read_array.size());
for(int i = 0; i < read_array.size(); i++)
{
ret_array[i] = read_array[i];
}
return ret_array;
}
static meshBone * CreateBones(JsonNode& json_obj,
const std::string& key)
{
meshBone * root_bone = NULL;
JsonNode * base_obj = GetJSONLevelNodeFromKey(json_obj, key);
std::map<int, std::pair<meshBone *, std::vector<int> > > bone_data;
std::set<int> child_set;
// layout bones
for (JsonIterator it = JsonBegin(base_obj->value);
it != JsonEnd(base_obj->value);
++it)
{
JsonNode * cur_node = *it;
std::string cur_name(cur_node->key);
int cur_id = (int)GetJSONNodeFromKey(*cur_node, "id")->value.toNumber();
glm::mat4 cur_parent_mat = ReadJSONMat4(*cur_node, "restParentMat");
glm::vec4 cur_local_rest_start_pt = ReadJSONVec4_2(*cur_node, "localRestStartPt");
glm::vec4 cur_local_rest_end_pt = ReadJSONVec4_2(*cur_node, "localRestEndPt");
std::vector<int> cur_children_ids = ReadIntArray(*cur_node, "children");
meshBone * new_bone = new meshBone(cur_name,
glm::vec4(0),
glm::vec4(0),
cur_parent_mat);
new_bone->getLocalRestStartPt() = cur_local_rest_start_pt;
new_bone->getLocalRestEndPt() = cur_local_rest_end_pt;
new_bone->calcRestData();
new_bone->setTagId(cur_id);
bone_data[cur_id] = std::make_pair(new_bone, cur_children_ids);
for(auto& cur_child_id : cur_children_ids) {
child_set.insert(cur_child_id);
}
}
// Find root
for(auto& cur_data : bone_data)
{
int cur_id = cur_data.first;
if(child_set.count(cur_id) <= 0) {
// not a child, so is root
root_bone = cur_data.second.first;
break;
}
}
// construct hierarchy
for(auto& cur_data : bone_data)
{
meshBone * cur_bone = cur_data.second.first;
const std::vector<int>& children_ids = cur_data.second.second;
for(auto& cur_child_id : children_ids)
{
meshBone * child_bone = bone_data[cur_child_id].first;
cur_bone->addChild(child_bone);
}
}
return root_bone;
}
static std::vector<meshRenderRegion *> CreateRegions(JsonNode& json_obj,
const std::string& key,
glm::uint32 * indices_in,
glm::float32 * rest_pts_in,
glm::float32 * uvs_in)
{
std::vector<meshRenderRegion *> ret_regions;
JsonNode * base_obj = GetJSONNodeFromKey(json_obj, key);
for (JsonIterator it = JsonBegin(base_obj->value);
it != JsonEnd(base_obj->value);
++it)
{
JsonNode * cur_node = *it;
std::string cur_name(cur_node->key);
int cur_id = (int)GetJSONNodeFromKey(*cur_node, "id")->value.toNumber();
int cur_start_pt_index = (int)GetJSONNodeFromKey(*cur_node, "start_pt_index")->value.toNumber();
int cur_end_pt_index = (int)GetJSONNodeFromKey(*cur_node, "end_pt_index")->value.toNumber();
int cur_start_index = (int)GetJSONNodeFromKey(*cur_node, "start_index")->value.toNumber();
int cur_end_index = (int)GetJSONNodeFromKey(*cur_node, "end_index")->value.toNumber();
meshRenderRegion * new_region = new meshRenderRegion(indices_in,
rest_pts_in,
uvs_in,
cur_start_pt_index,
cur_end_pt_index,
cur_start_index,
cur_end_index);
new_region->setName(cur_name);
new_region->setTagId(cur_id);
// Read in weights
std::unordered_map<std::string, std::vector<float> >& weight_map =
new_region->getWeights();
JsonNode * weight_obj = GetJSONNodeFromKey(*cur_node, "weights");
for (JsonIterator w_it = JsonBegin(weight_obj->value);
w_it != JsonEnd(weight_obj->value);
++w_it)
{
JsonNode * w_node = *w_it;
const std::string& key(w_node->key);
std::vector<float> values = ReadFloatArray(*weight_obj, key);
weight_map[key] = values;
}
ret_regions.push_back(new_region);
}
return ret_regions;
}
static std::pair<int, int> GetStartEndTimes(JsonNode& json_obj,
const std::string& key)
{
std::pair<int, int> ret_times(0,0);
bool first = true;
JsonNode * base_obj = GetJSONNodeFromKey(json_obj, key);
for (JsonIterator it = JsonBegin(base_obj->value);
it != JsonEnd(base_obj->value);
++it)
{
JsonNode * cur_node = *it;
int cur_val = atoi(cur_node->key);
if(first) {
ret_times.first = cur_val;
ret_times.second = cur_val;
first = false;
}
else {
if(cur_val > ret_times.second) {
ret_times.second = cur_val;
}
if(cur_val < ret_times.first) {
ret_times.first = cur_val;
}
}
}
return ret_times;
}
static void FillBoneCache(JsonNode& json_obj,
const std::string& key,
int start_time,
int end_time,
meshBoneCacheManager& cache_manager)
{
JsonNode * base_obj = GetJSONNodeFromKey(json_obj, key);
cache_manager.init(start_time, end_time);
int prev_time = start_time;
for (JsonIterator it = JsonBegin(base_obj->value);
it != JsonEnd(base_obj->value);
++it)
{
JsonNode * cur_node = *it;
int cur_time = atoi(cur_node->key);
std::vector<meshBoneCache> cache_list;
for (JsonIterator bone_it = JsonBegin(cur_node->value);
bone_it != JsonEnd(cur_node->value);
++bone_it)
{
JsonNode * bone_node = *bone_it;
std::string cur_name(bone_node->key);
glm::vec4 cur_start_pt = ReadJSONVec4_2(*bone_node, "start_pt");
glm::vec4 cur_end_pt = ReadJSONVec4_2(*bone_node, "end_pt");
meshBoneCache cache_data(cur_name);
cache_data.setWorldStartPt(cur_start_pt);
cache_data.setWorldEndPt(cur_end_pt);
cache_list.push_back(cache_data);
}
int set_index = cache_manager.getIndexByTime(cur_time);
cache_manager.getCacheTable()[set_index] = cache_list;
auto gap_diff = cur_time - prev_time;
if (gap_diff > 1)
{
auto ptInterp = [&](const glm::vec4& src_pt, const glm::vec4& target_pt, float fraction)
{
return ((1.0f - fraction ) * src_pt) + (fraction * target_pt);
};
// Gap Step
auto prev_index = cache_manager.getIndexByTime(prev_time);
for (int j = 1; j < gap_diff; j++)
{
auto gap_fraction = (float)j / (float)gap_diff;
std::vector<meshBoneCache> gap_cache_list;
gap_cache_list.reserve(cache_list.size());
for (size_t k = 0; k < cache_list.size(); k++)
{
auto& cur_data = cache_manager.getCacheTable()[set_index][k];
auto& prev_data = cache_manager.getCacheTable()[prev_index][k];
meshBoneCache gap_cache_data(cur_data.getKey());
gap_cache_data.setWorldStartPt(
ptInterp(prev_data.getWorldStartPt(), cur_data.getWorldStartPt(), gap_fraction));
gap_cache_data.setWorldEndPt(
ptInterp(prev_data.getWorldEndPt(), cur_data.getWorldEndPt(), gap_fraction));
gap_cache_list.push_back(gap_cache_data);
}
cache_manager.getCacheTable()[prev_index + j] = gap_cache_list;
}
}
prev_time = cur_time;
}
cache_manager.makeAllReady();
}
static void FillDeformationCache(JsonNode& json_obj,
const std::string& key,
int start_time,
int end_time,
meshDisplacementCacheManager& cache_manager)
{
JsonNode * base_obj = GetJSONNodeFromKey(json_obj, key);
cache_manager.init(start_time, end_time);
int prev_time = start_time;
for (JsonIterator it = JsonBegin(base_obj->value);
it != JsonEnd(base_obj->value);
++it)
{
JsonNode * cur_node = *it;
int cur_time = atoi(cur_node->key);
std::vector<meshDisplacementCache> cache_list;
for (JsonIterator mesh_it = JsonBegin(cur_node->value);
mesh_it != JsonEnd(cur_node->value);
++mesh_it)
{
JsonNode * mesh_node = *mesh_it;
std::string cur_name(mesh_node->key);
meshDisplacementCache cache_data(cur_name);
bool use_local_displacement = GetJSONNodeFromKey(*mesh_node, "use_local_displacements")->value.toBool();
bool use_post_displacement = GetJSONNodeFromKey(*mesh_node, "use_post_displacements")->value.toBool();
if(use_local_displacement) {
std::vector<glm::vec2> read_pts = ReadJSONPoints2DVector(*mesh_node, "local_displacements");
cache_data.setLocalDisplacements(read_pts);
}
if(use_post_displacement) {
std::vector<glm::vec2> read_pts = ReadJSONPoints2DVector(*mesh_node, "post_displacements");
cache_data.setPostDisplacements(read_pts);
}
cache_list.push_back(cache_data);
}
int set_index = cache_manager.getIndexByTime(cur_time);
cache_manager.getCacheTable()[set_index] = cache_list;
auto gap_diff = cur_time - prev_time;
if (gap_diff > 1)
{
auto ptsInterp = [&](const std::vector<glm::vec2> src_pts, const std::vector<glm::vec2> target_pts, float fraction)
{
std::vector<glm::vec2> ret_pts(src_pts.size());
for (size_t i = 0; i < src_pts.size(); i++)
{
ret_pts[i] = ((1.0f - fraction) * src_pts[i]) + (fraction * target_pts[i]);
}
return ret_pts;
};
// Gap Step
auto prev_index = cache_manager.getIndexByTime(prev_time);
for (int j = 1; j < gap_diff; j++)
{
auto gap_fraction = (float)j / (float)gap_diff;
std::vector<meshDisplacementCache> gap_cache_list;
gap_cache_list.reserve(cache_list.size());
for (size_t k = 0; k < cache_list.size(); k++)
{
auto& cur_data = cache_manager.getCacheTable()[set_index][k];
auto& prev_data = cache_manager.getCacheTable()[prev_index][k];
meshDisplacementCache gap_cache_data(cur_data.getKey());
if (!cur_data.getLocalDisplacements().empty())
{
gap_cache_data.setLocalDisplacements(
ptsInterp(prev_data.getLocalDisplacements(), cur_data.getLocalDisplacements(), gap_fraction));
}
else {
gap_cache_data.setPostDisplacements(
ptsInterp(prev_data.getPostDisplacements(), cur_data.getPostDisplacements(), gap_fraction));
}
gap_cache_list.push_back(gap_cache_data);
}
cache_manager.getCacheTable()[prev_index + j] = gap_cache_list;
}
}
prev_time = cur_time;
}
cache_manager.makeAllReady();
}
static void FillUVSwapCache(JsonNode& json_obj,
const std::string& key,
int start_time,
int end_time,
meshUVWarpCacheManager& cache_manager)
{
JsonNode * base_obj = GetJSONNodeFromKey(json_obj, key);
cache_manager.init(start_time, end_time);
for (JsonIterator it = JsonBegin(base_obj->value);
it != JsonEnd(base_obj->value);
++it)
{
JsonNode * cur_node = *it;
int cur_time = atoi(cur_node->key);
std::vector<meshUVWarpCache> cache_list;
for (JsonIterator uv_it = JsonBegin(cur_node->value);
uv_it != JsonEnd(cur_node->value);
++uv_it)
{
JsonNode * uv_node = *uv_it;
std::string cur_name(uv_node->key);
meshUVWarpCache cache_data(cur_name);
bool use_uv = GetJSONNodeFromKey(*uv_node, "enabled")->value.toBool();
cache_data.setEnabled(use_uv);
if(use_uv) {
glm::vec2 local_offset = ReadJSONVec2(*uv_node, "local_offset");
glm::vec2 global_offset = ReadJSONVec2(*uv_node, "global_offset");
glm::vec2 scale = ReadJSONVec2(*uv_node, "scale");
cache_data.setUvWarpLocalOffset(local_offset);
cache_data.setUvWarpGlobalOffset(global_offset);
cache_data.setUvWarpScale(scale);
}
cache_list.push_back(cache_data);
}
int set_index = cache_manager.getIndexByTime(cur_time);
cache_manager.getCacheTable()[set_index] = cache_list;
}
cache_manager.makeAllReady();
}
static void FillOpacityCache(JsonNode& json_obj,
const std::string& key,
int start_time,
int end_time,
meshOpacityCacheManager& cache_manager)
{
JsonNode * base_obj = GetJSONNodeFromKey(json_obj, key);
cache_manager.init(start_time, end_time);
if (base_obj == NULL)
{
return;
}
int prev_time = start_time;
for (JsonIterator it = JsonBegin(base_obj->value);
it != JsonEnd(base_obj->value);
++it)
{
JsonNode * cur_node = *it;
int cur_time = atoi(cur_node->key);
std::vector<meshOpacityCache> cache_list;
for (JsonIterator uv_it = JsonBegin(cur_node->value);
uv_it != JsonEnd(cur_node->value);
++uv_it)
{
JsonNode * opacity_node = *uv_it;
std::string cur_name(opacity_node->key);
meshOpacityCache cache_data(cur_name);
float cur_opacity = (float)GetJSONNodeFromKey(*opacity_node, "opacity")->value.toNumber();
cache_data.setOpacity(cur_opacity);
cache_list.push_back(cache_data);
}
int set_index = cache_manager.getIndexByTime(cur_time);
cache_manager.getCacheTable()[set_index] = cache_list;
auto gap_diff = cur_time - prev_time;
if (gap_diff > 1)
{
auto scalarInterp = [&](const float& src_val, const float& target_val, float fraction)
{
return ((1.0f - fraction) * src_val) + (fraction * target_val);
};
// Gap Step
auto prev_index = cache_manager.getIndexByTime(prev_time);
for (int j = 1; j < gap_diff; j++)
{
auto gap_fraction = (float)j / (float)gap_diff;
std::vector<meshOpacityCache> gap_cache_list;
gap_cache_list.reserve(cache_list.size());
for (size_t k = 0; k < cache_list.size(); k++)
{
auto& cur_data = cache_manager.getCacheTable()[set_index][k];
auto& prev_data = cache_manager.getCacheTable()[prev_index][k];
meshOpacityCache gap_cache_data(cur_data.getKey());
gap_cache_data.setOpacity(scalarInterp(prev_data.getOpacity(), cur_data.getOpacity(), gap_fraction));
gap_cache_list.push_back(gap_cache_data);
}
cache_manager.getCacheTable()[prev_index + j] = gap_cache_list;
}
}
prev_time = cur_time;
}
cache_manager.makeAllReady();
}
static std::map<std::string, std::vector<CreatureModule::CreatureUVSwapPacket> >
FillSwapUVPacketMap(JsonNode& json_obj)
{
std::map<std::string, std::vector<CreatureModule::CreatureUVSwapPacket> > ret_map;
for (JsonIterator it = JsonBegin(json_obj.value);
it != JsonEnd(json_obj.value);
++it)
{
JsonNode * cur_node = *it;
std::string cur_name(cur_node->key);
std::vector<CreatureModule::CreatureUVSwapPacket> cur_packets;
for (JsonIterator node_it = JsonBegin(cur_node->value);
node_it != JsonEnd(cur_node->value);
++node_it)
{
JsonNode * packet_node = *node_it;
CreatureModule::CreatureUVSwapPacket new_packet(ReadJSONVec2(*packet_node, "local_offset"),
ReadJSONVec2(*packet_node, "global_offset"),
ReadJSONVec2(*packet_node, "scale"),
(int)GetJSONNodeFromKey(*packet_node, "tag")->value.toNumber());
cur_packets.push_back(new_packet);
}
ret_map[cur_name] = cur_packets;
}
return ret_map;
}
static std::map<std::string, glm::vec2>
FillAnchorPointMap(JsonNode& json_obj)
{
auto anchor_data_node = GetJSONNodeFromKey(json_obj, "AnchorPoints");
std::map<std::string, glm::vec2> ret_map;
for (JsonIterator it = JsonBegin(anchor_data_node->value);
it != JsonEnd(anchor_data_node->value);
++it)
{
JsonNode * cur_node = *it;
glm::vec2 cur_pt = ReadJSONVec2(*cur_node, "point");
std::string cur_name(GetJSONNodeFromKey(*cur_node, "anim_clip_name")->value.toString());
ret_map[cur_name] = cur_pt;
}
return ret_map;
}
namespace CreatureModule {
// Load the json structure
void LoadCreatureJSONData(const std::string& filename_in,
CreatureLoadDataPacket& load_data)
{
std::ifstream read_file;
read_file.open(filename_in.c_str());
std::stringstream str_stream;
str_stream << read_file.rdbuf();
read_file.close();
LoadCreatureJSONDataFromString(str_stream.str(), load_data);
}
void LoadCreatureJSONDataFromString(const std::string& string_in,
CreatureLoadDataPacket& load_data)
{
char *endptr;
size_t source_size = string_in.size();
char *source_chars = new char[source_size+1];
source_chars[source_size]=0;
memcpy(source_chars,string_in.c_str(),source_size);
JsonParseStatus status = jsonParse(source_chars, &endptr, &load_data.base_node, load_data.allocator);
load_data.src_chars = source_chars;
if(status != JSON_PARSE_OK) {
std::cerr<<"LoadCreatureJSONData() - Error parsing JSON!"<<std::endl;
}
}
// Creature class
Creature::Creature(CreatureLoadDataPacket& load_data)
{
anchor_points_active = false;
LoadFromData(load_data);
}
Creature::~Creature()
{
delete global_pts;
delete global_indices;
delete global_uvs;
delete render_colours;
delete render_composition;
delete render_pts;
}
glm::uint32 *
Creature::GetGlobalIndices()
{
return global_indices;
}
glm::float32 *
Creature::GetGlobalPts()
{
return global_pts;
}
glm::float32 *
Creature::GetGlobalUvs()
{
return global_uvs;
}
glm::float32 *
Creature::GetRenderPts()
{
return render_pts;
}
glm::uint8 *
Creature::GetRenderColours()
{
return render_colours;
}
int
Creature::GetTotalNumPoints() const
{
return total_num_pts;
}
int
Creature::GetTotalNumIndices() const
{
return total_num_indices;
}
meshRenderBoneComposition *
Creature::GetRenderComposition()
{
return render_composition;
}
void
Creature::FillRenderColours(glm::uint8 r, glm::uint8 g, glm::uint8 b, glm::uint8 a)
{
for(int i = 0; i < total_num_pts; i++)
{
glm::uint8 * cur_colour = render_colours + (i * 4);
cur_colour[0] = r;
cur_colour[1] = g;
cur_colour[2] = b;
cur_colour[3] = a;
}
}
const std::vector<std::string>&
Creature::GetAnimationNames() const
{
return animation_names;
}
const std::map<std::string, std::vector<CreatureUVSwapPacket> >&
Creature::GetUvSwapPackets() const
{
return uv_swap_packets;
}
void
Creature::SetActiveItemSwap(const std::string& region_name, int swap_idx)
{
active_uv_swap_actions[region_name] = swap_idx;
}
void
Creature::RemoveActiveItemSwap(const std::string& region_name)
{
active_uv_swap_actions.erase(region_name);
}
std::map<std::string, int>&
Creature::GetActiveItemSwaps()
{
return active_uv_swap_actions;
}
void Creature::SetAnchorPointsActive(bool flag_in)
{
anchor_points_active = flag_in;
}
bool Creature::GetAnchorPointsActive() const
{
return anchor_points_active;
}
glm::vec2 Creature::GetAnchorPoint(const std::string & anim_clip_name_in) const
{
if (anchor_point_map.count(anim_clip_name_in) > 0)
{
return anchor_point_map.at(anim_clip_name_in);
}
return glm::vec2(0, 0);
}
void
Creature::LoadFromData(CreatureLoadDataPacket& load_data)
{
JsonNode * json_root = load_data.base_node.toNode();
// Load points and topology
JsonNode * json_mesh = GetJSONLevelNodeFromKey(*json_root, "mesh");
global_pts = ReadJSONPoints3D(*json_mesh, "points", total_num_pts);
global_indices = ReadJSONUints(*json_mesh,"indices", total_num_indices);
global_uvs = ReadJSONPoints2D(*json_mesh, "uvs", total_num_pts);
render_colours = new glm::uint8[total_num_pts * 4];
render_pts = new glm::float32[total_num_pts * 3];
FillRenderColours(255, 255, 255, 255);
// Load bones
meshBone * root_bone = CreateBones(*json_root, "skeleton");
// Load regions
std::vector<meshRenderRegion *> regions = CreateRegions(*json_mesh,
"regions",
global_indices,
global_pts,
global_uvs);
// Add into composition
render_composition = new meshRenderBoneComposition();
render_composition->setRootBone(root_bone);
render_composition->getRootBone()->computeRestParentTransforms();
for(auto& cur_region : regions) {
cur_region->setMainBoneKey(root_bone->getKey());
cur_region->determineMainBone(root_bone);
render_composition->addRegion(cur_region);
}
render_composition->initBoneMap();
render_composition->initRegionsMap();
for(auto& cur_region : regions) {
cur_region->initFastNormalWeightMap(render_composition->getBonesMap());
}
render_composition->resetToWorldRestPts();
// Fill up available animation names
JsonNode * json_anim_base = GetJSONLevelNodeFromKey(*json_root, "animation");
animation_names = GetJSONKeysFromNode(*json_anim_base);
// Fill up uv swap packets
JsonNode * json_uv_swap_base = GetJSONLevelNodeFromKey(*json_root, "uv_swap_items");
if (json_uv_swap_base)
{
uv_swap_packets = FillSwapUVPacketMap(*json_uv_swap_base);
}
// Load Anchor Points
JsonNode * anchor_point_base = GetJSONLevelNodeFromKey(*json_root, "anchor_points_items");
if (anchor_point_base)
{
anchor_point_map = FillAnchorPointMap(*anchor_point_base);
}
}
// CreatureAnimation class
CreatureAnimation::CreatureAnimation(CreatureLoadDataPacket& load_data,
const std::string& name_in)
: name(name_in)
{
LoadFromData(name_in, load_data);
}
CreatureAnimation::~CreatureAnimation()
{
if(!cache_pts.empty())
{