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