]> git.mxchange.org Git - flightgear.git/blob - Scenery/mesh.c
Looking at potential scenery transformation/coordinate system problems.
[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 #include <stdio.h>
31 #include <stdlib.h>  /* atof(), atoi() */
32 #include <string.h>
33
34 #include <GL/glut.h>
35
36 #include "scenery.h"
37 #include "mesh.h"
38 #include "common.h"
39
40
41 /* Temporary hack until we get the scenery management system running */
42 extern GLint mesh_hack;
43 extern struct mesh eg;
44
45 /* initialize the non-array mesh values */
46 void mesh_init(struct mesh *m) {
47     m->originx = 0.0;
48     m->originy = 0.0;
49
50     m->rows = 0;
51     m->cols = 0;
52
53     m->row_step = 0.0;
54     m->col_step = 0.0;
55
56     m->cur_row = 0;
57     m->cur_col = 0;
58     m->do_data = 0;
59 }
60
61
62 /* return a pointer to a new mesh structure (no data array allocated yet) */
63 struct mesh *(new_mesh)() {
64     struct mesh *mesh_ptr;
65
66     mesh_ptr = (struct mesh *)malloc(sizeof(struct mesh));
67
68     if ( mesh_ptr == 0 ) {
69         printf("Virtual memory exceeded\n");
70         exit(-1);
71     }
72
73     mesh_ptr->cur_row = 0;
74     mesh_ptr->cur_col = 0;
75
76     return(mesh_ptr);
77 }
78
79
80 /* return a pointer to a dynamically allocated array */
81 float *(new_mesh_data)(int nrows, int ncols) {
82     float *mesh_data_ptr;
83
84     mesh_data_ptr = (float *)malloc(nrows * ncols * sizeof(float));
85
86     if ( mesh_data_ptr == 0 ) {
87         printf("Virtual memory exceeded\n");
88         exit(-1);
89     }
90
91     printf("Allocated float(%d, %d)\n", nrows, ncols);
92
93     return(mesh_data_ptr);
94 }
95
96
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);
101     } else {
102         strncpy(m->option_name, name, MAX_IDENT_LEN - 1);
103         m->option_name[MAX_IDENT_LEN - 1] = '\0';
104     }
105     if ( strcmp(m->option_name, "do_data") == 0 ) {
106         m->do_data = 1;
107     } else {
108         m->do_data = 0;
109     }
110 }
111
112
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); */
116
117     if ( m->do_data ) {
118         /* mesh data is a pseudo 2d array */
119         /* printf("Setting mesh_data[%d][%d] to %s\n", m->cur_row, m->cur_col, 
120                value); */
121         m->mesh_data[m->cur_row * m->cols + m->cur_col] = atof(value);
122         m->cur_col++;
123         if ( m->cur_col >= m->cols ) {
124             m->cur_col = 0;
125             m->cur_row++;
126             if ( m->cur_row > m->rows ) {
127                 m->do_data = 0;
128             }
129         }
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);
142     } else {
143         printf("Unknown option %s with value %s, ignoring ...\n", 
144                m->option_name, value);
145     }
146 }
147
148
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);
153 }
154
155
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 */
160
161     double xlocal, ylocal, dx, dy, zA, zB, elev;
162     int x1, y1, z1, x2, y2, z2, x3, y3, z3;
163     int xindex, yindex;
164     int skip;
165
166     skip = cur_scenery_params.terrain_skip;
167     /* determine if we are in the lower triangle or the upper triangle 
168        ______
169        |   /|
170        |  / |
171        | /  |
172        |/   |
173        ------
174
175        then calculate our end points
176      */
177
178     xlocal = (lon - eg.originx) / eg.col_step;
179     ylocal = (lat - eg.originy) / eg.row_step;
180
181     xindex = (int)(xlocal / skip) * skip;
182     yindex = (int)(ylocal / skip) * skip;
183
184     if ( (xindex < 0) || (xindex + skip >= eg.cols) ||
185          (yindex < 0) || (yindex + skip >= eg.rows) ) {
186         return(-9999);
187     }
188
189     dx = xlocal - xindex;
190     dy = ylocal - yindex;
191
192     if ( dx > dy ) {
193         /* lower triangle */
194         /* printf("  Lower triangle\n"); */
195
196         x1 = xindex; 
197         y1 = yindex; 
198         z1 = eg.mesh_data[y1 * eg.cols + x1];
199
200         x2 = xindex + skip; 
201         y2 = yindex; 
202         z2 = eg.mesh_data[y2 * eg.cols + x2];
203                                   
204         x3 = xindex + skip; 
205         y3 = yindex + skip; 
206         z3 = eg.mesh_data[y3 * eg.cols + x3];
207
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); */
212
213         zA = dx * (z2 - z1) / skip + z1;
214         zB = dx * (z3 - z1) / skip + z1;
215         
216         /* printf("  zA = %.2f  zB = %.2f\n", zA, zB); */
217
218         elev = dy * (zB - zA) / dx + zA;
219     } else {
220         /* upper triangle */
221         /* printf("  Upper triangle\n"); */
222
223         x1 = xindex; 
224         y1 = yindex; 
225         z1 = eg.mesh_data[y1 * eg.cols + x1];
226
227         x2 = xindex; 
228         y2 = yindex + skip; 
229         z2 = eg.mesh_data[y2 * eg.cols + x2];
230                                   
231         x3 = xindex + skip; 
232         y3 = yindex + skip; 
233         z3 = eg.mesh_data[y3 * eg.cols + x3];
234
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); */
239  
240         zA = dy * (z2 - z1) / skip + z1;
241         zB = dy * (z3 - z1) / skip + z1;
242         
243         /* printf("  zA = %.2f  zB = %.2f\n", zA, zB );
244         printf("  xB - xA = %.2f\n", eg.col_step * dy / eg.row_step); */
245
246         elev = dx * (zB - zA) / dy    + zA;
247     }
248
249     return(elev);
250 }
251
252
253 /* $Log$
254 /* Revision 1.13  1997/07/12 02:27:11  curt
255 /* Looking at potential scenery transformation/coordinate system problems.
256 /*
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.
260  *
261  * Revision 1.11  1997/07/11 01:30:02  curt
262  * More tweaking of terrian floor.
263  *
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
268  * grid data set.
269  *
270  * Revision 1.9  1997/07/10 02:22:10  curt
271  * Working on terrain elevation interpolation routine.
272  *
273  * Revision 1.8  1997/07/09 21:31:15  curt
274  * Working on making the ground "hard."
275  *
276  * Revision 1.7  1997/07/08 18:20:13  curt
277  * Working on establishing a hard ground.
278  *
279  * Revision 1.6  1997/06/29 21:16:49  curt
280  * More twiddling with the Scenery Management system.
281  *
282  * Revision 1.5  1997/06/22 21:44:41  curt
283  * Working on intergrating the VRML (subset) parser.
284  *
285  * Revision 1.4  1997/05/30 19:30:17  curt
286  * The LaRCsim flight model is starting to look like it is working.
287  *
288  * Revision 1.3  1997/05/23 15:40:41  curt
289  * Added GNU copyright headers.
290  *
291  * Revision 1.2  1997/05/19 18:20:50  curt
292  * Slight change to origin key words.
293  *
294  * Revision 1.1  1997/05/16 16:07:04  curt
295  * Initial revision.
296  *
297  */