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