This repository has been archived by the owner on Oct 25, 2018. It is now read-only.
/
gui.cpp
1794 lines (1528 loc) · 56.9 KB
/
gui.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
#define SIM_IMPLEMENTATION
#include "sim.h"
#include "gui.h"
#include <algorithm>
#include <vector>
#include "lib/jo_gif.cpp"
#include <time.h>
#include <stdlib.h>
#include <stdint.h>
typedef float r32;
typedef uint64_t u64;
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u08;
typedef int32_t s32;
typedef int16_t s16;
typedef int8_t s08;
#define global static
#define persist static
#include "SDL.h"
#include "SDL_opengl.h"
#include "SDL_assert.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include "lib/imgui/imgui_draw.cpp"
#include "lib/imgui/imgui.cpp"
#include "lib/imgui/imgui_demo.cpp"
#include "lib/imgui/imgui_impl_sdl.cpp"
// Allocate thirty minutes worth of real time history
#define History_Max_Length ((int)(10.0f * 60.0f / Sim_Timestep))
#define Assert SDL_assert
#define Printf SDL_Log
static int fps_lock = 15;
struct Color
{
r32 r, g, b, a;
};
struct VideoMode
{
SDL_Window *window;
int width;
int height;
int gl_major;
int gl_minor;
int double_buffer;
int depth_bits;
int stencil_bits;
int multisamples;
// 0 for immediate updates, 1 for updates synchronized with the
// vertical retrace. If the system supports it, you may
// specify -1 to allow late swaps to happen immediately
// instead of waiting for the next retrace.
int swap_interval;
// Instead of using vsync, you can specify a desired framerate
// that the application will attempt to keep. If a frame rendered
// too fast, it will sleep the remaining time. Leave swap_interval
// at 0 when using this.
int fps_lock;
};
struct Noise_Object{
float position_offset_target;
float freq_position_offset_target;
float position_offset_target_horizon;
float freq_noise_horizon;
float percent_target_in_view;
float angle_offset_target;
float freq_angle_offset_target;
float angle_offset_down_camera;
float freq_angle_offset_down_camera;
float position_offset_drone;
float freq_position_offset_drone;
};
const char *gl_error_message(GLenum error)
{
switch (error)
{
case 0: return "NO_ERROR";
case 0x0500: return "INVALID_ENUM";
case 0x0501: return "INVALID_VALUE";
case 0x0502: return "INVALID_OPERATION";
case 0x0503: return "STACK_OVERFLOW";
case 0x0504: return "STACK_UNDERFLOW";
case 0x0505: return "OUT_OF_MEMORY";
case 0x0506: return "INVALID_FRAMEBUFFER_OPERATION";
default: return "UNKNOWN";
}
}
u64 get_tick()
{
return SDL_GetPerformanceCounter();
}
r32 get_elapsed_time(u64 begin, u64 end)
{
u64 frequency = SDL_GetPerformanceFrequency();
return (r32)(end - begin) / (r32)frequency;
}
r32 time_since(u64 then)
{
u64 now = get_tick();
return get_elapsed_time(then, now);
}
global r32 NDC_SCALE_X;
global r32 NDC_SCALE_Y;
global sim_State STATE;
global sim_State HISTORY_STATE[History_Max_Length];
global sim_Observed_State HISTORY_OBSERVED_STATE[History_Max_Length];
global sim_Command HISTORY_CMD[History_Max_Length];
global int HISTORY_LENGTH;
sim_Observed_State generate_noise(sim_State state, Noise_Object noise )
{
double offset_x;
double offset_y;
double offset_z;
double offset_angle;
double freq;
sim_Observed_State result = {};
result.elapsed_time = state.elapsed_time;
//generate noise for drone
//generate x offset
freq = (float)rand()/(float)(RAND_MAX/noise.freq_position_offset_drone);
if(freq > 1.0){
offset_x = (float)rand()/(float)(RAND_MAX/noise.position_offset_drone)-noise.position_offset_drone/2.0;
}else{
offset_x = 0.0;
}
//generate y offset
freq = (float)rand()/(float)(RAND_MAX/noise.freq_position_offset_drone);
if(freq > 1.0){
offset_y = (float)rand()/(float)(RAND_MAX/noise.position_offset_drone)-noise.position_offset_drone/2.0;
}else{
offset_y = 0.0;
}
//generate z offset
freq = (float)rand()/(float)(RAND_MAX/noise.freq_position_offset_drone);
if(freq > 1.0){
offset_z = (float)rand()/(float)(RAND_MAX/noise.position_offset_drone)-noise.position_offset_drone/2.0;
}else{
offset_z = 0.0;
}
//add noise
result.drone_x = state.drone.x + offset_x;
result.drone_y = state.drone.y + offset_y;
result.drone_z = state.drone.z + offset_z;
result.drone_cmd_done = state.drone.cmd_done;
sim_Robot *targets = state.robots;
sim_Robot *obstacles = state.robots + Num_Targets;
float visible_radius_down_camera = compute_drone_view_radius(state.drone.z);
for (unsigned int i = 0; i < Num_Targets; i++)
{
float dx = state.drone.x - targets[i].x;
float dy = state.drone.y - targets[i].y;
float distance = vector_length(dx,dy);
float unit_x = dx/distance;
float unit_y = dy/distance;
float sin_angle = distance / vector_length(distance,state.drone.z);
float cos_angle = state.drone.z / vector_length(distance,state.drone.z);
freq = (float)rand()/(float)(RAND_MAX/1.0);
if (freq*sin_angle >= noise.percent_target_in_view ){
result.target_in_view[i] = false;
}else{
result.target_in_view[i] = true;
}
result.target_removed[i] = targets[i].removed;
result.target_reward[i] = targets[i].reward;
freq = (float)rand()/(float)(RAND_MAX/noise.freq_position_offset_target);
if(freq > 1.0){
offset_x = (float)rand()/(float)(RAND_MAX/noise.position_offset_target)-noise.position_offset_target/2.0;
}else{
offset_x = 0.0;
}
//generate y offset
freq = (float)rand()/(float)(RAND_MAX/noise.freq_position_offset_target);
if(freq > 1.0){
offset_y = (float)rand()/(float)(RAND_MAX/noise.position_offset_target)-noise.position_offset_target/2.0;
}else{
offset_y = 0.0;
}
freq = (float)rand()/(float)(RAND_MAX/noise.freq_noise_horizon);
float horizon_x = 0;
float horizon_y = 0;
if(freq > 1.0){
float t = (float)rand()/(float)(RAND_MAX/noise.position_offset_target_horizon) - noise.position_offset_target_horizon/2.0;
horizon_x = t*(1.0-cos_angle)*unit_x;
horizon_y = t*(1.0-cos_angle)*unit_y;
}
result.target_x[i] = targets[i].x + offset_x+horizon_x;
result.target_y[i] = targets[i].y + offset_y+horizon_y;
if(distance < visible_radius_down_camera){
freq = (float)rand()/(float)(RAND_MAX/noise.freq_angle_offset_down_camera);
if(freq > 1.0){
offset_angle = (float)rand()/(float)(RAND_MAX/(noise.angle_offset_down_camera*0.0174533))-(noise.angle_offset_down_camera*0.0174533)/2.0;
}else{
offset_angle = 0.0;
}
}else{
freq = (float)rand()/(float)(RAND_MAX/noise.freq_angle_offset_target);
if(freq > 1.0){
offset_angle = (float)rand()/(float)(RAND_MAX/(noise.angle_offset_target*0.0174533))-(noise.angle_offset_target*0.0174533)/2.0;
}else{
offset_angle = 0.0;
}
}
result.target_q[i] = targets[i].q + offset_angle;
result.target_reversing[i] = (targets[i].state == Robot_Reverse);
}
for (unsigned int i = 0; i < Num_Obstacles; i++)
{
float dx = state.drone.x - obstacles[i].x;
float dy = state.drone.y - obstacles[i].y;
float distance = vector_length(dx,dy);
float unit_x = dx/distance;
float unit_y = dy/distance;
float sin_angle = distance / vector_length(distance,state.drone.z);
float cos_angle = state.drone.z / vector_length(distance,state.drone.z);
freq = (float)rand()/(float)(RAND_MAX/1.0);
freq = (float)rand()/(float)(RAND_MAX/noise.freq_position_offset_target);
if(freq > 1.0){
offset_x = (float)rand()/(float)(RAND_MAX/noise.position_offset_target)-noise.position_offset_target/2.0;
}else{
offset_x = 0.0;
}
//generate y offset
freq = (float)rand()/(float)(RAND_MAX/noise.freq_position_offset_target);
if(freq > 1.0){
offset_y = (float)rand()/(float)(RAND_MAX/noise.position_offset_target)-noise.position_offset_target/2.0;
}else{
offset_y = 0.0;
}
freq = (float)rand()/(float)(RAND_MAX/noise.freq_noise_horizon);
float horizon_x = 0;
float horizon_y = 0;
if(freq > 1.0){
float t = (float)rand()/(float)(RAND_MAX/noise.position_offset_target_horizon) - noise.position_offset_target_horizon/2.0;
horizon_x = t*(1.0-cos_angle)*unit_x;
horizon_y = t*(1.0-cos_angle)*unit_y;
}
result.obstacle_x[i] = obstacles[i].x + offset_x+horizon_x;
result.obstacle_y[i] = obstacles[i].y + offset_y+horizon_y;
if(distance < visible_radius_down_camera){
freq = (float)rand()/(float)(RAND_MAX/noise.freq_angle_offset_down_camera);
if(freq > 1.0){
offset_angle = (float)rand()/(float)(RAND_MAX/(noise.angle_offset_down_camera*0.0174533))-(noise.angle_offset_down_camera*0.0174533)/2.0;
}else{
offset_angle = 0.0;
}
}else{
freq = (float)rand()/(float)(RAND_MAX/noise.freq_angle_offset_target);
if(freq > 1.0){
offset_angle = (float)rand()/(float)(RAND_MAX/(noise.angle_offset_target*0.0174533))-(noise.angle_offset_target*0.0174533)/2.0;
}else{
offset_angle = 0.0;
}
}
result.obstacle_q[i] = obstacles[i].q + offset_angle;
}
return result;
}
void add_history(sim_Command cmd, sim_State state, sim_Observed_State observed_state)
{
if (HISTORY_LENGTH < History_Max_Length)
{
HISTORY_CMD[HISTORY_LENGTH] = cmd;
HISTORY_STATE[HISTORY_LENGTH] = state;
HISTORY_OBSERVED_STATE[HISTORY_LENGTH] = observed_state;
HISTORY_LENGTH++;
}
}
void world_to_ndc(r32 x_world, r32 y_world,
r32 *x_ndc, r32 *y_ndc)
{
*x_ndc = (x_world - 10.0f) * NDC_SCALE_X;
*y_ndc = (y_world - 10.0f) * NDC_SCALE_Y;
}
void vertex2f(r32 x, r32 y)
{
r32 x_ndc, y_ndc;
world_to_ndc(x, y, &x_ndc, &y_ndc);
glVertex2f(x_ndc, y_ndc);
}
void color4f(Color color)
{
glColor4f(color.r, color.g, color.b, color.a);
}
void draw_line(r32 x1, r32 y1, r32 x2, r32 y2)
{
vertex2f(x1, y1);
vertex2f(x2, y2);
}
void fill_square(r32 x1, r32 y1, r32 x2, r32 y2)
{
r32 x1n, y1n, x2n, y2n;
world_to_ndc(x1, y1, &x1n, &y1n);
world_to_ndc(x2, y2, &x2n, &y2n);
glVertex2f(x1n, y1n);
glVertex2f(x2n, y1n);
glVertex2f(x2n, y2n);
glVertex2f(x2n, y2n);
glVertex2f(x1n, y2n);
glVertex2f(x1n, y1n);
}
void draw_painted_square(r32 x1, r32 y1, r32 x2, r32 y2)
{
r32 x1n, y1n, x2n, y2n;
world_to_ndc(x1, y1, &x1n, &y1n);
world_to_ndc(x2, y2, &x2n, &y2n);
glVertex2f(x1n, y1n);
glVertex2f(x2n, y1n);
glVertex2f(x2n, y2n);
}
void fill_circle(r32 x, r32 y, r32 r)
{
int n = int(2* r *10.0);
for (int i = 0; i < n; i++){
float x1 = -r + 2*r*float(i)/( float(n));
float y1 = sqrt(pow(r,2)-pow(x1,2));
float x2 = x1+2*r/float(n);
float y2 = -y1;
fill_square(x1+x,y1+y,x2+x,y2+y);
}
}
void draw_circle(r32 x, r32 y, r32 r, u32 n = 32)
{
for (u32 i = 0; i < n; i++)
{
r32 t1 = TWO_PI * i / (r32)n;
r32 t2 = TWO_PI * (i + 1) / (r32)n;
r32 x1 = x + r*cos(t1);
r32 y1 = y + r*sin(t1);
r32 x2 = x + r*cos(t2);
r32 y2 = y + r*sin(t2);
draw_line(x1, y1, x2, y2);
}
}
void draw_robot(sim_Robot robot)
{
r32 x = robot.x;
r32 y = robot.y;
r32 l = 2*Sim_Robot_Radius;
r32 q = robot.q;
draw_circle(x, y, 0.5f*l);
draw_line(x, y, x + l*cos(q), y + l*sin(q));
}
void draw_observed_robot(r32 x, r32 y, r32 q)
{
r32 l = 2*Sim_Robot_Radius;
draw_circle(x, y, 0.5f*l);
draw_line(x, y, x + l*cos(q), y + l*sin(q));
}
void draw_drone(float radius,float x,float y,bool flag_fancy_drone){
glBegin(GL_LINES);
float propel_radius = radius/3.0;
if(! flag_fancy_drone){
draw_circle(x,y,radius);
glEnd();
}else{
// //propel 1
radius = radius*2; // radius is the size of the undercarriage, radius*2 this will give a more realistic size of actual drone
float propel_1_x = x + sin(PI/4.0)*radius;
float propel_1_y = y + cos(PI/4.0)*radius;
draw_line(propel_1_x-propel_radius,propel_1_y,propel_1_x+propel_radius,propel_1_y);
draw_line(propel_1_x,propel_1_y-propel_radius,propel_1_x,propel_1_y+propel_radius);
draw_circle(propel_1_x,propel_1_y,propel_radius);
//propel 2
float propel_2_x = x - sin(PI/4.0)*radius;
float propel_2_y = y - cos(PI/4.0)*radius;
draw_line(propel_2_x-propel_radius,propel_2_y,propel_2_x+propel_radius,propel_2_y);
draw_line(propel_2_x,propel_2_y-propel_radius,propel_2_x,propel_2_y+propel_radius);
draw_circle(propel_2_x,propel_2_y,propel_radius);
//propel 3
float propel_3_x = x + sin(PI/4.0)*radius;
float propel_3_y = y - cos(PI/4.0)*radius;
draw_line(propel_3_x-propel_radius,propel_3_y,propel_3_x+propel_radius,propel_3_y);
draw_line(propel_3_x,propel_3_y-propel_radius,propel_3_x,propel_3_y+propel_radius);
draw_circle(propel_3_x,propel_3_y,propel_radius);
// //propel 4
float propel_4_x = x - sin(PI/4.0)*radius;
float propel_4_y = y + cos(PI/4.0)*radius;
draw_line(propel_4_x-propel_radius,propel_4_y,propel_4_x+propel_radius,propel_4_y);
draw_line(propel_4_x,propel_4_y-propel_radius,propel_4_x,propel_4_y+propel_radius);
draw_circle(propel_4_x,propel_4_y,propel_radius);
glEnd();
glBegin(GL_TRIANGLES);
//draw body
fill_square(x-radius/4,y-radius*0.7,x+radius/4,y+radius*0.7);
fill_square(x-radius/3,y+radius*0.8,x+radius/3,y+radius*0.6);
fill_square(x-radius/3,y-radius*0.8,x+radius/3,y-radius*0.6);
glEnd();
}
}
void draw_planks(sim_Robot robot)
{
r32 x = robot.x;
r32 y = robot.y;
r32 q = robot.q;
r32 plank_angle = robot.plank_angle;
robot_Internal internal = robot.internal;
float plank_behind = std::max((internal.time_since_last_reverse- Reverse_Length) * Robot_Speed,0.0f);
float plank_ahead = std::min(internal.time_to_next_reverse * Robot_Speed,(Reverse_Interval- Reverse_Length) * Robot_Speed);
draw_line(x, y, x + plank_ahead*cos(plank_angle), y + plank_ahead*sin(plank_angle));
draw_line(x, y, x + plank_behind*cos(plank_angle-PI), y + plank_behind*sin(plank_angle-PI));
}
void draw_observed_plank(r32 x,r32 y, r32 q ,r32 plank_angle,robot_Internal internal)
{
float plank_behind = std::max((internal.time_since_last_reverse- Reverse_Length) * Robot_Speed,0.0f);
float plank_ahead = std::min(internal.time_to_next_reverse * Robot_Speed,(Reverse_Interval- Reverse_Length) * Robot_Speed);
draw_line(x, y, x + plank_ahead*cos(plank_angle), y + plank_ahead*sin(plank_angle));
draw_line(x, y, x + plank_behind*cos(plank_angle-PI), y + plank_behind*sin(plank_angle-PI));
}
struct FileData
{
int length;
u32 seed;
sim_Command cmds[History_Max_Length];
};
bool write_history(char *filename)
{
FILE *f = fopen(filename, "wb");
if (!f)
return false;
static FileData data;
data.seed = STATE.seed;
data.length = HISTORY_LENGTH;
for (int i = 0; i < HISTORY_LENGTH; i++)
data.cmds[i] = HISTORY_CMD[i];
fwrite((char*)&data, 1, sizeof(data), f);
fclose(f);
return true;
}
bool read_history(char *filename)
{
FILE *f = fopen(filename, "rb");
if (!f)
{
Printf("Failed to open file\n");
return false;
}
static FileData data;
fread((char*)&data, sizeof(data), 1, f);
HISTORY_LENGTH = data.length;
STATE = sim_init(data.seed);
for (int i = 0; i < HISTORY_LENGTH; i++)
{
HISTORY_CMD[i] = data.cmds[i];
STATE = sim_tick(STATE, HISTORY_CMD[i]);
HISTORY_STATE[i] = STATE;
}
fclose(f);
return true;
}
float heat_blue_color(float x){
if(x<0.0){
return 1.0;
}else if(x>=0.0 && x < 0.25){
return 1.0;
}else if(x>= 0.25 && x<0.5){
return (-4.0*(x-0.5));
}else if(x>=0.5){
return 0.0;
}
}
float heat_green_color(float x){
if(x<0.0){
return 0.0;
}else if(x>=0.0 && x < 0.25){
return 4.0*x;
}else if(x>= 0.25 && x<0.75){
return 1.0;
}else if(x>=0.75 && x < 1.0){
return (-4.0*(x-1.0));
}else{
return 0.0;
}
}
float heat_red_color(float x){
if(x<0.5){
return 0.0;
}else if(x>=0.5 && x < 0.75){
return (4.0*(x-0.5));
}else if(x>= 0.75){
return 1.0;
}
}
float transform_heat_color(float x){
return 0.1205*log(4000.0*x+1.0);
}
// Draw a formatted text string at window coordinate (x,y) measured from top-left
// Usage: DrawString(200, 300, "Hello world %.2f %d", 3.1415926f, 42);
void DrawString(int unique_id, float x, float y, float height, float width, const char* fmt, ...)
{
float anchor_point_x = width/2;
float anchor_point_y = height/2;
y = 20.0-y; //reverse for correct plot
y = y-10; //move to origo
x = x - 10; // move to origo
y = y*height/24; //scale
x = x*height/24; //scale
x = x+anchor_point_x ;//translate
y = y+anchor_point_y; //translate
//x = x+width/2; //center
char name[1024];
sprintf(name, "draw_string_%d", unique_id);
va_list args;
va_start(args, fmt);
ImGui::SetNextWindowPos(ImVec2(x, y));
ImGui::Begin(name, 0, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoSavedSettings|ImGuiWindowFlags_AlwaysAutoResize);
ImGui::TextV(fmt, args);
ImGui::End();
va_end(args);
}
void gui_tick(VideoMode mode, r32 gui_time, r32 gui_dt,int k)
{
persist bool flag_grid = false;
persist bool flag_plank = false;
persist bool flag_DrawDroneGoto = true;
persist bool flag_draw_observation = false;
persist bool flag_DrawDrone = true;
persist bool flag_DrawVisibleRegion = true;
persist bool flag_DrawTargets = true;
persist bool flag_DrawObstacles = true;
persist bool flag_Paused = false;
persist bool flag_Recording = false;
persist bool flag_SetupRecord = false;
persist bool flag_probability_distribution = false;
persist bool flag_custom_drone_text = false;
persist bool flag_custom_target_text = false;
persist bool flag_view_target_text = true;
persist bool flag_view_drone_text = true;
persist bool flag_send_perfect_data = true;
persist bool flag_fancy_drone = false;
persist int record_from = 0;
persist int record_to = 0;
persist int record_frame_skip = 1;
persist int record_width = 0;
persist int record_height = 0;
persist float record_region_x = -1.0f;
persist float record_region_y = -1.0f;
persist float record_region_scale = 2.0f;
persist jo_gif_t record_gif;
persist int seek_cursor = 0;
persist int selected_target = -1;
persist Color color_Clear = { 0.00f, 0.00f, 0.00f, 1.00f };
persist Color color_Tiles = { 0.20f, 0.35f, 0.46f, 0.66f };
persist Color color_Grid = { 0.00f, 0.00f, 0.00f, 1.00f };
persist Color color_VisibleRegion = { 1.0f, 1.0f, 1.0f, 0.10f };
persist Color color_GreenLine = { 0.10f, 1.00f, 0.20f, 1.00f };
persist Color color_SelectedTarget = { 0.85f, 0.34f, 0.32f, 1.00f };
persist Color color_Targets = { 0.85f, 0.83f, 0.37f, 1.00f };
persist Color color_Targets_prob = { 0.00f, 0.00f, 0.00f, 1.00f };
persist Color color_Obstacles = { 0.43f, 0.76f, 0.79f, 1.00f };
persist Color color_Drone = { 0.05f, 0.05f, 0.05f, 1.0f };
persist Color color_DroneGoto = { 0.87f, 0.93f, 0.84f, 0.50f };
persist Color color_Planks = { 0.85f, 0.83f, 0.37f, 0.50f };
persist Color color_transp_SelectedTarget = { 0.85f, 0.34f, 0.32f, 0.25f };
persist Color color_transp_Targets = { 0.85f, 0.83f, 0.37f, 0.25f };
persist Color color_transp_Targets_prob = { 0.00f, 0.00f, 0.00f, 0.25f };
persist Color color_transp_Obstacles = { 0.43f, 0.76f, 0.79f, 0.25f };
persist Color color_transp_Drone = { 0.05f, 0.05f, 0.05f, 0.25f };
persist Color color_transp_Planks = { 0.85f, 0.83f, 0.37f, 0.12f };
persist Color white = { 1.0f, 1.0f, 1.0f, 1.0f };
persist Color yellow = { 1.0f, 1.0f, 0.0f, 1.0f };
#define RGBA(C) C.r, C.g, C.b, C.a
persist float send_timer = 0.0f;
persist float send_interval = 0.2f;// In simulation time units
persist float position_offset_target = 0.15;
persist float freq_position_offset_target = 1.0;
persist float position_offset_target_horizon =0.9;
persist float freq_noise_horizon = 1.9;
persist float percent_target_in_view = 0.9;
persist float angle_offset_target = 180.0;
persist float freq_angle_offset_target = 1.5;
persist float position_offset_drone = 0.2;
persist float freq_position_offset_drone = 1.5;
persist float freq_angle_offset_down_camera = 1.5;
persist float angle_offset_down_camera = 20.0;
Noise_Object noise;
noise.position_offset_target = position_offset_target;
noise.freq_position_offset_target = freq_position_offset_target;
noise.position_offset_target_horizon = position_offset_target_horizon;
noise.freq_noise_horizon = freq_noise_horizon;
noise.percent_target_in_view = percent_target_in_view;
noise.angle_offset_target = angle_offset_target;
noise.freq_angle_offset_target = freq_angle_offset_target;
noise.position_offset_drone = position_offset_drone;
noise.freq_position_offset_drone = freq_position_offset_drone;
noise.angle_offset_down_camera = angle_offset_down_camera;
noise.freq_angle_offset_down_camera = freq_angle_offset_down_camera;
NDC_SCALE_X = (mode.height / (r32)mode.width) / 12.0f;
NDC_SCALE_Y = 1.0f / 12.0f;
if (flag_Recording || flag_SetupRecord)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
float Ax = 2.0f / record_region_scale;
float Bx = -1.0f - Ax*record_region_x;
float Ay = 2.0f / record_region_scale;
float By = -1.0f - Ay*record_region_y;
float modelview[] = {
Ax, 0.0f, 0.0f, 0.0f,
0.0f, Ay, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
Bx, By, 0.0f, 1.0f
};
glLoadMatrixf(modelview);
}
else
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
if (!flag_Paused)
{
if (flag_Recording)
{
if (seek_cursor >= record_to)
{
flag_Paused = true;
flag_Recording = false;
seek_cursor = record_from;
jo_gif_end(&record_gif);
}
else if (seek_cursor + record_frame_skip >= record_to)
{
// clamp to end
seek_cursor = record_to;
}
else
{
seek_cursor += record_frame_skip;
}
}
else if (seek_cursor < HISTORY_LENGTH-1)
{
seek_cursor++;
}
else
{
sim_Command cmd;
if (!sim_recv_cmd(&cmd))
{
cmd.type = sim_CommandType_NoCommand;
cmd.x = -1.0;
cmd.y = -1.0;
cmd.i = -1;
for(int v = 0; v <20*20*pixels_each_meter*pixels_each_meter; v++){
cmd.heatmap[v] = HISTORY_CMD[HISTORY_LENGTH-1].heatmap[v];
}
for(int v = 0; v <100*Num_Targets; v++){
cmd.text[v] = HISTORY_CMD[HISTORY_LENGTH-1].text[v];
}
cmd.reward = -1.0;
}
/** if(cmd.type = sim_CommandType_NoCommand){
cmd.x = -1.0;
cmd.y = -1.0;
cmd.i = -1;
}
else if(cmd.type = sim_CommandType_Track){
cmd.x = -1.0;
cmd.y = -1.0;
}
else if(cmd.type = sim_CommandType_Search){
cmd.i = -1.0;
}
else if(cmd.type = sim_CommandType_LandOnTopOf){
cmd.x = -1.0;
cmd.y = -1.0;
}
else if(cmd.type = sim_CommandType_LandInFrontOf){
cmd.x = -1.0;
cmd.y = -1.0;
}
else if(cmd.type = sim_CommandType_Land){
cmd.i = -1.0;
}**/
//for(int bit = 0; bit < pixels_each_meter*pixels_each_meter*20*20; bit++ )
//{
// cmd.heatmap[bit] = 0.0;
//}
//for(int bit = 0; bit < 256*Num_Targets; bit++ )
//{
// cmd.text[bit] = ;
//}
STATE = sim_tick(STATE, cmd);
//add some noise to simulate perception
sim_Observed_State observed_state = generate_noise(STATE,noise);
sim_Observed_State perfect_data = sim_observe_state(STATE);
add_history(cmd, STATE, observed_state);
seek_cursor = HISTORY_LENGTH-1;
send_timer -= Sim_Timestep;
if (send_timer <= 0.0f)
{
if(flag_send_perfect_data){
sim_send_state(&perfect_data);
}else{
sim_send_state(&observed_state);
}
send_timer += send_interval;
}
}
}
sim_State draw_state = HISTORY_STATE[seek_cursor];
sim_Observed_State observed = HISTORY_OBSERVED_STATE[seek_cursor];
sim_Command cmd_state = HISTORY_CMD[seek_cursor];
sim_Drone drone = draw_state.drone;
//sim_Command cmd_state = HISTORY_CMD[seek_cursor];
sim_Robot *robots = draw_state.robots;
sim_Robot *targets = draw_state.robots;
sim_Robot *obstacles = draw_state.robots + Num_Targets;
if (flag_Recording || flag_SetupRecord)
{
glViewport(0, 0, record_width, record_height);
}
else
{
glViewport(0, 0, mode.width, mode.height);
}
glClearColor(RGBA(color_Clear));
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(2.0f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// draw grid tiles
glBegin(GL_TRIANGLES);
{
color4f(color_Tiles);
for (int yi = 0; yi < 20; yi++)
for (int xi = 0; xi < 20; xi++)
{
r32 x = xi*1.0f;
r32 y = yi*1.0f;
fill_square(x, y, x+1.0f, y+1.0f);
}
}
// draw heat map
if (flag_probability_distribution)
{
int iterations = pixels_each_meter * 20;
double unit = 1.0 / float(pixels_each_meter);
for(int yi = 0; yi < iterations; yi++){
for (int xi = 0; xi < iterations; xi++)
{
float value = cmd_state.heatmap[yi*iterations + xi];
value = transform_heat_color(value);
float blue = heat_blue_color(value);
float green = heat_green_color(value);
float red = heat_red_color(value);
Color fading_color = { red , green, blue, 1.00f };
color4f(fading_color);
r32 x = xi * unit;
r32 y = yi* unit;
fill_square(x ,y, x+unit, y+unit);
}
}
}
// draw visible region
if (flag_DrawVisibleRegion)
{
color4f(color_VisibleRegion);
fill_circle(drone.x, drone.y, compute_drone_view_radius(drone.z));
}
glEnd();
glBegin(GL_LINES);
{
// draw grid lines
if(flag_grid)
{
color4f(color_Grid);
for (int i = 0; i <= 20; i++)
{
r32 x = (r32)i;
draw_line(x, 0.0f, x, 20.0f);
draw_line(0.0f, x, 20.0f, x);
}
}
// draw green line
color4f(color_GreenLine);
draw_line(0.0f, 20.0f, 20.0f, 20.0f);
// draw targets
if (flag_DrawTargets)
{
if(flag_probability_distribution){
color4f(color_Targets_prob);
if(flag_draw_observation){
color4f(color_transp_Targets_prob);
}
}else{
color4f(color_Targets);
if(flag_draw_observation){
color4f(color_transp_Targets);
}
}
for (int i = 0; i < Num_Targets; i++)
draw_robot(targets[i]);
if (selected_target >= 0)
{
color4f(color_SelectedTarget);
if(flag_draw_observation){
color4f(color_transp_SelectedTarget);
}
draw_robot(targets[selected_target]);
}
}
// draw Planks
if (flag_plank)
{
color4f(color_Planks);
if(flag_draw_observation){
color4f(color_transp_Planks);
}
for (int i = 0; i < Num_Targets; i++){
draw_planks(targets[i]);
}
}
// draw obstacles
if (flag_DrawObstacles)
{
color4f(color_Obstacles);
if(flag_draw_observation){
color4f(color_transp_Obstacles);
}
for (int i = 0; i < Num_Obstacles; i++)
draw_robot(obstacles[i]);
}
// draw observed things
if(flag_draw_observation){
//draw observed targets
if (flag_DrawTargets)
{
/**
if(flag_probability_distribution){
color4f(color_Targets_prob);
}else{
color4f(color_Targets);
}**/
color4f(white);