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