]> git.mxchange.org Git - flightgear.git/blob - Main/mesh2GL.c
33192c7670e27d38c397ded5f000ad9b52751c57
[flightgear.git] / Main / mesh2GL.c
1 /**************************************************************************
2  * mesh2GL.c -- walk through a mesh data structure and make GL calls
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 #ifdef GLUT
28     #include <GL/glut.h>
29 #elif TIGER
30     /* assumes -I/usr/include/mesa in compile command */
31     #include "gltk.h"
32 #endif
33
34 #include "../scenery/mesh.h"
35 #include "../mat3/mat3.h"
36
37
38 /* walk through mesh and make ogl calls */
39 GLint mesh2GL(struct mesh *m) {
40     GLint mesh;
41     static GLfloat color[4] = { 0.3, 0.7, 0.2, 1.0 };
42
43     float x1, y1, x2, y2, z11, z12, z21, z22;
44
45     MAT3vec v1, v2, normal; 
46     int i, j, istep, jstep, iend, jend;
47     float temp;
48
49     istep = jstep = 25;  /* Detail level 1 -- 1200 ... */
50
51     mesh = glGenLists(1);
52     glNewList(mesh, GL_COMPILE);
53
54     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
55         
56     iend = m->cols - 1;
57     jend = m->rows - 1;
58     
59     y1 = m->originy;
60     y2 = y1 + (m->col_step * istep);
61     
62     for ( i = 0; i < iend; i += istep ) {
63         x1 = m->originx;
64         x2 = x1 + (m->row_step * jstep);
65
66         glBegin(GL_TRIANGLE_STRIP);
67
68         for ( j = 0; j < jend; j += jstep ) {
69             z11 = 0.03 * m->mesh_data[j         * m->rows + i        ];
70             z12 = 0.03 * m->mesh_data[j         * m->rows + (i+istep)];
71             z21 = 0.03 * m->mesh_data[(j+jstep) * m->rows + i        ];
72             z22 = 0.03 * m->mesh_data[(j+jstep) * m->rows + (i+istep)];
73
74             v1[0] = x2 - x1; v1[1] = 0;       v1[2] = z21 - z11;
75             v2[0] = 0;       v2[1] = y2 - y1; v2[2] = z12 - z11;
76             MAT3cross_product(normal, v1, v2);
77             MAT3_NORMALIZE_VEC(normal,temp);
78             glNormal3d(normal[0], normal[1], normal[2]);
79
80             if ( j == 0 ) {
81                 /* first time through */
82                 glVertex3f(x1, y1, z11);
83                 glVertex3f(x1, y2, z12);
84             }
85
86             glVertex3f(x2, y1, z21);
87             
88             v1[0] = x2 - x1; v1[1] = y1 - y2; v1[2] = z21 - z12;
89             v2[0] = x2 - x1; v2[1] = 0; v2[2] = z22 - z12;
90             MAT3cross_product(normal, v1, v2);
91             MAT3_NORMALIZE_VEC(normal,temp);
92             glNormal3d(normal[0], normal[1], normal[2]);
93             glVertex3f(x2, y2, z22);
94
95             x1 = x2;
96             x2 = x1 + (m->row_step * jstep);
97         }
98         glEnd();
99
100         y1 = y2;
101         y2 = y1 + (m->col_step * istep);
102     }
103
104     glEndList();
105
106     return(mesh);
107 }
108
109
110 /* $Log$
111 /* Revision 1.20  1997/06/18 04:10:32  curt
112 /* A couple more runway tweaks ...
113 /*
114  * Revision 1.19  1997/06/18 02:21:24  curt
115  * Hacked in a runway
116  *
117  * Revision 1.18  1997/06/17 04:19:17  curt
118  * More timer related tweaks with respect to view direction changes.
119  *
120  * Revision 1.17  1997/06/16 19:32:52  curt
121  * Starting to add general timer support.
122  *
123  * Revision 1.16  1997/06/02 03:40:07  curt
124  * A tiny bit more view tweaking.
125  *
126  * Revision 1.15  1997/06/02 03:01:38  curt
127  * Working on views (side, front, back, transitions, etc.)
128  *
129  * Revision 1.14  1997/05/31 19:16:26  curt
130  * Elevator trim added.
131  *
132  * Revision 1.13  1997/05/31 04:13:53  curt
133  * WE CAN NOW FLY!!!
134  *
135  * Continuing work on the LaRCsim flight model integration.
136  * Added some MSFS-like keyboard input handling.
137  *
138  * Revision 1.12  1997/05/30 23:26:20  curt
139  * Added elevator/aileron controls.
140  *
141  * Revision 1.11  1997/05/30 19:27:02  curt
142  * The LaRCsim flight model is starting to look like it is working.
143  *
144  * Revision 1.10  1997/05/30 03:54:11  curt
145  * Made a bit more progress towards integrating the LaRCsim flight model.
146  *
147  * Revision 1.9  1997/05/29 22:39:51  curt
148  * Working on incorporating the LaRCsim flight model.
149  *
150  * Revision 1.8  1997/05/29 12:31:40  curt
151  * Minor tweaks, moving towards general flight model integration.
152  *
153  * Revision 1.7  1997/05/29 02:33:24  curt
154  * Updated to reflect changing interfaces in other "modules."
155  *
156  * Revision 1.6  1997/05/27 17:44:32  curt
157  * Renamed & rearranged variables and routines.   Added some initial simple
158  * timer/alarm routines so the flight model can be updated on a regular 
159  * interval.
160  *
161  * Revision 1.5  1997/05/24 01:45:32  curt
162  * Fixed surface normals for triangle mesh.
163  *
164  * Revision 1.4  1997/05/23 20:05:24  curt
165  * First stab at using GL_TRIANGLE_STRIP's instead of GL_POLYGONS (to conserve
166  * memory)
167  *
168  * Revision 1.3  1997/05/23 15:40:26  curt
169  * Added GNU copyright headers.
170  * Fog now works!
171  *
172  * Revision 1.2  1997/05/23 00:35:13  curt
173  * Trying to get fog to work ...
174  *
175  * Revision 1.1  1997/05/21 15:57:52  curt
176  * Renamed due to added GLUT support.
177  *
178  * Revision 1.3  1997/05/19 18:22:42  curt
179  * Parameter tweaking ... starting to stub in fog support.
180  *
181  * Revision 1.2  1997/05/17 00:17:35  curt
182  * Trying to stub in support for standard OpenGL.
183  *
184  * Revision 1.1  1997/05/16 16:05:52  curt
185  * Initial revision.
186  *
187  */