-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.h
1514 lines (1285 loc) · 47.8 KB
/
Main.h
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
#include <iostream>
// #include <GL/glew.h>
#include <caffe/caffe.hpp>
#include "util/FileUtil.h"
#include "util/ArgParser.h"
#include "scenarios/DrawScenarioKinMotion.h"
#include "scenarios/DrawScenarioSimChar.h"
#include "scenarios/ScenarioSimChar.h"
#include "scenarios/DrawScenarioTrackMotion.h"
#include "scenarios/DrawScenarioJump.h"
#include "scenarios/DrawScenarioClimb.h"
#include "scenarios/DrawScenarioVelCtrl.h"
#include "scenarios/DrawScenarioTrain.h"
#include "scenarios/DrawScenarioTrainCacla.h"
#include "scenarios/DrawScenarioTrainCaclaDV.h"
#include "scenarios/DrawScenarioTrainMACE.h"
#include "scenarios/DrawScenarioTrainDPG.h"
#include "scenarios/DrawScenarioTrainMACEDPG.h"
#include "scenarios/DrawScenarioPoliEval.h"
#include "scenarios/DrawScenarioMimicEval.h"
#include "scenarios/DrawScenarioImitateEval.h"
#include "scenarios/DrawScenarioImitateVizEval.h"
#include "scenarios/DrawScenarioImitateTargetEval.h"
#include "scenarios/DrawScenarioHikeEval.h"
#include "scenarios/DrawScenarioHikeGeneralEval.h"
#include "scenarios/DrawScenarioSoccerEval.h"
#include "scenarios/DrawScenarioSoccerGeneralEval.h"
#include "scenarios/DrawScenarioImitateStepEval.h"
#include "scenarios/DrawScenarioTrainDMACE.h"
#include "scenarios/DrawScenarioImitate.h"
#include "scenarios/DrawScenarioImitateTarget.h"
#include "scenarios/DrawScenarioImitateStep.h"
#include "scenarios/DrawScenarioTrainHike.h"
#include "scenarios/DrawScenarioTrainSoccer.h"
#include "scenarios/DrawScenarioMimic.h"
#include "scenarios/DrawScenarioMimicRNN.h"
#include "scenarios/DrawScenarioTerrainViewer.h"
#include "scenarios/DrawScenarioHackMultChar.h"
#include "scenarios/DrawScenarioSpaceMultChar.h"
#include "scenarios/DrawScenarioTestCaseMultChar.h"
#include "scenarios/DrawScenarioMultChar.h"
#include "scenarios/DrawScenarioMultCharRugby.h"
#include "scenarios/DrawScenarioMultCharConcentricCircle.h"
#include "scenarios/DrawScenarioMultiTaskImitateVizEval.h"
#include "scenarios/DrawScenarioImitateEvalMultiTask.h"
// #include "optimizer/scenarios/OptScenarioClimb.h"
// #include "optimizer/scenarios/OptScenarioPoliEval.h"
#include "render/Camera.h"
#include "render/DrawUtil.h"
#include "render/TextureDesc.h"
#include "render/ShadowMap.h"
#include "util/BVHReader.h"
#include "util/MotionDB.h"
#ifndef USE_OpenGLES
#include <GL/glut.h>
#include <GL/freeglut_ext.h>
#endif
//#define USE_OpenGLES
// Dimensions of the window we are drawing into.
int gWinWidth = 800;
int gWinHeight = static_cast<int>(gWinWidth * 9.0 / 16.0);
//int gWinWidth = 720;
//int gWinHeight = 480;
bool gReshaping = false;
const tVector gBKGColor = tVector(0.97, 0.97, 1, 0);
//const tVector gBKGColor = tVector(1, 1, 1, 0);
// camera attributes
double gViewWidth = 4.5;
// double gViewWidth = 10.5;
double gViewHeight = (gViewWidth * gWinHeight) / gWinWidth;
double gViewNearZ = 5;
//double gViewNearZ = 25;
double gViewFarZ = -40;
// intermediate frame buffers
std::unique_ptr<cTextureDesc> gDefaultFrameBuffer;
std::shared_ptr<cTextureDesc> gIntermediateFrameBuffer;
tVector gCameraPosition = tVector(0, 0, 40, 0);
tVector gCameraFocus = tVector(gCameraPosition[0], gCameraPosition[1], 0.0, 0.0);
tVector gCameraUp = tVector(0, 1, 0, 0);
cCamera gCamera;
// anim
const double gFPS = 30.0;
double gAnimStep = 1.0 / gFPS;
const int gDisplayAnimTime = static_cast<int>(gAnimStep * 1000);
bool gAnimate = true;
int gPrevTime = 0;
int gNextTime = 0;
int gDispalyPrevTime = 0;
double gUpdatesPerSec = 0;
double gPlaybackSpeed = 1.0;
const double gPlaybackDelta = 0.05;
bool gForceClear = false;
bool gRenderFilmStrip = false;
bool gDrawInfo = true;
const double gFilmStripPeriod = 0.5;
tVector gPrevCamPos = tVector::Zero();
// arg parser
std::shared_ptr<cArgParser> gArgParser = nullptr;
std::shared_ptr<cDrawScenario> gScenario = nullptr;
int gArgc = 0;
char** gArgv = nullptr;
void SetupCamProjection()
{
gCamera.SetupGLProj();
}
void ResizeCamera()
{
double prev_view_w = gCamera.GetWidth();
double prev_view_h = gCamera.GetHeight();
double new_view_w = (prev_view_h * gWinWidth) / gWinHeight;
double new_view_h = (prev_view_w * gWinHeight) / gWinWidth;
gCamera.Resize(new_view_w, new_view_h);
gViewWidth = gCamera.GetWidth();
gViewHeight = gCamera.GetHeight();
gForceClear = true;
}
void InitCamera()
{
gCamera = cCamera(gCameraPosition, gCameraFocus, gCameraUp,
gViewWidth, gViewHeight, gViewNearZ, gViewFarZ);
gCamera.SetProj(cCamera::eProjOrtho);
ResizeCamera();
}
void ClearScenario()
{
gScenario.reset();
gScenario = nullptr;
}
void SetupScenario()
{
InitCamera();
ClearScenario();
std::string scenario_name = "";
gArgParser->ParseString("scenario", scenario_name);
if (scenario_name == "kin_motion")
{
gScenario = std::shared_ptr<cDrawScenarioKinMotion>(new cDrawScenarioKinMotion(gCamera));
}
else if (scenario_name == "sim_char")
{
gScenario = std::shared_ptr<cDrawScenarioSimChar>(new cDrawScenarioSimChar(gCamera));
}
else if (scenario_name == "track_motion")
{
gScenario = std::shared_ptr<cDrawScenarioTrackMotion>(new cDrawScenarioTrackMotion(gCamera));
}
else if (scenario_name == "jump")
{
gScenario = std::shared_ptr<cDrawScenarioJump>(new cDrawScenarioJump(gCamera));
}
else if (scenario_name == "climb")
{
gScenario = std::shared_ptr<cDrawScenarioClimb>(new cDrawScenarioClimb(gCamera));
}
else if (scenario_name == "vel_ctrl")
{
gScenario = std::shared_ptr<cDrawScenarioVelCtrl>(new cDrawScenarioVelCtrl(gCamera));
}
else if (scenario_name == "train")
{
gScenario = std::shared_ptr<cDrawScenarioTrain>(new cDrawScenarioTrain(gCamera));
}
else if (scenario_name == "train_cacla")
{
gScenario = std::shared_ptr<cDrawScenarioTrainCacla>(new cDrawScenarioTrainCacla(gCamera));
}
else if (scenario_name == "train_cacla_dq")
{
gScenario = std::shared_ptr<cDrawScenarioTrainCacla>(new cDrawScenarioTrainCacla(gCamera));
}
else if (scenario_name == "train_cacla_dv")
{
gScenario = std::shared_ptr<cDrawScenarioTrainCaclaDV>(new cDrawScenarioTrainCaclaDV(gCamera));
}
else if (scenario_name == "train_mace")
{
gScenario = std::shared_ptr<cDrawScenarioTrainMACE>(new cDrawScenarioTrainMACE(gCamera));
}
else if (scenario_name == "train_dpg")
{
gScenario = std::shared_ptr<cDrawScenarioTrainDPG>(new cDrawScenarioTrainDPG(gCamera));
}
else if (scenario_name == "train_mace_dpg")
{
gScenario = std::shared_ptr<cDrawScenarioTrainMACEDPG>(new cDrawScenarioTrainMACEDPG(gCamera));
}
else if (scenario_name == "train_dmace")
{
gScenario = std::shared_ptr<cDrawScenarioTrainDMACE>(new cDrawScenarioTrainDMACE(gCamera));
}
else if (scenario_name == "mimic")
{
gScenario = std::shared_ptr<cDrawScenarioMimic>(new cDrawScenarioMimic(gCamera));
}
else if (scenario_name == "mimic_rnn")
{
gScenario = std::shared_ptr<cDrawScenarioMimicRNN>(new cDrawScenarioMimicRNN(gCamera));
}
else if (scenario_name == "imitate")
{
gScenario = std::shared_ptr<cDrawScenarioImitate>(new cDrawScenarioImitate(gCamera));
}
else if (scenario_name == "imitate_target")
{
gScenario = std::shared_ptr<cDrawScenarioImitateTarget>(new cDrawScenarioImitateTarget(gCamera));
}
else if (scenario_name == "imitate_step")
{
gScenario = std::shared_ptr<cDrawScenarioImitateStep>(new cDrawScenarioImitateStep(gCamera));
}
else if (scenario_name == "train_hike")
{
gScenario = std::shared_ptr<cDrawScenarioTrainHike>(new cDrawScenarioTrainHike(gCamera));
}
else if (scenario_name == "train_soccer")
{
gScenario = std::shared_ptr<cDrawScenarioTrainSoccer>(new cDrawScenarioTrainSoccer(gCamera));
}
else if (scenario_name == "poli_eval")
{
gScenario = std::shared_ptr<cDrawScenarioPoliEval>(new cDrawScenarioPoliEval(gCamera));
}
else if (scenario_name == "mimic_eval")
{
gScenario = std::shared_ptr<cDrawScenarioMimicEval>(new cDrawScenarioMimicEval(gCamera));
}
else if (scenario_name == "imitate_eval")
{
gScenario = std::shared_ptr<cDrawScenarioImitateEval>(new cDrawScenarioImitateEval(gCamera));
}
else if (scenario_name == "imitate_eval_multitask")
{
std::cout << "Starting multi task terrain sim" << std::endl;
gScenario = std::shared_ptr<cDrawScenarioImitateEvalMultiTask>(new cDrawScenarioImitateEvalMultiTask(gCamera));
}
else if (scenario_name == "imitate_viz_eval")
{
gScenario = std::shared_ptr<cDrawScenarioImitateVizEval>(new cDrawScenarioImitateVizEval(gCamera));
}
else if (scenario_name == "multitask_imitate_viz")
{
gScenario = std::shared_ptr<cDrawScenarioMultiTaskImitateVizEval>(new cDrawScenarioMultiTaskImitateVizEval(gCamera));
}
else if (scenario_name == "imitate_target_eval")
{
gScenario = std::shared_ptr<cDrawScenarioImitateTargetEval>(new cDrawScenarioImitateTargetEval(gCamera));
}
else if (scenario_name == "imitate_step_eval")
{
gScenario = std::shared_ptr<cDrawScenarioImitateStepEval>(new cDrawScenarioImitateStepEval(gCamera));
}
else if (scenario_name == "hike_eval")
{
gScenario = std::shared_ptr<cDrawScenarioHikeEval>(new cDrawScenarioHikeEval(gCamera));
}
else if (scenario_name == "soccer_eval")
{
gScenario = std::shared_ptr<cDrawScenarioSoccerEval>(new cDrawScenarioSoccerEval(gCamera));
}
else if (scenario_name == "soccer_eval_general")
{
gScenario = std::shared_ptr<cDrawScenarioSoccerGeneralEval>(new cDrawScenarioSoccerGeneralEval(gCamera));
}
else if (scenario_name == "hike_eval_general")
{
gScenario = std::shared_ptr<cDrawScenarioHikeGeneralEval>(new cDrawScenarioHikeGeneralEval(gCamera));
}
else if (scenario_name == "terrain_viewer")
{
gScenario = std::shared_ptr<cDrawScenarioTerrainViewer>(new cDrawScenarioTerrainViewer(gCamera));
}
else if (scenario_name == "hack_mult_char")
{
gScenario = std::shared_ptr<cDrawScenarioHackMultChar>(new cDrawScenarioHackMultChar(gCamera));
}
else if (scenario_name == "multi_char")
{
gScenario = std::shared_ptr<cDrawScenarioMultChar>(new cDrawScenarioMultChar(gCamera));
}
else if (scenario_name == "multi_char_rugby")
{
gScenario = std::shared_ptr<cDrawScenarioMultCharRugby>(new cDrawScenarioMultCharRugby(gCamera));
}
else if (scenario_name == "multi_char_circle")
{
gScenario = std::shared_ptr<cDrawScenarioMultCharConcentricCircle>(new cDrawScenarioMultCharConcentricCircle(gCamera));
}
else if (scenario_name == "space_mult_char")
{
gScenario = std::shared_ptr<cDrawScenarioSpaceMultChar>(new cDrawScenarioSpaceMultChar(gCamera));
}
else if (scenario_name == "testcase_mult_char")
{
gScenario = std::shared_ptr<cDrawScenarioTestCaseMultChar>(new cDrawScenarioTestCaseMultChar(gCamera));
}
else if (scenario_name == "poli_eval")
{
// gScenario = std::shared_ptr<cScenarioSimChar>(new cScenarioSimChar());
/*
auto scene = std::shared_ptr<cOptScenarioPoliEval>(new cOptScenarioPoliEval());
scene->SetTimeStep(cOptScenario::gTimeStep);
scene->SetPoolSize(gNumThreads);
gScenario = scene;
*/
}
if (gScenario != NULL)
{
auto sim_char_scene = std::dynamic_pointer_cast<cDrawScenarioTerrainRL>(gScenario);
if (sim_char_scene != nullptr)
{
sim_char_scene->SetOutputTex(gIntermediateFrameBuffer);
}
gScenario->ParseArgs(gArgParser);
gScenario->Init();
printf("Loaded scenario: %s\n", gScenario->GetName().c_str());
}
}
void UpdateScenario(double time_step)
{
if (gScenario != NULL)
{
int num_steps = 1;
if (gRenderFilmStrip)
{
num_steps = static_cast<int>(gFilmStripPeriod / time_step);
}
for (int i = 0; i < num_steps; ++i)
{
gScenario->Update(time_step);
}
}
}
void DrawInfo()
{
if (!gRenderFilmStrip && gDrawInfo)
{
cDrawUtil::MatrixMode(Util::PROJECTION_MAT);
cDrawUtil::PushMatrix();
cDrawUtil::LoadIdentity();
cDrawUtil::MatrixMode(Util::MODELVIEW_MAT);
cDrawUtil::PushMatrix();
cDrawUtil::LoadIdentity();
const double aspect = gCamera.GetAspectRatio();
const double text_size = 0.09;
const tVector scale = tVector(text_size / aspect, text_size, 1, 0);
const double line_offset = text_size;
cDrawUtil::SetLineWidth(1.5);
cDrawUtil::SetColor(tVector(0, 0, 0, 1.0));
cDrawUtil::Translate(tVector(-0.96, 0.88, -1, 0));
double curr_fps = gUpdatesPerSec;
char buffer[128];
sprintf(buffer, "FPS: %.2f (%.2fx)\n", curr_fps, gPlaybackSpeed);
std::string text_info = std::string(buffer);
if (gScenario != nullptr)
{
text_info += gScenario->BuildTextInfoStr();
}
cDrawUtil::DrawString(text_info.c_str(), scale);
cDrawUtil::MatrixMode(Util::PROJECTION_MAT);
cDrawUtil::PopMatrix();
cDrawUtil::MatrixMode(Util::MODELVIEW_MAT);
cDrawUtil::PopMatrix();
}
}
void ClearFrame()
{
bool clear = true;
if (gRenderFilmStrip && !gForceClear)
{
const tVector& cam_pos = gCamera.GetPosition();
if (cam_pos != gPrevCamPos)
{
gPrevCamPos = cam_pos;
}
else
{
clear = false;
}
}
if (clear)
{
cDrawUtil::ClearColor(gBKGColor);
cDrawUtil::ClearDepth(1);
}
gForceClear = false;
}
void DrawScene()
{
if (gScenario != NULL)
{
gScenario->Draw();
}
}
void CopyFrame()
{
cDrawUtil::CopyTexture(*gIntermediateFrameBuffer, *gDefaultFrameBuffer);
}
void UpdateIntermediateBuffer()
{
if (!gReshaping)
{
if (gWinWidth != gIntermediateFrameBuffer->GetWidth()
|| gWinHeight != gIntermediateFrameBuffer->GetHeight())
{
gIntermediateFrameBuffer->Reshape(gWinWidth, gWinHeight);
gScenario->Reshape(gWinWidth, gWinHeight);
}
}
}
void Display_(void)
{
UpdateIntermediateBuffer();
cDrawUtil::MatrixMode(Util::PROJECTION_MAT);
SetupCamProjection();
cDrawUtil::MatrixMode(Util::MODELVIEW_MAT);
cDrawUtil::LoadIdentity();
gIntermediateFrameBuffer->BindBuffer();
ClearFrame();
DrawScene();
DrawInfo();
gIntermediateFrameBuffer->UnbindBuffer();
CopyFrame();
#ifndef NO_GLUT
glutSwapBuffers();
checkError();
gDispalyPrevTime = glutGet(GLUT_ELAPSED_TIME);
#endif
gReshaping = false;
}
void Reshape(int w, int h)
{
gReshaping = true;
gWinWidth = w;
gWinHeight = h;
glViewport(0, 0, gWinWidth, gWinHeight);
checkError();
gDefaultFrameBuffer->Reshape(w, h);
UpdateScenario(0);
ResizeCamera();
cDrawUtil::MatrixMode(Util::PROJECTION_MAT);
SetupCamProjection();
#ifndef NO_GLUT
glutPostRedisplay();
checkError();
#endif
}
void StepAnim(double time_step)
{
UpdateScenario(time_step);
gAnimate = false;
#ifndef NO_GLUT
glutPostRedisplay();
checkError();
#endif
}
void ParseArgs(int argc, char** argv)
{
gArgParser = std::shared_ptr<cArgParser>(new cArgParser(argv, argc));
std::string arg_file = "";
gArgParser->ParseString("arg_file", arg_file);
if (arg_file != "")
{
// append the args from the file to the ones from the commandline
// this allows the cmd args to overwrite the file args
gArgParser->AppendArgs(arg_file);
}
}
void InitTime()
{
#ifndef NO_GLUT
gPrevTime = glutGet(GLUT_ELAPSED_TIME);
checkError();
#else
gPrevTime = 0;
#endif
gNextTime = gPrevTime;
gDispalyPrevTime = gPrevTime;
gUpdatesPerSec = 0.f;
}
void Reload()
{
ParseArgs(gArgc, gArgv);
SetupScenario();
InitTime();
gForceClear = true;
}
void Reset()
{
if (gScenario != NULL)
{
gScenario->Reset();
}
gForceClear = true;
}
void Update(double time_elapsed)
{
UpdateScenario(time_elapsed);
}
int GetNumTimeSteps()
{
int num_steps = static_cast<int>(gPlaybackSpeed);
if (num_steps == 0)
{
num_steps = 1;
}
num_steps = std::abs(num_steps);
return num_steps;
}
int CalcDisplayAnimTime()
{
int anim_time = static_cast<int>(gDisplayAnimTime * GetNumTimeSteps() / gPlaybackSpeed);
anim_time = std::abs(anim_time);
return anim_time;
}
void Shutdown()
{
if (gScenario != nullptr)
{
gScenario->Shutdown();
/// TODO: Find a way to properly close GLUT.
// glutLeaveMainLoop();
#ifndef NO_GLUT
glutExit();
checkError();
#endif
}
// exit(0);
}
void Animate(int callback_val)
{
// std::cout << "Animating " << std::endl;
if (gAnimate)
{
int num_steps = GetNumTimeSteps();
#ifndef NO_GLUT
int current_time = glutGet(GLUT_ELAPSED_TIME);
int elapsedTime = current_time - gPrevTime;
gPrevTime = current_time;
double timestep = (gPlaybackSpeed < 0) ? -gAnimStep : gAnimStep;
for (int i = 0; i < num_steps; ++i)
{
Update(timestep);
}
int update_dur = glutGet(GLUT_ELAPSED_TIME) - current_time;
glutPostRedisplay();
gUpdatesPerSec = num_steps / (elapsedTime * 0.001);
int timer_step = CalcDisplayAnimTime();
timer_step -= update_dur;
timer_step = std::max(timer_step, 0);
glutTimerFunc(timer_step, Animate, 0);
#endif
}
if (gScenario != nullptr)
{
if (gScenario->IsDone())
{
Shutdown();
}
}
}
void ToggleAnimate()
{
gAnimate = !gAnimate;
if (gAnimate)
{
#ifndef NO_GLUT
glutTimerFunc(gDisplayAnimTime, Animate, 0);
#endif
}
}
void ChangePlaybackSpeed(double delta)
{
double prev_playback = gPlaybackSpeed;
gPlaybackSpeed += delta;
if (std::abs(prev_playback) < 0.0001 && std::abs(gPlaybackSpeed) > 0.0001)
{
#ifndef NO_GLUT
glutTimerFunc(gDisplayAnimTime, Animate, 0);
#endif
}
}
void ToggleDrawInfo()
{
gDrawInfo = !gDrawInfo;
/*
if (gDrawInfo)
{
printf("Draw info enabled\n");
}
else
{
printf("Draw info disabled\n");
}
*/
}
void ToggleFilmStrip()
{
gRenderFilmStrip = !gRenderFilmStrip;
gForceClear = true;
if (gScenario != NULL)
{
gScenario->EnableFilmstrip(gRenderFilmStrip);
}
if (gRenderFilmStrip)
{
printf("Filmstrip mode enabled\n");
}
else
{
printf("Filmstrip mode disabled\n");
}
}
void Keyboard(unsigned char key, int x, int y) {
if (gScenario != NULL)
{
gScenario->Keyboard(key, x, y);
}
bool update = false;
switch (key) {
// Quit.
#ifndef _LINUX_
// case CTRL_CLOSE_EVENT:
// case CTRL_C_EVENT:
#endif
case 27: // escape
Shutdown();
return;
case 13: // enter
break;
case ' ':
ToggleAnimate();
break;
case '>':
StepAnim(gAnimStep);
break;
case '<':
StepAnim(-gAnimStep);
break;
case ',':
ChangePlaybackSpeed(-gPlaybackDelta);
break;
case '.':
ChangePlaybackSpeed(gPlaybackDelta);
break;
case '/':
ChangePlaybackSpeed(-gPlaybackSpeed + 1);
break;
case 'l':
Reload();
break;
case 'j':
ToggleDrawInfo();
break;
case 'q':
ToggleFilmStrip();
break;
case 'r':
Reset();
break;
default:
break;
}
#ifndef NO_GLUT
// glutPostRedisplay();
#endif
}
void MouseClick(int button, int state, int x, int y)
{
double screen_x = static_cast<double>(x) / gWinWidth;
double screen_y = static_cast<double>(y) / gWinHeight;
screen_x = (screen_x - 0.5f) * 2.f;
screen_y = (screen_y - 0.5f) * -2.f;
if (gScenario != NULL)
{
gScenario->MouseClick(button, state, screen_x, screen_y);
}
#ifndef NO_GLUT
glutPostRedisplay();
#endif
}
void MouseMove(int x, int y)
{
double screen_x = static_cast<double>(x) / gWinWidth;
double screen_y = static_cast<double>(y) / gWinHeight;
screen_x = (screen_x - 0.5f) * 2.f;
screen_y = (screen_y - 0.5f) * -2.f;
if (gScenario != NULL)
{
gScenario->MouseMove(screen_x, screen_y);
}
#ifndef NO_GLUT
glutPostRedisplay();
#endif
}
void InitOpenGl(void)
{
#ifndef USE_OpenGLES
glewInit();
#endif
std::string fpath;
bool succ_ = gArgParser->ParseString("relative_file_path", fpath);
if (succ_)
{
std::cout << "Setting DrawUtil relative shader path: " << fpath << std::endl;
cDrawUtil::mRelativePath = fpath;
}
cDrawUtil::InitDrawUtil();
gDefaultFrameBuffer = std::unique_ptr<cTextureDesc>(new cTextureDesc(0, 0, 0, gWinWidth, gWinHeight, 1, GL_RGBA, GL_RGBA));
gIntermediateFrameBuffer = std::shared_ptr<cTextureDesc>(new cTextureDesc(gWinWidth, gWinHeight, GL_RGBA, GL_RGBA, GL_UNSIGNED_BYTE, false));
}
void HackDogMeasurements()
{
double scale = 0.1136;
tVector origin = tVector(12.73, 19.85, 0, 0);
tMatrix m = cMathUtil::ScaleMat(scale) * cMathUtil::TranslateMat(-origin);
m = cMathUtil::ScaleMat(tVector(tVector(-1, -1, 1, 0))) * m;
tVector root = m * tVector(11.07, 9.15, 0, 1);
tVector root_spine0 = m * tVector(11.68, 9.22, 0, 1);
tVector spine0_spine1 = m * tVector(11.48, 8.76, 0, 1);
tVector spine1_spine2 = m * tVector(11.08, 8.25, 0, 1);
tVector spine2_spine3 = m * tVector(10.67, 7.71, 0, 1);
tVector spine3_torso = m * tVector(10.05, 6.88, 0, 1);
tVector torso_neck0 = m * tVector(8.94, 5.39, 0, 1);
tVector neck0_neck1 = m * tVector(9.13, 4.6, 0, 1);
tVector neck1_head = m * tVector(9.26, 3.85, 0, 1);
tVector head_end = m * tVector(7.91, 3.4, 0, 1);
tVector root_tail0 = m * tVector(72.89, 13.09, 0, 1);
tVector tail0_tail1 = m * tVector(71.34, 15.15, 0, 1);
tVector tail1_tail2 = m * tVector(69.79, 17.26, 0, 1);
tVector tail2_tail3 = m * tVector(67.55, 18.99, 0, 1);
tVector tail3_end = m * tVector(65.39, 19.42, 0, 1);
tVector torso_upperarm = m * tVector(8.81, 6.17, 0, 1);
tVector upper_arm_lower_arm = m * tVector(8.98, 8.14, 0, 1);
tVector lower_arm_hand = m * tVector(8.04, 9.77, 0, 1);
tVector lower_hand_finger = m * tVector(7.42, 10.5, 0, 1);
tVector finger_end = m * tVector(6.86, 10.51, 0, 1);
tVector root_thigh = m * tVector(11.54, 10.16, 0, 1);
tVector thigh_leg = m * tVector(10.03, 8.91, 0, 1);
tVector leg_foot = m * tVector(9.14, 10.77, 0, 1);
tVector foot_toe = m * tVector(8, 10.57, 0, 1);
tVector toe_end = m * tVector(7.43, 10.57, 0, 1);
/*
double scale = 0.6;
tVector root = tVector(0, 1.61, 0, 1) * scale;
tVector root_spine0 = tVector(0.09, 1.69, 0, 1) * scale;
tVector spine0_spine1 = tVector(0.19, 1.71, 0, 1) * scale;
tVector spine1_spine2 = tVector(0.31, 1.69, 0, 1) * scale;
tVector spine2_spine3 = tVector(0.44, 1.69, 0, 1) * scale;
tVector spine3_torso = tVector(0.62, 1.68, 0, 1) * scale;
tVector torso_neck0 = tVector(0.98, 1.71, 0, 1) * scale;
tVector neck0_neck1 = tVector(1.07, 1.82, 0, 1) * scale;
tVector neck1_head = tVector(1.16, 1.93, 0, 1) * scale;
tVector head_end = tVector(1.36, 1.43, 0, 1) * scale;
tVector root_tail0 = tVector(-0.14, 1.6, 0, 1) * scale;
tVector tail0_tail1 = tVector(-0.28, 1.6, 0, 1) * scale;
tVector tail1_tail2 = tVector(-0.43, 1.6, 0, 1) * scale;
tVector tail2_tail3 = tVector(-0.57, 1.6, 0, 1) * scale;
tVector tail3_end = tVector(-0.71, 1.6, 0, 1) * scale;
tVector torso_upperarm = tVector(0.88, 1.6, 0, 1) * scale;
tVector upper_arm_lower_arm = tVector(0.69, 1.27, 0, 1) * scale;
tVector lower_arm_hand = tVector(0.81, 0.94, 0, 1) * scale;
tVector lower_hand_finger = tVector(0.89, 0.78, 0, 1) * scale;
tVector finger_end = tVector(0.99, 0.78, 0, 1) * scale;
tVector root_thigh = tVector(-0.05, 1.58, 0, 1) * scale;
tVector thigh_leg = tVector(0.17, 1.27, 0, 1) * scale;
tVector leg_foot = tVector(-0.08, 0.97, 0, 1) * scale;
tVector foot_toe = tVector(0.05, 0.79, 0, 1) * scale;
tVector toe_end = tVector(0.15, 0.79, 0, 1) * scale;
*/
tVector root_spine0_bone = (root_spine0 - root);
tVector spine0 = (spine0_spine1 - root_spine0);
tVector spine1 = (spine1_spine2 - spine0_spine1);
tVector spine2 = (spine2_spine3 - spine1_spine2);
tVector spine3 = (spine3_torso - spine2_spine3);
tVector torso = (torso_neck0 - spine3_torso);
tVector neck0 = (neck0_neck1 - torso_neck0);
tVector neck1 = (neck1_head - neck0_neck1);
tVector head = (head_end - neck1_head);
tVector root_tail0_bone = (root_tail0 - root);
tVector tail0 = (tail0_tail1 - root_tail0);
tVector tail1 = (tail1_tail2 - tail0_tail1);
tVector tail2 = (tail2_tail3 - tail1_tail2);
tVector tail3 = (tail3_end - tail2_tail3);
tVector torso_upperarm_bone = (torso_upperarm - spine3_torso);
tVector forearm = (upper_arm_lower_arm - torso_upperarm);
tVector arm = (lower_arm_hand - upper_arm_lower_arm);
tVector hand = (lower_hand_finger - lower_arm_hand);
tVector finger = (finger_end - lower_hand_finger);
tVector root_thigh_bone = (root_thigh - root);
tVector thigh = (thigh_leg - root_thigh);
tVector leg = (leg_foot - thigh_leg);
tVector foot = (foot_toe - leg_foot);
tVector toe = (toe_end - foot_toe);
double root_spine0_len = root_spine0_bone.segment(0, 3).norm();
double spine0_len = spine0.segment(0, 3).norm();
double spine1_len = spine1.segment(0, 3).norm();
double spine2_len = spine2.segment(0, 3).norm();
double spine3_len = spine3.segment(0, 3).norm();
double torso_len = torso.segment(0, 3).norm();
double neck0_len = neck0.segment(0, 3).norm();
double neck1_len = neck1.segment(0, 3).norm();
double head_len = head.segment(0, 3).norm();
double root_tail0_len = root_tail0_bone.segment(0, 3).norm();
double tail0_len = tail0.segment(0, 3).norm();
double tail1_len = tail1.segment(0, 3).norm();
double tail2_len = tail2.segment(0, 3).norm();
double tail3_len = tail3.segment(0, 3).norm();
double torso_upperarm_len = torso_upperarm_bone.segment(0, 3).norm();
double forearm_len = forearm.segment(0, 3).norm();
double arm_len = arm.segment(0, 3).norm();
double hand_len = hand.segment(0, 3).norm();
double finger_len = finger.segment(0, 3).norm();
double root_thigh_len = root_thigh_bone.segment(0, 3).norm();
double thigh_len = thigh.segment(0, 3).norm();
double leg_len = leg.segment(0, 3).norm();
double foot_len = foot.segment(0, 3).norm();
double toe_len = toe.segment(0, 3).norm();
double root_x = root[0];
double root_y = root[1];
double root_theta = std::atan2(root_spine0_bone[1], root_spine0_bone[0]) - 0.72664234068172606;
double spine0_theta = std::atan2(spine0[1], spine0[0]);
double spine1_theta = std::atan2(spine1[1], spine1[0]);
double spine2_theta = std::atan2(spine2[1], spine2[0]);
double spin3_theta = std::atan2(spine3[1], spine3[0]);
double torso_theta = std::atan2(torso[1], torso[0]) - 0.083141231888441317;
double neck0_theta = std::atan2(neck0[1], neck0[0]);
double neck1_theta = std::atan2(neck1[1], neck1[0]);
double head_theta = std::atan2(head[1], head[0]) + M_PI * 0.5;
double tail0_theta = std::atan2(tail0[1], tail0[0]) - M_PI;
double tail1_theta = std::atan2(tail1[1], tail1[0]) - M_PI;
double tail2_theta = std::atan2(tail2[1], tail2[0]) - M_PI;
double tail3_theta = std::atan2(tail3[1], tail3[0]) - M_PI;
double forearm_theta = std::atan2(forearm[1], forearm[0]) + M_PI * 0.5;
double arm_theta = std::atan2(arm[1], arm[0]) + M_PI * 0.5;
double hand_theta = std::atan2(hand[1], hand[0]) + M_PI * 0.5;
double finger_theta = std::atan2(finger[1], finger[0]);
double thigh_theta = std::atan2(thigh[1], thigh[0]) + M_PI * 0.5;
double leg_theta = std::atan2(leg[1], leg[0]) + M_PI * 0.5;
double foot_theta = std::atan2(foot[1], foot[0]) + M_PI * 0.5;
double toe_theta = std::atan2(toe[1], toe[0]);
double spine0_theta_rel = spine0_theta - root_theta;
double spine1_theta_rel = spine1_theta - spine0_theta;
double spine2_theta_rel = spine2_theta - spine1_theta;
double spin3_theta_rel = spin3_theta - spine2_theta;
double torso_theta_rel = torso_theta - spin3_theta;
double neck0_theta_rel = neck0_theta - torso_theta;
double neck1_theta_rel = neck1_theta - neck0_theta;
double head_theta_rel = head_theta - neck1_theta;
double tail0_theta_rel = tail0_theta - root_theta;
double tail1_theta_rel = tail1_theta - tail0_theta;
double tail2_theta_rel = tail2_theta - tail1_theta;
double tail3_theta_rel = tail3_theta - tail2_theta;
double forearm_theta_rel = forearm_theta - torso_theta;
double arm_theta_rel = arm_theta - forearm_theta;
double hand_theta_rel = hand_theta - arm_theta;
double finger_theta_rel = finger_theta - hand_theta;
double thigh_theta_rel = thigh_theta - root_theta;
double leg_theta_rel = leg_theta - thigh_theta;
double foot_theta_rel = foot_theta - leg_theta;
double toe_theta_rel = toe_theta - foot_theta;
spine0_theta_rel = (spine0_theta_rel > M_PI) ? -(2 * M_PI - spine0_theta_rel) : spine0_theta_rel;
spine1_theta_rel = (spine1_theta_rel > M_PI) ? -(2 * M_PI - spine1_theta_rel) : spine1_theta_rel;
spine2_theta_rel = (spine2_theta_rel > M_PI) ? -(2 * M_PI - spine2_theta_rel) : spine2_theta_rel;
spin3_theta_rel = (spin3_theta_rel > M_PI) ? -(2 * M_PI - spin3_theta_rel) : spin3_theta_rel;
torso_theta_rel = (torso_theta_rel > M_PI) ? -(2 * M_PI - torso_theta_rel) : torso_theta_rel;
neck0_theta_rel = (neck0_theta_rel > M_PI) ? -(2 * M_PI - neck0_theta_rel) : neck0_theta_rel;
neck1_theta_rel = (neck1_theta_rel > M_PI) ? -(2 * M_PI - neck1_theta_rel) : neck1_theta_rel;
head_theta_rel = (head_theta_rel > M_PI) ? -(2 * M_PI - head_theta_rel) : head_theta_rel;
tail0_theta_rel = (tail0_theta_rel > M_PI) ? -(2 * M_PI - tail0_theta_rel) : tail0_theta_rel;
tail1_theta_rel = (tail1_theta_rel > M_PI) ? -(2 * M_PI - tail1_theta_rel) : tail1_theta_rel;
tail2_theta_rel = (tail2_theta_rel > M_PI) ? -(2 * M_PI - tail2_theta_rel) : tail2_theta_rel;
tail3_theta_rel = (tail3_theta_rel > M_PI) ? -(2 * M_PI - tail3_theta_rel) : tail3_theta_rel;