-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflight_sim.c
857 lines (653 loc) · 21.5 KB
/
flight_sim.c
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
/*
* Thabang Ngazimbi
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#ifdef WIN32
#include <windows.h>
#endif
#if defined (__APPLE__) || defined(MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include "readoff.h"
#include "readppm.h"
#include "glm.h"
/*****************************************************************/
/******************** MY FUNCTION DEFINITIONS ********************/
/*****************************************************************/
static void myInitGL(void);
static void myInitData(int *argc, char **argv);
static void myInitMenus (void);
static void myInitCheckers(void);
/* my callback functions needed for GLUT event handling */
static void myHandleMenu(int value);
static void myDisplay(void);
static void myReshape(int width, int height);
static void myMotion(int curx, int cury);
static void myButton(int button, int state, int x, int y);
static void myVisibility(int state);
static void myKeyboard(unsigned char key, int x, int y);
static void myMenus (int value);
static void timerCallback (int value);
void myRollBall (GLfloat pDeltaX, GLfloat pDeltaY, GLfloat *pModelMatrix );
void drawmodel(void);
void drawscene(void);
void drawcircle(float radius);
void pos_nose_camera(void);
void vec_mat_mult( GLfloat M[], GLfloat vec[], GLfloat out_vec[]);
void output(int x, int y, char *string);
#define Radius 10
float normal[3] = {1.0,0.0,0.0};
GLMmodel* pmodel = NULL;
GLMmodel* smodel = NULL;
/***************************************************************/
/******************** MY GLOBAL CONSTANTS **********************/
/***************************************************************/
#define MY_MAX(A,B) ((A) >= (B) ? (A) : (B))
/* my constants for GLUT window defaults */
#define MY_WIND_X_SIZE 600
#define MY_WIND_Y_SIZE 600
#define MY_WIND_X_POS 100
#define MY_WIND_Y_POS 100
/* my menu IDs for GLUT menu-handling functions */
#define MY_MENU_EXIT -9999
#define MY_MENU_LIGHT 30
#define MY_MENU_OBJECT 40
#define MY_ROT_LIGHT 50
#define MY_MENU_PROJECT 100
#define MY_CHECKERS_WIDTH 64
#define MY_CHECKERS_HEIGHT 64
/***************************************************************/
/******************** MY GLOBAL VARIABLES **********************/
/***************************************************************/
static GLubyte CheckersImg[MY_CHECKERS_HEIGHT][MY_CHECKERS_WIDTH][3];
static GLuint TextureName;
static GLboolean TextureOn = GL_TRUE;
static GLboolean key_f1 = GL_TRUE;
static GLboolean key_f2 = GL_TRUE;
static TexMap TMap; /* global for texture from readppm */
static GLfloat LightPos0[4];
static GLfloat LightPos1[4];
static GLfloat rot_angle = 0.0;
static GLfloat sunMatrix[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
static GLfloat planeMatrix[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
static GLfloat azumMatrix[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
GLfloat gRotationMatrix[16] = {1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
static GLboolean help_display = GL_FALSE;
/* user interaction */
static GLint InteractionMode;
static GLint XmouseDown, YmouseDown;
static GLint XmouseCurrent, YmouseCurrent;
static GLint XmousePrev, YmousePrev, DX, DY;
static GLint WindHeight, WindWidth;
static GLint KeybModifiers;
static GLint mouseButtons = 0;
/* transformations */
static GLdouble TX=0, TY=0, TZ=0;
/* 3D Projection */
static GLdouble XCamera, YCamera, ZCamera;
static GLdouble FOVangle, FOVratio, ClipNear, ClipFar;
/****************************camera struct*******************************/
typedef struct{
float pos[3];
float look_at[3];
float up[3];
}Camera;
Camera nose_camera ;
Camera global_camera ;
static float plane_pos[3] = { 0.0, 0.0, 0.0 };
static GLint pov = 1;
/**************************** MAIN *****************************/
int main(int argc, char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowPosition(MY_WIND_X_POS, MY_WIND_Y_POS);
glutInitWindowSize(MY_WIND_X_SIZE, MY_WIND_Y_SIZE);
glutCreateWindow("B481 Flight Simulator");
glutDisplayFunc (myDisplay);
glutReshapeFunc (myReshape);
glutMouseFunc (myButton);
glutMotionFunc (myMotion);
glutPassiveMotionFunc (NULL);
glutIdleFunc (NULL);
glutKeyboardFunc (myKeyboard);
glutVisibilityFunc (myVisibility);
fprintf(stderr,"Help: \n ESC or q: quit \n");
myInitGL();
myInitData(&argc,argv);
myInitMenus();
glutMainLoop();
return(EXIT_SUCCESS);
} /* main() */
/* --------------------------------------- */
/* initialize my data */
/* --------------------------------------- */
void myInitData(int *argc, char **argv) {
TX = 1.1; TY = 0.8; TZ = -2.0;
XmouseDown = -1;
YmouseDown = -1;
XmouseCurrent = XmousePrev = -1;
YmouseCurrent = YmousePrev = -1;
DX = DY = 0;
KeybModifiers = 0;
} /* myInitData() */
/***************************************************************/
/*********************** MOUSE CALLBACKS ***********************/
/***************************************************************/
/* ------------------------------------------------------------------------ */
/* Mouse Button UP/DOWN or DOWN/UP transition only */
/* ------------------------------------------------------------------------ */
void myButton(int button, int state, int x, int y) {
XmouseDown = x;
YmouseDown = WindHeight - y - 1; /* Invert to right-handed system */
/* This permits detection of left, middle, left+middle */
if (state == GLUT_DOWN)
mouseButtons |= button;
else
mouseButtons &= (~(button));
/* bitmask with GLUT_ACTIVE_SHIFT, GLUT_ACTIVE_CTRL, GLUT_ACTIVE_ALT */
KeybModifiers = glutGetModifiers();
if (button == GLUT_LEFT_BUTTON || button == GLUT_MIDDLE_BUTTON) {
if (state == GLUT_DOWN) {
XmouseCurrent = XmousePrev = XmouseDown;
YmouseCurrent = YmousePrev = YmouseDown;
} else {
/* at button up, invalidate: */
XmouseCurrent = XmousePrev = XmouseDown = -1;
YmouseCurrent = YmousePrev = YmouseDown = -1;
KeybModifiers = 0;
}
DX = DY = 0;
}
glutPostRedisplay();
} /* myButton() */
/* ------------------------------------------------------------------------ */
/* Mouse motion WHILE BUTTON DOWN */
/* COMPLAINS if you call glutGetModifiers() here!! */
/* ------------------------------------------------------------------------ */
void myMotion(int x, int y) {
float rMDeltaX = 0.0;
float rMDeltaY = 0.0;
XmouseCurrent = x;
YmouseCurrent = WindHeight - y - 1; /* Invert to right-handed system */
DX = XmouseCurrent - XmousePrev;
DY = YmouseCurrent - YmousePrev;
XmousePrev = XmouseCurrent;
YmousePrev = YmouseCurrent;
if(InteractionMode == MY_ROT_LIGHT){
rMDeltaX = DX * 0.05;
rMDeltaY = DY * 0.05;
myRollBall(rMDeltaX, rMDeltaY, gRotationMatrix);
}
glutPostRedisplay();
} /* myMotion(int x, int y) */
/* ------------------------------------------------------------------------ */
/* Handle Keyboard */
/* ------------------------------------------------------------------------ */
void myKeyboard(unsigned char key, int x, int y) {
/* Option: To force all lower case */
if (key >= 'A' && key <= 'Z') key = (key-'A') + 'a';
switch (key) {
case 'h':
if( help_display == GL_TRUE ){
help_display = GL_FALSE ;
}
else {
help_display = GL_TRUE ;
}
break;
case 'q' :
case 27 : /* 27 -> ESC */
fprintf(stderr,"Normal Exit.\n");
exit(EXIT_SUCCESS);
break;
case 'O':
case 'o':
if(key_f1){
key_f1 =GL_FALSE;
glDisable(GL_LIGHT0);
} else {
key_f1 = GL_TRUE;
glEnable(GL_LIGHT0);
}
break;
case 'P':
case 'p':
if(key_f2){
key_f2 =GL_FALSE;
glDisable(GL_LIGHT1);
} else {
key_f2 = GL_TRUE;
glEnable(GL_LIGHT1);
}
break;
case 'W':
case 'w':
TY += 0.01;
break;
case 'S':
case 's':
if(TY <= 0.5){
TY = 0.5;
}else{
TY -= 0.05;
}
break;
case 'A':
case 'a':
if( rot_angle >= 360.0 ){
rot_angle = 0.0 ;
}
rot_angle += 1.0;
break;
case 'D':
case 'd':
if(rot_angle <= 0.0){
rot_angle = 360.0 ;
}
rot_angle -= 1.0;
break;
case 'G':
case 'g':
pov = 1;
break;
case 'V':
case 'v':
pov = 2;
break;
case 'B':
case 'b':
pov = 3;
break;
case 'N':
case 'n':
pov = 4;
break;
default: fprintf(stderr,"Unhandled key: %c [%d] \n",key,key);
}
glutPostRedisplay();
} /* myKeyboard() */
/* -------------------------------------------------------------- */
/* myMenus() - callback for GLUT menus */
/* -------------------------------------------------------------- */
void myMenus (int value) {
switch(value) {
case MY_MENU_LIGHT:
InteractionMode = MY_MENU_LIGHT;
break;
case MY_MENU_OBJECT:
InteractionMode = MY_MENU_OBJECT;
break;
case MY_ROT_LIGHT:
InteractionMode = MY_ROT_LIGHT;
break;
case MY_MENU_PROJECT:
InteractionMode = MY_MENU_PROJECT;
break;
case MY_MENU_EXIT:
exit(0);
break;
default:
fprintf(stderr,"unknown menu entry");
} /* switch () */
glutPostRedisplay(); /* make sure we Display according to changes */
} /* myMenus() */
/* -------------------------------------------------------------- */
/* initialize GLUT menus */
/* -------------------------------------------------------------- */
void myInitMenus (void) {
glutCreateMenu(myMenus);
glutAddMenuEntry("Move Light", MY_MENU_LIGHT);
glutAddMenuEntry("Rotate Light", MY_ROT_LIGHT);
glutAddMenuEntry("Move Object", MY_MENU_OBJECT);
glutAddMenuEntry(" ", -3);
glutAddMenuEntry("Exit", MY_MENU_EXIT);
glutAttachMenu(GLUT_RIGHT_BUTTON);
} /* myInitMenus() */
/***************************************************************/
/************************** INIT GL ****************************/
/***************************************************************/
void myInitGL (void) {
GLfloat light_ambient0[] = {0.1,0.1,0.1, 1.0};
GLfloat light_diffuse0[] = {1.0,1.0,1.0, 1.0};
GLfloat light_specular0[] = {1.0,1.0,1.0, 1.0};
GLfloat light_ambient1[] = {0.1,0.1,0.0, 1.0};
GLfloat light_diffuse1[] = {.5,.5,0.0, 1.0};
GLfloat light_specular1[] = {0.5,0.5,0.0, 1.0};
int i;
/* projection pipeline initialization */
FOVangle = 90.0;
FOVratio = 1.0;
ClipNear = 0.1;
ClipFar = 1000.0;
XCamera = YCamera = 0.7;
ZCamera = 5.0;
WindWidth = glutGet(GLUT_WINDOW_WIDTH);
WindHeight = glutGet(GLUT_WINDOW_HEIGHT);
glViewport(0, 0, WindWidth, WindHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
FOVratio = (float)WindWidth / (float)WindHeight; /* keep viewed x/y ratio == 1 */
/* instead of glFrustum(-1.0*ratio, 1.0*ratio, -1.0,1.0, FLength, 25.0); */
gluPerspective(FOVangle, FOVratio, ClipNear, ClipFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* Colors, Lighting, Material initialization */
glClearColor( 0.4f, 0.6f, 0.5f, 1.0f);
glPointSize(1); glEnable(GL_POINT_SMOOTH);
glLineWidth(1); glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/* we do **not** want model materials to follow the current GL color: */
glDisable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH); /* or GL_FLAT */
glEnable(GL_NORMALIZE);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
/* light0 is the spot light */
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse0);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular0);
LightPos0[0] = -1.0; LightPos0[1] = 2.0;
LightPos0[2] = 0.5; LightPos0[3] = 1.0;
glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse1);
glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular1);
LightPos1[0] = 0.0; LightPos1[1] = 20.0;
LightPos1[2] = 0.0; LightPos1[3] = 1.0;
glLightfv(GL_LIGHT0, GL_POSITION, LightPos0);
glLightfv(GL_LIGHT1, GL_POSITION, LightPos1);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glutTimerFunc(25, timerCallback, 0);
i = ReadPPM("data/temp.ppm", &TMap);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TMap.texc, TMap.texr,
0, GL_RGB, GL_UNSIGNED_BYTE, TMap.texture);
} /* myInitGL() */
/***************************************************************/
/********************** DRAW FUNCTIONS *************************/
/***************************************************************/
void drawmodel(void){
if(!pmodel){
pmodel = glmReadOBJ("data/f-16.obj");
if(!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
}
void drawscene(void){
if(!smodel){
smodel = glmReadOBJ("data/scene.obj");
if(!smodel) exit(0);
glmUnitize(smodel);
glmFacetNormals(smodel);
glmVertexNormals(smodel, 90.0);
}
glmDraw(smodel,GLM_SMOOTH);
}
void drawcircle(float radius){
int i;
float vert[4];
float texC[2];
float angle = 2.0* 3.14/100;
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,1);
glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
glBegin(GL_TRIANGLE_FAN);
texC[0] = 0.5;
texC[1] = 0.5;
glTexCoord2fv(texC);
vert[0] = 0.0;
vert[1] = 0.0;
vert[2] = 0.0;
vert[3] = 1.0;
glVertex4fv(vert);
for(i = 0; i < 100; i++){
texC[0] = (cos(angle*i) + 1.0)/2;
texC[1] = (sin(angle*i) + 1.0)/2;
glTexCoord2fv(texC);
vert[0] = sin(angle*i) * radius;
vert[1] = cos(angle*i) * radius;
vert[2] = 0.0;
vert[3] = 1.0;
glVertex4fv(vert);
}
texC[0] = 1.0;
texC[1] = 0.5;
glTexCoord2fv(texC);
vert[0] = 0.0;
vert[1] = radius;
vert[2] = 0.0;
vert[3] = 1.0;
glVertex4fv(vert);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void vec_mat_mult( GLfloat M[], GLfloat vec[], GLfloat out_vec[]){
out_vec[0] = M[0]*vec[0] + M[4]*vec[1] + M[8]*vec[2] + M[12]*vec[3] ;
out_vec[1] = M[1]*vec[0] + M[5]*vec[1] + M[9]*vec[2] + M[13]*vec[3] ;
out_vec[2] = M[2]*vec[0] + M[6]*vec[1] + M[10]*vec[2] + M[14]*vec[3] ;
out_vec[3] = M[3]*vec[0] + M[7]*vec[1] + M[11]*vec[2] + M[15]*vec[3] ;
return;
}
void pos_nose_camera(void){
GLfloat M[16] ;
GLfloat start_pos[] = {0, 0, 0, 1 } ;
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glRotatef(rot_angle, 0, 1, 0);
glTranslatef( TX, TY, TZ) ;
glGetFloatv(GL_MODELVIEW_MATRIX, M );
vec_mat_mult( M, start_pos, nose_camera.pos );
glPopMatrix();
glPushMatrix();
glRotatef(rot_angle, 0, 1, 0);
glGetFloatv(GL_MODELVIEW_MATRIX, M );
start_pos[0] = nose_camera.look_at[0];
start_pos[1] = nose_camera.look_at[1];
start_pos[2] = nose_camera.look_at[2];
start_pos[3] = nose_camera.look_at[3];
vec_mat_mult( M, start_pos, nose_camera.look_at );
glPopMatrix();
return ;
}
void output(int x, int y, char *string)
{
int len, i;
glRasterPos2f(x, y);
len = (int) strlen(string);
for (i = 0; i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, string[i]);
}
}
/* ------------------------------------------------------------------------ */
void myDisplay(void) {
int wind_hi, wind_wid ;
wind_hi = glutGet(GLUT_WINDOW_HEIGHT) ;
wind_wid = glutGet(GLUT_WINDOW_WIDTH) ;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if( help_display ){
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0.0, glutGet(GLUT_WINDOW_WIDTH),
0.0, glutGet(GLUT_WINDOW_HEIGHT));
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor3f(1.0,0.0,0.0);
output( 20, wind_hi-120, "Navigation:");
output( 20, wind_hi-140, "'w' => UP 's' => DOWN");
output( 20, wind_hi-160, "'a' => LEFT 'd' => RIGHT");
output( 20, wind_hi-220, "View:");
output( 20, wind_hi-240, "'g' => GLOBAL 'v' => NOSE 'b' => TOP 'n' => JET" );
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix() ;
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(pov == 1 )
{
gluLookAt(0, 8, 0, 0, 0, -1, 0, 1, 0);
}
if(pov == 3)
{
gluLookAt(nose_camera.pos[0], nose_camera.pos[1] + 8, nose_camera.pos[2], nose_camera.pos[0], 0, nose_camera.pos[2], 0, 0, -1);
}
if(pov == 4)
{
gluLookAt( nose_camera.pos[0] + TX, nose_camera.pos[1] + (TY + 1), nose_camera.pos[2] - 0.5,
nose_camera.look_at[0], nose_camera.look_at[1], nose_camera.look_at[2],
0,1,0 );
}
if(pov == 2)
{
gluLookAt( nose_camera.pos[0], nose_camera.pos[1], nose_camera.pos[2],
nose_camera.look_at[0], nose_camera.look_at[1], nose_camera.look_at[2],
0,1,0 );
}
/* lights positioning */
if (InteractionMode == MY_MENU_LIGHT) {
/* move camera */
if ((KeybModifiers & GLUT_ACTIVE_SHIFT) == 0) {
LightPos0[0] = LightPos0[0] + 0.01*(GLdouble)DX;
LightPos0[1] = LightPos0[1] + 0.01*(GLdouble)DY;
} else {
LightPos0[0] = LightPos0[0] + 0.01*(GLdouble)DX;
LightPos0[2] = LightPos0[2] + 0.01*(GLdouble)DY;
}
}
glDisable(GL_LIGHTING);
glPushMatrix();
if (InteractionMode == MY_ROT_LIGHT)
{
glMultMatrixf(gRotationMatrix);
glLightfv(GL_LIGHT0, GL_POSITION, LightPos0);
}
/* draw a little sphere to mark the moon light's position: */
glTranslatef(LightPos0[0],LightPos0[1] + 2,LightPos0[2]);
glLightfv(GL_LIGHT0, GL_POSITION, LightPos0);
glColor3f(1.0,1.0,1.0);
glutSolidSphere(0.1, 50, 50);
glPopMatrix();
glEnable(GL_LIGHTING);
glDisable(GL_LIGHTING);
glPushMatrix();
glMultMatrixf(sunMatrix);
glTranslatef(LightPos1[0],LightPos1[1],LightPos1[2]);
glLightfv(GL_LIGHT1, GL_POSITION, LightPos1);
glColor3f(1.0,1.0,0.0);
glutSolidSphere(0.75, 50, 50);
glPopMatrix();
glEnable(GL_LIGHTING);
glPushMatrix();
glMultMatrixf(planeMatrix);
glTranslatef(plane_pos[0],plane_pos[1],plane_pos[2]);
glRotatef(rot_angle, 0, 1, 0);
glScalef(0.75, 0.75, 0.75);
drawmodel();
glPopMatrix();
glPushMatrix();
glScalef(15.0, 9.5, 15.5);
drawscene();
glPopMatrix();
glPushMatrix();
glTranslatef(-8, -1.35, -0.75);
glRotatef(90, -1, 0, 0);
drawcircle(5.5);
glPopMatrix();
glutSwapBuffers();
} /* myDisplay() */
/***************************************************************/
/*********************** GLUT FUNCTIONS ************************/
/***************************************************************/
/* ------------------------------------------------------------------------ */
void myReshape(int w, int h) {
w = MY_MAX(w,128);
h = MY_MAX(h,128);
WindHeight = h;
WindWidth = w;
glutReshapeWindow(w,h);
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
FOVratio = (float)w / (float)h; /* keep world coord x/y ratio == 1 */
gluPerspective(FOVangle, FOVratio, ClipNear, ClipFar);
glMatrixMode(GL_MODELVIEW);
} /* myReshape() */
/* ------------------------------------------------------------------------ */
void myVisibility(int state) {
/* ------------------------------------------------------------------------ */
if (state == GLUT_VISIBLE){
glutPostRedisplay();
}
} /* myVisibility() */
void myRollBall (GLfloat pDeltaX, GLfloat pDeltaY, GLfloat *pModelMatrix ) {
GLfloat rotateR = sqrt(pDeltaX*pDeltaX + pDeltaY*pDeltaY);
/* only rotate when there is a non-zero angle! */
if (rotateR > 0.01) {
normal[0] = -pDeltaY/rotateR;
normal[1] = pDeltaX/rotateR;
float theta = atan2(pDeltaY, pDeltaX);
theta = rotateR ;//theta*180.0/M_PI;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(theta, normal[0], normal[1], normal[2]);
glMultMatrixf(pModelMatrix);
glGetFloatv(GL_MODELVIEW_MATRIX, pModelMatrix);
}
glutPostRedisplay();
} /* myRollBall() */
#define DEGTORAD (3.14/180.0)
#define SPEED 0.05
static float sun_angle = 0.0 ;
static void timerCallback (int value) {
TX = SPEED * sin( rot_angle * DEGTORAD ) ;
TZ = SPEED * cos( rot_angle * DEGTORAD ) ;
plane_pos[0] += TX ;
plane_pos[1] = TY ;
plane_pos[2] += TZ ;
nose_camera.pos[0] += TX ;
nose_camera.pos[1] = TY ;
nose_camera.pos[2] += TZ ;
nose_camera.look_at[0] = plane_pos[0] + TX ;
nose_camera.look_at[1] = TY - .05;
nose_camera.look_at[2] = plane_pos[2] + TZ ;
glMatrixMode(GL_MODELVIEW);
glPushMatrix() ;
glLoadIdentity() ;
glTranslatef(TX, TY, TZ);
glGetFloatv(GL_MODELVIEW_MATRIX, planeMatrix);
glPopMatrix();
if(sun_angle >= 360){
sun_angle = 0.0;
} else {
sun_angle += 1.0;
}
glPushMatrix();
glLoadIdentity();
glRotatef(sun_angle, 0.0, 0.0, 1.0);
glGetFloatv(GL_MODELVIEW_MATRIX, sunMatrix);
glPopMatrix();
glutPostRedisplay();
glutTimerFunc(10, timerCallback, 0);
}
/* ------------------------------------------------------------------------ */