1 /**************************************************************************
2 * mesh2GL.c -- walk through a mesh data structure and make GL calls
4 * Written by Curtis Olson, started May 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
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"
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;
48 static GLfloat color[4] = { 1.0, 0.25, 0.25, 1.0 };
50 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
52 elev = mesh_altitude(lon, lat) * 0.001;
53 printf("Elevation of test structure is: %.2f\n", elev);
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,
60 origin = fgRotateCartesianPoint(origin);
61 printf("Center of structure is: (%.4f, %.4f\n", origin.y, origin.z);
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);
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);
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);
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);
82 /* walk through mesh and make ogl calls */
83 GLint mesh2GL(struct mesh *m) {
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 };
89 float x1, y1, x2, y2, z11, z12, z21, z22;
90 struct fgCartesianPoint p11, p12, p21, p22;
92 MAT3vec v1, v2, normal;
93 int i, j, istep, jstep, iend, jend;
96 printf("In mesh2GL(), generating GL call list.\n");
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 ;
103 /* setup the batch transformation */
104 fgRotateBatchInit(-m->originx * ARCSEC_TO_RAD, -m->originy * ARCSEC_TO_RAD);
106 mesh = glGenLists(1);
107 glNewList(mesh, GL_COMPILE);
109 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
115 y2 = y1 + (m->col_step * istep);
117 for ( i = 0; i < iend; i += istep ) {
119 x2 = x1 + (m->row_step * jstep);
121 glBegin(GL_TRIANGLE_STRIP);
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); */
130 p12 = fgGeodetic2Cartesian(x1*ARCSEC_TO_RAD, y2*ARCSEC_TO_RAD);
131 p12 = fgRotateCartesianPoint(p12);
133 p21 = fgGeodetic2Cartesian(x2*ARCSEC_TO_RAD, y1*ARCSEC_TO_RAD);
134 p21 = fgRotateCartesianPoint(p21);
136 p22 = fgGeodetic2Cartesian(x2*ARCSEC_TO_RAD, y2*ARCSEC_TO_RAD);
137 p22 = fgRotateCartesianPoint(p22);
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)];
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],
153 /* first time through */
154 glVertex3d(p12.y, p12.z, z12);
155 glVertex3d(p11.y, p11.z, z11);
158 glVertex3d(p22.y, p22.z, z22);
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],
167 glVertex3d(p21.y, p21.z, z21);
169 x1 += m->row_step * jstep;
170 x2 += m->row_step * jstep;
174 y1 += m->col_step * istep;
175 y2 += m->col_step * istep;
178 /* this will go, it's only here for testing/debugging */
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);
188 for ( i = 0; i < 200; i++ ) {
189 randx = fg_random() * 3600.0;
190 randy = fg_random() * 3600.0;
192 mesh_make_test_object(m->originx + randx, m->originy + randy);
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()
207 * Revision 1.36 1997/07/18 23:41:25 curt
208 * Tweaks for building with Cygnus Win32 compiler.
210 * Revision 1.35 1997/07/18 14:28:35 curt
211 * Hacked in some support for wind/turbulence.
213 * Revision 1.34 1997/07/16 20:04:50 curt
214 * Minor tweaks to aid Win32 port.
216 * Revision 1.33 1997/07/14 16:26:04 curt
217 * Testing/playing -- placed objects randomly across the entire terrain.
219 * Revision 1.32 1997/07/12 03:50:21 curt
220 * Added an #include <Windows32/Base.h> to help compiling for Win32
222 * Revision 1.31 1997/07/12 02:27:07 curt
223 * Looking at potential scenery transformation/coordinate system problems.
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.
229 * Revision 1.29 1997/07/11 01:29:58 curt
230 * More tweaking of terrian floor.
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
238 * Revision 1.27 1997/07/09 21:31:13 curt
239 * Working on making the ground "hard."
241 * Revision 1.26 1997/07/08 18:20:13 curt
242 * Working on establishing a hard ground.
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.
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.
251 * Revision 1.23 1997/07/03 00:51:14 curt
252 * Playing with terrain color.
254 * Revision 1.22 1997/06/29 21:19:17 curt
255 * Working on scenery management system.
257 * Revision 1.21 1997/06/21 17:12:54 curt
258 * Capitalized subdirectory names.
260 * Revision 1.20 1997/06/18 04:10:32 curt
261 * A couple more runway tweaks ...
263 * Revision 1.19 1997/06/18 02:21:24 curt
266 * Revision 1.18 1997/06/17 04:19:17 curt
267 * More timer related tweaks with respect to view direction changes.
269 * Revision 1.17 1997/06/16 19:32:52 curt
270 * Starting to add general timer support.
272 * Revision 1.16 1997/06/02 03:40:07 curt
273 * A tiny bit more view tweaking.
275 * Revision 1.15 1997/06/02 03:01:38 curt
276 * Working on views (side, front, back, transitions, etc.)
278 * Revision 1.14 1997/05/31 19:16:26 curt
279 * Elevator trim added.
281 * Revision 1.13 1997/05/31 04:13:53 curt
284 * Continuing work on the LaRCsim flight model integration.
285 * Added some MSFS-like keyboard input handling.
287 * Revision 1.12 1997/05/30 23:26:20 curt
288 * Added elevator/aileron controls.
290 * Revision 1.11 1997/05/30 19:27:02 curt
291 * The LaRCsim flight model is starting to look like it is working.
293 * Revision 1.10 1997/05/30 03:54:11 curt
294 * Made a bit more progress towards integrating the LaRCsim flight model.
296 * Revision 1.9 1997/05/29 22:39:51 curt
297 * Working on incorporating the LaRCsim flight model.
299 * Revision 1.8 1997/05/29 12:31:40 curt
300 * Minor tweaks, moving towards general flight model integration.
302 * Revision 1.7 1997/05/29 02:33:24 curt
303 * Updated to reflect changing interfaces in other "modules."
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
310 * Revision 1.5 1997/05/24 01:45:32 curt
311 * Fixed surface normals for triangle mesh.
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
317 * Revision 1.3 1997/05/23 15:40:26 curt
318 * Added GNU copyright headers.
321 * Revision 1.2 1997/05/23 00:35:13 curt
322 * Trying to get fog to work ...
324 * Revision 1.1 1997/05/21 15:57:52 curt
325 * Renamed due to added GLUT support.
327 * Revision 1.3 1997/05/19 18:22:42 curt
328 * Parameter tweaking ... starting to stub in fog support.
330 * Revision 1.2 1997/05/17 00:17:35 curt
331 * Trying to stub in support for standard OpenGL.
333 * Revision 1.1 1997/05/16 16:05:52 curt