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 */
58 #include "tunnel_draw.h"
60 #ifdef STANDALONE /* For NRAND() */
61 #include "xlockmoreI.h" /* in xscreensaver distribution */
62 #else /* STANDALONE */
63 #include "xlock.h" /* in xlockmore distribution */
64 #endif /* STANDALONE */
68 float x, y, z; /* Point coordinates */
71 typedef struct _tnPath
78 static const cvPoint initpath[]={
79 {0.000000, 0.000000, 0.000000},
80 {2.000000, 1.000000, 0.000000},
81 {4.000000, 0.000000, 0.000000},
82 {6.000000, 1.000000, 0.000000},
83 {8.000000, 0.000000, 1.000000},
84 {10.000000, 1.000000, 1.000000},
85 {12.000000, 1.500000, 0.000000},
86 {14.000000, 0.000000, 0.000000},
87 {16.000000, 1.000000, 0.000000},
88 {18.000000, 0.000000, 0.000000},
89 {20.000000, 0.000000, 1.000000},
90 {22.000000, 1.000000, 0.000000},
91 {24.000000, 0.000000, 1.000000},
92 {26.000000, 0.000000, 1.000000},
93 {28.000000, 1.000000, 0.000000},
94 {30.000000, 0.000000, 2.000000},
95 {32.000000, 1.000000, 0.000000},
96 {34.000000, 0.000000, 2.000000},
97 {-1.000000, -1.000000, -1.000000}
101 struct tunnel_state {
105 float cam_t; /* Camera variables */
109 int tFlag; /* Tunnel Drawing Variables */
110 cvPoint prev_points[10];
113 float ModeX; /* Modes */
117 /*=================== Vector normalization ==================================*/
119 normalize(cvPoint *V)
124 d = (float)sqrt(V->x*V->x + V->y*V->y + V->z*V->z);
131 /*=================== C = A x B (Vector multiply) ==========================*/
134 vect_mult(cvPoint *A, cvPoint *B, cvPoint *C)
136 /* Vector multiply */
137 C->x = A->y*B->z - A->z*B->y;
138 C->y = A->z*B->x - A->x*B->z;
139 C->z = A->x*B->y - A->y*B->x;
143 /* Catmull-Rom Curve calculations */
145 cvCatmullRom(cvPoint *p, float t, cvPoint *outp)
153 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;
154 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;
155 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;
158 /*=================== Point Rotating Around Line ===========================
159 // p - original point
161 // pl - pivot line (vector)
162 // a - angle to rotate in radians
163 // outp - output point
164 //==========================================================================
167 RotateAroundLine(cvPoint *p, cvPoint *pp, cvPoint *pl, float a, cvPoint *outp)
170 float l, m, n, ca, sa;
183 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);
184 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);
185 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));
187 outp->x = p2.x + pp->x;
188 outp->y = p2.y + pp->y;
189 outp->z = p2.z + pp->z;
193 /*=================== Load camera and tunnel path ==========================*/
195 LoadPath(struct tunnel_state *st)
198 tnPath *path1=NULL, *path2=NULL;
200 cvPoint *f = (cvPoint *)initpath;
209 if (st->path == NULL)
211 st->path = (tnPath *)malloc(sizeof(tnPath));
216 if (!path1) path1 = st->path;
217 path2 = (tnPath *)malloc(sizeof(tnPath));
228 st->cam_pos = st->path;
232 /*=================== Tunnel Initialization ================================*/
233 struct tunnel_state *
234 atunnel_InitTunnel(void)
236 struct tunnel_state *st = (struct tunnel_state *) calloc (1, sizeof(*st));
238 st->current_texture = NRAND(MAX_TEXTURE);
243 atunnel_DrawTunnel(struct tunnel_state *st,
244 int do_texture, int do_light, GLuint *textures)
246 tnPath *p, *p1, *cmpos;
247 cvPoint op, p4[4], T, ppp, op1, op2;
251 GLfloat light_position[4];
254 /* Select current tunnel texture */
256 glBindTexture(GL_TEXTURE_2D, textures[st->current_texture]);
259 /* Get current curve */
260 if (st->cam_pos->next &&
261 st->cam_pos->next->next &&
262 st->cam_pos->next->next->next)
281 /* Get current camera position */
282 cvCatmullRom(p4, st->cam_t, &op);
284 /* Next camera position */
288 st->cam_t = st->cam_t - 1;
289 cmpos = st->cam_pos->next;
292 /* Get curve for next camera position */
293 if (cmpos->next->next->next)
312 /* Get next camera position */
313 cvCatmullRom(p4, st->cam_t, &op1);
316 glRotatef(st->alpha, 0, 0, -1);
318 /* Set camera position */
319 gluLookAt(op.x, op.y, op.z, op1.x, op1.y, op1.z, 0, 1, 0);
321 /* Set light position */
324 light_position[0] = op.x;
325 light_position[1] = op.y;
326 light_position[2] = op.z;
327 light_position[3] = 1;
328 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
335 /* Draw tunnel from current curve and next 2 curves */
339 if (p->next->next->next)
358 cvCatmullRom(p4, t, &op);
372 if (p->next->next->next)
392 cvCatmullRom(p4, t, &op1);
402 RotateAroundLine(&ppp, &op, &T, ((float)i*36.0*M_PI/180.0), &op2);
408 st->prev_points[i].x = op2.x;
409 st->prev_points[i].y = op2.y;
410 st->prev_points[i].z = op2.z;
420 /* Draw 10 polygons for current point */
425 glNormal3f(0, 0, 1); /* Normal for lighting */
426 glTexCoord2f(0, 0); glVertex3f(st->prev_points[i].x, st->prev_points[i].y, st->prev_points[i].z);
428 glTexCoord2f(1, 0); glVertex3f(points[i].x, points[i].y, points[i].z);
430 glTexCoord2f(1, 1); glVertex3f(points[j].x, points[j].y, points[j].z);
432 glTexCoord2f(0, 1); glVertex3f(st->prev_points[j].x, st->prev_points[j].y, st->prev_points[j].z);
434 /* Save current polygon coordinates for next position */
437 st->prev_points[i].x = points[i].x;
438 st->prev_points[i].y = points[i].y;
439 st->prev_points[i].z = points[i].z;
446 /* =================== Show splash screen =================================== */
448 atunnel_SplashScreen(struct tunnel_state *st,
449 int do_wire, int do_texture, int do_light)
453 /* Reset tunnel and camera position */
456 st->cam_pos = st->path;
460 st->current_texture++;
461 if (st->current_texture >= MAX_TEXTURE) st->current_texture = 0;
463 /* Now we want to draw splash screen */
465 /* Disable all unused features */
466 glDisable(GL_DEPTH_TEST);
467 glDisable(GL_LIGHTING);
469 glDisable(GL_CULL_FACE);
471 glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
473 glDisable(GL_TEXTURE_2D);
474 glColor4f(1, 1, 1, st->ModeX);
476 /* Draw splash screen (simply quad) */
478 glVertex3f(-10, -10, -1);
479 glVertex3f(10, -10, -1);
480 glVertex3f(10, 10, -1);
481 glVertex3f(-10, 10, -1);
485 if (st->ModeX <= 0) st->ModeX = 0;
489 glEnable(GL_CULL_FACE);
490 glEnable(GL_DEPTH_TEST);
494 glEnable(GL_LIGHTING);
499 glEnable(GL_TEXTURE_2D);
502 glColor4f(1, 1, 1, 1);
507 atunnel_FreeTunnel(struct tunnel_state *st)