]> git.mxchange.org Git - flightgear.git/blob - Main/GLmain.c
WE CAN NOW FLY!!!
[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 #include <signal.h>    /* for timer routines */
31
32 #if defined(__WATCOMC__) || defined(__MSC__)
33 #  include <time.h>
34 #else
35 #  include <sys/time.h>
36 #endif
37
38 #ifdef GLUT
39     #include <GL/glut.h>
40     #include "GLUTkey.h"
41 #elif MESA_TK
42     /* assumes -I/usr/include/mesa in compile command */
43     #include "gltk.h"
44     #include "GLTKkey.h"
45 #endif
46
47 #include "../aircraft/aircraft.h"
48 #include "../scenery/scenery.h"
49 #include "../mat3/mat3.h"
50
51
52 #define FG_RAD_2_DEG(RAD) ((RAD) * 180.0 / M_PI)
53 #define FG_DEG_2_RAD(DEG) ((DEG) * M_PI / 180.0)
54
55 /* This is a record containing all the info for the aircraft currently
56    being operated */
57 struct aircraft_params current_aircraft;
58
59 /* view parameters */
60 static GLfloat win_ratio = 1.0;
61
62 /* sun direction */
63 static GLfloat sun_vec[4] = {-3.0, 1.0, 2.0, 0.0 };
64
65 /* temporary hack */
66 extern struct mesh *mesh_ptr;
67 /* Function prototypes */
68 GLint fgSceneryCompile();
69 static void fgSceneryDraw();
70 /* pointer to terrain mesh structure */
71 static GLint mesh;
72
73 /* Another hack */
74 double fogDensity = 2000.0;
75
76 /* Another hack */
77 #define DEFAULT_MODEL_HZ 20
78 #define DEFAULT_MULTILOOP 6
79 double Simtime;
80
81
82 /**************************************************************************
83  * fgInitVisuals() -- Initialize various GL/view parameters
84  **************************************************************************/
85
86 static void fgInitVisuals() {
87     /* if the 4th field is 0.0, this specifies a direction ... */
88     static GLfloat color[4] = { 0.3, 0.7, 0.2, 1.0 };
89     static GLfloat fogColor[4] = {0.65, 0.65, 0.85, 1.0};
90     
91     glEnable( GL_DEPTH_TEST );
92     glFrontFace(GL_CW);
93     glEnable( GL_CULL_FACE );
94
95     /* If enabled, normal vectors specified with glNormal are scaled
96        to unit length after transformation.  See glNormal. */
97     glEnable( GL_NORMALIZE );
98
99     glLightfv( GL_LIGHT0, GL_POSITION, sun_vec );
100     glEnable( GL_LIGHTING );
101     glEnable( GL_LIGHT0 );
102
103     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
104     glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */
105
106     glEnable( GL_FOG );
107     glFogi (GL_FOG_MODE, GL_LINEAR);
108     /* glFogf (GL_FOG_START, 1.0); */
109     glFogf (GL_FOG_END, fogDensity);
110     glFogfv (GL_FOG_COLOR, fogColor);
111     /* glFogf (GL_FOG_DENSITY, fogDensity); */
112     /* glHint (GL_FOG_HINT, GL_FASTEST); */
113
114     glClearColor(0.6, 0.6, 0.9, 1.0);
115 }
116
117
118 /**************************************************************************
119  * Update the view volume, position, and orientation
120  **************************************************************************/
121
122 static void fgUpdateViewParams() {
123     double pos_x, pos_y, pos_z;
124     struct flight_params *f;
125     MAT3mat R, tmp;
126     MAT3vec vec, forward, up;
127     MAT3hvec sun;
128
129     f = &current_aircraft.flight;
130
131     /* Tell GL we are about to modify the projection parameters */
132     glMatrixMode(GL_PROJECTION);
133     glLoadIdentity();
134     gluPerspective(45.0, 1.0/win_ratio, 1.0, 6000.0);
135
136     glMatrixMode(GL_MODELVIEW);
137     glLoadIdentity();
138     
139     /* calculate position in arc seconds */
140     pos_x = FG_RAD_2_DEG(FG_Longitude) * 3600.0;
141     pos_y = FG_RAD_2_DEG(FG_Latitude) * 3600.0;
142     pos_z = FG_Altitude * 0.01; /* (Convert feet to aproximate arcsecs) */
143
144     /* build current rotation matrix */
145     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
146     MAT3rotate(R, vec, FG_Phi);
147     /* printf("Roll matrix\n"); */
148     /* MAT3print(R, stdout); */
149
150     MAT3_SET_VEC(vec, 0.0, -1.0, 0.0);
151     MAT3rotate(tmp, vec, FG_Theta);
152     /* printf("Pitch matrix\n"); */
153     /* MAT3print(tmp, stdout); */
154     MAT3mult(R, R, tmp);
155
156     MAT3_SET_VEC(vec, 0.0, 0.0, -1.0);
157     MAT3rotate(tmp, vec, M_PI + M_PI_2 + FG_Psi );
158     /* printf("Yaw matrix\n");
159     MAT3print(tmp, stdout); */
160     MAT3mult(R, R, tmp);
161
162     /* MAT3print(R, stdout); */
163
164     /* generate the current forward and up vectors */
165     MAT3_SET_VEC(vec, 1.0, 0.0, 0.0);
166     MAT3mult_vec(forward, vec, R);
167     printf("Forward vector is (%.2f,%.2f,%.2f)\n", forward[0], forward[1], 
168            forward[2]);
169     MAT3_SET_VEC(vec, 0.0, 0.0, 1.0);
170     MAT3mult_vec(up, vec, R);
171
172     gluLookAt(pos_x, pos_y, pos_z,
173               pos_x + forward[0], pos_y + forward[1], pos_z + forward[2],
174               up[0], up[1], up[2]);
175
176     glLightfv( GL_LIGHT0, GL_POSITION, sun_vec );
177 }
178
179
180 /**************************************************************************
181  * Update all Visuals (redraws anything graphics related)
182  **************************************************************************/
183
184 static void fgUpdateVisuals( void ) {
185     /* update view volume parameters */
186     fgUpdateViewParams();
187
188     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
189
190     /* Tell GL we are switching to model view parameters */
191     glMatrixMode(GL_MODELVIEW);
192     /* glLoadIdentity(); */
193
194     /* draw terrain mesh */
195     fgSceneryDraw();
196
197     #ifdef GLUT
198       glutSwapBuffers();
199     #elif MESA_TK
200       tkSwapBuffers();
201     #endif
202 }
203
204
205 /**************************************************************************
206  * Timer management routines
207  **************************************************************************/
208
209 static struct itimerval t, ot;
210
211 /* This routine catches the SIGALRM */
212 void fgTimerCatch() {
213     struct flight_params *f;
214     static double lastSimtime = -99.9;
215     int Overrun;
216
217     /* ignore any SIGALRM's until we come back from our EOM iteration */
218     signal(SIGALRM, SIG_IGN);
219
220     f = &current_aircraft.flight;
221
222     /* printf("In fgTimerCatch()\n"); */
223
224     Overrun = (lastSimtime == Simtime);
225
226     /* add this back in when you get simtime working */
227     /* if ( Overrun ) {
228         printf("OVERRUN!!!\n");
229     } */
230
231     /* update the flight model */
232     fgFlightModelUpdate(FG_LARCSIM, f, DEFAULT_MULTILOOP);
233
234     lastSimtime = Simtime;
235     signal(SIGALRM, fgTimerCatch);
236 }
237
238 /* this routine initializes the interval timer to generate a SIGALRM after
239  * the specified interval (dt) */
240 void fgTimerInit(float dt) {
241     int terr;
242     int isec;
243     float usec;
244
245     isec = (int) dt;
246     usec = 1000000* (dt - (float) isec);
247
248     t.it_interval.tv_sec = isec;
249     t.it_interval.tv_usec = usec;
250     t.it_value.tv_sec = isec;
251     t.it_value.tv_usec = usec;
252     /* printf("fgTimerInit() called\n"); */
253     fgTimerCatch();   /* set up for SIGALRM signal catch */
254     terr = setitimer( ITIMER_REAL, &t, &ot );
255     if (terr) perror("Error returned from setitimer");
256 }
257
258
259 /**************************************************************************
260  * Scenery management routines
261  **************************************************************************/
262
263 static void fgSceneryInit() {
264     /* make terrain mesh */
265     mesh = fgSceneryCompile();
266 }
267
268
269 /* create the terrain mesh */
270 GLint fgSceneryCompile() {
271     GLint mesh;
272
273     mesh = mesh2GL(mesh_ptr);
274
275     return(mesh);
276 }
277
278
279 /* draw the terrain mesh */
280 static void fgSceneryDraw() {
281     glCallList(mesh);
282 }
283
284
285 /* What should we do when we have nothing else to do?  How about get
286  * ready for the next move?*/
287 static void fgMainLoop( void )
288 {
289     fgSlewUpdate();
290     aircraft_debug(1);
291
292     fgUpdateVisuals();
293 }
294
295
296 /**************************************************************************
297  * Handle new window size or exposure
298  **************************************************************************/
299
300 static void fgReshape( int width, int height ) {
301     /* Do this so we can call fgReshape(0,0) ourselves without having to know
302      * what the values of width & height are. */
303     if ( (height > 0) && (width > 0) ) {
304         win_ratio = (GLfloat) height / (GLfloat) width;
305     }
306
307     /* Inform gl of our view window size */
308     glViewport(0, 0, (GLint)width, (GLint)height);
309
310     fgUpdateViewParams();
311     
312     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
313 }
314
315
316 /**************************************************************************
317  * Main ...
318  **************************************************************************/
319
320 int main( int argc, char *argv[] ) {
321     struct flight_params *f;
322
323     f = &current_aircraft.flight;
324
325     /* parse the scenery file */
326     parse_scenery(argv[1]);
327
328     #ifdef GLUT
329       /* initialize GLUT */
330       glutInit(&argc, argv);
331
332       /* Define Display Parameters */
333       glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
334
335       /* Define initial window size */
336       glutInitWindowSize(640, 480);
337
338       /* Initialize the main window */
339       glutCreateWindow("Terrain Demo");
340     #elif MESA_TK
341       /* Define initial window size */
342       tkInitPosition(0, 0, 640, 480);
343
344       /* Define Display Parameters */
345       tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT );
346
347       /* Initialize the main window */
348       if (tkInitWindow("Terrain Demo") == GL_FALSE) {
349           tkQuit();
350       }
351     #endif
352
353     /* setup view parameters, only makes GL calls */
354     fgInitVisuals();
355
356     /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
357
358     /* Initial Position */
359     FG_Latitude  = FG_DEG_2_RAD(  120625.64 / 3600.0 );
360     FG_Longitude = FG_DEG_2_RAD( -398673.28 / 3600.0 );
361     FG_Altitude  = 3.758099E+00;
362
363     printf("Initial position is: (%.4f, %.4f, %.2f)\n", FG_Latitude, 
364            FG_Longitude, FG_Altitude);
365
366     /* Initial Velocity */
367     FG_V_north = 0.0 /*  7.287719E+00 */;
368     FG_V_east  = 0.0 /*  1.521770E+03 */;
369     FG_V_down  = 0.0 /* -1.265722E-05 */;
370
371     /* Initial Orientation */
372     FG_Phi   = -2.658474E-06;
373     FG_Theta =  7.401790E-03;
374     FG_Psi   =  2.14 /* 4.38 */;
375
376     /* Initial Angular B rates */
377     FG_P_body = 7.206685E-05;
378     FG_Q_body = 0.000000E+00;
379     FG_R_body = 9.492658E-05;
380
381     FG_Earth_position_angle = 0.000000E+00;
382
383     /* Mass properties and geometry values */
384     FG_Mass = 8.547270E+01;
385     FG_I_xx = 1.048000E+03;
386     FG_I_yy = 3.000000E+03;
387     FG_I_zz = 3.530000E+03;
388     FG_I_xz = 0.000000E+00;
389
390     /* CG position w.r.t. ref. point */
391     FG_Dx_cg = 0.000000E+00;
392     FG_Dy_cg = 0.000000E+00;
393     FG_Dz_cg = 0.000000E+00;
394
395     /* Set initial position and slew parameters */
396     /* fgSlewInit(-398391.3, 120070.4, 244, 3.1415); */ /* GLOBE Airport */
397     /* fgSlewInit(-335340,162540, 15, 4.38); */
398     /* fgSlewInit(-398673.28,120625.64, 53, 4.38); */
399
400     fgFlightModelInit(FG_LARCSIM, f, 1.0/(DEFAULT_MODEL_HZ*DEFAULT_MULTILOOP));
401
402     /* build all objects */
403     fgSceneryInit();
404
405     /* initialize timer */
406     fgTimerInit( 1.0 / DEFAULT_MODEL_HZ );
407
408     #ifdef GLUT
409       /* call fgReshape() on window resizes */
410       glutReshapeFunc( fgReshape );
411
412       /* call key() on keyboard event */
413       glutKeyboardFunc( GLUTkey );
414       glutSpecialFunc( GLUTspecialkey );
415
416       /* call fgMainLoop() whenever there is nothing else to do */
417       glutIdleFunc( fgMainLoop );
418
419       /* draw the scene */
420       glutDisplayFunc( fgUpdateVisuals );
421
422       /* pass control off to the GLUT event handler */
423       glutMainLoop();
424     #elif MESA_TK
425       /* call fgReshape() on expose events */
426       tkExposeFunc( fgReshape );
427
428       /* call fgReshape() on window resizes */
429       tkReshapeFunc( fgReshape );
430
431       /* call key() on keyboard event */
432       tkKeyDownFunc( GLTKkey );
433
434       /* call fgMainLoop() whenever there is nothing else to do */
435       tkIdleFunc( fgMainLoop );
436
437       /* draw the scene */
438       tkDisplayFunc( fgUpdateVisuals );
439
440       /* pass control off to the tk event handler */
441       tkExec();
442     #endif
443
444     return(0);
445 }
446
447
448 /* $Log$
449 /* Revision 1.10  1997/05/31 04:13:52  curt
450 /* WE CAN NOW FLY!!!
451 /*
452 /* Continuing work on the LaRCsim flight model integration.
453 /* Added some MSFS-like keyboard input handling.
454 /*
455  * Revision 1.9  1997/05/30 19:27:01  curt
456  * The LaRCsim flight model is starting to look like it is working.
457  *
458  * Revision 1.8  1997/05/30 03:54:10  curt
459  * Made a bit more progress towards integrating the LaRCsim flight model.
460  *
461  * Revision 1.7  1997/05/29 22:39:49  curt
462  * Working on incorporating the LaRCsim flight model.
463  *
464  * Revision 1.6  1997/05/29 12:31:39  curt
465  * Minor tweaks, moving towards general flight model integration.
466  *
467  * Revision 1.5  1997/05/29 02:33:23  curt
468  * Updated to reflect changing interfaces in other "modules."
469  *
470  * Revision 1.4  1997/05/27 17:44:31  curt
471  * Renamed & rearranged variables and routines.   Added some initial simple
472  * timer/alarm routines so the flight model can be updated on a regular 
473  * interval.
474  *
475  * Revision 1.3  1997/05/23 15:40:25  curt
476  * Added GNU copyright headers.
477  * Fog now works!
478  *
479  * Revision 1.2  1997/05/23 00:35:12  curt
480  * Trying to get fog to work ...
481  *
482  * Revision 1.1  1997/05/21 15:57:51  curt
483  * Renamed due to added GLUT support.
484  *
485  * Revision 1.3  1997/05/19 18:22:42  curt
486  * Parameter tweaking ... starting to stub in fog support.
487  *
488  * Revision 1.2  1997/05/17 00:17:34  curt
489  * Trying to stub in support for standard OpenGL.
490  *
491  * Revision 1.1  1997/05/16 16:05:52  curt
492  * Initial revision.
493  *
494  */