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