forked from yozlet/interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1057 lines (890 loc) · 31.5 KB
/
Copy pathmain.cpp
File metadata and controls
1057 lines (890 loc) · 31.5 KB
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
//
//
// Interface
//
// Show a field of objects rendered in 3D, with yaw and pitch of scene driven
// by accelerometer data
// serial port connected to Maple board/arduino.
//
// Keyboard Commands:
//
// / = toggle stats display
// n = toggle noise in firing on/off
// c = clear all cells and synapses to zero
// s = clear cells to zero but preserve synapse weights
//
#ifdef __APPLE__
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
#else
#include <GL/glut.h>
#endif
#include <iostream>
#include <fstream>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <pthread.h>
// These includes are for the serial port reading/writing
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include "glm/glm.hpp"
#include <portaudio.h>
// Bring in OpenCV
#include <opencv2/opencv.hpp>
#include "SerialInterface.h"
#include "field.h"
#include "world.h"
#include "util.h"
#include "network.h"
#include "audio.h"
#include "head.h"
#include "hand.h"
#include "particle.h"
#include "texture.h"
#include "cloud.h"
#include "agent.h"
#include "menu.h"
menu menu_items;
#include "markers.h"
#include "marker_acquisition_view.h"
using namespace std;
int audio_on = 0; // Whether to turn on the audio support
int simulate_on = 1;
// Network Socket Stuff
// For testing, add milliseconds of delay for received UDP packets
int UDP_socket;
int delay = 0;
char* incoming_packet;
timeval ping_start;
int ping_count = 0;
float ping_msecs = 0.0;
int packetcount = 0;
int packets_per_second = 0;
int bytes_per_second = 0;
int bytescount = 0;
// Getting a target location from other machine (or loopback) to display
int target_x, target_y;
int target_display = 0;
int head_mirror = 1; // Whether to mirror the head when viewing it
int WIDTH = 1200;
int HEIGHT = 800;
#define HAND_RADIUS 0.25 // Radius of in-world 'hand' of you
Head myHead; // The rendered head of oneself or others
Hand myHand(HAND_RADIUS,
glm::vec3(0,1,1)); // My hand (used to manipulate things in world)
glm::vec3 box(WORLD_SIZE,WORLD_SIZE,WORLD_SIZE);
ParticleSystem balls(0,
box,
false, // Wrap?
0.02, // Noise
0.3, // Size scale
0.0 // Gravity
);
Cloud cloud(100000, // Particles
box, // Bounding Box
false // Wrap
);
float cubes_position[MAX_CUBES*3];
float cubes_scale[MAX_CUBES];
float cubes_color[MAX_CUBES*3];
int cube_count = 0;
#define RENDER_FRAME_MSECS 5
#define SLEEP 0
float yaw =0.f; // The yaw, pitch for the avatar head
float pitch = 0.f; //
float start_yaw = 90.0;
float render_yaw = start_yaw;
float render_pitch = 0.f;
float render_yaw_rate = 0.f;
float render_pitch_rate = 0.f;
float lateral_vel = 0.f;
// Manage speed and direction of motion
GLfloat fwd_vec[] = { 0.0, 0.0, 1.0};
GLfloat start_location[] = { WORLD_SIZE*1.5, -WORLD_SIZE/2.0, -WORLD_SIZE/3.0};
GLfloat location[] = {start_location[0], start_location[1], start_location[2]};
float fwd_vel = 0.0f;
#define MAX_FILE_CHARS 100000 // Biggest file size that can be read to the system
int stats_on = 1; // Whether to show onscreen text overlay with stats
int noise_on = 0; // Whether to add random noise
float noise = 1.0; // Overall magnitude scaling for random noise levels
int step_on = 0;
int display_levels = 0;
int display_head = 0;
int display_hand = 0;
int display_field = 0;
int display_head_mouse = 1; // Display sample mouse pointer controlled by head movement
int head_mouse_x, head_mouse_y;
int head_lean_x, head_lean_y;
int mouse_x, mouse_y; // Where is the mouse
int mouse_pressed = 0; // true if mouse has been pressed (clear when finished)
int speed;
//
// Serial USB Variables
//
int serial_on = 0;
int latency_display = 1;
int adc_channels[NUM_CHANNELS];
float avg_adc_channels[NUM_CHANNELS];
int sensor_samples = 0;
int sensor_LED = 0;
glm::vec3 gravity;
int first_measurement = 1;
int samplecount = 0;
// Frame rate Measurement
int framecount = 0;
float FPS = 120.f;
timeval timer_start, timer_end;
timeval last_frame;
double elapsedTime;
// Particles
// To add a new texture:
// 1. Add to the XCode project in the Resources/images group
// (ensure "Copy file" is checked
// 2. Add to the "Copy files" build phase in the project
char texture_filename[] = "./int-texture256-v4.png";
unsigned int texture_width = 256;
unsigned int texture_height = 256;
float particle_attenuation_quadratic[] = { 0.0f, 0.0f, 2.0f }; // larger Z = smaller particles
float pointer_attenuation_quadratic[] = { 1.0f, 0.0f, 0.0f }; // for 2D view
/*** Marker Capture ***/
#define MARKER_CAPTURE_INTERVAL 1
MarkerCapture marker_capturer(CV_CAP_ANY); // Create a new marker capturer, attached to any valid camera.
MarkerAcquisitionView marker_acq_view(&marker_capturer);
bool marker_capture_enabled = true;
bool marker_capture_display = false;
IplImage* marker_capture_frame;
IplImage* marker_capture_blob_frame;
pthread_mutex_t frame_lock;
// Every second, check the frame rates and other stuff
void Timer(int extra)
{
gettimeofday(&timer_end, NULL);
FPS = (float)framecount / ((float)diffclock(timer_start,timer_end) / 1000.f);
packets_per_second = (float)packetcount / ((float)diffclock(timer_start,timer_end) / 1000.f);
bytes_per_second = (float)bytescount / ((float)diffclock(timer_start,timer_end) / 1000.f);
framecount = 0;
samplecount = 0;
packetcount = 0;
bytescount = 0;
glutTimerFunc(1000,Timer,0);
gettimeofday(&timer_start, NULL);
// Send a message to the spaceserver telling it we are ALIVE
notify_spaceserver(UDP_socket, location[0], location[1], location[2]);
}
void display_stats(void)
{
// bitmap chars are about 10 pels high
char legend[] = "/ - toggle this display, Q - exit, H - show head, M - show hand, T - test audio";
drawtext(10, 15, 0.10, 0, 1.0, 0, legend);
char stats[200];
sprintf(stats, "FPS = %3.0f, Ping = %4.1f Pkts/s = %d, Bytes/s = %d",
FPS, ping_msecs, packets_per_second, bytes_per_second);
drawtext(10, 30, 0.10, 0, 1.0, 0, stats);
if (serial_on) {
sprintf(stats, "ADC samples = %d, LED = %d",
sensor_samples, sensor_LED);
drawtext(500, 30, 0.10, 0, 1.0, 0, stats);
}
if(marker_capture_enabled){
char marker_stats[200];
sprintf(marker_stats, "> Marker capture: FPS = %3.0f", marker_capturer.fps);
drawtext(10, 45, 0.10, 0, 1.0, 0, marker_stats);
}
/*
char adc[200];
sprintf(adc, "pitch_rate = %i, yaw_rate = %i, accel_lat = %i, accel_fwd = %i, loc[0] = %3.1f loc[1] = %3.1f, loc[2] = %3.1f",
(int)(adc_channels[0] - avg_adc_channels[0]),
(int)(adc_channels[1] - avg_adc_channels[1]),
(int)(adc_channels[2] - avg_adc_channels[2]),
(int)(adc_channels[3] - avg_adc_channels[3]),
location[0], location[1], location[2]
);
drawtext(10, 50, 0.10, 0, 1.0, 0, adc);
*/
}
void position_updated(MarkerCapture* inst, MarkerPositionEstimate position){
printf("<Marker(%dx%d, %dx%d) distance:%f angle:%f>\n",
position.blob0_center.x, position.blob0_center.y,
position.blob1_center.x, position.blob0_center.y,
position.distance,
position.angle);
myHead.setRoll(position.angle);
}
void marker_frame_available(MarkerCapture* inst, IplImage* image, IplImage* thresh_image){
if(marker_capture_display){
pthread_mutex_lock(&frame_lock);
if(marker_capture_frame){
cvReleaseImage(&marker_capture_frame);
}
if(marker_capture_blob_frame){
cvReleaseImage(&marker_capture_blob_frame);
}
marker_capture_frame = cvCloneImage(image);
marker_capture_blob_frame = cvCloneImage(thresh_image);
pthread_mutex_unlock(&frame_lock);
}
}
void initDisplay(void)
{
// Set up blending function so that we can NOT clear the display
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glShadeModel (GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
load_png_as_texture(texture_filename);
//glutFullScreen();
}
void init(void)
{
int i;
if (audio_on) {
Audio::init();
printf( "Audio started.\n" );
}
// Clear serial channels
for (i = i; i < NUM_CHANNELS; i++)
{
adc_channels[i] = 0;
avg_adc_channels[i] = 0.0;
}
head_mouse_x = WIDTH/2;
head_mouse_y = HEIGHT/2;
head_lean_x = WIDTH/2;
head_lean_y = HEIGHT/2;
// Initialize Field values
field_init();
printf( "Field Initialized.\n" );
if (noise_on)
{
myHand.setNoise(noise);
myHead.setNoise(noise);
}
// turning cubes off for the moment -
// uncomment to re-enable
/*
int index = 0;
while (index < MAX_CUBES) {
cubes_position[index*3] = randFloat()*WORLD_SIZE;
cubes_position[index*3+1] = randFloat()*WORLD_SIZE;
cubes_position[index*3+2] = randFloat()*WORLD_SIZE;
cubes_scale[index] = WORLD_SIZE/powf(2,2+rand()%8);
float color = randFloat();
cubes_color[index*3] = color;
cubes_color[index*3 + 1] = color;
cubes_color[index*3 + 2] = color;
index++;
}
cube_count = index;
// Recursive build
float location[] = {0,0,0};
float scale = 10.0;
int j = 0;
while (index < (MAX_CUBES/2)) {
index = 0;
j++;
makeCubes(location, scale, &index, cubes_position, cubes_scale, cubes_color);
std::cout << "Run " << j << " Made " << index << " cubes\n";
cube_count = index;
}
*/
if (serial_on)
{
// Call readsensors for a while to get stable initial values on sensors
printf( "Stabilizing sensors... " );
gettimeofday(&timer_start, NULL);
read_sensors(1, &avg_adc_channels[0], &adc_channels[0], &sensor_samples, &sensor_LED);
int done = 0;
while (!done)
{
read_sensors(0, &avg_adc_channels[0], &adc_channels[0], &sensor_samples, &sensor_LED);
gettimeofday(&timer_end, NULL);
if (diffclock(timer_start,timer_end) > 1000) done = 1;
}
gravity.x = avg_adc_channels[ACCEL_X];
gravity.y = avg_adc_channels[ACCEL_Y];
gravity.z = avg_adc_channels[ACCEL_Z];
std::cout << "Gravity: " << gravity.x << "," << gravity.y << "," << gravity.z << "\n";
printf( "Done.\n" );
}
if(marker_capture_enabled){
marker_capturer.position_updated(&position_updated);
marker_capturer.frame_updated(&marker_frame_available);
if(!marker_capturer.init_capture()){
printf("Camera-based marker capture initialized.\n");
}else{
printf("Error initializing camera-based marker capture.\n");
}
}
gettimeofday(&timer_start, NULL);
gettimeofday(&last_frame, NULL);
}
void terminate () {
// Close serial port
//close(serial_fd);
if (audio_on) {
Audio::terminate();
}
exit(EXIT_SUCCESS);
}
void reset_sensors()
{
//
// Reset serial I/O sensors
//
render_yaw = start_yaw;
yaw = render_yaw_rate = 0;
pitch = render_pitch = render_pitch_rate = 0;
lateral_vel = 0;
location[0] = start_location[0];
location[1] = start_location[1];
location[2] = start_location[2];
fwd_vel = 0.0;
head_mouse_x = WIDTH/2;
head_mouse_y = HEIGHT/2;
head_lean_x = WIDTH/2;
head_lean_y = HEIGHT/2;
myHead.reset();
myHand.reset();
if (serial_on) read_sensors(1, &avg_adc_channels[0], &adc_channels[0], &sensor_samples, &sensor_LED);
}
void update_pos(float frametime)
// Using serial data, update avatar/render position and angles
{
float measured_pitch_rate = adc_channels[PITCH_RATE] - avg_adc_channels[PITCH_RATE];
float measured_yaw_rate = adc_channels[YAW_RATE] - avg_adc_channels[YAW_RATE];
float measured_lateral_accel = adc_channels[ACCEL_X] - avg_adc_channels[ACCEL_X];
float measured_fwd_accel = avg_adc_channels[ACCEL_Z] - adc_channels[ACCEL_Z];
myHead.UpdatePos(frametime, &adc_channels[0], &avg_adc_channels[0], head_mirror, &gravity);
// Update head_mouse model
const float MIN_MOUSE_RATE = 30.0;
const float MOUSE_SENSITIVITY = 0.1;
if (powf(measured_yaw_rate*measured_yaw_rate +
measured_pitch_rate*measured_pitch_rate, 0.5) > MIN_MOUSE_RATE)
{
head_mouse_x -= measured_yaw_rate*MOUSE_SENSITIVITY;
head_mouse_y += measured_pitch_rate*MOUSE_SENSITIVITY*(float)HEIGHT/(float)WIDTH;
}
head_mouse_x = max(head_mouse_x, 0);
head_mouse_x = min(head_mouse_x, WIDTH);
head_mouse_y = max(head_mouse_y, 0);
head_mouse_y = min(head_mouse_y, HEIGHT);
// Update hand/manipulator location for measured forces from serial channel
/*
const float MIN_HAND_ACCEL = 30.0;
const float HAND_FORCE_SCALE = 0.5;
glm::vec3 hand_accel(-(avg_adc_channels[6] - adc_channels[6]),
-(avg_adc_channels[7] - adc_channels[7]),
-(avg_adc_channels[5] - adc_channels[5]));
if (glm::length(hand_accel) > MIN_HAND_ACCEL)
{
myHand.addVel(frametime*hand_accel*HAND_FORCE_SCALE);
}
*/
// Update render direction (pitch/yaw) based on measured gyro rates
const int MIN_YAW_RATE = 300;
const float YAW_SENSITIVITY = 0.03;
const int MIN_PITCH_RATE = 300;
const float PITCH_SENSITIVITY = 0.04;
if (fabs(measured_yaw_rate) > MIN_YAW_RATE)
{
if (measured_yaw_rate > 0)
render_yaw_rate -= (measured_yaw_rate - MIN_YAW_RATE) * YAW_SENSITIVITY * frametime;
else
render_yaw_rate -= (measured_yaw_rate + MIN_YAW_RATE) * YAW_SENSITIVITY * frametime;
}
if (fabs(measured_pitch_rate) > MIN_PITCH_RATE)
{
if (measured_pitch_rate > 0)
render_pitch_rate += (measured_pitch_rate - MIN_PITCH_RATE) * PITCH_SENSITIVITY * frametime;
else
render_pitch_rate += (measured_pitch_rate + MIN_PITCH_RATE) * PITCH_SENSITIVITY * frametime;
}
render_yaw += render_yaw_rate;
render_pitch += render_pitch_rate;
// Decay render_pitch toward zero because we never look constantly up/down
render_pitch *= (1.f - 2.0*frametime);
// Decay angular rates toward zero
render_pitch_rate *= (1.f - 5.0*frametime);
render_yaw_rate *= (1.f - 7.0*frametime);
// Update slide left/right based on accelerometer reading
const int MIN_LATERAL_ACCEL = 20;
const float LATERAL_SENSITIVITY = 0.001;
if (fabs(measured_lateral_accel) > MIN_LATERAL_ACCEL)
{
if (measured_lateral_accel > 0)
lateral_vel += (measured_lateral_accel - MIN_LATERAL_ACCEL) * LATERAL_SENSITIVITY * frametime;
else
lateral_vel += (measured_lateral_accel + MIN_LATERAL_ACCEL) * LATERAL_SENSITIVITY * frametime;
}
//slide += lateral_vel;
lateral_vel *= (1.f - 4.0*frametime);
// Update fwd/back based on accelerometer reading
const int MIN_FWD_ACCEL = 20;
const float FWD_SENSITIVITY = 0.001;
if (fabs(measured_fwd_accel) > MIN_FWD_ACCEL)
{
if (measured_fwd_accel > 0)
fwd_vel += (measured_fwd_accel - MIN_FWD_ACCEL) * FWD_SENSITIVITY * frametime;
else
fwd_vel += (measured_fwd_accel + MIN_FWD_ACCEL) * FWD_SENSITIVITY * frametime;
}
// Decrease forward velocity
fwd_vel *= (1.f - 4.0*frametime);
// Update forward vector based on pitch and yaw
fwd_vec[0] = -sinf(render_yaw*PI/180);
fwd_vec[1] = sinf(render_pitch*PI/180);
fwd_vec[2] = cosf(render_yaw*PI/180);
// Advance location forward
location[0] += fwd_vec[0]*fwd_vel;
location[1] += fwd_vec[1]*fwd_vel;
location[2] += fwd_vec[2]*fwd_vel;
// Slide location sideways
location[0] += fwd_vec[2]*-lateral_vel;
location[2] += fwd_vec[0]*lateral_vel;
// Update head and manipulator objects with object with current location
myHead.setPos(glm::vec3(location[0], location[1], location[2]));
balls.updateHand(myHead.getPos() + myHand.getPos(), glm::vec3(0,0,0), myHand.getRadius());
// Update all this stuff to any agents that are nearby and need to see it!
const int MAX_BROADCAST_STRING = 200;
char broadcast_string[MAX_BROADCAST_STRING];
int broadcast_bytes = myHead.getBroadcastData(broadcast_string);
broadcast_to_agents(UDP_socket, broadcast_string, broadcast_bytes);
}
void display(void)
{
glEnable (GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LINE_SMOOTH);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
GLfloat light_position0[] = { 1.0, 1.0, 0.0, 0.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
GLfloat ambient_color[] = { 0.125, 0.305, 0.5 };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color);
GLfloat diffuse_color[] = { 0.5, 0.42, 0.33 };
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_color);
GLfloat specular_color[] = { 1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT0, GL_SPECULAR, specular_color);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular_color);
glMateriali(GL_FRONT, GL_SHININESS, 96);
// Rotate, translate to camera location
glRotatef(render_pitch, 1, 0, 0);
glRotatef(render_yaw, 0, 1, 0);
glTranslatef(location[0], location[1], location[2]);
glPushMatrix();
//glTranslatef(-WORLD_SIZE/2, -WORLD_SIZE/2, -WORLD_SIZE/2);
int i = 0;
while (i < cube_count) {
glPushMatrix();
glTranslatef(cubes_position[i*3], cubes_position[i*3+1], cubes_position[i*3+2]);
glColor3fv(&cubes_color[i*3]);
glutSolidCube(cubes_scale[i]);
glPopMatrix();
i++;
}
glPopMatrix();
/* Draw Point Sprites */
glDisable( GL_POINT_SPRITE_ARB );
glDisable( GL_TEXTURE_2D );
if (!display_head) cloud.render();
// Show field vectors
if (display_field) field_render();
// Render my own head
if (display_head) {
glPushMatrix();
glLoadIdentity();
glTranslatef(0.f, 0.f, -7.f);
myHead.render();
glPopMatrix();
}
// Render heads of other agents
if (!display_head) render_agents();
if (display_hand) myHand.render();
if (!display_head) balls.render();
// Render the world box
if (!display_head && stats_on) render_world_box();
//glm::vec3 test(0.5, 0.5, 0.5);
//render_vector(&test);
glPopMatrix();
// Render 2D overlay: I/O level bar graphs and text
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, WIDTH, HEIGHT, 0);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
//drawvec3(100, 100, 0.15, 0, 1.0, 0, myHead.getPos(), 0, 1, 0);
glPointParameterfvARB( GL_POINT_DISTANCE_ATTENUATION_ARB, pointer_attenuation_quadratic );
menu_items.draw(mouse_x, mouse_y, mouse_pressed);
if (mouse_pressed == 1)
{
if(mouse_x >200){
glPointSize( 10.0f );
glColor3f(1,1,1);
//glEnable(GL_POINT_SMOOTH);
glBegin(GL_POINTS);
glVertex2f(target_x, target_y);
glEnd();
char val[20];
sprintf(val, "%d,%d", target_x, target_y);
drawtext(target_x, target_y-20, 0.08, 0, 1.0, 0, val, 0, 1, 0);
}
}
if (display_head_mouse && !display_head && stats_on)
{
glPointSize(10.0f);
glColor4f(1.0, 1.0, 0.0, 0.8);
glEnable(GL_POINT_SMOOTH);
glBegin(GL_POINTS);
glVertex2f(head_mouse_x, head_mouse_y);
glEnd();
}
// Show detected levels from the serial I/O ADC channel sensors
if (display_levels)
{
int i;
int disp_x = 10;
const int GAP = 16;
char val[10];
for(i = 0; i < NUM_CHANNELS; i++)
{
// Actual value
glLineWidth(2.0);
glColor4f(1, 1, 1, 1);
glBegin(GL_LINES);
glVertex2f(disp_x, HEIGHT*0.95);
glVertex2f(disp_x, HEIGHT*(0.25 + 0.75f*adc_channels[i]/4096));
glEnd();
// Trailing Average value
glColor4f(1, 1, 0, 1);
glBegin(GL_LINES);
glVertex2f(disp_x + 2, HEIGHT*0.95);
glVertex2f(disp_x + 2, HEIGHT*(0.25 + 0.75f*avg_adc_channels[i]/4096));
glEnd();
glColor3f(1,0,0);
glBegin(GL_LINES);
glLineWidth(2.0);
glVertex2f(disp_x - 10, HEIGHT*0.5 - (adc_channels[i] - avg_adc_channels[i]));
glVertex2f(disp_x + 10, HEIGHT*0.5 - (adc_channels[i] - avg_adc_channels[i]));
glEnd();
sprintf(val, "%d", adc_channels[i]);
drawtext(disp_x-GAP/2, (HEIGHT*0.95)+2, 0.08, 90, 1.0, 0, val, 0, 1, 0);
disp_x += GAP;
}
// Display Serial latency block
if (latency_display && sensor_LED) {
glColor3f(1,0,0);
glBegin(GL_QUADS); {
glVertex2f(WIDTH - 100, HEIGHT - 100);
glVertex2f(WIDTH, HEIGHT - 100);
glVertex2f(WIDTH, HEIGHT);
glVertex2f(WIDTH - 100, HEIGHT);
}
glEnd();
}
}
if (stats_on) display_stats();
/* Render marker acquisition stuff */
pthread_mutex_lock(&frame_lock);
if(marker_capture_frame){
marker_acq_view.render(marker_capture_frame); // render the acquisition view, if it's visible.
}
// Draw marker images, if requested.
if (marker_capture_enabled && marker_capture_display && marker_capture_frame){
marker_capturer.glDrawIplImage(marker_capture_frame, WIDTH - 140, 10, 0.1, -0.1);
marker_capturer.glDrawIplImage(marker_capture_blob_frame, WIDTH - 280, 10, 0.1, -0.1);
}
pthread_mutex_unlock(&frame_lock);
/* Done rendering marker acquisition stuff */
glPopMatrix();
glutSwapBuffers();
framecount++;
}
void specialkey(int k, int x, int y)
{
if (k == GLUT_KEY_UP) fwd_vel += 0.05;
if (k == GLUT_KEY_DOWN) fwd_vel -= 0.05;
if (k == GLUT_KEY_LEFT) {
if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) lateral_vel -= 0.02;
else render_yaw_rate -= 0.25;
}
if (k == GLUT_KEY_RIGHT) {
if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) lateral_vel += 0.02;
else render_yaw_rate += 0.25;
}
}
void key(unsigned char k, int x, int y)
{
// Process keypresses
if (k == 'q') ::terminate();
// marker capture
if(k == 'x'){
printf("Toggling marker capture display.\n");
marker_capture_display = !marker_capture_display;
marker_capture_display ? marker_acq_view.show() : marker_acq_view.hide();
}
// delegate keypresses to the marker acquisition view when it's visible;
// override other key mappings by returning...
if(marker_acq_view.visible){
marker_acq_view.handle_key(k);
return;
}
if (k == '/') stats_on = !stats_on; // toggle stats
if (k == 'n')
{
noise_on = !noise_on; // Toggle noise
if (noise_on)
{
myHand.setNoise(noise);
myHead.setNoise(noise);
}
else
{
myHand.setNoise(0);
myHead.setNoise(0);
}
}
if (k == 'h') display_head = !display_head;
if (k == 'b') display_hand = !display_hand;
if (k == 'm') head_mirror = !head_mirror;
if (k == 'f') display_field = !display_field;
if (k == 'l') display_levels = !display_levels;
if (k == 'e') location[1] -= WORLD_SIZE/100.0;
if (k == 'c') location[1] += WORLD_SIZE/100.0;
if (k == 'w') fwd_vel += 0.05;
if (k == 's') fwd_vel -= 0.05;
if (k == ' ') reset_sensors();
if (k == 'a') render_yaw_rate -= 0.25;
if (k == 'd') render_yaw_rate += 0.25;
if (k == 'o') simulate_on = !simulate_on;
if (k == 'p')
{
// Add to field vector
float pos[] = {5,5,5};
float add[] = {0.001, 0.001, 0.001};
field_add(add, pos);
}
if ((k == 't') && (audio_on)) {
Audio::writeTone(0, 400, 1.0f, 0.5f);
}
if (k == '1')
{
myHead.SetNewHeadTarget((randFloat()-0.5)*20.0, (randFloat()-0.5)*20.0);
}
}
//
// Check for and process incoming network packets
//
void read_network()
{
// Receive packets
in_addr from_addr;
int bytes_recvd = network_receive(UDP_socket, &from_addr, incoming_packet, delay);
if (bytes_recvd > 0)
{
packetcount++;
bytescount += bytes_recvd;
// If packet is a Mouse data packet, copy it over
if (incoming_packet[0] == 'M') {
//
// mouse location packet
//
sscanf(incoming_packet, "M %d %d", &target_x, &target_y);
target_display = 1;
printf("X = %d Y = %d\n", target_x, target_y);
} else if (incoming_packet[0] == 'P') {
//
// Ping packet - check time and record
//
timeval check;
gettimeofday(&check, NULL);
ping_msecs = (float)diffclock(ping_start, check);
} else if (incoming_packet[0] == 'S') {
//
// Message from Spaceserver
//
update_agents(&incoming_packet[1], bytes_recvd - 1);
} else if (incoming_packet[0] == 'H') {
//
// Broadcast packet from another agent
//
update_agent(from_addr, &incoming_packet[1], bytes_recvd - 1);
}
}
}
void idle(void)
{
timeval check;
gettimeofday(&check, NULL);
// Check and render display frame
if (diffclock(last_frame,check) > RENDER_FRAME_MSECS)
{
// Simulation
update_pos(1.f/FPS);
if (simulate_on) {
field_simulate(1.f/FPS);
myHead.simulate(1.f/FPS);
myHand.simulate(1.f/FPS);
balls.simulate(1.f/FPS);
cloud.simulate(1.f/FPS);
}
if (!step_on) glutPostRedisplay();
last_frame = check;
// Every 30 frames or so, check ping time
ping_count++;
if (ping_count >= 30) {
ping_start = network_send_ping(UDP_socket);
ping_count = 0;
}
}
// Read network packets
read_network();
// Read serial data
if (serial_on) samplecount += read_sensors(0, &avg_adc_channels[0], &adc_channels[0],
&sensor_samples, &sensor_LED);
if (SLEEP)
{
usleep(SLEEP);
}
}
void reshape(int width, int height)
{
WIDTH = width;
HEIGHT = height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); //hello
glLoadIdentity();
gluPerspective(45, //view angle
1.0, //aspect ratio
0.1, //near clip
50.0);//far clip
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouseFunc( int button, int state, int x, int y )
{
if( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )
{
mouse_x = x;
mouse_y = y;
mouse_pressed = 1;
}
if( button == GLUT_LEFT_BUTTON && state == GLUT_UP )
{
mouse_x = x;
mouse_y = y;
mouse_pressed = 0;
}
}
void motionFunc( int x, int y)
{
mouse_x = x;
mouse_y = y;
if (mouse_pressed == 1)
{
// Send network packet containing mouse location
char mouse_string[20];
sprintf(mouse_string, "M %d %d\n", mouse_x, mouse_y);
network_send(UDP_socket, mouse_string, strlen(mouse_string));
}
}
void *poll_marker_capture(void *threadarg){
while(1){
marker_capturer.tick();
}
}
//these are functions called by the menus
void quit(){
exit(0);
}
void delay_none(){
delay=0;
}
void delay_50(){
delay=50;
}
void delay_100(){
delay=100;
}
void delay_200(){
delay=200;
}
int main(int argc, char** argv)
{
// Create network socket and buffer
UDP_socket = network_init();
if (UDP_socket) printf( "Created UDP socket.\n" );
incoming_packet = new char[MAX_PACKET_SIZE];
//initialize menu items
menu_items.init(80, 70);
menu_items.add_top_item((char*)"FILE");
menu_items.add_top_item((char*)"DELAY");
menu_items.add_menu_item(0, (char*)"QUIT", quit);
menu_items.add_menu_item(1, (char*)"None", delay_none);
menu_items.add_menu_item(1, (char*)"50 msecs", delay_50);
menu_items.add_menu_item(1, (char*)"50 msecs", delay_50);
menu_items.add_menu_item(1, (char*)"100msecs", delay_100);
menu_items.add_menu_item(1, (char*)"200msecs", delay_200);
// Test network loopback
in_addr from_addr;
char test_data[] = "Test!";
int bytes_sent = network_send(UDP_socket, test_data, 5);
if (bytes_sent) printf("%d bytes sent.", bytes_sent);
int test_recv = network_receive(UDP_socket, &from_addr, incoming_packet, delay);
printf("Received %i bytes\n", test_recv);
// Load textures