]> git.mxchange.org Git - flightgear.git/blob - Main/mesh2GL.c
Trying to get fog to work ...
[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  * $Id$
7  * (Log is kept at end of this file)
8  **************************************************************************/
9
10
11 #ifdef GLUT
12     #include <GL/glut.h>
13 #elif TIGER
14     /* assumes -I/usr/include/mesa in compile command */
15     #include "gltk.h"
16 #endif
17
18 #include "../scenery/mesh.h"
19 #include "mat3.h"
20
21
22 /* Sets the first vector to be the cross-product of the last two
23     vectors. */
24 static void mat3_cross_product(float result_vec[3], register float vec1[3], 
25                        register float vec2[3]) {
26    float tempvec[3];
27    register float *temp = tempvec;
28
29    temp[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
30    temp[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
31    temp[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
32
33    MAT3_COPY_VEC(result_vec, temp);
34 }
35
36
37 /* walk through mesh and make ogl calls */
38 GLint mesh_to_ogl(struct mesh *m) {
39     GLint mesh;
40     /* static GLfloat color[4] = { 0.3, 0.7, 0.2, 1.0 }; */
41
42     float x1, y1, x2, y2, z11, z12, z21, z22;
43     float v1[3], v2[3], normal[3]; 
44     int i, j, istep, jstep, iend, jend;
45     float temp;
46
47     istep = jstep = 25;  /* Detail level 1 -- 1200 ... */
48
49     mesh = glGenLists(1);
50     glNewList(mesh, GL_COMPILE);
51     /* glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color ); */
52
53     iend = m->cols - 1;
54     jend = m->rows - 1;
55     
56     y1 = 0.0; /* y1 = m->originy; */
57     y2 = y1 + (m->col_step * istep);
58     
59     for ( i = 0; i < iend; i += istep ) {
60         x1 = 0.0; /* x1 = m->originx; */
61         x2 = x1 + (m->row_step * jstep);
62         for ( j = 0; j < jend; j += jstep ) {
63             z11 = 0.03 * m->mesh_data[j         * m->rows + i        ];
64             z12 = 0.03 * m->mesh_data[j         * m->rows + (i+istep)];
65             z21 = 0.03 * m->mesh_data[(j+jstep) * m->rows + i        ];
66             z22 = 0.03 * m->mesh_data[(j+jstep) * m->rows + (i+istep)];
67
68             /* printf("x1 = %f  y1 = %f\n", x1, y1);
69             printf("x2 = %f  y2 = %f\n", x2, y2);
70             printf("z11 = %f  z12 = %f  z21 = %f  z22 = %f\n", 
71                    z11, z12, z21, z22); */
72
73             v1[0] = x2 - x1; v1[1] = 0;       v1[2] = z21 - z11;
74             v2[0] = x2 - x1; v2[1] = y2 - y1; v2[2] = z22 - z11;
75             mat3_cross_product(normal, v1, v2);
76             MAT3_NORMALIZE_VEC(normal,temp);
77             glBegin(GL_POLYGON);
78             glNormal3fv(normal);
79             glVertex3f(x1, y1, z11);
80             glVertex3f(x2, y1, z21);
81             glVertex3f(x2, y2, z22);
82             /* printf("(%f, %f, %f)\n", x1, y1, z11);
83             printf("(%f, %f, %f)\n", x2, y1, z21);
84             printf("(%f, %f, %f)\n", x2, y2, z22); */
85             glEnd();
86             
87             v1[0] = x2 - x1; v1[1] = y2 - y1; v1[2] = z22 - z11;
88             v2[0] = 0;       v2[1] = y2 - y1; v2[2] = z12 - z11;
89             mat3_cross_product(normal, v1, v2);
90             MAT3_NORMALIZE_VEC(normal,temp);
91             glBegin(GL_POLYGON);
92             glNormal3fv(normal);
93             glVertex3f(x1, y1, z11);
94             glVertex3f(x2, y2, z22);
95             glVertex3f(x1, y2, z12);
96             /* printf("(%f, %f, %f)\n", x1, y1, z11);
97             printf("(%f, %f, %f)\n", x2, y2, z22);
98             printf("(%f, %f, %f)\n", x1, y2, z12); */
99             glEnd();
100
101             x1 = x2;
102             x2 = x1 + (m->row_step * jstep);
103         }
104         y1 = y2;
105         y2 = y1 + (m->col_step * istep);
106     }
107
108     glEndList();
109
110     return(mesh);
111 }
112
113
114 /* $Log$
115 /* Revision 1.2  1997/05/23 00:35:13  curt
116 /* Trying to get fog to work ...
117 /*
118  * Revision 1.1  1997/05/21 15:57:52  curt
119  * Renamed due to added GLUT support.
120  *
121  * Revision 1.3  1997/05/19 18:22:42  curt
122  * Parameter tweaking ... starting to stub in fog support.
123  *
124  * Revision 1.2  1997/05/17 00:17:35  curt
125  * Trying to stub in support for standard OpenGL.
126  *
127  * Revision 1.1  1997/05/16 16:05:52  curt
128  * Initial revision.
129  *
130  */