]> git.mxchange.org Git - flightgear.git/blob - Main/GLmain.c
Renamed due to added GLUT support.
[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  * $Id$
7  * (Log is kept at end of this file)
8  **************************************************************************/
9
10
11 #include <math.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/time.h>
15
16 #ifdef GLUT
17     #include <GL/glut.h>
18     #include "GLUTkey.h"
19 #elif MESA_TK
20     /* assumes -I/usr/include/mesa in compile command */
21     #include "gltk.h"
22     #include "GLTKkey.h"
23 #endif
24
25 #include "../aircraft/aircraft.h"
26 #include "../scenery/scenery.h"
27
28
29 /* This is a record containing all the info for the aircraft currently
30    being operated */
31 struct aircraft_params current_aircraft;
32
33 /* temporary hack */
34 extern struct mesh *mesh_ptr;
35
36 /* Function prototypes */
37 GLint make_mesh();
38 static void draw_mesh();
39
40
41 /* view parameters */
42 static GLfloat win_ratio = 1.0;
43
44 /* pointer to terrain mesh structure */
45 static GLint mesh;
46
47 /* init_view() -- Setup view parameters */
48 static void init_view() {
49     /* if the 4th field is 0.0, this specifies a direction ... */
50     static GLfloat pos[4] = {-3.0, 1.0, 3.0, 0.0 };
51     static GLfloat fogColor[4] = {0.5, 0.5, 0.5, 1.0};
52     
53     glLightfv( GL_LIGHT0, GL_POSITION, pos );
54     glEnable( GL_CULL_FACE );
55     glEnable( GL_LIGHTING );
56     glEnable( GL_LIGHT0 );
57     glEnable( GL_DEPTH_TEST );
58
59     glEnable( GL_FOG );
60     glFogi (GL_FOG_MODE, GL_LINEAR);
61     /* glFogf (GL_FOG_START, 1.0); */
62     glFogf (GL_FOG_END, 1000.0);
63     glFogfv (GL_FOG_COLOR, fogColor);
64     glFogf (GL_FOG_DENSITY, 0.04);
65     glHint(GL_FOG_HINT, GL_FASTEST);
66     
67     glClearColor(0.6, 0.6, 0.9, 1.0);
68 }
69
70
71 /* init_scene() -- build all objects */
72 static void init_scene() {
73
74     /* make terrain mesh */
75     mesh = make_mesh();
76
77     /* If enabled, normal vectors specified with glNormal are scaled
78        to unit length after transformation.  See glNormal. */
79     glEnable( GL_NORMALIZE );
80 }
81
82
83 /* create the terrain mesh */
84 GLint make_mesh() {
85     GLint mesh;
86
87     mesh = mesh_to_ogl(mesh_ptr);
88
89     return(mesh);
90 }
91
92
93 /* create the terrain mesh */
94 GLint make_mesh_old() {
95     GLint mesh;
96     static GLfloat color[4] = { 0.3, 0.7, 0.2, 1.0 };
97
98     mesh = glGenLists(1);
99     glNewList(mesh, GL_COMPILE);
100     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
101     glShadeModel( GL_FLAT ); /*  glShadeModel( GL_SMOOTH ); */
102
103     glBegin(GL_POLYGON);
104         glVertex3f(-10.0, -10.0, 0.0);
105         glVertex3f(0.0, -10.0, 0.0);
106         glVertex3f(0.0, 0.0, 1.0);
107         glVertex3f(-10.0, 0.0, 1.0);
108     glEnd();
109
110     glBegin(GL_POLYGON);
111         glVertex3f(-10.0, 0.0, 1.0);
112         glVertex3f(0.0, 0.0, 1.0);
113         glVertex3f(0.0, 10.0, 0.0);
114         glVertex3f(-10.0, 10.0, 0.0);
115     glEnd();
116
117     glBegin(GL_POLYGON);
118         glVertex3f(0.0, 0.0, 0.0);
119         glVertex3f(10.0, 0.0, 2.0);
120         glVertex3f(10.0, 10.0, 2.0);
121         glVertex3f(0.0, 10.0, 0.0);
122     glEnd();
123
124     glBegin(GL_POLYGON);
125         glVertex3f(0.0, -10.0, -1.0);
126         glVertex3f(10.0, -10.0, 0.0);
127         glVertex3f(10.0, 0.0, -1.0);
128         glVertex3f(0.0, 0.0, 0.0);
129     glEnd();
130
131     glEndList();
132
133     return(mesh);
134 }
135
136
137 /* update the view volume */
138 static void update_view() {
139     struct flight_params *f;
140
141     f = &current_aircraft.flight;
142
143     /* Tell GL we are about to modify the projection parameters */
144     glMatrixMode(GL_PROJECTION);
145     glLoadIdentity();
146
147     gluPerspective(45.0, 1.0/win_ratio, 1.0, 6000.0);
148     gluLookAt(f->pos_x, f->pos_y, f->pos_z,
149               f->pos_x + cos(f->Psi), f->pos_y + sin(f->Psi), f->pos_z,
150               0.0, 0.0, 1.0);
151 }
152
153
154 /* draw the scene */
155 static void draw_scene( void ) {
156     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
157
158     /* update view volume parameters */
159     update_view();
160
161     /* Tell GL we are switching to model view parameters */
162     glMatrixMode(GL_MODELVIEW);
163     glLoadIdentity();
164
165     /* glTranslatef(0.0, 0.0, -5.0); */
166
167     glPushMatrix();
168
169     /* draw terrain mesh */
170     draw_mesh();
171
172     glPopMatrix();
173
174     #ifdef GLUT
175       glutSwapBuffers();
176     #elif MESA_TK
177       tkSwapBuffers();
178     #endif
179 }
180
181
182 /* draw the terrain mesh */
183 static void draw_mesh() {
184     glCallList(mesh);
185 }
186
187
188 /* What should we do when we have nothing else to do?  How about get
189  * ready for the next move?*/
190 static void idle( void )
191 {
192     slew_update();
193     aircraft_debug(1);
194
195     draw_scene();
196 }
197
198
199 /* new window size or exposure */
200 static void reshape( int width, int height ) {
201     /* Do this so we can call reshape(0,0) ourselves without having to know
202      * what the values of width & height are. */
203     if ( (height > 0) && (width > 0) ) {
204         win_ratio = (GLfloat) height / (GLfloat) width;
205     }
206
207     /* Inform gl of our view window size */
208     glViewport(0, 0, (GLint)width, (GLint)height);
209
210     update_view();
211     
212     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
213 }
214
215
216 /**************************************************************************
217  * Main ...
218  **************************************************************************/
219
220 int main( int argc, char *argv[] ) {
221     /* parse the scenery file */
222     parse_scenery(argv[1]);
223
224     #ifdef GLUT
225       /* initialize GLUT */
226       glutInit(&argc, argv);
227
228       /* Define initial window size */
229       glutInitWindowSize(640, 400);
230
231       /* Define Display Parameters */
232       glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
233
234       /* Initialize the main window */
235       glutCreateWindow("Terrain Demo");
236     #elif MESA_TK
237       /* Define initial window size */
238       tkInitPosition(0, 0, 640, 400);
239
240       /* Define Display Parameters */
241       tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT );
242
243       /* Initialize the main window */
244       if (tkInitWindow("Terrain Demo") == GL_FALSE) {
245           tkQuit();
246       }
247     #endif
248
249     /* setup view parameters, only makes GL calls */
250     init_view();
251
252     /* build all objects */
253     init_scene();
254
255     /* Set initial position and slew parameters */
256     /* slew_init(-398391.3, 120070.4, 244, 3.1415); */ /* GLOBE Airport */
257     slew_init(-398673.28,120625.64, 53, 4.38);
258
259     #ifdef GLUT
260       /* call reshape() on window resizes */
261       glutReshapeFunc( reshape );
262
263       /* call key() on keyboard event */
264       glutKeyboardFunc( GLUTkey );
265       glutSpecialFunc( GLUTkey );
266
267       /* call idle() whenever there is nothing else to do */
268       glutIdleFunc( idle );
269
270       /* draw the scene */
271       glutDisplayFunc( draw_scene );
272
273       /* pass control off to the GLUT event handler */
274       glutMainLoop();
275     #elif MESA_TK
276       /* call reshape() on expose events */
277       tkExposeFunc( reshape );
278
279       /* call reshape() on window resizes */
280       tkReshapeFunc( reshape );
281
282       /* call key() on keyboard event */
283       tkKeyDownFunc( GLTKkey );
284
285       /* call idle() whenever there is nothing else to do */
286       tkIdleFunc( idle );
287
288       /* draw the scene */
289       tkDisplayFunc( draw_scene );
290
291       /* pass control off to the tk event handler */
292       tkExec();
293     #endif
294
295     return(0);
296 }
297
298
299 /* $Log$
300 /* Revision 1.1  1997/05/21 15:57:51  curt
301 /* Renamed due to added GLUT support.
302 /*
303  * Revision 1.3  1997/05/19 18:22:42  curt
304  * Parameter tweaking ... starting to stub in fog support.
305  *
306  * Revision 1.2  1997/05/17 00:17:34  curt
307  * Trying to stub in support for standard OpenGL.
308  *
309  * Revision 1.1  1997/05/16 16:05:52  curt
310  * Initial revision.
311  *
312  */