From http://www.jwz.org/xscreensaver/xscreensaver-5.18.tar.gz
[xscreensaver] / hacks / glx / tunnel_draw.c
1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* atunnels --- OpenGL Advanced Tunnel Demo */
3
4 #if 0
5 static const char sccsid[] = "@(#)tunnel_draw.c 5.13 2004/05/25 xlockmore";
6 #endif
7
8 /* Copyright (c) E. Lassauge, 2002-2004. */
9
10 /*
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.
16  *
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.
22  *
23  * The original code for this mode was written by Roman Podobedov
24  * Email: romka@ut.ee
25  * WEB: http://romka.demonews.com
26  *
27  * Eric Lassauge  (May-25-2004) <lassauge@users.sourceforge.net>
28  *                                  http://lassauge.free.fr/linux.html
29  *
30  * REVISION HISTORY:
31  * E.Lassauge - 25-May-2004:
32  *      - added more texture
33  *      - random texture init
34  *
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #ifdef USE_GL /* whole file */
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <math.h>
46
47 #ifdef STANDALONE
48 # ifndef HAVE_COCOA
49 #  include <GL/gl.h>
50 #  include <GL/glu.h>
51 # endif
52 #endif
53
54 #include "tunnel_draw.h"
55
56 #ifdef STANDALONE /* For NRAND() */
57 #include "xlockmoreI.h"          /* in xscreensaver distribution */
58 #else /* STANDALONE */
59 #include "xlock.h"              /* in xlockmore distribution */
60 #endif /* STANDALONE */
61
62 typedef struct 
63 {
64   float x, y, z; /* Point coordinates */
65 } cvPoint;
66
67 typedef struct _tnPath
68 {
69         cvPoint p;
70         struct _tnPath *next;
71 } tnPath;
72
73
74 static const cvPoint initpath[]={
75 {0.000000, 0.000000, 0.000000},
76 {2.000000, 1.000000, 0.000000},
77 {4.000000, 0.000000, 0.000000},
78 {6.000000, 1.000000, 0.000000},
79 {8.000000, 0.000000, 1.000000},
80 {10.000000, 1.000000, 1.000000},
81 {12.000000, 1.500000, 0.000000},
82 {14.000000, 0.000000, 0.000000},
83 {16.000000, 1.000000, 0.000000},
84 {18.000000, 0.000000, 0.000000},
85 {20.000000, 0.000000, 1.000000},
86 {22.000000, 1.000000, 0.000000},
87 {24.000000, 0.000000, 1.000000},
88 {26.000000, 0.000000, 1.000000},
89 {28.000000, 1.000000, 0.000000},
90 {30.000000, 0.000000, 2.000000},
91 {32.000000, 1.000000, 0.000000},
92 {34.000000, 0.000000, 2.000000},
93 {-1.000000, -1.000000, -1.000000}
94 };
95
96
97 struct tunnel_state {
98
99   tnPath *path;
100
101   float cam_t;                                  /* Camera variables */
102   tnPath *cam_pos;
103   float alpha;
104
105   int tFlag;                                    /* Tunnel Drawing Variables */
106   cvPoint prev_points[10];
107   int current_texture;
108
109   float ModeX;                                  /* Modes */
110   int ModeXFlag;
111 };
112
113 /*=================== Vector normalization ==================================*/
114 static void
115 normalize(cvPoint *V)
116 {
117   float d;
118
119   /* Vector length */
120   d = (float)sqrt(V->x*V->x + V->y*V->y + V->z*V->z);
121
122   /* Normalization */
123   V->x /= d; 
124   V->y /= d; 
125   V->z /= d; 
126 }
127 /*=================== C = A x B  (Vector multiply) ==========================*/
128 #if 0
129 static void
130 vect_mult(cvPoint *A, cvPoint *B, cvPoint *C)
131 {
132         /* Vector multiply */
133         C->x = A->y*B->z - A->z*B->y;
134         C->y = A->z*B->x - A->x*B->z;
135         C->z = A->x*B->y - A->y*B->x;
136 }
137 #endif
138
139 /* Catmull-Rom Curve calculations */
140 static void
141 cvCatmullRom(cvPoint *p, float t, cvPoint *outp)
142 {
143         float t2, t3, t1;
144
145         t2 = t*t;
146         t3 = t*t*t;
147         t1 = (1-t)*(1-t);
148
149         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;
150         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;
151         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;
152 }
153
154 /*=================== Point Rotating Around Line ===========================
155 // p    - original point
156 // pp   - pivot point
157 // pl   - pivot line (vector)
158 // a    - angle to rotate in radians
159 // outp - output point
160 //==========================================================================
161 */
162 static void
163 RotateAroundLine(cvPoint *p, cvPoint *pp, cvPoint *pl, float a, cvPoint *outp)
164 {
165         cvPoint p1, p2;
166         float l, m, n, ca, sa;
167
168         p1.x = p->x - pp->x;
169         p1.y = p->y - pp->y;
170         p1.z = p->z - pp->z;
171
172         l = pl->x;
173         m = pl->y;
174         n = pl->z;
175
176         ca = cos(a);
177         sa = sin(a);
178
179         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);
180         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);
181         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));
182
183         outp->x = p2.x + pp->x;
184         outp->y = p2.y + pp->y;
185         outp->z = p2.z + pp->z;
186 }
187
188
189 /*=================== Load camera and tunnel path ==========================*/
190 static void
191 LoadPath(struct tunnel_state *st)
192 {
193         float x, y, z;
194         tnPath *path1=NULL, *path2=NULL;
195
196         cvPoint *f = (cvPoint *)initpath;
197         
198         while (f->x != -1.0)
199         {
200                 x = f->x;
201                 y = f->y;
202                 z = f->z;
203                 f++;
204
205                 if (st->path == NULL)
206                 {
207                         st->path = (tnPath *)malloc(sizeof(tnPath));
208                         path1 = st->path;
209                 }
210                 else
211                 {
212                         if (!path1) path1 = st->path;
213                         path2 = (tnPath *)malloc(sizeof(tnPath));
214                         path1->next = path2;
215                         path1 = path2;
216                 }
217                 
218                 path1->next = NULL;
219                 path1->p.x = x;
220                 path1->p.y = y;
221                 path1->p.z = z;
222         }
223
224         st->cam_pos = st->path;
225         st->cam_t = 0;
226 }
227
228 /*=================== Tunnel Initialization ================================*/
229 struct tunnel_state *
230 atunnel_InitTunnel(void)
231 {
232     struct tunnel_state *st = (struct tunnel_state *) calloc (1, sizeof(*st));
233         LoadPath(st);
234         st->current_texture = NRAND(MAX_TEXTURE);
235     return st;
236 }
237
238 void
239 atunnel_DrawTunnel(struct tunnel_state *st, 
240                 int do_texture, int do_light, GLuint *textures)
241 {
242         tnPath *p, *p1, *cmpos;
243         cvPoint op, p4[4], T, ppp, ppp1, op1, op2;
244         float t;
245         int i, j, k, flag;
246         cvPoint points[10];
247         GLfloat light_position[4];
248
249
250         /* Select current tunnel texture */
251         if (do_texture)
252                 glBindTexture(GL_TEXTURE_2D, textures[st->current_texture]);
253         
254         cmpos = st->cam_pos;
255         /* Get current curve */
256         if (st->cam_pos->next &&
257             st->cam_pos->next->next &&
258             st->cam_pos->next->next->next)
259         {
260                 p1 = st->cam_pos;
261                 for (i=0; i<4; i++)
262                 {
263                         p4[i].x = p1->p.x;
264                         p4[i].y = p1->p.y;
265                         p4[i].z = p1->p.z;
266                         p1 = p1->next;
267                 }
268         }
269         else 
270         {
271                 /* End of tunnel */
272                 st->ModeX = 1.0;
273                 st->ModeXFlag = 0;
274                 return;
275         };
276                 
277         /* Get current camera position */
278         cvCatmullRom(p4, st->cam_t, &op);
279
280         /* Next camera position */
281         st->cam_t += 0.02f;
282         if (st->cam_t >= 1)
283         {
284                 st->cam_t = st->cam_t - 1;
285                 cmpos = st->cam_pos->next;
286         }
287                 
288         /* Get curve for next camera position */
289         if (cmpos->next->next->next)
290         {
291                 p1 = cmpos;
292                 for (i=0; i<4; i++)
293                 {
294                         p4[i].x = p1->p.x;
295                         p4[i].y = p1->p.y;
296                         p4[i].z = p1->p.z;
297                         p1 = p1->next;
298                 }
299         }
300         else 
301         {       
302                 /*  End of tunnel */
303                 st->ModeX = 1.0;
304                 st->ModeXFlag = 0;
305                 return;
306         }
307         
308         /*  Get next camera position */
309         cvCatmullRom(p4, st->cam_t, &op1);
310         
311         /*  Rotate camera */
312         glRotatef(st->alpha, 0, 0, -1);
313         st->alpha += 1;
314         /*  Set camera position */
315         gluLookAt(op.x, op.y, op.z, op1.x, op1.y, op1.z, 0, 1, 0);
316
317         /*  Set light position */
318         if (do_light)
319         {
320                 light_position[0] = op.x;
321                 light_position[1] = op.y;
322                 light_position[2] = op.z;
323                 light_position[3] = 1;
324                 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
325         }
326         
327         p = st->cam_pos;
328         flag = 0;
329         t = 0;
330         k = 0;
331         /*  Draw tunnel from current curve and next 2 curves */
332         glBegin(GL_QUADS);
333         while (k < 3)
334         {
335                 if (p->next->next->next)
336                 {
337                         p1 = p;
338                         for (i=0; i<4; i++)
339                         {
340                                 p4[i].x = p1->p.x;
341                                 p4[i].y = p1->p.y;
342                                 p4[i].z = p1->p.z;
343                                 p1 = p1->next;
344                         }
345                 }
346                 else
347                 {
348                         /*  End of tunnel */
349                         st->ModeX = 1.0;
350                         st->ModeXFlag = 0;
351             glEnd();
352                         return;
353                 }
354                 cvCatmullRom(p4, t, &op);
355
356                 ppp.x = op.x;
357                 ppp.y = op.y;
358                 ppp.z = op.z + 0.25;
359
360                 t += 0.1;
361                 if (t >= 1.0)
362                 {
363                         t = t - 1;
364                         k++;
365                         p = p->next;
366                 }
367         
368                 if (p->next->next->next)
369                 {
370                         p1 = p;
371                         for (i=0; i<4; i++)
372                         {
373                                 p4[i].x = p1->p.x;
374                                 p4[i].y = p1->p.y;
375                                 p4[i].z = p1->p.z;
376                                 p1 = p1->next;
377                         }
378                 } 
379                 else
380                 {
381                         /*  End of tunnel */
382                         st->ModeX = 1.0;
383                         st->ModeXFlag = 0;
384             glEnd();
385                         return;
386                 }
387                         
388                 cvCatmullRom(p4, t, &op1);
389
390                 ppp1.x = op1.x;
391                 ppp1.y = op1.y;
392                 ppp1.z = op1.z + 0.25;
393
394                 T.x = op1.x - op.x;
395                 T.y = op1.y - op.y;
396                 T.z = op1.z - op.z;
397                         
398                 normalize(&T);
399
400                 for (i=0; i<10; i++)
401                 {
402                         RotateAroundLine(&ppp, &op, &T, ((float)i*36.0*M_PI/180.0), &op2);
403                         points[i].x = op2.x;
404                         points[i].y = op2.y;
405                         points[i].z = op2.z;
406                         if (!flag)
407                         {
408                                 st->prev_points[i].x = op2.x;
409                                 st->prev_points[i].y = op2.y;
410                                 st->prev_points[i].z = op2.z;
411                         }
412                 }
413         
414                 if (!flag)
415                 {
416                         flag = 1;
417                         continue;
418                 }
419                 
420                 /*  Draw 10 polygons for current point */
421                 for (i=0; i<10; i++)
422                 {
423                         j = i+1;
424                         if (j > 9) j = 0;
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);
427                         glNormal3f(0, 0, 1);
428                         glTexCoord2f(1, 0); glVertex3f(points[i].x, points[i].y, points[i].z);
429                         glNormal3f(0, 0, 1);
430                         glTexCoord2f(1, 1); glVertex3f(points[j].x, points[j].y, points[j].z);
431                         glNormal3f(0, 0, 1);
432                         glTexCoord2f(0, 1); glVertex3f(st->prev_points[j].x, st->prev_points[j].y, st->prev_points[j].z);
433                 }
434                 /*  Save current polygon coordinates for next position */
435                 for (i=0; i<10; i++)
436                 {
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;
440                 }
441         }
442         glEnd();
443         st->cam_pos = cmpos;
444 }
445
446 /* =================== Show splash screen =================================== */
447 void
448 atunnel_SplashScreen(struct tunnel_state *st, 
449                   int do_wire, int do_texture, int do_light)
450 {
451         if (st->ModeX > 0)
452         {
453                 /*  Reset tunnel and camera position */
454                 if (!st->ModeXFlag)
455                 {
456                         st->cam_pos = st->path;                 
457                         st->cam_t = 0;
458                         st->tFlag = 0;
459                         st->ModeXFlag = 1;
460                         st->current_texture++;
461                         if (st->current_texture >= MAX_TEXTURE) st->current_texture = 0;
462                 }
463                 /*  Now we want to draw splash screen */
464                 glLoadIdentity();
465                 /*  Disable all unused features */
466                 glDisable(GL_DEPTH_TEST);
467                 glDisable(GL_LIGHTING);
468                 glDisable(GL_FOG);
469                 glDisable(GL_CULL_FACE);
470
471                 glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
472                 glEnable(GL_BLEND);
473                 glDisable(GL_TEXTURE_2D);
474                 glColor4f(1, 1, 1, st->ModeX);
475                 
476                 /*  Draw splash screen (simply quad) */
477                 glBegin(GL_QUADS);
478                 glVertex3f(-10, -10, -1);
479                 glVertex3f(10, -10, -1);
480                 glVertex3f(10, 10, -1);
481                 glVertex3f(-10, 10, -1);
482                 glEnd();
483
484                 st->ModeX -= 0.05;
485                 if (st->ModeX <= 0)     st->ModeX = 0;
486
487                 if (!do_wire)
488                 {
489                         glEnable(GL_CULL_FACE);
490                         glEnable(GL_DEPTH_TEST);
491                 }
492                 if (do_light)
493                 {
494                         glEnable(GL_LIGHTING);
495                         glEnable(GL_FOG);
496                 }
497                 if (do_texture)
498                 {
499                         glEnable(GL_TEXTURE_2D);
500                 }
501                 glDisable(GL_BLEND);
502                 glColor4f(1, 1, 1, 1);
503         }
504 }
505
506 void
507 atunnel_FreeTunnel(struct tunnel_state *st)
508 {
509   free (st);
510 }
511
512 #endif