]> git.mxchange.org Git - flightgear.git/blob - Main/mesh2GL.c
5524fab3148216724aa01dae91d8230cc1bc5c95
[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.h"
36
37
38 /* Sets the first vector to be the cross-product of the last two
39     vectors. */
40 static void mat3_cross_product(float result_vec[3], register float vec1[3], 
41                        register float vec2[3]) {
42    float tempvec[3];
43    register float *temp = tempvec;
44
45    temp[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
46    temp[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
47    temp[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
48
49    MAT3_COPY_VEC(result_vec, temp);
50 }
51
52
53 /* walk through mesh and make ogl calls */
54 GLint mesh2GL(struct mesh *m) {
55     GLint mesh;
56
57     float x1, y1, x2, y2, z11, z12, z21, z22;
58     float v1[3], v2[3], normal[3]; 
59     int i, j, istep, jstep, iend, jend;
60     float temp;
61
62     istep = jstep = 8;  /* Detail level 1 -- 1200 ... */
63
64     mesh = glGenLists(1);
65     glNewList(mesh, GL_COMPILE);
66
67     iend = m->cols - 1;
68     jend = m->rows - 1;
69     
70     y1 = m->originy;
71     y2 = y1 + (m->col_step * istep);
72     
73     for ( i = 0; i < iend; i += istep ) {
74         x1 = m->originx;
75         x2 = x1 + (m->row_step * jstep);
76
77         glBegin(GL_TRIANGLE_STRIP);
78
79         for ( j = 0; j < jend; j += jstep ) {
80             z11 = 0.03 * m->mesh_data[j         * m->rows + i        ];
81             z12 = 0.03 * m->mesh_data[j         * m->rows + (i+istep)];
82             z21 = 0.03 * m->mesh_data[(j+jstep) * m->rows + i        ];
83             z22 = 0.03 * m->mesh_data[(j+jstep) * m->rows + (i+istep)];
84
85             v1[0] = x2 - x1; v1[1] = 0;       v1[2] = z21 - z11;
86             v2[0] = 0;       v2[1] = y2 - y1; v2[2] = z12 - z11;
87             mat3_cross_product(normal, v1, v2);
88             MAT3_NORMALIZE_VEC(normal,temp);
89             glNormal3fv(normal);
90
91             if ( j == 0 ) {
92                 /* first time through */
93                 glVertex3f(x1, y1, z11);
94                 glVertex3f(x1, y2, z12);
95             }
96
97             glVertex3f(x2, y1, z21);
98             
99             v1[0] = x2 - x1; v1[1] = y1 - y2; v1[2] = z21 - z12;
100             v2[0] = x2 - x1; v2[1] = 0; v2[2] = z22 - z12;
101             mat3_cross_product(normal, v1, v2);
102             MAT3_NORMALIZE_VEC(normal,temp);
103             glNormal3fv(normal);
104             glVertex3f(x2, y2, z22);
105
106             x1 = x2;
107             x2 = x1 + (m->row_step * jstep);
108         }
109         glEnd();
110
111         y1 = y2;
112         y2 = y1 + (m->col_step * istep);
113     }
114
115     glEndList();
116
117     return(mesh);
118 }
119
120
121 /* $Log$
122 /* Revision 1.8  1997/05/29 12:31:40  curt
123 /* Minor tweaks, moving towards general flight model integration.
124 /*
125  * Revision 1.7  1997/05/29 02:33:24  curt
126  * Updated to reflect changing interfaces in other "modules."
127  *
128  * Revision 1.6  1997/05/27 17:44:32  curt
129  * Renamed & rearranged variables and routines.   Added some initial simple
130  * timer/alarm routines so the flight model can be updated on a regular 
131  * interval.
132  *
133  * Revision 1.5  1997/05/24 01:45:32  curt
134  * Fixed surface normals for triangle mesh.
135  *
136  * Revision 1.4  1997/05/23 20:05:24  curt
137  * First stab at using GL_TRIANGLE_STRIP's instead of GL_POLYGONS (to conserve
138  * memory)
139  *
140  * Revision 1.3  1997/05/23 15:40:26  curt
141  * Added GNU copyright headers.
142  * Fog now works!
143  *
144  * Revision 1.2  1997/05/23 00:35:13  curt
145  * Trying to get fog to work ...
146  *
147  * Revision 1.1  1997/05/21 15:57:52  curt
148  * Renamed due to added GLUT support.
149  *
150  * Revision 1.3  1997/05/19 18:22:42  curt
151  * Parameter tweaking ... starting to stub in fog support.
152  *
153  * Revision 1.2  1997/05/17 00:17:35  curt
154  * Trying to stub in support for standard OpenGL.
155  *
156  * Revision 1.1  1997/05/16 16:05:52  curt
157  * Initial revision.
158  *
159  */