]> git.mxchange.org Git - flightgear.git/blob - Scenery/mesh.c
Working on making the ground "hard."
[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 "mesh.h"
35 #include "common.h"
36
37
38 /* Temporary hack until we get the scenery management system running */
39 extern GLint mesh_hack;
40 extern struct mesh eg;
41
42 /* initialize the non-array mesh values */
43 void mesh_init(struct mesh *m) {
44     m->originx = 0.0;
45     m->originy = 0.0;
46
47     m->rows = 0;
48     m->cols = 0;
49
50     m->row_step = 0.0;
51     m->col_step = 0.0;
52
53     m->cur_row = 0;
54     m->cur_col = 0;
55     m->do_data = 0;
56 }
57
58
59 /* return a pointer to a new mesh structure (no data array allocated yet) */
60 struct mesh *(new_mesh)() {
61     struct mesh *mesh_ptr;
62
63     mesh_ptr = (struct mesh *)malloc(sizeof(struct mesh));
64
65     if ( mesh_ptr == 0 ) {
66         printf("Virtual memory exceeded\n");
67         exit(-1);
68     }
69
70     mesh_ptr->cur_row = 0;
71     mesh_ptr->cur_col = 0;
72
73     return(mesh_ptr);
74 }
75
76
77 /* return a pointer to a dynamically allocated array */
78 float *(new_mesh_data)(int nrows, int ncols) {
79     float *mesh_data_ptr;
80
81     mesh_data_ptr = (float *)malloc(nrows * ncols * sizeof(float));
82
83     if ( mesh_data_ptr == 0 ) {
84         printf("Virtual memory exceeded\n");
85         exit(-1);
86     }
87
88     printf("Allocated float(%d, %d)\n", nrows, ncols);
89
90     return(mesh_data_ptr);
91 }
92
93
94 /* set the option name in the mesh data structure */
95 void mesh_set_option_name(struct mesh *m, char *name) {
96     if ( strlen(name) < MAX_IDENT_LEN ) {
97         strcpy(m->option_name, name);
98     } else {
99         strncpy(m->option_name, name, MAX_IDENT_LEN - 1);
100         m->option_name[MAX_IDENT_LEN - 1] = '\0';
101     }
102     if ( strcmp(m->option_name, "do_data") == 0 ) {
103         m->do_data = 1;
104     } else {
105         m->do_data = 0;
106     }
107 }
108
109
110 /* set an option value in the mesh data structure */
111 void mesh_set_option_value(struct mesh *m, char *value) {
112     /* printf("Setting %s to %s\n", m->option_name, value); */
113
114     if ( m->do_data ) {
115         /* mesh data is a pseudo 2d array */
116         /* printf("Setting mesh_data[%d][%d] to %s\n", m->cur_row, m->cur_col, 
117                value); */
118         m->mesh_data[m->cur_row * m->rows + m->cur_col] = atof(value);
119         m->cur_col++;
120         if ( m->cur_col >= m->cols ) {
121             m->cur_col = 0;
122             m->cur_row++;
123             if ( m->cur_row > m->rows ) {
124                 m->do_data = 0;
125             }
126         }
127     } else if ( strcmp(m->option_name, "origin_lon") == 0 ) {
128         m->originx = atof(value);
129     } else if ( strcmp(m->option_name, "origin_lat") == 0 ) {
130         m->originy = atof(value);
131     } else if ( strcmp(m->option_name, "rows") == 0 ) {
132         m->rows = atoi(value);
133     } else if ( strcmp(m->option_name, "cols") == 0 ) {
134         m->cols = atoi(value);
135     } else if ( strcmp(m->option_name, "row_step") == 0 ) {
136         m->row_step = atof(value);
137     } else if ( strcmp(m->option_name, "col_step") == 0 ) {
138         m->col_step = atof(value);
139     } else {
140         printf("Unknown option %s with value %s, ignoring ...\n", 
141                m->option_name, value);
142     }
143 }
144
145
146 /* do whatever needs to be done with the mesh now that it's been
147    loaded, such as generating the OpenGL call list. */
148 void mesh_do_it(struct mesh *m) {
149     mesh_hack = mesh2GL(m);
150 }
151
152
153 /* return the current altitude based on mesh data.  We should rewrite
154  * this to interpolate exact values, but for now this is good enough */
155 double mesh_altitude(double lon, double lat) {
156     /* we expect incoming (lon,lat) to be in arcsec for now */
157
158     double xoffset, yoffset;
159     int xindex, yindex;
160
161     /* determine if we are in the lower triangle or the upper triangle 
162        ______
163        |   /|
164        |  / |
165        | /  |
166        |/   |
167        ------
168      */
169
170     xoffset = lon - eg.originx;
171     yoffset = lat - eg.originy;
172
173     xindex = xoffset / eg.col_step;
174     yindex = yoffset / eg.row_step;
175
176     if ( xindex > yindex ) {
177     }
178     if ( (xindex >= 0) && (xindex < eg.cols) ) {
179         if ( (yindex >= 0) && (yindex < eg.rows) ) {
180             return( eg.mesh_data[xindex * eg.rows + yindex] );
181         }
182     }
183
184     /*
185       given (x1, y1, z1) (x2, y2, z2) and (x3, y3, z3) 
186       calculate z = ax + by + c (the equation of the plane intersecting the 
187       three given points
188
189       Then, given a position we can calculate the current ground elevation
190
191       tmp1 = (x2 * z1 / x1 - z2);
192       tmp2 = (y2 - x2 * y1 / x1);
193       tmp3 = (x2 * y1 / x1 - y2);
194       tmp4 = (1 - x2 / x1);
195       tmp5 = (x3*(z1 + y1*tmp1 / tmp2) / x1 - z3 + y3*tmp1 / tmp3);
196       tmp6 = x3*(y1*tmp4 / tmp2 - 1);
197       tmp7 = tmp5 / (y3*tmp4 / tmp2 - tmp6 / x1 - 1);
198       tmp8 = (tmp6 / x1 + y3*tmp4 / tmp3 + 1);
199       tmp9 = (z1 + tmp5 / tmp8);
200       tmp10 = (tmp7 + x2*tmp9 / x1 - z2);
201       
202       a = (tmp9 + y1*tmp10 / tmp2) / x1;
203       
204       b = tmp10 / tmp3;
205       
206       c = tmp7;
207      */
208
209 }
210
211
212 /* $Log$
213 /* Revision 1.8  1997/07/09 21:31:15  curt
214 /* Working on making the ground "hard."
215 /*
216  * Revision 1.7  1997/07/08 18:20:13  curt
217  * Working on establishing a hard ground.
218  *
219  * Revision 1.6  1997/06/29 21:16:49  curt
220  * More twiddling with the Scenery Management system.
221  *
222  * Revision 1.5  1997/06/22 21:44:41  curt
223  * Working on intergrating the VRML (subset) parser.
224  *
225  * Revision 1.4  1997/05/30 19:30:17  curt
226  * The LaRCsim flight model is starting to look like it is working.
227  *
228  * Revision 1.3  1997/05/23 15:40:41  curt
229  * Added GNU copyright headers.
230  *
231  * Revision 1.2  1997/05/19 18:20:50  curt
232  * Slight change to origin key words.
233  *
234  * Revision 1.1  1997/05/16 16:07:04  curt
235  * Initial revision.
236  *
237  */