]> git.mxchange.org Git - flightgear.git/blob - Scenery/mesh.c
Working on intergrating the VRML (subset) parser.
[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 "mesh.h"
33 #include "common.h"
34
35
36 /* initialize the non-array mesh values */
37 void mesh_init(struct mesh *m) {
38     m->originx = 0.0;
39     m->originy = 0.0;
40
41     m->rows = 0;
42     m->cols = 0;
43
44     m->row_step = 0.0;
45     m->col_step = 0.0;
46
47     m->cur_row = 0;
48     m->cur_col = 0;
49     m->do_data = 0;
50 }
51
52
53 /* return a pointer to a new mesh structure (no data array allocated yet) */
54 struct mesh *(new_mesh)() {
55     struct mesh *mesh_ptr;
56
57     mesh_ptr = (struct mesh *)malloc(sizeof(struct mesh));
58
59     if ( mesh_ptr == 0 ) {
60         printf("Virtual memory exceeded\n");
61         exit(-1);
62     }
63
64     mesh_ptr->cur_row = 0;
65     mesh_ptr->cur_col = 0;
66
67     return(mesh_ptr);
68 }
69
70
71 /* return a pointer to a dynamically allocated array */
72 float *(new_mesh_data)(int nrows, int ncols) {
73     float *mesh_data_ptr;
74
75     mesh_data_ptr = (float *)malloc(nrows * ncols * sizeof(float));
76
77     if ( mesh_data_ptr == 0 ) {
78         printf("Virtual memory exceeded\n");
79         exit(-1);
80     }
81
82     printf("Allocated float(%d, %d)\n", nrows, ncols);
83
84     return(mesh_data_ptr);
85 }
86
87
88 /* set the option name in the mesh data structure */
89 void mesh_set_option_name(struct mesh *m, char *name) {
90     if ( strlen(name) < MAX_IDENT_LEN ) {
91         strcpy(m->option_name, name);
92     } else {
93         strncpy(m->option_name, name, MAX_IDENT_LEN - 1);
94         m->option_name[MAX_IDENT_LEN - 1] = '\0';
95     }
96     if ( strcmp(m->option_name, "do_data") == 0 ) {
97         m->do_data = 1;
98     } else {
99         m->do_data = 0;
100     }
101 }
102
103
104 /* set an option value in the mesh data structure */
105 void mesh_set_option_value(struct mesh *m, char *value) {
106     /* printf("Setting %s to %s\n", m->option_name, value); */
107
108     if ( m->do_data ) {
109         /* mesh data is a pseudo 2d array */
110         /* printf("Setting mesh_data[%d][%d] to %s\n", m->cur_row, m->cur_col, 
111                value); */
112         m->mesh_data[m->cur_row * m->rows + m->cur_col] = atof(value);
113         m->cur_col++;
114         if ( m->cur_col >= m->cols ) {
115             m->cur_col = 0;
116             m->cur_row++;
117             if ( m->cur_row > m->rows ) {
118                 m->do_data = 0;
119             }
120         }
121     } else if ( strcmp(m->option_name, "origin_lon") == 0 ) {
122         m->originx = atof(value);
123     } else if ( strcmp(m->option_name, "origin_lat") == 0 ) {
124         m->originy = atof(value);
125     } else if ( strcmp(m->option_name, "rows") == 0 ) {
126         m->rows = atoi(value);
127     } else if ( strcmp(m->option_name, "cols") == 0 ) {
128         m->cols = atoi(value);
129     } else if ( strcmp(m->option_name, "row_step") == 0 ) {
130         m->row_step = atof(value);
131     } else if ( strcmp(m->option_name, "col_step") == 0 ) {
132         m->col_step = atof(value);
133     } else {
134         printf("Unknown option %s with value %s, ignoring ...\n", 
135                m->option_name, value);
136     }
137 }
138
139
140 /* do whatever needs to be done with the mesh now that it's been
141    loaded, such as generating the OpenGL call list. */
142 void mesh_do_it(struct mesh *m) {
143     mesh2GL(m);
144 }
145
146
147 /* $Log$
148 /* Revision 1.5  1997/06/22 21:44:41  curt
149 /* Working on intergrating the VRML (subset) parser.
150 /*
151  * Revision 1.4  1997/05/30 19:30:17  curt
152  * The LaRCsim flight model is starting to look like it is working.
153  *
154  * Revision 1.3  1997/05/23 15:40:41  curt
155  * Added GNU copyright headers.
156  *
157  * Revision 1.2  1997/05/19 18:20:50  curt
158  * Slight change to origin key words.
159  *
160  * Revision 1.1  1997/05/16 16:07:04  curt
161  * Initial revision.
162  *
163  */