]> git.mxchange.org Git - flightgear.git/blob - Scenery/mesh.c
Incorporated mesh2GL.c into mesh.c
[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 "../types.h"
43 #include "../Math/fg_geodesy.h"
44 #include "../Math/fg_random.h"
45 #include "../Math/mat3.h"
46 #include "../Math/polar.h"
47
48 #include "mesh.h"
49 #include "common.h"
50 #include "scenery.h"
51
52
53 /* Temporary hack until we get the scenery management system running */
54 extern GLint mesh_hack;
55 extern struct mesh eg;
56
57 /* initialize the non-array mesh values */
58 void mesh_init(struct mesh *m) {
59     m->originx = 0.0;
60     m->originy = 0.0;
61
62     m->rows = 0;
63     m->cols = 0;
64
65     m->row_step = 0.0;
66     m->col_step = 0.0;
67
68     m->cur_row = 0;
69     m->cur_col = 0;
70     m->do_data = 0;
71 }
72
73
74 /* return a pointer to a new mesh structure (no data array allocated yet) */
75 struct mesh *(new_mesh)() {
76     struct mesh *mesh_ptr;
77
78     mesh_ptr = (struct mesh *)malloc(sizeof(struct mesh));
79
80     if ( mesh_ptr == 0 ) {
81         printf("Virtual memory exceeded\n");
82         exit(-1);
83     }
84
85     mesh_ptr->cur_row = 0;
86     mesh_ptr->cur_col = 0;
87
88     return(mesh_ptr);
89 }
90
91
92 /* return a pointer to a dynamically allocated array */
93 float *(new_mesh_data)(int nrows, int ncols) {
94     float *mesh_data_ptr;
95
96     mesh_data_ptr = (float *)malloc(nrows * ncols * sizeof(float));
97
98     if ( mesh_data_ptr == 0 ) {
99         printf("Virtual memory exceeded\n");
100         exit(-1);
101     }
102
103     printf("Allocated float(%d, %d)\n", nrows, ncols);
104
105     return(mesh_data_ptr);
106 }
107
108
109 /* set the option name in the mesh data structure */
110 void mesh_set_option_name(struct mesh *m, char *name) {
111     if ( strlen(name) < MAX_IDENT_LEN ) {
112         strcpy(m->option_name, name);
113     } else {
114         strncpy(m->option_name, name, MAX_IDENT_LEN - 1);
115         m->option_name[MAX_IDENT_LEN - 1] = '\0';
116     }
117     if ( strcmp(m->option_name, "do_data") == 0 ) {
118         m->do_data = 1;
119     } else {
120         m->do_data = 0;
121     }
122 }
123
124
125 /* set an option value in the mesh data structure */
126 void mesh_set_option_value(struct mesh *m, char *value) {
127     /* printf("Setting %s to %s\n", m->option_name, value); */
128
129     if ( m->do_data ) {
130         /* mesh data is a pseudo 2d array */
131         /* printf("Setting mesh_data[%d][%d] to %s\n", m->cur_row, m->cur_col, 
132                value); */
133         m->mesh_data[m->cur_row * m->cols + m->cur_col] = atof(value);
134         m->cur_col++;
135         if ( m->cur_col >= m->cols ) {
136             m->cur_col = 0;
137             m->cur_row++;
138             if ( m->cur_row > m->rows ) {
139                 m->do_data = 0;
140             }
141         }
142     } else if ( strcmp(m->option_name, "origin_lon") == 0 ) {
143         m->originx = atof(value);
144     } else if ( strcmp(m->option_name, "origin_lat") == 0 ) {
145         m->originy = atof(value);
146     } else if ( strcmp(m->option_name, "rows") == 0 ) {
147         m->rows = atoi(value);
148     } else if ( strcmp(m->option_name, "cols") == 0 ) {
149         m->cols = atoi(value);
150     } else if ( strcmp(m->option_name, "row_step") == 0 ) {
151         m->row_step = atof(value);
152     } else if ( strcmp(m->option_name, "col_step") == 0 ) {
153         m->col_step = atof(value);
154     } else {
155         printf("Unknown option %s with value %s, ignoring ...\n", 
156                m->option_name, value);
157     }
158 }
159
160
161 /* do whatever needs to be done with the mesh now that it's been
162    loaded, such as generating the OpenGL call list. */
163 void mesh_do_it(struct mesh *m) {
164     mesh_hack = mesh_to_OpenGL(m);
165 }
166
167
168 /* return the current altitude based on mesh data.  We should rewrite
169  * this to interpolate exact values, but for now this is good enough */
170 double mesh_altitude(double lon, double lat) {
171     /* we expect incoming (lon,lat) to be in arcsec for now */
172
173     double xlocal, ylocal, dx, dy, zA, zB, elev;
174     int x1, y1, z1, x2, y2, z2, x3, y3, z3;
175     int xindex, yindex;
176     int skip;
177
178     skip = cur_scenery_params.terrain_skip;
179     /* determine if we are in the lower triangle or the upper triangle 
180        ______
181        |   /|
182        |  / |
183        | /  |
184        |/   |
185        ------
186
187        then calculate our end points
188      */
189
190     xlocal = (lon - eg.originx) / eg.col_step;
191     ylocal = (lat - eg.originy) / eg.row_step;
192
193     xindex = (int)(xlocal / skip) * skip;
194     yindex = (int)(ylocal / skip) * skip;
195
196     if ( (xindex < 0) || (xindex + skip >= eg.cols) ||
197          (yindex < 0) || (yindex + skip >= eg.rows) ) {
198         return(-9999);
199     }
200
201     dx = xlocal - xindex;
202     dy = ylocal - yindex;
203
204     if ( dx > dy ) {
205         /* lower triangle */
206         /* printf("  Lower triangle\n"); */
207
208         x1 = xindex; 
209         y1 = yindex; 
210         z1 = eg.mesh_data[y1 * eg.cols + x1];
211
212         x2 = xindex + skip; 
213         y2 = yindex; 
214         z2 = eg.mesh_data[y2 * eg.cols + x2];
215                                   
216         x3 = xindex + skip; 
217         y3 = yindex + skip; 
218         z3 = eg.mesh_data[y3 * eg.cols + x3];
219
220         /* printf("  dx = %.2f  dy = %.2f\n", dx, dy);
221         printf("  (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
222         printf("  (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
223         printf("  (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3); */
224
225         zA = dx * (z2 - z1) / skip + z1;
226         zB = dx * (z3 - z1) / skip + z1;
227         
228         /* printf("  zA = %.2f  zB = %.2f\n", zA, zB); */
229
230         if ( dx > EPSILON ) {
231             elev = dy * (zB - zA) / dx + zA;
232         } else {
233             elev = zA;
234         }
235     } else {
236         /* upper triangle */
237         /* printf("  Upper triangle\n"); */
238
239         x1 = xindex; 
240         y1 = yindex; 
241         z1 = eg.mesh_data[y1 * eg.cols + x1];
242
243         x2 = xindex; 
244         y2 = yindex + skip; 
245         z2 = eg.mesh_data[y2 * eg.cols + x2];
246                                   
247         x3 = xindex + skip; 
248         y3 = yindex + skip; 
249         z3 = eg.mesh_data[y3 * eg.cols + x3];
250
251         /* printf("  dx = %.2f  dy = %.2f\n", dx, dy);
252         printf("  (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
253         printf("  (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
254         printf("  (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3); */
255  
256         zA = dy * (z2 - z1) / skip + z1;
257         zB = dy * (z3 - z1) / skip + z1;
258         
259         /* printf("  zA = %.2f  zB = %.2f\n", zA, zB );
260         printf("  xB - xA = %.2f\n", eg.col_step * dy / eg.row_step); */
261
262         if ( dy > EPSILON ) {
263             elev = dx * (zB - zA) / dy    + zA;
264         } else {
265             elev = zA;
266         }
267     }
268
269     return(elev);
270 }
271
272
273 /* walk through mesh and make opengl calls */
274 GLint mesh_to_OpenGL(struct mesh *m) {
275     GLint mesh;
276     /* static GLfloat color[4] = { 0.5, 0.4, 0.25, 1.0 }; */ /* dark desert */
277     static GLfloat color[4] = { 0.5, 0.5, 0.25, 1.0 };
278     double randx, randy;
279
280     float x1, y1, x2, y2, z11, z12, z21, z22;
281     struct fgCartesianPoint p11, p12, p21, p22;
282     double gc_lon, gc_lat, sl_radius;
283
284     MAT3vec v1, v2, normal; 
285     int i, j, istep, jstep, iend, jend;
286     float temp;
287
288     printf("In mesh2GL(), generating GL call list.\n");
289
290     /* Detail level.  This is how big a step we take as we walk
291      * through the DEM data set.  This value is initialized in
292      * .../Scenery/scenery.c:fgSceneryInit() */
293     istep = jstep = cur_scenery_params.terrain_skip ;
294
295     mesh = glGenLists(1);
296     glNewList(mesh, GL_COMPILE);
297
298     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
299
300     iend = m->cols - 1;
301     jend = m->rows - 1;
302     
303     y1 = m->originy;
304     y2 = y1 + (m->col_step * istep);
305     
306     for ( i = 0; i < iend; i += istep ) {
307         x1 = m->originx;
308         x2 = x1 + (m->row_step * jstep);
309
310         glBegin(GL_TRIANGLE_STRIP);
311
312         for ( j = 0; j < jend; j += jstep ) {
313             z11 = m->mesh_data[i         * m->cols + j        ];
314             z12 = m->mesh_data[(i+istep) * m->cols + j        ];
315             z21 = m->mesh_data[i         * m->cols + (j+jstep)];
316             z22 = m->mesh_data[(i+istep) * m->cols + (j+jstep)];
317
318             /* printf("A geodetic point is (%.2f, %.2f, %.2f)\n", 
319                x1, y1, z11); */
320             gc_lon = x1*ARCSEC_TO_RAD;
321             fgGeodToGeoc(y1*ARCSEC_TO_RAD, z11, &sl_radius, &gc_lat);
322             /* printf("A geocentric point is (%.2f, %.2f, %.2f)\n", gc_lon, 
323                gc_lat, sl_radius+z11); */
324             p11 = fgPolarToCart(gc_lon, gc_lat, sl_radius+z11);
325             /* printf("A cart point is (%.8f, %.8f, %.8f)\n", 
326                p11.x, p11.y, p11.z); */
327
328             gc_lon = x1*ARCSEC_TO_RAD;
329             fgGeodToGeoc(y2*ARCSEC_TO_RAD, z12, &sl_radius, &gc_lat);
330             p12 = fgPolarToCart(gc_lon, gc_lat, sl_radius+z12);
331
332             gc_lon = x2*ARCSEC_TO_RAD;
333             fgGeodToGeoc(y1*ARCSEC_TO_RAD, z21, &sl_radius, &gc_lat);
334             p21 = fgPolarToCart(gc_lon, gc_lat, sl_radius+z21);
335
336             gc_lon = x2*ARCSEC_TO_RAD;
337             fgGeodToGeoc(y2*ARCSEC_TO_RAD, z22, &sl_radius, &gc_lat);
338             p22 = fgPolarToCart(gc_lon, gc_lat, sl_radius+z22);
339
340             v1[0] = p22.y - p11.y; v1[1] = p22.z - p11.z; v1[2] = z22 - z11;
341             v2[0] = p12.y - p11.y; v2[1] = p12.z - p11.z; v2[2] = z12 - z11;
342             MAT3cross_product(normal, v1, v2);
343             MAT3_NORMALIZE_VEC(normal,temp);
344             glNormal3d(normal[0], normal[1], normal[2]);
345             /* printf("normal 1 = (%.2f %.2f %.2f\n", normal[0], normal[1], 
346                    normal[2]); */
347             
348             if ( j == 0 ) {
349                 /* first time through */
350                 glVertex3d(p12.x, p12.y, p12.z);
351                 glVertex3d(p11.x, p11.y, p11.z);
352             }
353             
354             glVertex3d(p22.x, p22.y, p22.z);
355     
356             v2[0] = p21.y - p11.y; v2[1] = p21.z - p11.z; v2[2] = z21 - z11;
357             MAT3cross_product(normal, v2, v1);
358             MAT3_NORMALIZE_VEC(normal,temp);
359             glNormal3d(normal[0], normal[1], normal[2]);
360             /* printf("normal 2 = (%.2f %.2f %.2f\n", normal[0], normal[1], 
361                    normal[2]); */
362
363             glVertex3d(p21.x, p21.y, p21.z);
364
365             x1 += m->row_step * jstep;
366             x2 += m->row_step * jstep;
367         }
368         glEnd();
369
370         y1 += m->col_step * istep;
371         y2 += m->col_step * istep;
372     }
373
374     /* this will go, it's only here for testing/debugging */
375
376     for ( i = 0; i < 200; i++ ) {
377         randx = fg_random() * 3600.0;
378         randy = fg_random() * 3600.0;
379
380         /* mesh_make_test_object(m->originx + randx, m->originy + randy); */
381     }
382
383     glEndList();
384
385     return(mesh);
386 }
387
388
389 /* $Log$
390 /* Revision 1.18  1997/08/02 19:10:14  curt
391 /* Incorporated mesh2GL.c into mesh.c
392 /*
393  * Revision 1.17  1997/07/18 23:41:26  curt
394  * Tweaks for building with Cygnus Win32 compiler.
395  *
396  * Revision 1.16  1997/07/16 20:04:51  curt
397  * Minor tweaks to aid Win32 port.
398  *
399  * Revision 1.15  1997/07/14 16:26:04  curt
400  * Testing/playing -- placed objects randomly across the entire terrain.
401  *
402  * Revision 1.14  1997/07/12 04:01:14  curt
403  * Added #include <Windows32/Base.h> to help Win32 compiling.
404  *
405  * Revision 1.13  1997/07/12 02:27:11  curt
406  * Looking at potential scenery transformation/coordinate system problems.
407  *
408  * Revision 1.12  1997/07/11 03:23:19  curt
409  * Solved some scenery display/orientation problems.  Still have a positioning
410  * (or transformation?) problem.
411  *
412  * Revision 1.11  1997/07/11 01:30:02  curt
413  * More tweaking of terrian floor.
414  *
415  * Revision 1.10  1997/07/10 04:26:38  curt
416  * We now can interpolated ground elevation for any position in the grid.  We
417  * can use this to enforce a "hard" ground.  We still need to enforce some
418  * bounds checking so that we don't try to lookup data points outside the
419  * grid data set.
420  *
421  * Revision 1.9  1997/07/10 02:22:10  curt
422  * Working on terrain elevation interpolation routine.
423  *
424  * Revision 1.8  1997/07/09 21:31:15  curt
425  * Working on making the ground "hard."
426  *
427  * Revision 1.7  1997/07/08 18:20:13  curt
428  * Working on establishing a hard ground.
429  *
430  * Revision 1.6  1997/06/29 21:16:49  curt
431  * More twiddling with the Scenery Management system.
432  *
433  * Revision 1.5  1997/06/22 21:44:41  curt
434  * Working on intergrating the VRML (subset) parser.
435  *
436  * Revision 1.4  1997/05/30 19:30:17  curt
437  * The LaRCsim flight model is starting to look like it is working.
438  *
439  * Revision 1.3  1997/05/23 15:40:41  curt
440  * Added GNU copyright headers.
441  *
442  * Revision 1.2  1997/05/19 18:20:50  curt
443  * Slight change to origin key words.
444  *
445  * Revision 1.1  1997/05/16 16:07:04  curt
446  * Initial revision.
447  *
448  */