]> git.mxchange.org Git - flightgear.git/blob - Main/GLmain.c
Updated to reflect changing interfaces in other "modules."
[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     /* if ( Overrun ) {
169         printf("OVERRUN!!!\n");
170     } */
171
172     lastSimtime = Simtime;
173     signal(SIGALRM, fgTimerCatch);
174 }
175
176 /* this routine initializes the interval timer to generate a SIGALRM after
177  * the specified interval (dt) */
178 void fgTimerInit(float dt) {
179     int terr;
180     int isec;
181     float usec;
182
183     isec = (int) dt;
184     usec = 1000000* (dt - (float) isec);
185
186     t.it_interval.tv_sec = isec;
187     t.it_interval.tv_usec = usec;
188     t.it_value.tv_sec = isec;
189     t.it_value.tv_usec = usec;
190     /* printf("fgTimerInit() called\n"); */
191     fgTimerCatch();   /* set up for SIGALRM signal catch */
192     terr = setitimer( ITIMER_REAL, &t, &ot );
193     if (terr) perror("Error returned from setitimer");
194 }
195
196
197 /**************************************************************************
198  * Scenery management routines
199  **************************************************************************/
200
201 static void fgSceneryInit() {
202     /* make terrain mesh */
203     mesh = fgSceneryCompile();
204 }
205
206
207 /* create the terrain mesh */
208 GLint fgSceneryCompile() {
209     GLint mesh;
210
211     mesh = mesh2GL(mesh_ptr);
212
213     return(mesh);
214 }
215
216
217 /* draw the terrain mesh */
218 static void fgSceneryDraw() {
219     glCallList(mesh);
220 }
221
222
223 /* What should we do when we have nothing else to do?  How about get
224  * ready for the next move?*/
225 static void fgMainLoop( void )
226 {
227     fgSlewUpdate();
228     aircraft_debug(1);
229
230     fgUpdateVisuals();
231 }
232
233
234 /**************************************************************************
235  * Handle new window size or exposure
236  **************************************************************************/
237
238 static void fgReshape( int width, int height ) {
239     /* Do this so we can call fgReshape(0,0) ourselves without having to know
240      * what the values of width & height are. */
241     if ( (height > 0) && (width > 0) ) {
242         win_ratio = (GLfloat) height / (GLfloat) width;
243     }
244
245     /* Inform gl of our view window size */
246     glViewport(0, 0, (GLint)width, (GLint)height);
247
248     fgUpdateViewParams();
249     
250     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
251 }
252
253
254 /**************************************************************************
255  * Main ...
256  **************************************************************************/
257
258 int main( int argc, char *argv[] ) {
259     /* parse the scenery file */
260     parse_scenery(argv[1]);
261
262     #ifdef GLUT
263       /* initialize GLUT */
264       glutInit(&argc, argv);
265
266       /* Define Display Parameters */
267       glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
268
269       /* Define initial window size */
270       glutInitWindowSize(640, 400);
271
272       /* Initialize the main window */
273       glutCreateWindow("Terrain Demo");
274     #elif MESA_TK
275       /* Define initial window size */
276       tkInitPosition(0, 0, 640, 400);
277
278       /* Define Display Parameters */
279       tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT );
280
281       /* Initialize the main window */
282       if (tkInitWindow("Terrain Demo") == GL_FALSE) {
283           tkQuit();
284       }
285     #endif
286
287     /* setup view parameters, only makes GL calls */
288     fgInitVisuals();
289
290     /* Set initial position and slew parameters */
291     /* fgSlewInit(-398391.3, 120070.4, 244, 3.1415); */ /* GLOBE Airport */
292     /* fgSlewInit(-335340,162540, 15, 4.38); */
293     fgSlewInit(-398673.28,120625.64, 53, 4.38);
294
295     /* build all objects */
296     fgSceneryInit();
297
298     /* initialize timer */
299     fgTimerInit( 1.0 / DEFAULT_MODEL_HZ );
300
301     #ifdef GLUT
302       /* call fgReshape() on window resizes */
303       glutReshapeFunc( fgReshape );
304
305       /* call key() on keyboard event */
306       glutKeyboardFunc( GLUTkey );
307       glutSpecialFunc( GLUTkey );
308
309       /* call fgMainLoop() whenever there is nothing else to do */
310       glutIdleFunc( fgMainLoop );
311
312       /* draw the scene */
313       glutDisplayFunc( fgUpdateVisuals );
314
315       /* pass control off to the GLUT event handler */
316       glutMainLoop();
317     #elif MESA_TK
318       /* call fgReshape() on expose events */
319       tkExposeFunc( fgReshape );
320
321       /* call fgReshape() on window resizes */
322       tkReshapeFunc( fgReshape );
323
324       /* call key() on keyboard event */
325       tkKeyDownFunc( GLTKkey );
326
327       /* call fgMainLoop() whenever there is nothing else to do */
328       tkIdleFunc( fgMainLoop );
329
330       /* draw the scene */
331       tkDisplayFunc( fgUpdateVisuals );
332
333       /* pass control off to the tk event handler */
334       tkExec();
335     #endif
336
337     return(0);
338 }
339
340
341 /* $Log$
342 /* Revision 1.5  1997/05/29 02:33:23  curt
343 /* Updated to reflect changing interfaces in other "modules."
344 /*
345  * Revision 1.4  1997/05/27 17:44:31  curt
346  * Renamed & rearranged variables and routines.   Added some initial simple
347  * timer/alarm routines so the flight model can be updated on a regular 
348  * interval.
349  *
350  * Revision 1.3  1997/05/23 15:40:25  curt
351  * Added GNU copyright headers.
352  * Fog now works!
353  *
354  * Revision 1.2  1997/05/23 00:35:12  curt
355  * Trying to get fog to work ...
356  *
357  * Revision 1.1  1997/05/21 15:57:51  curt
358  * Renamed due to added GLUT support.
359  *
360  * Revision 1.3  1997/05/19 18:22:42  curt
361  * Parameter tweaking ... starting to stub in fog support.
362  *
363  * Revision 1.2  1997/05/17 00:17:34  curt
364  * Trying to stub in support for standard OpenGL.
365  *
366  * Revision 1.1  1997/05/16 16:05:52  curt
367  * Initial revision.
368  *
369  */