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