1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* atunnels --- OpenGL Advanced Tunnel Demo */
5 static const char sccsid[] = "@(#)tunnel_draw.c 5.13 2004/05/25 xlockmore";
8 /* Copyright (c) E. Lassauge, 2002-2004. */
11 * Permission to use, copy, modify, and distribute this software and its
12 * documentation for any purpose and without fee is hereby granted,
13 * provided that the above copyright notice appear in all copies and that
14 * both that copyright notice and this permission notice appear in
15 * supporting documentation.
17 * This file is provided AS IS with no warranties of any kind. The author
18 * shall have no liability with respect to the infringement of copyrights,
19 * trade secrets or any patents by this file or any part thereof. In no
20 * event will the author be liable for any lost revenue or profits or
21 * other special, indirect and consequential damages.
23 * The original code for this mode was written by Roman Podobedov
25 * WEB: http://romka.demonews.com
27 * Eric Lassauge (May-25-2004) <lassauge@users.sourceforge.net>
28 * http://lassauge.free.fr/linux.html
31 * E.Lassauge - 25-May-2004:
32 * - added more texture
33 * - random texture init
41 #ifdef USE_GL /* whole file */
50 #include "tunnel_draw.h"
52 #ifdef STANDALONE /* For NRAND() */
53 #include "xlockmoreI.h" /* in xscreensaver distribution */
54 #else /* STANDALONE */
55 #include "xlock.h" /* in xlockmore distribution */
56 #endif /* STANDALONE */
60 float x, y, z; /* Point coordinates */
63 typedef struct _tnPath
72 const cvPoint initpath[]={
73 {0.000000, 0.000000, 0.000000},
74 {2.000000, 1.000000, 0.000000},
75 {4.000000, 0.000000, 0.000000},
76 {6.000000, 1.000000, 0.000000},
77 {8.000000, 0.000000, 1.000000},
78 {10.000000, 1.000000, 1.000000},
79 {12.000000, 1.500000, 0.000000},
80 {14.000000, 0.000000, 0.000000},
81 {16.000000, 1.000000, 0.000000},
82 {18.000000, 0.000000, 0.000000},
83 {20.000000, 0.000000, 1.000000},
84 {22.000000, 1.000000, 0.000000},
85 {24.000000, 0.000000, 1.000000},
86 {26.000000, 0.000000, 1.000000},
87 {28.000000, 1.000000, 0.000000},
88 {30.000000, 0.000000, 2.000000},
89 {32.000000, 1.000000, 0.000000},
90 {34.000000, 0.000000, 2.000000},
91 {-1.000000, -1.000000, -1.000000}
94 /* Camera variables */
96 static tnPath *cam_pos;
99 /* Tunnel Drawing Variables */
101 static cvPoint prev_points[10];
102 static int current_texture;
105 static float ModeX=0;
106 static int ModeXFlag=0;
108 /*=================== Vector normalization ==================================*/
109 void normalize(cvPoint *V)
114 d = (float)sqrt(V->x*V->x + V->y*V->y + V->z*V->z);
121 /*=================== C = A x B (Vector multiply) ==========================*/
122 void vect_mult(cvPoint *A, cvPoint *B, cvPoint *C)
124 /* Vector multiply */
125 C->x = A->y*B->z - A->z*B->y;
126 C->y = A->z*B->x - A->x*B->z;
127 C->z = A->x*B->y - A->y*B->x;
130 /* Catmull-Rom Curve calculations */
131 void cvCatmullRom(cvPoint *p, float t, cvPoint *outp)
139 outp->x = (-t*t1*p[0].x + (2-5*t2+3*t3)*p[1].x + t*(1+4*t-3*t2)*p[2].x - t2*(1-t)*p[3].x)/2;
140 outp->y = (-t*t1*p[0].y + (2-5*t2+3*t3)*p[1].y + t*(1+4*t-3*t2)*p[2].y - t2*(1-t)*p[3].y)/2;
141 outp->z = (-t*t1*p[0].z + (2-5*t2+3*t3)*p[1].z + t*(1+4*t-3*t2)*p[2].z - t2*(1-t)*p[3].z)/2;
144 /*=================== Point Rotating Around Line ===========================
145 // p - original point
147 // pl - pivot line (vector)
148 // a - angle to rotate in radians
149 // outp - output point
150 //==========================================================================
152 void RotateAroundLine(cvPoint *p, cvPoint *pp, cvPoint *pl, float a, cvPoint *outp)
155 float l, m, n, ca, sa;
168 p2.x = p1.x*((l*l)+ca*(1-l*l)) + p1.y*(l*(1-ca)*m+n*sa) + p1.z*(l*(1-ca)*n-m*sa);
169 p2.y = p1.x*(l*(1-ca)*m-n*sa) + p1.y*(m*m+ca*(1-m*m)) + p1.z*(m*(1-ca)*n+l*sa);
170 p2.z = p1.x*(l*(1-ca)*n+m*sa) + p1.y*(m*(1-ca)*n-l*sa) + p1.z*(n*n+ca*(1-n*n));
172 outp->x = p2.x + pp->x;
173 outp->y = p2.y + pp->y;
174 outp->z = p2.z + pp->z;
178 /*=================== Load camera and tunnel path ==========================*/
179 static void LoadPath(void)
182 tnPath *path1=NULL, *path2=NULL;
184 cvPoint *f = (cvPoint *)initpath;
195 path = (tnPath *)malloc(sizeof(tnPath));
200 path2 = (tnPath *)malloc(sizeof(tnPath));
215 /*=================== Tunnel Initialization ================================*/
216 void InitTunnel(void)
219 current_texture = NRAND(MAX_TEXTURE);
222 void DrawTunnel(int do_texture, int do_light, GLuint *textures)
224 tnPath *p, *p1, *cmpos;
225 cvPoint op, p4[4], T, ppp, ppp1, op1, op2;
229 GLfloat light_position[4];
232 /* Select current tunnel texture */
234 glBindTexture(GL_TEXTURE_2D, textures[current_texture]);
237 /* Get current curve */
238 if (cam_pos->next->next->next)
257 /* Get current camera position */
258 cvCatmullRom(p4, cam_t, &op);
260 /* Next camera position */
265 cmpos = cam_pos->next;
268 /* Get curve for next camera position */
269 if (cmpos->next->next->next)
288 /* Get next camera position */
289 cvCatmullRom(p4, cam_t, &op1);
292 glRotatef(alpha, 0, 0, -1);
294 /* Set camera position */
295 gluLookAt(op.x, op.y, op.z, op1.x, op1.y, op1.z, 0, 1, 0);
297 /* Set light position */
300 light_position[0] = op.x;
301 light_position[1] = op.y;
302 light_position[2] = op.z;
303 light_position[3] = 1;
304 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
311 /* Draw tunnel from current curve and next 2 curves */
315 if (p->next->next->next)
333 cvCatmullRom(p4, t, &op);
347 if (p->next->next->next)
366 cvCatmullRom(p4, t, &op1);
370 ppp1.z = op1.z + 0.25;
380 RotateAroundLine(&ppp, &op, &T, ((float)i*36.0*M_PI/180.0), &op2);
386 prev_points[i].x = op2.x;
387 prev_points[i].y = op2.y;
388 prev_points[i].z = op2.z;
398 /* Draw 10 polygons for current point */
403 glNormal3f(0, 0, 1); /* Normal for lighting */
404 glTexCoord2f(0, 0); glVertex3f(prev_points[i].x, prev_points[i].y, prev_points[i].z);
406 glTexCoord2f(1, 0); glVertex3f(points[i].x, points[i].y, points[i].z);
408 glTexCoord2f(1, 1); glVertex3f(points[j].x, points[j].y, points[j].z);
410 glTexCoord2f(0, 1); glVertex3f(prev_points[j].x, prev_points[j].y, prev_points[j].z);
412 /* Save current polygon coordinates for next position */
415 prev_points[i].x = points[i].x;
416 prev_points[i].y = points[i].y;
417 prev_points[i].z = points[i].z;
424 /* =================== Show splash screen =================================== */
425 void SplashScreen(int do_wire, int do_texture, int do_light)
429 /* Reset tunnel and camera position */
437 if (current_texture >= MAX_TEXTURE) current_texture = 0;
439 /* Now we want to draw splash screen */
441 /* Disable all unused features */
442 glDisable(GL_DEPTH_TEST);
443 glDisable(GL_LIGHTING);
445 glDisable(GL_CULL_FACE);
447 glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
449 glDisable(GL_TEXTURE_2D);
450 glColor4f(1, 1, 1, ModeX);
452 /* Draw splash screen (simply quad) */
454 glVertex3f(-10, -10, -1);
455 glVertex3f(10, -10, -1);
456 glVertex3f(10, 10, -1);
457 glVertex3f(-10, 10, -1);
461 if (ModeX <= 0) ModeX = 0;
465 glEnable(GL_CULL_FACE);
466 glEnable(GL_DEPTH_TEST);
470 glEnable(GL_LIGHTING);
475 glEnable(GL_TEXTURE_2D);
478 glColor4f(1, 1, 1, 1);