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