]> git.mxchange.org Git - flightgear.git/blob - Main/mesh2GL.c
7ddf19a759988fe393ce30c6956729bea9bbef06
[flightgear.git] / Main / mesh2GL.c
1 /**************************************************************************
2  * mesh2ogl.c -- walk through a mesh data structure and make ogl 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 = 10;  /* Detail level 1 -- 1200 ... */
48
49     mesh = glGenLists(1);
50     glNewList(mesh, GL_COMPILE);
51     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color );
52     glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */
53
54     iend = m->cols - 1;
55     jend = m->rows - 1;
56     
57     y1 = m->originy;
58     y2 = y1 + (m->col_step * istep);
59     
60     for ( i = 0; i < iend; i += istep ) {
61         x1 = m->originx;
62         x2 = x1 + (m->row_step * jstep);
63         for ( j = 0; j < jend; j += jstep ) {
64             z11 = 0.03 * m->mesh_data[j         * m->rows + i        ];
65             z12 = 0.03 * m->mesh_data[j         * m->rows + (i+istep)];
66             z21 = 0.03 * m->mesh_data[(j+jstep) * m->rows + i        ];
67             z22 = 0.03 * m->mesh_data[(j+jstep) * m->rows + (i+istep)];
68
69             /* printf("x1 = %f  y1 = %f\n", x1, y1);
70             printf("x2 = %f  y2 = %f\n", x2, y2);
71             printf("z11 = %f  z12 = %f  z21 = %f  z22 = %f\n", 
72                    z11, z12, z21, z22); */
73
74             v1[0] = x2 - x1; v1[1] = 0;       v1[2] = z21 - z11;
75             v2[0] = x2 - x1; v2[1] = y2 - y1; v2[2] = z22 - z11;
76             mat3_cross_product(normal, v1, v2);
77             MAT3_NORMALIZE_VEC(normal,temp);
78             glNormal3fv(normal);
79             glBegin(GL_POLYGON);
80             glVertex3f(x1, y1, z11);
81             glVertex3f(x2, y1, z21);
82             glVertex3f(x2, y2, z22);
83             /* printf("(%f, %f, %f)\n", x1, y1, z11);
84             printf("(%f, %f, %f)\n", x2, y1, z21);
85             printf("(%f, %f, %f)\n", x2, y2, z22); */
86             glEnd();
87             
88             v1[0] = x2 - x1; v1[1] = y2 - y1; v1[2] = z22 - z11;
89             v2[0] = 0;       v2[1] = y2 - y1; v2[2] = z12 - z11;
90             mat3_cross_product(normal, v1, v2);
91             MAT3_NORMALIZE_VEC(normal,temp);
92             glNormal3fv(normal);
93             glBegin(GL_POLYGON);
94             glVertex3f(x1, y1, z11);
95             glVertex3f(x2, y2, z22);
96             glVertex3f(x1, y2, z12);
97             /* printf("(%f, %f, %f)\n", x1, y1, z11);
98             printf("(%f, %f, %f)\n", x2, y2, z22);
99             printf("(%f, %f, %f)\n", x1, y2, z12); */
100             glEnd();
101
102             x1 = x2;
103             x2 = x1 + (m->row_step * jstep);
104         }
105         y1 = y2;
106         y2 = y1 + (m->col_step * istep);
107     }
108
109     glEndList();
110
111     return(mesh);
112 }
113
114
115 /* $Log$
116 /* Revision 1.1  1997/05/21 15:57:52  curt
117 /* Renamed due to added GLUT support.
118 /*
119  * Revision 1.3  1997/05/19 18:22:42  curt
120  * Parameter tweaking ... starting to stub in fog support.
121  *
122  * Revision 1.2  1997/05/17 00:17:35  curt
123  * Trying to stub in support for standard OpenGL.
124  *
125  * Revision 1.1  1997/05/16 16:05:52  curt
126  * Initial revision.
127  *
128  */