]> git.mxchange.org Git - flightgear.git/blob - Scenery/mesh.c
Changed naming scheme of basic shared structures.
[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 = scenery.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 centerx, centery;
279     double randx, randy;
280
281     float x1, y1, x2, y2, z11, z12, z21, z22;
282     struct fgCartesianPoint p11, p12, p21, p22, c;
283     double gc_lon, gc_lat, sl_radius;
284
285     MAT3vec v1, v2, normal; 
286     int i, j, istep, jstep, iend, jend;
287     float temp;
288
289     printf("In mesh2GL(), generating GL call list.\n");
290
291     /* Detail level.  This is how big a step we take as we walk
292      * through the DEM data set.  This value is initialized in
293      * .../Scenery/scenery.c:fgSceneryInit() */
294     istep = jstep = scenery.terrain_skip ;
295
296     centerx = m->originx + (m->rows * m->row_step) / 2.0;
297     centery = m->originy + (m->cols * m->col_step) / 2.0;
298     fgGeodToGeoc(centery*ARCSEC_TO_RAD, 0, &sl_radius, &gc_lat);
299     c = fgPolarToCart(centerx*ARCSEC_TO_RAD, gc_lat, sl_radius);
300     scenery.center = c;
301
302     mesh = glGenLists(1);
303     glNewList(mesh, GL_COMPILE);
304
305     /* glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color ); */
306     glColor3fv(color);
307
308     iend = m->cols - 1;
309     jend = m->rows - 1;
310     
311     y1 = m->originy;
312     y2 = y1 + (m->col_step * istep);
313     
314     for ( i = 0; i < iend; i += istep ) {
315         x1 = m->originx;
316         x2 = x1 + (m->row_step * jstep);
317
318         glBegin(GL_TRIANGLE_STRIP);
319
320         for ( j = 0; j < jend; j += jstep ) {
321             z11 = m->mesh_data[i         * m->cols + j        ];
322             z12 = m->mesh_data[(i+istep) * m->cols + j        ];
323             z21 = m->mesh_data[i         * m->cols + (j+jstep)];
324             z22 = m->mesh_data[(i+istep) * m->cols + (j+jstep)];
325
326             /* printf("A geodetic point is (%.2f, %.2f, %.2f)\n", 
327                x1, y1, z11); */
328             gc_lon = x1*ARCSEC_TO_RAD;
329             fgGeodToGeoc(y1*ARCSEC_TO_RAD, z11, &sl_radius, &gc_lat);
330             /* printf("A geocentric point is (%.2f, %.2f, %.2f)\n", gc_lon, 
331                gc_lat, sl_radius+z11); */
332             p11 = fgPolarToCart(gc_lon, gc_lat, sl_radius+z11);
333             /* printf("A cart point is (%.8f, %.8f, %.8f)\n", 
334                p11.x, p11.y, p11.z); */
335
336             gc_lon = x1*ARCSEC_TO_RAD;
337             fgGeodToGeoc(y2*ARCSEC_TO_RAD, z12, &sl_radius, &gc_lat);
338             p12 = fgPolarToCart(gc_lon, gc_lat, sl_radius+z12);
339
340             gc_lon = x2*ARCSEC_TO_RAD;
341             fgGeodToGeoc(y1*ARCSEC_TO_RAD, z21, &sl_radius, &gc_lat);
342             p21 = fgPolarToCart(gc_lon, gc_lat, sl_radius+z21);
343
344             gc_lon = x2*ARCSEC_TO_RAD;
345             fgGeodToGeoc(y2*ARCSEC_TO_RAD, z22, &sl_radius, &gc_lat);
346             p22 = fgPolarToCart(gc_lon, gc_lat, sl_radius+z22);
347
348             v1[0] = p22.y - p11.y; v1[1] = p22.z - p11.z; v1[2] = z22 - z11;
349             v2[0] = p12.y - p11.y; v2[1] = p12.z - p11.z; v2[2] = z12 - z11;
350             MAT3cross_product(normal, v1, v2);
351             MAT3_NORMALIZE_VEC(normal,temp);
352             glNormal3d(normal[0], normal[1], normal[2]);
353             /* printf("normal 1 = (%.2f %.2f %.2f\n", normal[0], normal[1], 
354                    normal[2]); */
355             
356             if ( j == 0 ) {
357                 /* first time through */
358                 glVertex3d(p12.x - c.x, p12.y - c.y, p12.z - c.z);
359                 glVertex3d(p11.x - c.x, p11.y - c.y, p11.z - c.z);
360             }
361             
362             glVertex3d(p22.x - c.x, p22.y - c.y, p22.z - c.z);
363     
364             v2[0] = p21.y - p11.y; v2[1] = p21.z - p11.z; v2[2] = z21 - z11;
365             MAT3cross_product(normal, v2, v1);
366             MAT3_NORMALIZE_VEC(normal,temp);
367             glNormal3d(normal[0], normal[1], normal[2]);
368             /* printf("normal 2 = (%.2f %.2f %.2f\n", normal[0], normal[1], 
369                    normal[2]); */
370
371             glVertex3d(p21.x - c.x, p21.y - c.y, p21.z - c.z);
372
373             x1 += m->row_step * jstep;
374             x2 += m->row_step * jstep;
375         }
376         glEnd();
377
378         y1 += m->col_step * istep;
379         y2 += m->col_step * istep;
380     }
381
382     /* this will go, it's only here for testing/debugging */
383
384     for ( i = 0; i < 200; i++ ) {
385         randx = fg_random() * 3600.0;
386         randy = fg_random() * 3600.0;
387
388         /* mesh_make_test_object(m->originx + randx, m->originy + randy); */
389     }
390
391     glEndList();
392
393     return(mesh);
394 }
395
396
397 /* $Log$
398 /* Revision 1.21  1997/08/27 03:30:27  curt
399 /* Changed naming scheme of basic shared structures.
400 /*
401  * Revision 1.20  1997/08/19 23:55:08  curt
402  * Worked on better simulating real lighting.
403  *
404  * Revision 1.19  1997/08/06 00:24:28  curt
405  * Working on correct real time sun lighting.
406  *
407  * Revision 1.18  1997/08/02 19:10:14  curt
408  * Incorporated mesh2GL.c into mesh.c
409  *
410  * Revision 1.17  1997/07/18 23:41:26  curt
411  * Tweaks for building with Cygnus Win32 compiler.
412  *
413  * Revision 1.16  1997/07/16 20:04:51  curt
414  * Minor tweaks to aid Win32 port.
415  *
416  * Revision 1.15  1997/07/14 16:26:04  curt
417  * Testing/playing -- placed objects randomly across the entire terrain.
418  *
419  * Revision 1.14  1997/07/12 04:01:14  curt
420  * Added #include <Windows32/Base.h> to help Win32 compiling.
421  *
422  * Revision 1.13  1997/07/12 02:27:11  curt
423  * Looking at potential scenery transformation/coordinate system problems.
424  *
425  * Revision 1.12  1997/07/11 03:23:19  curt
426  * Solved some scenery display/orientation problems.  Still have a positioning
427  * (or transformation?) problem.
428  *
429  * Revision 1.11  1997/07/11 01:30:02  curt
430  * More tweaking of terrian floor.
431  *
432  * Revision 1.10  1997/07/10 04:26:38  curt
433  * We now can interpolated ground elevation for any position in the grid.  We
434  * can use this to enforce a "hard" ground.  We still need to enforce some
435  * bounds checking so that we don't try to lookup data points outside the
436  * grid data set.
437  *
438  * Revision 1.9  1997/07/10 02:22:10  curt
439  * Working on terrain elevation interpolation routine.
440  *
441  * Revision 1.8  1997/07/09 21:31:15  curt
442  * Working on making the ground "hard."
443  *
444  * Revision 1.7  1997/07/08 18:20:13  curt
445  * Working on establishing a hard ground.
446  *
447  * Revision 1.6  1997/06/29 21:16:49  curt
448  * More twiddling with the Scenery Management system.
449  *
450  * Revision 1.5  1997/06/22 21:44:41  curt
451  * Working on intergrating the VRML (subset) parser.
452  *
453  * Revision 1.4  1997/05/30 19:30:17  curt
454  * The LaRCsim flight model is starting to look like it is working.
455  *
456  * Revision 1.3  1997/05/23 15:40:41  curt
457  * Added GNU copyright headers.
458  *
459  * Revision 1.2  1997/05/19 18:20:50  curt
460  * Slight change to origin key words.
461  *
462  * Revision 1.1  1997/05/16 16:07:04  curt
463  * Initial revision.
464  *
465  */