File: perfdraw.c

package info (click to toggle)
glut 3.7-14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,556 kB
  • ctags: 45,170
  • sloc: ansic: 148,716; makefile: 35,208; ada: 2,062; yacc: 473; fortran: 290; lex: 131; csh: 51; sed: 49; sh: 33
file content (331 lines) | stat: -rw-r--r-- 9,203 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331

/*
 * perfdraw.c - $Revision: 1.4 $
 */

#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include "skyfly.h"

#if !defined(GL_VERSION_1_1)
#if defined(GL_EXT_texture_object)
#define glBindTexture(A,B)     glBindTextureEXT(A,B)
#define glGenTextures(A,B)     glGenTexturesEXT(A,B)
#define glDeleteTextures(A,B)  glDeleteTexturesEXT(A,B)
#else
#define glBindTexture(A,B)
#define glGenTextures(A,B)
#define glDeleteTextures(A,B)
#endif
#endif

/* static routine decls */

extern int clouds;
static void drawlitmesh_11(float *objdata);
static void drawcolrtexmesh_10(float *objdata);
static void drawclouds(float *objdata);

void drawperfobj(perfobj_t *perfobj)
{
    float     *vdata_ptr =(float *) perfobj->vdata;
    extern void texenv(int), lightpos(void);

    unsigned int *flagsptr = perfobj->flags;
    float     *dp;

    for (;;) {
        switch (*flagsptr) {
		/*
		 * A paper plane is a single tmesh folded on itself so the orientations
		 * of some triangles in the mesh are incorrect with respect to
		 * their normals. This is solved by drawing the tmesh twice;
		 * first draw only backfaces, then only frontfaces.
		 */
		case PD_DRAW_PAPER_PLANE:
			flagsptr += 1;
			glCullFace(GL_FRONT);
			drawlitmesh_11(vdata_ptr);
			glCullFace(GL_BACK);
			drawlitmesh_11((float *)((perfobj_vert_t *) vdata_ptr + 11));
			glPopMatrix();
			break;

		case PD_DRAW_TERRAIN_CELL:
			dp = *(float **) (flagsptr + 1);
			flagsptr += 2;
			drawcolrtexmesh_10(dp);
			break;

		case PD_DRAW_CLOUDS:
                        if (rgbmode) {
#if 0
                            glColor3ub(0x30, 0x40, 0xb0);
#else
                            glColor3f(1.0f, 1.0f, 1.0f);
#endif
                        }
			glDisable(GL_CULL_FACE);
			glDisable(GL_DEPTH_TEST);
			/*texenv(2);*/
			drawclouds(vdata_ptr);
			glEnable(GL_DEPTH_TEST);
			glEnable(GL_CULL_FACE);
			flagsptr += 1;
			break;

		case PD_PAPER_PLANE_MODE:
			switch (*(flagsptr + 1)) {
			case PLANES_START:
                            glShadeModel(GL_FLAT);
                            glEnable(GL_LIGHTING);
                            glDisable(GL_TEXTURE_2D);
                            if (fog && !rgbmode)
                                glDisable(GL_FOG);
                            break;
			case PLANES_END:
                            glShadeModel(GL_SMOOTH);
                            glDisable(GL_LIGHTING);
                            if (fog && !rgbmode && FOG_LEVELS > 1)
                                glEnable(GL_FOG);
                            break;
                        }	
			flagsptr += 2;
			break;

		case PD_PAPER_PLANE_POS:        /* contains the pushmatrix */
			glPushMatrix();
			glTranslatef(*(vdata_ptr), *(vdata_ptr + 1), *(vdata_ptr + 2));
			glRotatef(*(vdata_ptr + 3), 0.0, 0.0, 1.0);
			glRotatef(*(vdata_ptr + 4), 0.0, 1.0, 0.0);
			glRotatef(*(vdata_ptr + 5), 1.0, 0.0, 0.0);
			flagsptr += 1;
			break;

		case PD_VIEWER_POS:
			glLoadIdentity();
			glRotatef(-90., 1.0, 0., 0.);
			glRotatef(*(vdata_ptr + 3) * RAD_TO_DEG, 0.0, 0.0, 1.0); /* yaw */
			lightpos();
			glTranslatef(-*(vdata_ptr), -*(vdata_ptr + 1), -*(vdata_ptr + 2));
			flagsptr += 1;
			break;

		case PD_TEXTURE_BIND:
			glBindTexture(GL_TEXTURE_2D, *(flagsptr + 1));
			texenv(*(flagsptr + 1));
			glEnable(GL_TEXTURE_2D);
			flagsptr += 2;
			break;

		case PD_END:
			return;

		default:
			fprintf(stderr, "Bad PD primitive %d\n", *flagsptr);
			flagsptr++;
			break;
		}
	}
}

/*
 * Notice how the following routines unwind loops and pre-compute indexes
 * at compile time. This is crucial in obtaining the maximum data transfer 
 * from cpu to the graphics pipe.
 */
static void drawlitmesh_11(float *op)
{
    glBegin(GL_TRIANGLE_STRIP);
    /* one */
    glNormal3fv((op + PD_V_NORMAL));
    glVertex3fv((op + PD_V_POINT));
    /* two */
    glNormal3fv((op + (PD_V_SIZE + PD_V_NORMAL)));
    glVertex3fv((op + (PD_V_SIZE + PD_V_POINT)));
    /* three */
    glNormal3fv((op + (2 * PD_V_SIZE + PD_V_NORMAL)));
    glVertex3fv((op + (2 * PD_V_SIZE + PD_V_POINT)));
    /* four */
    glNormal3fv((op + (3 * PD_V_SIZE + PD_V_NORMAL)));
    glVertex3fv((op + (3 * PD_V_SIZE + PD_V_POINT)));
    /* five */
    glNormal3fv((op + (4 * PD_V_SIZE + PD_V_NORMAL)));
    glVertex3fv((op + (4 * PD_V_SIZE + PD_V_POINT)));
    /* six */
    glNormal3fv((op + (5 * PD_V_SIZE + PD_V_NORMAL)));
    glVertex3fv((op + (5 * PD_V_SIZE + PD_V_POINT)));
    /* seven */
    glNormal3fv((op + (6 * PD_V_SIZE + PD_V_NORMAL)));
    glVertex3fv((op + (6 * PD_V_SIZE + PD_V_POINT)));
    /* eight */
    glNormal3fv((op + (7 * PD_V_SIZE + PD_V_NORMAL)));
    glVertex3fv((op + (7 * PD_V_SIZE + PD_V_POINT)));
    /* nine */
    glNormal3fv((op + (8 * PD_V_SIZE + PD_V_NORMAL)));
    glVertex3fv((op + (8 * PD_V_SIZE + PD_V_POINT)));
    /* ten */
    glNormal3fv((op + (9 * PD_V_SIZE + PD_V_NORMAL)));
    glVertex3fv((op + (9 * PD_V_SIZE + PD_V_POINT)));
    /* eleven */
    glNormal3fv((op + (10 * PD_V_SIZE + PD_V_NORMAL)));
    glVertex3fv((op + (10 * PD_V_SIZE + PD_V_POINT)));

	glEnd();

}

static void drawcolrtexmesh_10(float *op)
{
    glBegin(GL_TRIANGLE_STRIP);
	/* one */
    glTexCoord2fv((op + PD_V_TEX));
    glColor3fv((op + PD_V_COLOR));
    glVertex3fv((op + PD_V_POINT));
    /* two */
    glTexCoord2fv((op + (PD_V_SIZE + PD_V_TEX)));
    glColor3fv((op + (PD_V_SIZE + PD_V_COLOR)));
    glVertex3fv((op + (PD_V_SIZE + PD_V_POINT)));
    /* three */
    glTexCoord2fv((op + (2 * PD_V_SIZE + PD_V_TEX)));
    glColor3fv((op + (2 * PD_V_SIZE + PD_V_COLOR)));
    glVertex3fv((op + (2 * PD_V_SIZE + PD_V_POINT)));
    /* four */
    glTexCoord2fv((op + (3 * PD_V_SIZE + PD_V_TEX)));
    glColor3fv((op + (3 * PD_V_SIZE + PD_V_COLOR)));
    glVertex3fv((op + (3 * PD_V_SIZE + PD_V_POINT)));
    /* five */
    glTexCoord2fv((op + (4 * PD_V_SIZE + PD_V_TEX)));
    glColor3fv((op + (4 * PD_V_SIZE + PD_V_COLOR)));
    glVertex3fv((op + (4 * PD_V_SIZE + PD_V_POINT)));
    /* six */
    glTexCoord2fv((op + (5 * PD_V_SIZE + PD_V_TEX)));
    glColor3fv((op + (5 * PD_V_SIZE + PD_V_COLOR)));
    glVertex3fv((op + (5 * PD_V_SIZE + PD_V_POINT)));
    /* seven */
    glTexCoord2fv((op + (6 * PD_V_SIZE + PD_V_TEX)));
    glColor3fv((op + (6 * PD_V_SIZE + PD_V_COLOR)));
    glVertex3fv((op + (6 * PD_V_SIZE + PD_V_POINT)));
    /* eight */
    glTexCoord2fv((op + (7 * PD_V_SIZE + PD_V_TEX)));
    glColor3fv((op + (7 * PD_V_SIZE + PD_V_COLOR)));
    glVertex3fv((op + (7 * PD_V_SIZE + PD_V_POINT)));
    /* nine */
    glTexCoord2fv((op + (8 * PD_V_SIZE + PD_V_TEX)));
    glColor3fv((op + (8 * PD_V_SIZE + PD_V_COLOR)));
    glVertex3fv((op + (8 * PD_V_SIZE + PD_V_POINT)));
    /* ten */
    glTexCoord2fv((op + (9 * PD_V_SIZE + PD_V_TEX)));
    glColor3fv((op + (9 * PD_V_SIZE + PD_V_COLOR)));
    glVertex3fv((op + (9 * PD_V_SIZE + PD_V_POINT)));

    glEnd();
}

static void drawclouds(float *op)
{
#define SKY_STRIPS 24

	/* Break into quad strips so cheap fog works better */
	if (0 == clouds) {

        GLfloat *vc0, *vc1, *vc2;
        GLfloat *tc0, *tc1, *tc2;
        GLfloat sky_s0, sky_s1, sky_t0, sky_t1;
        GLfloat sky_x0, sky_x1, sky_y0, sky_y1, sky_z;
        int ii, jj;
        GLfloat s0, s1, t0, t1;
        GLfloat x0, x1, y0, y1, z0;

        vc0 = op + PD_V_POINT;
        vc1 = op + PD_V_POINT + PD_V_SIZE;
        vc2 = op + PD_V_POINT + PD_V_SIZE * 2;

        tc0 = op + PD_V_TEX;
        tc1 = op + PD_V_TEX + PD_V_SIZE;
        tc2 = op + PD_V_TEX + PD_V_SIZE * 2;

        sky_s0 = tc0[0];
        sky_s1 = tc1[0];
        sky_t0 = tc0[1];
        sky_t1 = tc2[1];

        sky_x0 = vc0[0];
        sky_x1 = vc1[0];
        sky_y0 = vc0[1];
        sky_y1 = vc2[1];
        sky_z  = vc0[2];

        clouds = glGenLists(1);

        glNewList(clouds, GL_COMPILE);

        s1 = (1.0f / SKY_STRIPS) * (sky_s1 - sky_s0);
        t1 = (1.0f / SKY_STRIPS) * (sky_t1 - sky_t0);
        x1 = (1.0f / SKY_STRIPS) * (sky_x1 - sky_x0);
        y1 = (1.0f / SKY_STRIPS) * (sky_y1 - sky_y0);

        z0 = sky_z;
        s0 = sky_s0;
        x0 = sky_x0;

        for (ii = 0; ii < SKY_STRIPS; ii++, s0 += s1, x0 += x1) {

            t0 = sky_t0;
            y0 = sky_y0;

            glBegin(GL_QUAD_STRIP);

            glTexCoord2f(s0, t0);
            glVertex3f(x0, y0, z0);

            glTexCoord2f(s0 + s1, t0);
            glVertex3f(x0 + x1, y0, z0);

            for (jj = 0; jj < SKY_STRIPS; jj++, t0 += t1, y0 += y1) {

                glTexCoord2f(s0 + s1, t0 + t1);
                glVertex3f(x0 + x1, y0 + y1, z0);

                glTexCoord2f(s0, t0 + t1);
                glVertex3f(x0, y0 + y1, z0);
            }

            glEnd();

        }

        glEndList();
    }

	glCallList(clouds);
}

void putv3fdata(float *v, perfobj_vert_t *ptr)
{
  ptr->vert[0] = v[0];
  ptr->vert[1] = v[1];
  ptr->vert[2] = v[2];
}

void putc3fdata(float *c, perfobj_vert_t *ptr)
{
  ptr->color[0] = c[0];
  ptr->color[1] = c[1];
  ptr->color[2] = c[2];
}

void putn3fdata(float *n, perfobj_vert_t *ptr)
{
  ptr->normal[0] = n[0];
  ptr->normal[1] = n[1];
  ptr->normal[2] = n[2];
}

void putt2fdata(float *t, perfobj_vert_t *ptr)
{
  ptr->texture[0] = t[0];
  ptr->texture[1] = t[1];
}