1 /**************************************************************************
2 * mesh.c -- data structures and routines for processing terrain meshes
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 **************************************************************************/
36 #include <stdlib.h> /* atof(), atoi() */
41 #include "../constants.h"
43 #include "../Math/fg_geodesy.h"
44 #include "../Math/fg_random.h"
45 #include "../Math/mat3.h"
46 #include "../Math/polar.h"
53 /* Temporary hack until we get the scenery management system running */
54 extern GLint area_terrain;
55 extern struct MESH eg;
57 /* initialize the non-array mesh values */
58 void mesh_init(struct MESH *m) {
74 /* return a pointer to a new mesh structure (no data array allocated yet) */
75 struct MESH *(new_mesh)() {
76 struct MESH *mesh_ptr;
78 mesh_ptr = (struct MESH *)malloc(sizeof(struct MESH));
80 if ( mesh_ptr == 0 ) {
81 printf("Virtual memory exceeded\n");
85 mesh_ptr->cur_row = 0;
86 mesh_ptr->cur_col = 0;
92 /* return a pointer to a dynamically allocated array */
93 float *(new_mesh_data)(int nrows, int ncols) {
96 mesh_data_ptr = (float *)malloc(nrows * ncols * sizeof(float));
98 if ( mesh_data_ptr == 0 ) {
99 printf("Virtual memory exceeded\n");
103 printf("Allocated float(%d, %d)\n", nrows, ncols);
105 return(mesh_data_ptr);
109 /* set the option name in the mesh data structure */
110 void mesh_set_option_name(struct MESH *m, char *name) {
111 if ( strlen(name) < MAX_IDENT_LEN ) {
112 strcpy(m->option_name, name);
114 strncpy(m->option_name, name, MAX_IDENT_LEN - 1);
115 m->option_name[MAX_IDENT_LEN - 1] = '\0';
117 if ( strcmp(m->option_name, "do_data") == 0 ) {
125 /* set an option value in the mesh data structure */
126 void mesh_set_option_value(struct MESH *m, char *value) {
127 /* printf("Setting %s to %s\n", m->option_name, value); */
130 /* mesh data is a pseudo 2d array */
131 /* printf("Setting mesh_data[%d][%d] to %s\n", m->cur_row, m->cur_col,
133 m->mesh_data[m->cur_row * m->cols + m->cur_col] = atof(value);
135 if ( m->cur_col >= m->cols ) {
138 if ( m->cur_row > m->rows ) {
142 } else if ( strcmp(m->option_name, "origin_lon") == 0 ) {
143 m->originx = atof(value);
144 } else if ( strcmp(m->option_name, "origin_lat") == 0 ) {
145 m->originy = atof(value);
146 } else if ( strcmp(m->option_name, "rows") == 0 ) {
147 m->rows = atoi(value);
148 } else if ( strcmp(m->option_name, "cols") == 0 ) {
149 m->cols = atoi(value);
150 } else if ( strcmp(m->option_name, "row_step") == 0 ) {
151 m->row_step = atof(value);
152 } else if ( strcmp(m->option_name, "col_step") == 0 ) {
153 m->col_step = atof(value);
155 printf("Unknown option %s with value %s, ignoring ...\n",
156 m->option_name, value);
161 /* do whatever needs to be done with the mesh now that it's been
162 loaded, such as generating the OpenGL call list. */
163 void mesh_do_it(struct MESH *m) {
164 area_terrain = mesh_to_OpenGL(m);
168 /* return the current altitude based on mesh data. We should rewrite
169 * this to interpolate exact values, but for now this is good enough */
170 double mesh_altitude(double lon, double lat) {
171 /* we expect incoming (lon,lat) to be in arcsec for now */
173 double xlocal, ylocal, dx, dy, zA, zB, elev;
174 int x1, y1, z1, x2, y2, z2, x3, y3, z3;
178 skip = scenery.terrain_skip;
179 /* determine if we are in the lower triangle or the upper triangle
187 then calculate our end points
190 xlocal = (lon - eg.originx) / eg.col_step;
191 ylocal = (lat - eg.originy) / eg.row_step;
193 xindex = (int)(xlocal / skip) * skip;
194 yindex = (int)(ylocal / skip) * skip;
196 if ( (xindex < 0) || (xindex + skip >= eg.cols) ||
197 (yindex < 0) || (yindex + skip >= eg.rows) ) {
201 dx = xlocal - xindex;
202 dy = ylocal - yindex;
206 /* printf(" Lower triangle\n"); */
210 z1 = eg.mesh_data[y1 * eg.cols + x1];
214 z2 = eg.mesh_data[y2 * eg.cols + x2];
218 z3 = eg.mesh_data[y3 * eg.cols + x3];
220 /* printf(" dx = %.2f dy = %.2f\n", dx, dy);
221 printf(" (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
222 printf(" (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
223 printf(" (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3); */
225 zA = dx * (z2 - z1) / skip + z1;
226 zB = dx * (z3 - z1) / skip + z1;
228 /* printf(" zA = %.2f zB = %.2f\n", zA, zB); */
230 if ( dx > EPSILON ) {
231 elev = dy * (zB - zA) / dx + zA;
237 /* printf(" Upper triangle\n"); */
241 z1 = eg.mesh_data[y1 * eg.cols + x1];
245 z2 = eg.mesh_data[y2 * eg.cols + x2];
249 z3 = eg.mesh_data[y3 * eg.cols + x3];
251 /* printf(" dx = %.2f dy = %.2f\n", dx, dy);
252 printf(" (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
253 printf(" (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
254 printf(" (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3); */
256 zA = dy * (z2 - z1) / skip + z1;
257 zB = dy * (z3 - z1) / skip + z1;
259 /* printf(" zA = %.2f zB = %.2f\n", zA, zB );
260 printf(" xB - xA = %.2f\n", eg.col_step * dy / eg.row_step); */
262 if ( dy > EPSILON ) {
263 elev = dx * (zB - zA) / dy + zA;
273 /* walk through mesh and make opengl calls */
274 GLint mesh_to_OpenGL(struct MESH *m) {
276 /* static GLfloat color[4] = { 0.5, 0.4, 0.25, 1.0 }; */ /* dark desert */
277 static GLfloat color[4] = { 0.5, 0.5, 0.25, 1.0 };
278 double centerx, centery;
281 float x1, y1, x2, y2, z11, z12, z21, z22;
282 struct fgCartesianPoint p11, p12, p21, p22, c;
283 double gc_lon, gc_lat, sl_radius;
285 MAT3vec v1, v2, normal;
286 int i, j, istep, jstep, iend, jend;
289 printf("In mesh2GL(), generating GL call list.\n");
291 /* Detail level. This is how big a step we take as we walk
292 * through the DEM data set. This value is initialized in
293 * .../Scenery/scenery.c:fgSceneryInit() */
294 istep = jstep = scenery.terrain_skip ;
296 centerx = m->originx + (m->rows * m->row_step) / 2.0;
297 centery = m->originy + (m->cols * m->col_step) / 2.0;
298 fgGeodToGeoc(centery*ARCSEC_TO_RAD, 0, &sl_radius, &gc_lat);
299 c = fgPolarToCart(centerx*ARCSEC_TO_RAD, gc_lat, sl_radius);
302 mesh = glGenLists(1);
303 glNewList(mesh, GL_COMPILE);
305 /* glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color ); */
312 y2 = y1 + (m->col_step * istep);
314 for ( i = 0; i < iend; i += istep ) {
316 x2 = x1 + (m->row_step * jstep);
318 glBegin(GL_TRIANGLE_STRIP);
320 for ( j = 0; j < jend; j += jstep ) {
321 z11 = m->mesh_data[i * m->cols + j ];
322 z12 = m->mesh_data[(i+istep) * m->cols + j ];
323 z21 = m->mesh_data[i * m->cols + (j+jstep)];
324 z22 = m->mesh_data[(i+istep) * m->cols + (j+jstep)];
326 /* printf("A geodetic point is (%.2f, %.2f, %.2f)\n",
328 gc_lon = x1*ARCSEC_TO_RAD;
329 fgGeodToGeoc(y1*ARCSEC_TO_RAD, z11, &sl_radius, &gc_lat);
330 /* printf("A geocentric point is (%.2f, %.2f, %.2f)\n", gc_lon,
331 gc_lat, sl_radius+z11); */
332 p11 = fgPolarToCart(gc_lon, gc_lat, sl_radius+z11);
333 /* printf("A cart point is (%.8f, %.8f, %.8f)\n",
334 p11.x, p11.y, p11.z); */
336 gc_lon = x1*ARCSEC_TO_RAD;
337 fgGeodToGeoc(y2*ARCSEC_TO_RAD, z12, &sl_radius, &gc_lat);
338 p12 = fgPolarToCart(gc_lon, gc_lat, sl_radius+z12);
340 gc_lon = x2*ARCSEC_TO_RAD;
341 fgGeodToGeoc(y1*ARCSEC_TO_RAD, z21, &sl_radius, &gc_lat);
342 p21 = fgPolarToCart(gc_lon, gc_lat, sl_radius+z21);
344 gc_lon = x2*ARCSEC_TO_RAD;
345 fgGeodToGeoc(y2*ARCSEC_TO_RAD, z22, &sl_radius, &gc_lat);
346 p22 = fgPolarToCart(gc_lon, gc_lat, sl_radius+z22);
348 v1[0] = p22.y - p11.y; v1[1] = p22.z - p11.z; v1[2] = z22 - z11;
349 v2[0] = p12.y - p11.y; v2[1] = p12.z - p11.z; v2[2] = z12 - z11;
350 MAT3cross_product(normal, v1, v2);
351 MAT3_NORMALIZE_VEC(normal,temp);
352 glNormal3d(normal[0], normal[1], normal[2]);
353 /* printf("normal 1 = (%.2f %.2f %.2f\n", normal[0], normal[1],
357 /* first time through */
358 glVertex3d(p12.x - c.x, p12.y - c.y, p12.z - c.z);
359 glVertex3d(p11.x - c.x, p11.y - c.y, p11.z - c.z);
362 glVertex3d(p22.x - c.x, p22.y - c.y, p22.z - c.z);
364 v2[0] = p21.y - p11.y; v2[1] = p21.z - p11.z; v2[2] = z21 - z11;
365 MAT3cross_product(normal, v2, v1);
366 MAT3_NORMALIZE_VEC(normal,temp);
367 glNormal3d(normal[0], normal[1], normal[2]);
368 /* printf("normal 2 = (%.2f %.2f %.2f\n", normal[0], normal[1],
371 glVertex3d(p21.x - c.x, p21.y - c.y, p21.z - c.z);
373 x1 += m->row_step * jstep;
374 x2 += m->row_step * jstep;
378 y1 += m->col_step * istep;
379 y2 += m->col_step * istep;
382 /* this will go, it's only here for testing/debugging */
384 for ( i = 0; i < 200; i++ ) {
385 randx = fg_random() * 3600.0;
386 randy = fg_random() * 3600.0;
388 /* mesh_make_test_object(m->originx + randx, m->originy + randy); */
398 /* Revision 1.22 1997/10/28 21:00:21 curt
399 /* Changing to new terrain format.
401 * Revision 1.21 1997/08/27 03:30:27 curt
402 * Changed naming scheme of basic shared structures.
404 * Revision 1.20 1997/08/19 23:55:08 curt
405 * Worked on better simulating real lighting.
407 * Revision 1.19 1997/08/06 00:24:28 curt
408 * Working on correct real time sun lighting.
410 * Revision 1.18 1997/08/02 19:10:14 curt
411 * Incorporated mesh2GL.c into mesh.c
413 * Revision 1.17 1997/07/18 23:41:26 curt
414 * Tweaks for building with Cygnus Win32 compiler.
416 * Revision 1.16 1997/07/16 20:04:51 curt
417 * Minor tweaks to aid Win32 port.
419 * Revision 1.15 1997/07/14 16:26:04 curt
420 * Testing/playing -- placed objects randomly across the entire terrain.
422 * Revision 1.14 1997/07/12 04:01:14 curt
423 * Added #include <Windows32/Base.h> to help Win32 compiling.
425 * Revision 1.13 1997/07/12 02:27:11 curt
426 * Looking at potential scenery transformation/coordinate system problems.
428 * Revision 1.12 1997/07/11 03:23:19 curt
429 * Solved some scenery display/orientation problems. Still have a positioning
430 * (or transformation?) problem.
432 * Revision 1.11 1997/07/11 01:30:02 curt
433 * More tweaking of terrian floor.
435 * Revision 1.10 1997/07/10 04:26:38 curt
436 * We now can interpolated ground elevation for any position in the grid. We
437 * can use this to enforce a "hard" ground. We still need to enforce some
438 * bounds checking so that we don't try to lookup data points outside the
441 * Revision 1.9 1997/07/10 02:22:10 curt
442 * Working on terrain elevation interpolation routine.
444 * Revision 1.8 1997/07/09 21:31:15 curt
445 * Working on making the ground "hard."
447 * Revision 1.7 1997/07/08 18:20:13 curt
448 * Working on establishing a hard ground.
450 * Revision 1.6 1997/06/29 21:16:49 curt
451 * More twiddling with the Scenery Management system.
453 * Revision 1.5 1997/06/22 21:44:41 curt
454 * Working on intergrating the VRML (subset) parser.
456 * Revision 1.4 1997/05/30 19:30:17 curt
457 * The LaRCsim flight model is starting to look like it is working.
459 * Revision 1.3 1997/05/23 15:40:41 curt
460 * Added GNU copyright headers.
462 * Revision 1.2 1997/05/19 18:20:50 curt
463 * Slight change to origin key words.
465 * Revision 1.1 1997/05/16 16:07:04 curt