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 **************************************************************************/
31 #include <stdlib.h> /* atof(), atoi() */
41 /* Temporary hack until we get the scenery management system running */
42 extern GLint mesh_hack;
43 extern struct mesh eg;
45 /* initialize the non-array mesh values */
46 void mesh_init(struct mesh *m) {
62 /* return a pointer to a new mesh structure (no data array allocated yet) */
63 struct mesh *(new_mesh)() {
64 struct mesh *mesh_ptr;
66 mesh_ptr = (struct mesh *)malloc(sizeof(struct mesh));
68 if ( mesh_ptr == 0 ) {
69 printf("Virtual memory exceeded\n");
73 mesh_ptr->cur_row = 0;
74 mesh_ptr->cur_col = 0;
80 /* return a pointer to a dynamically allocated array */
81 float *(new_mesh_data)(int nrows, int ncols) {
84 mesh_data_ptr = (float *)malloc(nrows * ncols * sizeof(float));
86 if ( mesh_data_ptr == 0 ) {
87 printf("Virtual memory exceeded\n");
91 printf("Allocated float(%d, %d)\n", nrows, ncols);
93 return(mesh_data_ptr);
97 /* set the option name in the mesh data structure */
98 void mesh_set_option_name(struct mesh *m, char *name) {
99 if ( strlen(name) < MAX_IDENT_LEN ) {
100 strcpy(m->option_name, name);
102 strncpy(m->option_name, name, MAX_IDENT_LEN - 1);
103 m->option_name[MAX_IDENT_LEN - 1] = '\0';
105 if ( strcmp(m->option_name, "do_data") == 0 ) {
113 /* set an option value in the mesh data structure */
114 void mesh_set_option_value(struct mesh *m, char *value) {
115 /* printf("Setting %s to %s\n", m->option_name, value); */
118 /* mesh data is a pseudo 2d array */
119 /* printf("Setting mesh_data[%d][%d] to %s\n", m->cur_row, m->cur_col,
121 m->mesh_data[m->cur_row * m->cols + m->cur_col] = atof(value);
123 if ( m->cur_col >= m->cols ) {
126 if ( m->cur_row > m->rows ) {
130 } else if ( strcmp(m->option_name, "origin_lon") == 0 ) {
131 m->originx = atof(value);
132 } else if ( strcmp(m->option_name, "origin_lat") == 0 ) {
133 m->originy = atof(value);
134 } else if ( strcmp(m->option_name, "rows") == 0 ) {
135 m->rows = atoi(value);
136 } else if ( strcmp(m->option_name, "cols") == 0 ) {
137 m->cols = atoi(value);
138 } else if ( strcmp(m->option_name, "row_step") == 0 ) {
139 m->row_step = atof(value);
140 } else if ( strcmp(m->option_name, "col_step") == 0 ) {
141 m->col_step = atof(value);
143 printf("Unknown option %s with value %s, ignoring ...\n",
144 m->option_name, value);
149 /* do whatever needs to be done with the mesh now that it's been
150 loaded, such as generating the OpenGL call list. */
151 void mesh_do_it(struct mesh *m) {
152 mesh_hack = mesh2GL(m);
156 /* return the current altitude based on mesh data. We should rewrite
157 * this to interpolate exact values, but for now this is good enough */
158 double mesh_altitude(double lon, double lat) {
159 /* we expect incoming (lon,lat) to be in arcsec for now */
161 double xlocal, ylocal, dx, dy, zA, zB, elev;
162 int x1, y1, z1, x2, y2, z2, x3, y3, z3;
166 skip = cur_scenery_params.terrain_skip;
167 /* determine if we are in the lower triangle or the upper triangle
175 then calculate our end points
178 xlocal = (lon - eg.originx) / eg.col_step;
179 ylocal = (lat - eg.originy) / eg.row_step;
181 xindex = (int)(xlocal / skip) * skip;
182 yindex = (int)(ylocal / skip) * skip;
184 if ( (xindex < 0) || (xindex + skip >= eg.cols) ||
185 (yindex < 0) || (yindex + skip >= eg.rows) ) {
189 dx = xlocal - xindex;
190 dy = ylocal - yindex;
194 /* printf(" Lower triangle\n"); */
198 z1 = eg.mesh_data[y1 * eg.cols + x1];
202 z2 = eg.mesh_data[y2 * eg.cols + x2];
206 z3 = eg.mesh_data[y3 * eg.cols + x3];
208 /* printf(" dx = %.2f dy = %.2f\n", dx, dy);
209 printf(" (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
210 printf(" (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
211 printf(" (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3); */
213 zA = dx * (z2 - z1) / skip + z1;
214 zB = dx * (z3 - z1) / skip + z1;
216 /* printf(" zA = %.2f zB = %.2f\n", zA, zB); */
218 elev = dy * (zB - zA) / dx + zA;
221 /* printf(" Upper triangle\n"); */
225 z1 = eg.mesh_data[y1 * eg.cols + x1];
229 z2 = eg.mesh_data[y2 * eg.cols + x2];
233 z3 = eg.mesh_data[y3 * eg.cols + x3];
235 /* printf(" dx = %.2f dy = %.2f\n", dx, dy);
236 printf(" (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
237 printf(" (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
238 printf(" (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3); */
240 zA = dy * (z2 - z1) / skip + z1;
241 zB = dy * (z3 - z1) / skip + z1;
243 /* printf(" zA = %.2f zB = %.2f\n", zA, zB );
244 printf(" xB - xA = %.2f\n", eg.col_step * dy / eg.row_step); */
246 elev = dx * (zB - zA) / dy + zA;
254 /* Revision 1.13 1997/07/12 02:27:11 curt
255 /* Looking at potential scenery transformation/coordinate system problems.
257 * Revision 1.12 1997/07/11 03:23:19 curt
258 * Solved some scenery display/orientation problems. Still have a positioning
259 * (or transformation?) problem.
261 * Revision 1.11 1997/07/11 01:30:02 curt
262 * More tweaking of terrian floor.
264 * Revision 1.10 1997/07/10 04:26:38 curt
265 * We now can interpolated ground elevation for any position in the grid. We
266 * can use this to enforce a "hard" ground. We still need to enforce some
267 * bounds checking so that we don't try to lookup data points outside the
270 * Revision 1.9 1997/07/10 02:22:10 curt
271 * Working on terrain elevation interpolation routine.
273 * Revision 1.8 1997/07/09 21:31:15 curt
274 * Working on making the ground "hard."
276 * Revision 1.7 1997/07/08 18:20:13 curt
277 * Working on establishing a hard ground.
279 * Revision 1.6 1997/06/29 21:16:49 curt
280 * More twiddling with the Scenery Management system.
282 * Revision 1.5 1997/06/22 21:44:41 curt
283 * Working on intergrating the VRML (subset) parser.
285 * Revision 1.4 1997/05/30 19:30:17 curt
286 * The LaRCsim flight model is starting to look like it is working.
288 * Revision 1.3 1997/05/23 15:40:41 curt
289 * Added GNU copyright headers.
291 * Revision 1.2 1997/05/19 18:20:50 curt
292 * Slight change to origin key words.
294 * Revision 1.1 1997/05/16 16:07:04 curt