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