]> git.mxchange.org Git - flightgear.git/blob - Main/GLmain.c
23c5245a61d8400675cf41a9550216a0c427609f
[flightgear.git] / Main / GLmain.c
1 /**************************************************************************
2  * GLmain.c -- top level sim routines
3  *
4  * Written by Curtis Olson for OpenGL, started May 1997.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #include <math.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31
32 #ifdef GLUT
33     #include <GL/glut.h>
34     #include "GLUTkey.h"
35 #elif MESA_TK
36     /* assumes -I/usr/include/mesa in compile command */
37     #include "gltk.h"
38     #include "GLTKkey.h"
39 #endif
40
41 #include "../Aircraft/aircraft.h"
42 #include "../Scenery/scenery.h"
43 #include "../mat3/mat3.h"
44 #include "../Timer/fg_timer.h"
45
46
47 #define DEG_TO_RAD       0.017453292
48 #define RAD_TO_DEG       57.29577951
49
50 #ifndef PI2                                     
51 #define PI2  (M_PI + M_PI)
52 #endif                                                           
53
54 /* This is a record containing all the info for the aircraft currently
55    being operated */
56 struct aircraft_params current_aircraft;
57
58 /* view parameters */
59 static GLfloat win_ratio = 1.0;
60
61 /* sun direction */
62 static GLfloat sun_vec[4] = {-3.0, 1.0, 2.0, 0.0 };
63
64 /* temporary hack */
65 extern struct mesh *mesh_ptr;
66 /* Function prototypes */
67 GLint fgSceneryCompile();
68 static void fgSceneryDraw();
69
70 /* pointer to scenery structure */
71 static GLint scenery, runway;
72
73 /* Another hack */
74 double fogDensity = 2000.0;
75 double view_offset = 0.0;
76 double goal_view_offset = 0.0;
77
78 /* Another hack */
79 #define DEFAULT_TIMER_HZ 20
80 #define DEFAULT_MULTILOOP 6
81 #define DEFAULT_MODEL_HZ (DEFAULT_TIMER_HZ * DEFAULT_MULTILOOP)
82
83 double Simtime;
84
85 /* Another hack */
86 int use_signals = 0;
87
88
89 /**************************************************************************
90  * fgInitVisuals() -- Initialize various GL/view parameters
91  **************************************************************************/
92
93 static void fgInitVisuals() {
94     /* if the 4th field is 0.0, this specifies a direction ... */
95     static GLfloat fogColor[4] = {0.65, 0.65, 0.85, 1.0};
96     
97     glEnable( GL_DEPTH_TEST );
98     glFrontFace(GL_CW);
99     glEnable( GL_CULL_FACE );
100
101     /* If enabled, normal vectors specified with glNormal are scaled
102        to unit length after transformation.  See glNormal. */
103     glEnable( GL_NORMALIZE );
104
105     glLightfv( GL_LIGHT0, GL_POSITION, sun_vec );
106     glEnable( GL_LIGHTING );
107     glEnable( GL_LIGHT0 );
108
109     glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */
110
111     glEnable( GL_FOG );
112     glFogi (GL_FOG_MODE, GL_LINEAR);
113     /* glFogf (GL_FOG_START, 1.0); */
114     glFogf (GL_FOG_END, fogDensity);
115     glFogfv (GL_FOG_COLOR, fogColor);
116     /* glFogf (GL_FOG_DENSITY, fogDensity); */
117     /* glHint (GL_FOG_HINT, GL_FASTEST); */
118
119     glClearColor(0.6, 0.6, 0.9, 1.0);
120 }
121
122
123 /**************************************************************************
124  * Update the view volume, position, and orientation
125  **************************************************************************/
126
127 static void fgUpdateViewParams() {
128     double pos_x, pos_y, pos_z;
129     struct flight_params *f;
130     MAT3mat R, TMP;
131     MAT3vec vec, up, forward, fwrd_view;
132
133     f = &current_aircraft.flight;
134
135     /* Tell GL we are about to modify the projection parameters */
136     glMatrixMode(GL_PROJECTION);
137     glLoadIdentity();
138     gluPerspective(45.0, 1.0/win_ratio, 0.01, 6000.0);
139
140     glMatrixMode(GL_MODELVIEW);
141     glLoadIdentity();
142     
143     /* calculate position in arc seconds */
144     pos_x = (FG_Longitude * RAD_TO_DEG) * 3600.0;
145     pos_y = (FG_Latitude   * RAD_TO_DEG) * 3600.0;
146     pos_z = FG_Altitude * 0.01; /* (Convert feet to aproximate arcsecs) */
147
148     printf("*** pos_z = %.2f\n", pos_z);
149
150     /* build current rotation matrix */
151     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
152     MAT3rotate(R, vec, FG_Phi);
153     /* printf("Roll matrix\n"); */
154     /* MAT3print(R, stdout); */
155
156     MAT3_SET_VEC(vec, 0.0, 1.0, 0.0);
157     /* MAT3mult_vec(vec, vec, R); */
158     MAT3rotate(TMP, vec, -FG_Theta);
159     /* printf("Pitch matrix\n"); */
160     /* MAT3print(TMP, stdout); */
161     MAT3mult(R, R, TMP);
162
163     MAT3_SET_VEC(vec, 0.0, 0.0, -1.0);
164     /* MAT3mult_vec(vec, vec, R); */
165     /* MAT3rotate(TMP, vec, M_PI + M_PI_2 + FG_Psi + view_offset); */
166     MAT3rotate(TMP, vec, FG_Psi - M_PI_2);
167     /* printf("Yaw matrix\n");
168     MAT3print(TMP, stdout); */
169     MAT3mult(R, R, TMP);
170
171     /* MAT3print(R, stdout); */
172
173     /* generate the current up, forward, and fwrd-view vectors */
174     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
175     MAT3mult_vec(up, vec, R);
176
177     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
178     MAT3mult_vec(forward, vec, R);
179     printf("Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1], 
180            forward[2]);
181
182     MAT3rotate(TMP, up, view_offset);
183     MAT3mult_vec(fwrd_view, forward, TMP);
184
185     gluLookAt(pos_x, pos_y, pos_z,
186               pos_x + fwrd_view[0], pos_y + fwrd_view[1], pos_z + fwrd_view[2],
187               up[0], up[1], up[2]);
188
189     glLightfv( GL_LIGHT0, GL_POSITION, sun_vec );
190 }
191
192
193 /**************************************************************************
194  * Update all Visuals (redraws anything graphics related)
195  **************************************************************************/
196
197 static void fgUpdateVisuals( void ) {
198     /* update view volume parameters */
199     fgUpdateViewParams();
200
201     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
202
203     /* Tell GL we are switching to model view parameters */
204     glMatrixMode(GL_MODELVIEW);
205     /* glLoadIdentity(); */
206
207     /* draw scenery */
208     fgSceneryDraw();
209
210     #ifdef GLUT
211       glutSwapBuffers();
212     #elif MESA_TK
213       tkSwapBuffers();
214     #endif
215 }
216
217
218 /**************************************************************************
219  * Update internal time dependent calculations (i.e. flight model)
220  **************************************************************************/
221
222 void fgUpdateTimeDepCalcs(int multi_loop) {
223     struct flight_params *f;
224     int i;
225
226     f = &current_aircraft.flight;
227
228     /* update the flight model */
229     if ( multi_loop < 0 ) {
230         multi_loop = DEFAULT_MULTILOOP;
231     }
232
233     /* printf("updating flight model x %d\n", multi_loop); */
234     fgFlightModelUpdate(FG_LARCSIM, f, multi_loop);
235
236     for ( i = 0; i < multi_loop; i++ ) {
237         if ( fabs(goal_view_offset - view_offset) < 0.05 ) {
238             view_offset = goal_view_offset;
239             break;
240         } else {
241             /* move view_offset towards goal_view_offset */
242             if ( goal_view_offset > view_offset ) {
243                 if ( goal_view_offset - view_offset < M_PI ) {
244                     view_offset += 0.01;
245                 } else {
246                     view_offset -= 0.01;
247                 }
248             } else {
249                 if ( view_offset - goal_view_offset < M_PI ) {
250                     view_offset -= 0.01;
251                 } else {
252                     view_offset += 0.01;
253                 }
254             }
255             if ( view_offset > PI2 ) {
256                 view_offset -= PI2;
257             } else if ( view_offset < 0 ) {
258                 view_offset += PI2;
259             }
260         }
261     }
262 }
263
264
265 void fgInitTimeDepCalcs() {
266     /* initialize timer */
267     fgTimerInit( 1.0 / DEFAULT_TIMER_HZ, fgUpdateTimeDepCalcs );
268 }
269
270
271 /**************************************************************************
272  * Scenery management routines
273  **************************************************************************/
274
275 static void fgSceneryInit() {
276     /* make scenery */
277     scenery = fgSceneryCompile();
278     runway = fgRunwayHack(0.69, 53.07);
279 }
280
281
282 /* create the scenery */
283 GLint fgSceneryCompile() {
284     GLint scenery;
285
286     scenery = mesh2GL(mesh_ptr);
287
288     return(scenery);
289 }
290
291
292 /* hack in a runway */
293 GLint fgRunwayHack(double width, double length) {
294     static GLfloat concrete[4] = { 0.5, 0.5, 0.5, 1.0 };
295     static GLfloat line[4]     = { 0.9, 0.9, 0.9, 1.0 };
296     int i;
297     int num_lines = 16;
298     float line_len, line_width_2, cur_pos;
299
300     runway = glGenLists(1);
301     glNewList(runway, GL_COMPILE);
302
303     /* draw concrete */
304     glBegin(GL_POLYGON);
305     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, concrete );
306     glNormal3f(0.0, 0.0, 1.0);
307
308     glVertex3d( 0.0,   -width/2.0, 0.0);
309     glVertex3d( 0.0,    width/2.0, 0.0);
310     glVertex3d(length,  width/2.0, 0.0);
311     glVertex3d(length, -width/2.0, 0.0);
312     glEnd();
313
314     /* draw center line */
315     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, line );
316     line_len = length / ( 2 * num_lines + 1);
317     printf("line_len = %.3f\n", line_len);
318     line_width_2 = 0.02;
319     cur_pos = line_len;
320     for ( i = 0; i < num_lines; i++ ) {
321         glBegin(GL_POLYGON);
322         glVertex3d( cur_pos, -line_width_2, 0.005);
323         glVertex3d( cur_pos,  line_width_2, 0.005);
324         cur_pos += line_len;
325         glVertex3d( cur_pos,  line_width_2, 0.005);
326         glVertex3d( cur_pos, -line_width_2, 0.005);
327         cur_pos += line_len;
328         glEnd();
329     }
330
331     glEndList();
332
333     return(runway);
334 }
335
336
337 /* draw the scenery */
338 static void fgSceneryDraw() {
339     static float z = 32.35;
340
341     glPushMatrix();
342
343     glCallList(scenery);
344
345     printf("*** Drawing runway at %.2f\n", z);
346
347     glTranslatef( -398391.28, 120070.41, 32.35);
348     glRotatef(170.0, 0.0, 0.0, 1.0);
349     glCallList(runway);
350
351     glPopMatrix();
352 }
353
354
355 /* What should we do when we have nothing else to do?  How about get
356  * ready for the next move?*/
357 static void fgMainLoop( void ) {
358     static int remainder = 0;
359     int elapsed, multi_loop;
360
361     elapsed = fgGetTimeInterval();
362     printf("Time interval is = %d, previous remainder is = %d\n", elapsed, 
363            remainder);
364     printf("--> Frame rate is = %.2f\n", 1000.0 / (float)elapsed);
365     elapsed += remainder;
366
367     multi_loop = ((float)elapsed * 0.001) * DEFAULT_MODEL_HZ;
368     remainder = elapsed - ((multi_loop*1000) / DEFAULT_MODEL_HZ);
369     printf("Model iterations needed = %d, new remainder = %d\n", multi_loop, 
370            remainder);
371
372     aircraft_debug(1);
373     fgUpdateVisuals();
374
375     if ( ! use_signals ) {
376         fgUpdateTimeDepCalcs(multi_loop);
377     }
378 }
379
380
381 /**************************************************************************
382  * Handle new window size or exposure
383  **************************************************************************/
384
385 static void fgReshape( int width, int height ) {
386     /* Do this so we can call fgReshape(0,0) ourselves without having to know
387      * what the values of width & height are. */
388     if ( (height > 0) && (width > 0) ) {
389         win_ratio = (GLfloat) height / (GLfloat) width;
390     }
391
392     /* Inform gl of our view window size */
393     glViewport(0, 0, (GLint)width, (GLint)height);
394
395     fgUpdateViewParams();
396     
397     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
398 }
399
400
401 /**************************************************************************
402  * Main ...
403  **************************************************************************/
404
405 int main( int argc, char *argv[] ) {
406     struct flight_params *f;
407
408     f = &current_aircraft.flight;
409
410     #ifdef GLUT
411       /* initialize GLUT */
412       glutInit(&argc, argv);
413
414       /* Define Display Parameters */
415       glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
416
417       /* Define initial window size */
418       glutInitWindowSize(640, 480);
419
420       /* Initialize the main window */
421       glutCreateWindow("Flight Gear");
422     #elif MESA_TK
423       /* Define initial window size */
424       tkInitPosition(0, 0, 640, 480);
425
426       /* Define Display Parameters */
427       tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT );
428
429       /* Initialize the main window */
430       if (tkInitWindow("Flight Gear") == GL_FALSE) {
431           tkQuit();
432       }
433     #endif
434
435     /* setup view parameters, only makes GL calls */
436     fgInitVisuals();
437
438     /* Globe Aiport, AZ */
439     FG_Runway_altitude = 3234.5;
440     FG_Runway_latitude = 120070.41;
441     FG_Runway_longitude = -398391.28;
442     FG_Runway_heading = 102.0 * DEG_TO_RAD;
443
444     /* Initial Position */
445     FG_Latitude  = (  120070.41 / 3600.0 ) * DEG_TO_RAD;
446     FG_Longitude = ( -398391.28 / 3600.0 ) * DEG_TO_RAD;
447     FG_Altitude  = FG_Runway_altitude + 3.758099;
448
449     printf("Initial position is: (%.4f, %.4f, %.2f)\n", FG_Latitude, 
450            FG_Longitude, FG_Altitude);
451
452   
453     /* Initial Velocity */
454     FG_V_north = 0.0 /*  7.287719E+00 */;
455     FG_V_east  = 0.0 /*  1.521770E+03 */;
456     FG_V_down  = 0.0 /* -1.265722E-05 */;
457
458     /* Initial Orientation */
459     FG_Phi   = -2.658474E-06;
460     FG_Theta =  7.401790E-03;
461     FG_Psi   =  282.0 * DEG_TO_RAD;
462
463     /* Initial Angular B rates */
464     FG_P_body = 7.206685E-05;
465     FG_Q_body = 0.000000E+00;
466     FG_R_body = 9.492658E-05;
467
468     FG_Earth_position_angle = 0.000000E+00;
469
470     /* Mass properties and geometry values */
471     FG_Mass = 8.547270E+01;
472     FG_I_xx = 1.048000E+03;
473     FG_I_yy = 3.000000E+03;
474     FG_I_zz = 3.530000E+03;
475     FG_I_xz = 0.000000E+00;
476
477     /* CG position w.r.t. ref. point */
478     FG_Dx_cg = 0.000000E+00;
479     FG_Dy_cg = 0.000000E+00;
480     FG_Dz_cg = 0.000000E+00;
481
482     /* Set initial position and slew parameters */
483     /* fgSlewInit(-398391.3, 120070.41, 244, 3.1415); */ /* GLOBE Airport */
484     /* fgSlewInit(-335340,162540, 15, 4.38); */
485     /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
486
487     fgFlightModelInit( FG_LARCSIM, f, 1.0 / DEFAULT_MODEL_HZ );
488
489     if ( use_signals ) {
490         /* init timer routines, signals, etc.  Arrange for an alarm
491            signal to be generated, etc. */
492         fgInitTimeDepCalcs();
493     }
494
495     /* build all objects */
496
497     /* parse the scenery file, and build the OpenGL call list */
498     /* this function will eventually move to the scenery management system */
499     if ( strlen(argv[1]) ) {
500         parse_scenery(argv[1]);
501     }
502
503     /* initialize the scenery */
504     fgSceneryInit();
505
506     #ifdef GLUT
507       /* call fgReshape() on window resizes */
508       glutReshapeFunc( fgReshape );
509
510       /* call key() on keyboard event */
511       glutKeyboardFunc( GLUTkey );
512       glutSpecialFunc( GLUTspecialkey );
513
514       /* call fgMainLoop() whenever there is nothing else to do */
515       glutIdleFunc( fgMainLoop );
516
517       /* draw the scene */
518       glutDisplayFunc( fgUpdateVisuals );
519
520       /* pass control off to the GLUT event handler */
521       glutMainLoop();
522     #elif MESA_TK
523       /* call fgReshape() on expose events */
524       tkExposeFunc( fgReshape );
525
526       /* call fgReshape() on window resizes */
527       tkReshapeFunc( fgReshape );
528
529       /* call key() on keyboard event */
530       tkKeyDownFunc( GLTKkey );
531
532       /* call fgMainLoop() whenever there is nothing else to do */
533       tkIdleFunc( fgMainLoop );
534
535       /* draw the scene */
536       tkDisplayFunc( fgUpdateVisuals );
537
538       /* pass control off to the tk event handler */
539       tkExec();
540     #endif
541
542     return(0);
543 }
544
545
546 /* $Log$
547 /* Revision 1.21  1997/06/22 21:44:41  curt
548 /* Working on intergrating the VRML (subset) parser.
549 /*
550  * Revision 1.20  1997/06/21 17:12:53  curt
551  * Capitalized subdirectory names.
552  *
553  * Revision 1.19  1997/06/18 04:10:31  curt
554  * A couple more runway tweaks ...
555  *
556  * Revision 1.18  1997/06/18 02:21:24  curt
557  * Hacked in a runway
558  *
559  * Revision 1.17  1997/06/17 16:51:58  curt
560  * Timer interval stuff now uses gettimeofday() instead of ftime()
561  *
562  * Revision 1.16  1997/06/17 04:19:16  curt
563  * More timer related tweaks with respect to view direction changes.
564  *
565  * Revision 1.15  1997/06/17 03:41:10  curt
566  * Nonsignal based interval timing is now working.
567  * This would be a good time to look at cleaning up the code structure a bit.
568  *
569  * Revision 1.14  1997/06/16 19:32:51  curt
570  * Starting to add general timer support.
571  *
572  * Revision 1.13  1997/06/02 03:40:06  curt
573  * A tiny bit more view tweaking.
574  *
575  * Revision 1.12  1997/06/02 03:01:38  curt
576  * Working on views (side, front, back, transitions, etc.)
577  *
578  * Revision 1.11  1997/05/31 19:16:25  curt
579  * Elevator trim added.
580  *
581  * Revision 1.10  1997/05/31 04:13:52  curt
582  * WE CAN NOW FLY!!!
583  *
584  * Continuing work on the LaRCsim flight model integration.
585  * Added some MSFS-like keyboard input handling.
586  *
587  * Revision 1.9  1997/05/30 19:27:01  curt
588  * The LaRCsim flight model is starting to look like it is working.
589  *
590  * Revision 1.8  1997/05/30 03:54:10  curt
591  * Made a bit more progress towards integrating the LaRCsim flight model.
592  *
593  * Revision 1.7  1997/05/29 22:39:49  curt
594  * Working on incorporating the LaRCsim flight model.
595  *
596  * Revision 1.6  1997/05/29 12:31:39  curt
597  * Minor tweaks, moving towards general flight model integration.
598  *
599  * Revision 1.5  1997/05/29 02:33:23  curt
600  * Updated to reflect changing interfaces in other "modules."
601  *
602  * Revision 1.4  1997/05/27 17:44:31  curt
603  * Renamed & rearranged variables and routines.   Added some initial simple
604  * timer/alarm routines so the flight model can be updated on a regular 
605  * interval.
606  *
607  * Revision 1.3  1997/05/23 15:40:25  curt
608  * Added GNU copyright headers.
609  * Fog now works!
610  *
611  * Revision 1.2  1997/05/23 00:35:12  curt
612  * Trying to get fog to work ...
613  *
614  * Revision 1.1  1997/05/21 15:57:51  curt
615  * Renamed due to added GLUT support.
616  *
617  * Revision 1.3  1997/05/19 18:22:42  curt
618  * Parameter tweaking ... starting to stub in fog support.
619  *
620  * Revision 1.2  1997/05/17 00:17:34  curt
621  * Trying to stub in support for standard OpenGL.
622  *
623  * Revision 1.1  1997/05/16 16:05:52  curt
624  * Initial revision.
625  *
626  */