]> git.mxchange.org Git - flightgear.git/blob - Main/gltkmain.c
Initial revision.
[flightgear.git] / Main / gltkmain.c
1 /**************************************************************************
2  * gltkmain.c -- top level sim routines
3  *
4  * Written by Curtis Olson for Mesa (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 /* assumes -I/usr/include/mesa in compile command */
17 #include "gltk.h"
18
19 #include "gltkkey.h"
20 #include "../aircraft/aircraft.h"
21 #include "../scenery/scenery.h"
22
23
24 /* This a record containing all the info for the aircraft currently
25    being operated */
26 struct aircraft_params current_aircraft;
27
28 /* temporary hack */
29 extern struct mesh *mesh_ptr;
30
31 /* Function prototypes */
32 GLint make_mesh();
33 static void draw_mesh();
34
35
36 /* view parameters */
37 static GLfloat win_ratio = 1.0;
38
39 /* pointer to terrain mesh structure */
40 static GLint mesh;
41
42 /* init_view() -- Setup view parameters */
43 static void init_view() {
44     /* if the 4th field is 0.0, this specifies a direction ... */
45     static GLfloat pos[4] = {2.0, 2.0, 3.0, 0.0 };
46     
47     glLightfv( GL_LIGHT0, GL_POSITION, pos );
48     glEnable( GL_CULL_FACE );
49     glEnable( GL_LIGHTING );
50     glEnable( GL_LIGHT0 );
51     glEnable( GL_DEPTH_TEST );
52     /* glEnable( GL_FOG );
53     glFog( GL_FOG_MODE, GL_LINEAR );
54     glFogf( GL_FOG_END, 1000.0 ); */
55     glClearColor(0.6, 0.6, 0.9, 1.0);
56 }
57
58
59 /* init_scene() -- build all objects */
60 static void init_scene() {
61
62     /* make terrain mesh */
63     mesh = make_mesh();
64
65     /* If enabled, normal vectors specified with glNormal are scaled
66        to unit length after transformation.  See glNormal. */
67     glEnable( GL_NORMALIZE );
68 }
69
70
71 /* create the terrain mesh */
72 GLint make_mesh() {
73     GLint mesh;
74
75     mesh = mesh_to_ogl(mesh_ptr);
76
77     return(mesh);
78 }
79
80
81 /* create the terrain mesh */
82 GLint make_mesh_old() {
83     GLint mesh;
84     static GLfloat color[4] = { 0.3, 0.7, 0.2, 1.0 };
85
86     mesh = glGenLists(1);
87     glNewList(mesh, GL_COMPILE);
88     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
89     glShadeModel( GL_FLAT ); /*  glShadeModel( GL_SMOOTH ); */
90
91     glBegin(GL_POLYGON);
92         glVertex3f(-10.0, -10.0, 0.0);
93         glVertex3f(0.0, -10.0, 0.0);
94         glVertex3f(0.0, 0.0, 1.0);
95         glVertex3f(-10.0, 0.0, 1.0);
96     glEnd();
97
98     glBegin(GL_POLYGON);
99         glVertex3f(-10.0, 0.0, 1.0);
100         glVertex3f(0.0, 0.0, 1.0);
101         glVertex3f(0.0, 10.0, 0.0);
102         glVertex3f(-10.0, 10.0, 0.0);
103     glEnd();
104
105     glBegin(GL_POLYGON);
106         glVertex3f(0.0, 0.0, 0.0);
107         glVertex3f(10.0, 0.0, 2.0);
108         glVertex3f(10.0, 10.0, 2.0);
109         glVertex3f(0.0, 10.0, 0.0);
110     glEnd();
111
112     glBegin(GL_POLYGON);
113         glVertex3f(0.0, -10.0, -1.0);
114         glVertex3f(10.0, -10.0, 0.0);
115         glVertex3f(10.0, 0.0, -1.0);
116         glVertex3f(0.0, 0.0, 0.0);
117     glEnd();
118
119     glEndList();
120
121     return(mesh);
122 }
123
124
125 /* update the view volume */
126 static void update_view() {
127     struct flight_params *f;
128
129     f = &current_aircraft.flight;
130
131     /* Tell GL we are about to modify the projection parameters */
132     glMatrixMode(GL_PROJECTION);
133     glLoadIdentity();
134
135     gluPerspective(80.0, 1.0/win_ratio, 1.0, 6000.0);
136     gluLookAt(f->pos_x, f->pos_y, f->pos_z,
137               f->pos_x + cos(f->Psi), f->pos_y + sin(f->Psi), f->pos_z - 0.5,
138               0.0, 0.0, 1.0);
139 }
140
141
142 /* draw the scene */
143 static void draw_scene( void ) {
144     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
145
146     /* update view volume parameters */
147     update_view();
148
149     /* Tell GL we are switching to model view parameters */
150     glMatrixMode(GL_MODELVIEW);
151     glLoadIdentity();
152
153     /* glTranslatef(0.0, 0.0, -5.0); */
154
155     glPushMatrix();
156
157     /* draw terrain mesh */
158     draw_mesh();
159
160     glPopMatrix();
161
162     tkSwapBuffers();
163 }
164
165
166 /* draw the terrain mesh */
167 static void draw_mesh() {
168     glCallList(mesh);
169 }
170
171
172 /* What should we do when we have nothing else to do?  How about get
173  * ready for the next move?*/
174 static void idle( void )
175 {
176     slew_update();
177     aircraft_debug(1);
178
179     draw_scene();
180 }
181
182
183 /* new window size or exposure */
184 static void reshape( int width, int height ) {
185     /* Do this so we can call reshape(0,0) ourselves without having to know
186      * what the values of width & height are. */
187     if ( (height > 0) && (width > 0) ) {
188         win_ratio = (GLfloat) height / (GLfloat) width;
189     }
190
191     /* Inform gl of our view window size */
192     glViewport(0, 0, (GLint)width, (GLint)height);
193
194     update_view();
195     
196     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
197 }
198
199
200 /**************************************************************************
201  * Main ...
202  **************************************************************************/
203
204 int main( int argc, char *argv[] ) {
205
206     /* parse the scenery file */
207     parse_scenery(argv[1]);
208
209     /* Define initial window size */
210     tkInitPosition(0, 0, 400, 400);
211
212     /* Define Display Parameters */
213     tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT );
214
215     /* Initialize the main window */
216     if (tkInitWindow("Terrain Demo") == GL_FALSE) {
217         tkQuit();
218     }
219
220     /* setup view parameters */
221     init_view();
222
223     /* build all objects */
224     init_scene();
225
226     /* Set initial position and slew parameters */
227     slew_init(-406658.0, 129731.0, 344, 0.79);
228
229     /* call reshape() on expose events */
230     tkExposeFunc( reshape );
231
232     /* call reshape() on window resizes */
233     tkReshapeFunc( reshape );
234
235     /* call key() on keyboard event */
236     tkKeyDownFunc( key );
237
238     /* call idle() whenever there is nothing else to do */
239     tkIdleFunc( idle );
240
241     /* draw the scene */
242     tkDisplayFunc( draw_scene );
243
244     /* pass control off to the tk event handler */
245     tkExec();
246
247     return(0);
248 }
249
250
251 /* $Log$
252 /* Revision 1.1  1997/05/16 16:05:52  curt
253 /* Initial revision.
254 /*
255  */