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