]> git.mxchange.org Git - flightgear.git/blob - Scenery/mesh.c
Tweaks for building with Cygnus Win32 compiler.
[flightgear.git] / Scenery / mesh.c
1 /**************************************************************************
2  * mesh.c -- data structures and routines for processing terrain meshes
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 #ifndef __CYGWIN32__
28 #  include <malloc.h>
29 #endif
30
31 #ifdef WIN32
32 #  include <windows.h>
33 #endif
34
35 #include <stdio.h>
36 #include <stdlib.h>  /* atof(), atoi() */
37 #include <string.h>
38
39 #include <GL/glut.h>
40
41 #include "../constants.h"
42 #include "scenery.h"
43 #include "mesh.h"
44 #include "common.h"
45
46
47 /* Temporary hack until we get the scenery management system running */
48 extern GLint mesh_hack;
49 extern struct mesh eg;
50
51 /* initialize the non-array mesh values */
52 void mesh_init(struct mesh *m) {
53     m->originx = 0.0;
54     m->originy = 0.0;
55
56     m->rows = 0;
57     m->cols = 0;
58
59     m->row_step = 0.0;
60     m->col_step = 0.0;
61
62     m->cur_row = 0;
63     m->cur_col = 0;
64     m->do_data = 0;
65 }
66
67
68 /* return a pointer to a new mesh structure (no data array allocated yet) */
69 struct mesh *(new_mesh)() {
70     struct mesh *mesh_ptr;
71
72     mesh_ptr = (struct mesh *)malloc(sizeof(struct mesh));
73
74     if ( mesh_ptr == 0 ) {
75         printf("Virtual memory exceeded\n");
76         exit(-1);
77     }
78
79     mesh_ptr->cur_row = 0;
80     mesh_ptr->cur_col = 0;
81
82     return(mesh_ptr);
83 }
84
85
86 /* return a pointer to a dynamically allocated array */
87 float *(new_mesh_data)(int nrows, int ncols) {
88     float *mesh_data_ptr;
89
90     mesh_data_ptr = (float *)malloc(nrows * ncols * sizeof(float));
91
92     if ( mesh_data_ptr == 0 ) {
93         printf("Virtual memory exceeded\n");
94         exit(-1);
95     }
96
97     printf("Allocated float(%d, %d)\n", nrows, ncols);
98
99     return(mesh_data_ptr);
100 }
101
102
103 /* set the option name in the mesh data structure */
104 void mesh_set_option_name(struct mesh *m, char *name) {
105     if ( strlen(name) < MAX_IDENT_LEN ) {
106         strcpy(m->option_name, name);
107     } else {
108         strncpy(m->option_name, name, MAX_IDENT_LEN - 1);
109         m->option_name[MAX_IDENT_LEN - 1] = '\0';
110     }
111     if ( strcmp(m->option_name, "do_data") == 0 ) {
112         m->do_data = 1;
113     } else {
114         m->do_data = 0;
115     }
116 }
117
118
119 /* set an option value in the mesh data structure */
120 void mesh_set_option_value(struct mesh *m, char *value) {
121     /* printf("Setting %s to %s\n", m->option_name, value); */
122
123     if ( m->do_data ) {
124         /* mesh data is a pseudo 2d array */
125         /* printf("Setting mesh_data[%d][%d] to %s\n", m->cur_row, m->cur_col, 
126                value); */
127         m->mesh_data[m->cur_row * m->cols + m->cur_col] = atof(value);
128         m->cur_col++;
129         if ( m->cur_col >= m->cols ) {
130             m->cur_col = 0;
131             m->cur_row++;
132             if ( m->cur_row > m->rows ) {
133                 m->do_data = 0;
134             }
135         }
136     } else if ( strcmp(m->option_name, "origin_lon") == 0 ) {
137         m->originx = atof(value);
138     } else if ( strcmp(m->option_name, "origin_lat") == 0 ) {
139         m->originy = atof(value);
140     } else if ( strcmp(m->option_name, "rows") == 0 ) {
141         m->rows = atoi(value);
142     } else if ( strcmp(m->option_name, "cols") == 0 ) {
143         m->cols = atoi(value);
144     } else if ( strcmp(m->option_name, "row_step") == 0 ) {
145         m->row_step = atof(value);
146     } else if ( strcmp(m->option_name, "col_step") == 0 ) {
147         m->col_step = atof(value);
148     } else {
149         printf("Unknown option %s with value %s, ignoring ...\n", 
150                m->option_name, value);
151     }
152 }
153
154
155 /* do whatever needs to be done with the mesh now that it's been
156    loaded, such as generating the OpenGL call list. */
157 void mesh_do_it(struct mesh *m) {
158     mesh_hack = mesh2GL(m);
159 }
160
161
162 /* return the current altitude based on mesh data.  We should rewrite
163  * this to interpolate exact values, but for now this is good enough */
164 double mesh_altitude(double lon, double lat) {
165     /* we expect incoming (lon,lat) to be in arcsec for now */
166
167     double xlocal, ylocal, dx, dy, zA, zB, elev;
168     int x1, y1, z1, x2, y2, z2, x3, y3, z3;
169     int xindex, yindex;
170     int skip;
171
172     skip = cur_scenery_params.terrain_skip;
173     /* determine if we are in the lower triangle or the upper triangle 
174        ______
175        |   /|
176        |  / |
177        | /  |
178        |/   |
179        ------
180
181        then calculate our end points
182      */
183
184     xlocal = (lon - eg.originx) / eg.col_step;
185     ylocal = (lat - eg.originy) / eg.row_step;
186
187     xindex = (int)(xlocal / skip) * skip;
188     yindex = (int)(ylocal / skip) * skip;
189
190     if ( (xindex < 0) || (xindex + skip >= eg.cols) ||
191          (yindex < 0) || (yindex + skip >= eg.rows) ) {
192         return(-9999);
193     }
194
195     dx = xlocal - xindex;
196     dy = ylocal - yindex;
197
198     if ( dx > dy ) {
199         /* lower triangle */
200         /* printf("  Lower triangle\n"); */
201
202         x1 = xindex; 
203         y1 = yindex; 
204         z1 = eg.mesh_data[y1 * eg.cols + x1];
205
206         x2 = xindex + skip; 
207         y2 = yindex; 
208         z2 = eg.mesh_data[y2 * eg.cols + x2];
209                                   
210         x3 = xindex + skip; 
211         y3 = yindex + skip; 
212         z3 = eg.mesh_data[y3 * eg.cols + x3];
213
214         /* printf("  dx = %.2f  dy = %.2f\n", dx, dy);
215         printf("  (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
216         printf("  (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
217         printf("  (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3); */
218
219         zA = dx * (z2 - z1) / skip + z1;
220         zB = dx * (z3 - z1) / skip + z1;
221         
222         /* printf("  zA = %.2f  zB = %.2f\n", zA, zB); */
223
224         if ( dx > EPSILON ) {
225             elev = dy * (zB - zA) / dx + zA;
226         } else {
227             elev = zA;
228         }
229     } else {
230         /* upper triangle */
231         /* printf("  Upper triangle\n"); */
232
233         x1 = xindex; 
234         y1 = yindex; 
235         z1 = eg.mesh_data[y1 * eg.cols + x1];
236
237         x2 = xindex; 
238         y2 = yindex + skip; 
239         z2 = eg.mesh_data[y2 * eg.cols + x2];
240                                   
241         x3 = xindex + skip; 
242         y3 = yindex + skip; 
243         z3 = eg.mesh_data[y3 * eg.cols + x3];
244
245         /* printf("  dx = %.2f  dy = %.2f\n", dx, dy);
246         printf("  (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
247         printf("  (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
248         printf("  (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3); */
249  
250         zA = dy * (z2 - z1) / skip + z1;
251         zB = dy * (z3 - z1) / skip + z1;
252         
253         /* printf("  zA = %.2f  zB = %.2f\n", zA, zB );
254         printf("  xB - xA = %.2f\n", eg.col_step * dy / eg.row_step); */
255
256         if ( dy > EPSILON ) {
257             elev = dx * (zB - zA) / dy    + zA;
258         } else {
259             elev = zA;
260         }
261     }
262
263     return(elev);
264 }
265
266
267 /* $Log$
268 /* Revision 1.17  1997/07/18 23:41:26  curt
269 /* Tweaks for building with Cygnus Win32 compiler.
270 /*
271  * Revision 1.16  1997/07/16 20:04:51  curt
272  * Minor tweaks to aid Win32 port.
273  *
274  * Revision 1.15  1997/07/14 16:26:04  curt
275  * Testing/playing -- placed objects randomly across the entire terrain.
276  *
277  * Revision 1.14  1997/07/12 04:01:14  curt
278  * Added #include <Windows32/Base.h> to help Win32 compiling.
279  *
280  * Revision 1.13  1997/07/12 02:27:11  curt
281  * Looking at potential scenery transformation/coordinate system problems.
282  *
283  * Revision 1.12  1997/07/11 03:23:19  curt
284  * Solved some scenery display/orientation problems.  Still have a positioning
285  * (or transformation?) problem.
286  *
287  * Revision 1.11  1997/07/11 01:30:02  curt
288  * More tweaking of terrian floor.
289  *
290  * Revision 1.10  1997/07/10 04:26:38  curt
291  * We now can interpolated ground elevation for any position in the grid.  We
292  * can use this to enforce a "hard" ground.  We still need to enforce some
293  * bounds checking so that we don't try to lookup data points outside the
294  * grid data set.
295  *
296  * Revision 1.9  1997/07/10 02:22:10  curt
297  * Working on terrain elevation interpolation routine.
298  *
299  * Revision 1.8  1997/07/09 21:31:15  curt
300  * Working on making the ground "hard."
301  *
302  * Revision 1.7  1997/07/08 18:20:13  curt
303  * Working on establishing a hard ground.
304  *
305  * Revision 1.6  1997/06/29 21:16:49  curt
306  * More twiddling with the Scenery Management system.
307  *
308  * Revision 1.5  1997/06/22 21:44:41  curt
309  * Working on intergrating the VRML (subset) parser.
310  *
311  * Revision 1.4  1997/05/30 19:30:17  curt
312  * The LaRCsim flight model is starting to look like it is working.
313  *
314  * Revision 1.3  1997/05/23 15:40:41  curt
315  * Added GNU copyright headers.
316  *
317  * Revision 1.2  1997/05/19 18:20:50  curt
318  * Slight change to origin key words.
319  *
320  * Revision 1.1  1997/05/16 16:07:04  curt
321  * Initial revision.
322  *
323  */