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