1 /**************************************************************************
2 * GLmain.c -- top level sim routines
4 * Written by Curtis Olson for OpenGL, started May 1997.
7 * (Log is kept at end of this file)
8 **************************************************************************/
20 /* assumes -I/usr/include/mesa in compile command */
25 #include "../aircraft/aircraft.h"
26 #include "../scenery/scenery.h"
29 /* This is a record containing all the info for the aircraft currently
31 struct aircraft_params current_aircraft;
34 extern struct mesh *mesh_ptr;
36 /* Function prototypes */
38 static void draw_mesh();
42 static GLfloat win_ratio = 1.0;
44 /* pointer to terrain mesh structure */
47 double fogDensity = 0.04;
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};
56 glEnable( GL_DEPTH_TEST );
57 glEnable( GL_CULL_FACE );
59 /* If enabled, normal vectors specified with glNormal are scaled
60 to unit length after transformation. See glNormal. */
61 glEnable( GL_NORMALIZE );
63 glLightfv( GL_LIGHT0, GL_POSITION, pos );
64 glEnable( GL_LIGHTING );
65 glEnable( GL_LIGHT0 );
67 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
68 glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */
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); */
78 glClearColor(0.6, 0.6, 0.9, 1.0);
82 /* init_scene() -- build all objects */
83 static void init_scene() {
85 /* make terrain mesh */
90 /* create the terrain mesh */
94 mesh = mesh_to_ogl(mesh_ptr);
100 /* update the view volume */
101 static void update_view() {
102 struct flight_params *f;
104 f = ¤t_aircraft.flight;
106 /* Tell GL we are about to modify the projection parameters */
107 glMatrixMode(GL_PROJECTION);
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,
118 static void draw_scene( void ) {
119 /* update view volume parameters */
122 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
124 /* Tell GL we are switching to model view parameters */
125 glMatrixMode(GL_MODELVIEW);
128 /* draw terrain mesh */
139 /* draw the terrain mesh */
140 static void draw_mesh() {
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 )
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;
164 /* Inform gl of our view window size */
165 glViewport(0, 0, (GLint)width, (GLint)height);
169 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
173 /**************************************************************************
175 **************************************************************************/
177 int main( int argc, char *argv[] ) {
178 /* parse the scenery file */
179 parse_scenery(argv[1]);
182 /* initialize GLUT */
183 glutInit(&argc, argv);
185 /* Define Display Parameters */
186 glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
188 /* Define initial window size */
189 glutInitWindowSize(640, 400);
191 /* Initialize the main window */
192 glutCreateWindow("Terrain Demo");
194 /* Define initial window size */
195 tkInitPosition(0, 0, 640, 400);
197 /* Define Display Parameters */
198 tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT );
200 /* Initialize the main window */
201 if (tkInitWindow("Terrain Demo") == GL_FALSE) {
206 /* setup view parameters, only makes GL calls */
209 /* build all objects */
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);
218 /* call reshape() on window resizes */
219 glutReshapeFunc( reshape );
221 /* call key() on keyboard event */
222 glutKeyboardFunc( GLUTkey );
223 glutSpecialFunc( GLUTkey );
225 /* call idle() whenever there is nothing else to do */
226 glutIdleFunc( idle );
229 glutDisplayFunc( draw_scene );
231 /* pass control off to the GLUT event handler */
234 /* call reshape() on expose events */
235 tkExposeFunc( reshape );
237 /* call reshape() on window resizes */
238 tkReshapeFunc( reshape );
240 /* call key() on keyboard event */
241 tkKeyDownFunc( GLTKkey );
243 /* call idle() whenever there is nothing else to do */
247 tkDisplayFunc( draw_scene );
249 /* pass control off to the tk event handler */
258 /* Revision 1.2 1997/05/23 00:35:12 curt
259 /* Trying to get fog to work ...
261 * Revision 1.1 1997/05/21 15:57:51 curt
262 * Renamed due to added GLUT support.
264 * Revision 1.3 1997/05/19 18:22:42 curt
265 * Parameter tweaking ... starting to stub in fog support.
267 * Revision 1.2 1997/05/17 00:17:34 curt
268 * Trying to stub in support for standard OpenGL.
270 * Revision 1.1 1997/05/16 16:05:52 curt