]> git.mxchange.org Git - flightgear.git/blob - Main/mesh2GL.c
Moved PI definitions to ../constants.h
[flightgear.git] / Main / mesh2GL.c
1 /**************************************************************************
2  * mesh2GL.c -- walk through a mesh data structure and make GL calls
3  *
4  * Written by Curtis Olson, 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 #ifdef WIN32
28 #  include <windows.h>                     
29 #endif
30
31 #include <GL/glut.h>
32
33 #include "../constants.h"
34 #include "../Scenery/mesh.h"
35 #include "../Scenery/scenery.h"
36 #include "../Math/mat3.h"
37 #include "../Math/polar.h"
38 #include "../Utils/fg_random.h"
39
40
41 /* The following routine is a real hack used for testing puposes only
42  * and should probably be removed. */
43 void mesh_make_test_object(double lon, double lat) {
44     struct fgCartesianPoint origin;
45     double elev;
46     double b = 0.25;
47     double h = 0.50;
48     static GLfloat color[4] = { 1.0, 0.25, 0.25, 1.0 };
49
50     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
51
52     elev = mesh_altitude(lon, lat) * 0.001;
53     printf("Elevation of test structure is:  %.2f\n", elev);
54
55     printf("Center of structure geodetic:  (%.2f, %.2f\n", lon, lat);
56     origin = fgGeodetic2Cartesian(lon*ARCSEC_TO_RAD, lat*ARCSEC_TO_RAD);
57     printf("Center (unit circle) is (%.8f, %.8f, %.8f)\n", origin.x, origin.y, 
58            origin.z);
59
60     origin = fgRotateCartesianPoint(origin);
61     printf("Center of structure is:  (%.4f, %.4f\n", origin.y, origin.z);
62
63     glBegin(GL_TRIANGLES);
64     glVertex3d(origin.y - b, origin.z - b, elev);
65     glVertex3d(origin.y + b, origin.z - b, elev);
66     glVertex3d(origin.y, origin.z, elev+h);
67
68     glVertex3d(origin.y + b, origin.z - b, elev);
69     glVertex3d(origin.y + b, origin.z + b, elev);
70     glVertex3d(origin.y, origin.z, elev+h);
71
72     glVertex3d(origin.y + b, origin.z + b, elev);
73     glVertex3d(origin.y - b, origin.z + b, elev);
74     glVertex3d(origin.y, origin.z, elev+h);
75
76     glVertex3d(origin.y - b, origin.z + b, elev);
77     glVertex3d(origin.y - b, origin.z - b, elev);
78     glVertex3d(origin.y, origin.z, elev+h);
79     glEnd();
80 }
81
82 /* walk through mesh and make ogl calls */
83 GLint mesh2GL(struct mesh *m) {
84     GLint mesh;
85     /* static GLfloat color[4] = { 0.5, 0.4, 0.25, 1.0 }; */ /* dark desert */
86     static GLfloat color[4] = { 0.5, 0.5, 0.25, 1.0 };
87     double randx, randy;
88
89     float x1, y1, x2, y2, z11, z12, z21, z22;
90     struct fgCartesianPoint p11, p12, p21, p22;
91
92     MAT3vec v1, v2, normal; 
93     int i, j, istep, jstep, iend, jend;
94     float temp;
95
96     printf("In mesh2GL(), generating GL call list.\n");
97
98     /* Detail level.  This is how big a step we take as we walk
99      * through the DEM data set.  This value is initialized in
100      * .../Scenery/scenery.c:fgSceneryInit() */
101     istep = jstep = cur_scenery_params.terrain_skip ;
102
103     /* setup the batch transformation */
104     fgRotateBatchInit(-m->originx * ARCSEC_TO_RAD, -m->originy * ARCSEC_TO_RAD);
105
106     mesh = glGenLists(1);
107     glNewList(mesh, GL_COMPILE);
108
109     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
110         
111     iend = m->cols - 1;
112     jend = m->rows - 1;
113     
114     y1 = m->originy;
115     y2 = y1 + (m->col_step * istep);
116     
117     for ( i = 0; i < iend; i += istep ) {
118         x1 = m->originx;
119         x2 = x1 + (m->row_step * jstep);
120
121         glBegin(GL_TRIANGLE_STRIP);
122
123         for ( j = 0; j < jend; j += jstep ) {
124             p11 = fgGeodetic2Cartesian(x1*ARCSEC_TO_RAD, y1*ARCSEC_TO_RAD);
125             /* printf("A geodetic is (%.2f, %.2f)\n", x1, y1); */
126             /* printf("A cart is (%.8f, %.8f, %.8f)\n", p11.x, p11.y, p11.z); */
127             p11 = fgRotateCartesianPoint(p11);
128             /* printf("A point is (%.8f, %.8f, %.8f)\n", p11.y, p11.z, z11); */
129
130             p12 = fgGeodetic2Cartesian(x1*ARCSEC_TO_RAD, y2*ARCSEC_TO_RAD);
131             p12 = fgRotateCartesianPoint(p12);
132
133             p21 = fgGeodetic2Cartesian(x2*ARCSEC_TO_RAD, y1*ARCSEC_TO_RAD);
134             p21 = fgRotateCartesianPoint(p21);
135
136             p22 = fgGeodetic2Cartesian(x2*ARCSEC_TO_RAD, y2*ARCSEC_TO_RAD);
137             p22 = fgRotateCartesianPoint(p22);
138
139             z11 = 0.001 * m->mesh_data[i         * m->cols + j        ];
140             z12 = 0.001 * m->mesh_data[(i+istep) * m->cols + j        ];
141             z21 = 0.001 * m->mesh_data[i         * m->cols + (j+jstep)];
142             z22 = 0.001 * m->mesh_data[(i+istep) * m->cols + (j+jstep)];
143
144             v1[0] = p22.y - p11.y; v1[1] = p22.z - p11.z; v1[2] = z22 - z11;
145             v2[0] = p12.y - p11.y; v2[1] = p12.z - p11.z; v2[2] = z12 - z11;
146             MAT3cross_product(normal, v1, v2);
147             MAT3_NORMALIZE_VEC(normal,temp);
148             glNormal3d(normal[0], normal[1], normal[2]);
149             /* printf("normal 1 = (%.2f %.2f %.2f\n", normal[0], normal[1], 
150                    normal[2]); */
151             
152             if ( j == 0 ) {
153                 /* first time through */
154                 glVertex3d(p12.y, p12.z, z12);
155                 glVertex3d(p11.y, p11.z, z11);
156             }
157             
158             glVertex3d(p22.y, p22.z, z22);
159     
160             v2[0] = p21.y - p11.y; v2[1] = p21.z - p11.z; v2[2] = z21 - z11;
161             MAT3cross_product(normal, v2, v1);
162             MAT3_NORMALIZE_VEC(normal,temp);
163             glNormal3d(normal[0], normal[1], normal[2]);
164             /* printf("normal 2 = (%.2f %.2f %.2f\n", normal[0], normal[1], 
165                    normal[2]); */
166
167             glVertex3d(p21.y, p21.z, z21);
168
169             x1 += m->row_step * jstep;
170             x2 += m->row_step * jstep;
171         }
172         glEnd();
173
174         y1 += m->col_step * istep;
175         y2 += m->col_step * istep;
176     }
177
178     /* this will go, it's only here for testing/debugging */
179
180     /*
181     for ( i = m->originy; i < m->originy + (m->row_step * iend); i += 120 ) {
182         for ( j = m->originx; j < m->originx + (m->col_step * jend); j += 120) {
183             mesh_make_test_object(j, i);
184         }
185     }
186     */
187
188     for ( i = 0; i < 200; i++ ) {
189         randx = fg_random() * 3600.0;
190         randy = fg_random() * 3600.0;
191
192         mesh_make_test_object(m->originx + randx, m->originy + randy);
193     }
194
195     glEndList();
196
197     return(mesh);
198 }
199
200
201
202 /* $Log$
203 /* Revision 1.37  1997/07/19 22:34:03  curt
204 /* Moved PI definitions to ../constants.h
205 /* Moved random() stuff to ../Utils/ and renamed fg_random()
206 /*
207  * Revision 1.36  1997/07/18 23:41:25  curt
208  * Tweaks for building with Cygnus Win32 compiler.
209  *
210  * Revision 1.35  1997/07/18 14:28:35  curt
211  * Hacked in some support for wind/turbulence.
212  *
213  * Revision 1.34  1997/07/16 20:04:50  curt
214  * Minor tweaks to aid Win32 port.
215  *
216  * Revision 1.33  1997/07/14 16:26:04  curt
217  * Testing/playing -- placed objects randomly across the entire terrain.
218  *
219  * Revision 1.32  1997/07/12 03:50:21  curt
220  * Added an #include <Windows32/Base.h> to help compiling for Win32
221  *
222  * Revision 1.31  1997/07/12 02:27:07  curt
223  * Looking at potential scenery transformation/coordinate system problems.
224  *
225  * Revision 1.30  1997/07/11 03:23:18  curt
226  * Solved some scenery display/orientation problems.  Still have a positioning
227  * (or transformation?) problem.
228  *
229  * Revision 1.29  1997/07/11 01:29:58  curt
230  * More tweaking of terrian floor.
231  *
232  * Revision 1.28  1997/07/10 04:26:37  curt
233  * We now can interpolated ground elevation for any position in the grid.  We
234  * can use this to enforce a "hard" ground.  We still need to enforce some
235  * bounds checking so that we don't try to lookup data points outside the
236  * grid data set.
237  *
238  * Revision 1.27  1997/07/09 21:31:13  curt
239  * Working on making the ground "hard."
240  *
241  * Revision 1.26  1997/07/08 18:20:13  curt
242  * Working on establishing a hard ground.
243  *
244  * Revision 1.25  1997/07/07 20:59:50  curt
245  * Working on scenery transformations to enable us to fly fluidly over the
246  * poles with no discontinuity/distortion in scenery.
247  *
248  * Revision 1.24  1997/07/05 20:43:35  curt
249  * renamed mat3 directory to Math so we could add other math related routines.
250  *
251  * Revision 1.23  1997/07/03 00:51:14  curt
252  * Playing with terrain color.
253  *
254  * Revision 1.22  1997/06/29 21:19:17  curt
255  * Working on scenery management system.
256  *
257  * Revision 1.21  1997/06/21 17:12:54  curt
258  * Capitalized subdirectory names.
259  *
260  * Revision 1.20  1997/06/18 04:10:32  curt
261  * A couple more runway tweaks ...
262  *
263  * Revision 1.19  1997/06/18 02:21:24  curt
264  * Hacked in a runway
265  *
266  * Revision 1.18  1997/06/17 04:19:17  curt
267  * More timer related tweaks with respect to view direction changes.
268  *
269  * Revision 1.17  1997/06/16 19:32:52  curt
270  * Starting to add general timer support.
271  *
272  * Revision 1.16  1997/06/02 03:40:07  curt
273  * A tiny bit more view tweaking.
274  *
275  * Revision 1.15  1997/06/02 03:01:38  curt
276  * Working on views (side, front, back, transitions, etc.)
277  *
278  * Revision 1.14  1997/05/31 19:16:26  curt
279  * Elevator trim added.
280  *
281  * Revision 1.13  1997/05/31 04:13:53  curt
282  * WE CAN NOW FLY!!!
283  *
284  * Continuing work on the LaRCsim flight model integration.
285  * Added some MSFS-like keyboard input handling.
286  *
287  * Revision 1.12  1997/05/30 23:26:20  curt
288  * Added elevator/aileron controls.
289  *
290  * Revision 1.11  1997/05/30 19:27:02  curt
291  * The LaRCsim flight model is starting to look like it is working.
292  *
293  * Revision 1.10  1997/05/30 03:54:11  curt
294  * Made a bit more progress towards integrating the LaRCsim flight model.
295  *
296  * Revision 1.9  1997/05/29 22:39:51  curt
297  * Working on incorporating the LaRCsim flight model.
298  *
299  * Revision 1.8  1997/05/29 12:31:40  curt
300  * Minor tweaks, moving towards general flight model integration.
301  *
302  * Revision 1.7  1997/05/29 02:33:24  curt
303  * Updated to reflect changing interfaces in other "modules."
304  *
305  * Revision 1.6  1997/05/27 17:44:32  curt
306  * Renamed & rearranged variables and routines.   Added some initial simple
307  * timer/alarm routines so the flight model can be updated on a regular 
308  * interval.
309  *
310  * Revision 1.5  1997/05/24 01:45:32  curt
311  * Fixed surface normals for triangle mesh.
312  *
313  * Revision 1.4  1997/05/23 20:05:24  curt
314  * First stab at using GL_TRIANGLE_STRIP's instead of GL_POLYGONS (to conserve
315  * memory)
316  *
317  * Revision 1.3  1997/05/23 15:40:26  curt
318  * Added GNU copyright headers.
319  * Fog now works!
320  *
321  * Revision 1.2  1997/05/23 00:35:13  curt
322  * Trying to get fog to work ...
323  *
324  * Revision 1.1  1997/05/21 15:57:52  curt
325  * Renamed due to added GLUT support.
326  *
327  * Revision 1.3  1997/05/19 18:22:42  curt
328  * Parameter tweaking ... starting to stub in fog support.
329  *
330  * Revision 1.2  1997/05/17 00:17:35  curt
331  * Trying to stub in support for standard OpenGL.
332  *
333  * Revision 1.1  1997/05/16 16:05:52  curt
334  * Initial revision.
335  *
336  */