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