-
Notifications
You must be signed in to change notification settings - Fork 129
/
Main.cpp
569 lines (475 loc) · 11 KB
/
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
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
#include <iostream>
#include <caffe/caffe.hpp>
#include "util/FileUtil.h"
#include "util/ArgParser.h"
#include "scenarios/DrawScenarioSimChar.h"
#include "scenarios/DrawScenarioExp.h"
#include "scenarios/DrawScenarioExpCacla.h"
#include "scenarios/DrawScenarioExpMACE.h"
#include "scenarios/DrawScenarioTrain.h"
#include "scenarios/DrawScenarioTrainCacla.h"
#include "scenarios/DrawScenarioTrainMACE.h"
#include "scenarios/DrawScenarioPoliEval.h"
#include "render/Camera.h"
#include "render/DrawUtil.h"
#include "render/TextureDesc.h"
// Dimensions of the window we are drawing into.
int gWinWidth = 800;
int gWinHeight = static_cast<int>(gWinWidth * 9.0 / 16.0);
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 = 6.5;
double gViewHeight = (gViewWidth * gWinHeight) / gWinWidth;
double gViewNearZ = 2;
double gViewFarZ = 50;
// intermediate frame buffers
std::unique_ptr<cTextureDesc> gDefaultFrameBuffer;
std::shared_ptr<cTextureDesc> gIntermediateFrameBuffer;
tVector gCameraPosition = tVector(0, 0, 30, 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;
const 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;
const double gFilmStripPeriod = 0.5;
tVector gPrevCamPos = tVector::Zero();
// arg parser
cArgParser gArgParser;
std::shared_ptr<cDrawScenario> gScenario = NULL;
int gArgc = 0;
char** gArgv = NULL;
void SetupCamProjection()
{
gCamera.SetupGLProj();
}
void ResizeCamera()
{
gViewWidth = (gViewHeight * gWinWidth) / gWinHeight;
gCamera.Resize(gViewWidth, gViewHeight);
gForceClear = true;
}
void InitCamera()
{
gCamera = cCamera(gCameraPosition, gCameraFocus, gCameraUp,
gViewWidth, gViewHeight, gViewNearZ, gViewFarZ);
gCamera.SetProj(cCamera::eProjOrtho);
ResizeCamera();
}
void ClearScenario()
{
gScenario = NULL;
}
void SetupScenario()
{
InitCamera();
ClearScenario();
std::string scenario_name = "";
gArgParser.ParseString("scenario", scenario_name);
if (scenario_name == "sim_char")
{
gScenario = std::shared_ptr<cDrawScenarioSimChar>(new cDrawScenarioSimChar(gCamera));
}
else if (scenario_name == "exp")
{
gScenario = std::shared_ptr<cDrawScenarioExp>(new cDrawScenarioExp(gCamera));
}
else if (scenario_name == "exp_cacla")
{
gScenario = std::shared_ptr<cDrawScenarioExpCacla>(new cDrawScenarioExpCacla(gCamera));
}
else if (scenario_name == "exp_mace")
{
gScenario = std::shared_ptr<cDrawScenarioExpMACE>(new cDrawScenarioExpMACE(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_mace")
{
gScenario = std::shared_ptr<cDrawScenarioTrainMACE>(new cDrawScenarioTrainMACE(gCamera));
}
else if (scenario_name == "poli_eval")
{
gScenario = std::shared_ptr<cDrawScenarioPoliEval>(new cDrawScenarioPoliEval(gCamera));
}
if (gScenario != NULL)
{
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)
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
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, 0.5));
cDrawUtil::Translate(tVector(-0.96, 0.88, -1, 0));
double curr_fps = gUpdatesPerSec;
char buffer[128];
sprintf(buffer, "FPS: %.2f\nPlayback Speed: %.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);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
}
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();
glMatrixMode(GL_PROJECTION);
SetupCamProjection();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gIntermediateFrameBuffer->BindBuffer();
ClearFrame();
DrawScene();
DrawInfo();
gIntermediateFrameBuffer->UnbindBuffer();
CopyFrame();
glutSwapBuffers();
gDispalyPrevTime = glutGet(GLUT_ELAPSED_TIME);
gReshaping = false;
}
void Reshape(int w, int h)
{
gReshaping = true;
gWinWidth = w;
gWinHeight = h;
glViewport(0, 0, gWinWidth, gWinHeight);
gDefaultFrameBuffer->Reshape(w, h);
UpdateScenario(0);
ResizeCamera();
glMatrixMode(GL_PROJECTION);
SetupCamProjection();
glutPostRedisplay();
}
void StepAnim(double time_step)
{
UpdateScenario(time_step);
gAnimate = false;
glutPostRedisplay();
}
void ParseArgs(int argc, char** argv)
{
gArgParser = 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()
{
gPrevTime = glutGet(GLUT_ELAPSED_TIME);
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();
}
exit(0);
}
void Animate(int callback_val)
{
if (gAnimate)
{
int num_steps = GetNumTimeSteps();
int timer_step = CalcDisplayAnimTime();
glutTimerFunc(timer_step, Animate, 0);
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);
}
glutPostRedisplay();
gUpdatesPerSec = num_steps / (elapsedTime * 0.001);
}
if (gScenario != nullptr)
{
if (gScenario->IsDone())
{
Shutdown();
}
}
}
void ToggleAnimate()
{
gAnimate = !gAnimate;
if (gAnimate)
{
glutTimerFunc(gDisplayAnimTime, Animate, 0);
}
}
void ChangePlaybackSpeed(double delta)
{
double prev_playback = gPlaybackSpeed;
gPlaybackSpeed += delta;
if (std::abs(prev_playback) < 0.0001 && std::abs(gPlaybackSpeed) > 0.0001)
{
glutTimerFunc(gDisplayAnimTime, Animate, 0);
}
}
void ToogleFilmStrip()
{
gRenderFilmStrip = !gRenderFilmStrip;
gForceClear = true;
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();
break;
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 'q':
ToogleFilmStrip();
break;
case 'r':
Reset();
break;
default:
break;
}
glutPostRedisplay();
}
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);
}
glutPostRedisplay();
}
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);
}
glutPostRedisplay();
}
void InitOpenGl(void)
{
cDrawUtil::InitDrawUtil();
glewInit();
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 InitCaffe()
{
FLAGS_alsologtostderr = 1;
int caffe_argc = 1; // hack
caffe::GlobalInit(&caffe_argc, &gArgv);
}
int main(int argc, char** argv)
{
gArgc = argc;
gArgv = argv;
ParseArgs(gArgc, gArgv);
InitCaffe();
glutInit(&gArgc, gArgv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(gWinWidth, gWinHeight);
glutCreateWindow("Terrain RL");
InitOpenGl();
SetupScenario();
Reshape(gWinWidth, gWinHeight);
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Keyboard);
glutMouseFunc(MouseClick);
glutMotionFunc(MouseMove);
glutTimerFunc(gDisplayAnimTime, Animate, 0);
InitTime();
glutMainLoop();
return EXIT_SUCCESS;
}