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