X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=hacks%2Fglx%2Ftunnel_draw.c;h=8a87c7673d5bc142b764802b3adc920595020a0f;hb=aa75c7476aeaa84cf3abc192b376a8b03c325213;hp=719541003a7e51ea0ffca632cf43475f77cbac77;hpb=2c902d6065f9856adf31e8540a94f1e42e68e905;p=xscreensaver diff --git a/hacks/glx/tunnel_draw.c b/hacks/glx/tunnel_draw.c index 71954100..8a87c767 100644 --- a/hacks/glx/tunnel_draw.c +++ b/hacks/glx/tunnel_draw.c @@ -1,11 +1,11 @@ /* -*- Mode: C; tab-width: 4 -*- */ /* atunnels --- OpenGL Advanced Tunnel Demo */ -#if !defined( lint ) && !defined( SABER ) -static const char sccsid[] = "@(#)tunnel_draw.c 5.02 2002/03/16 xlockmore"; +#if 0 +static const char sccsid[] = "@(#)tunnel_draw.c 5.13 2004/05/25 xlockmore"; #endif -/* Copyright (c) E. Lassauge, 2002. */ +/* Copyright (c) E. Lassauge, 2002-2004. */ /* * Permission to use, copy, modify, and distribute this software and its @@ -24,10 +24,13 @@ static const char sccsid[] = "@(#)tunnel_draw.c 5.02 2002/03/16 xlockmore"; * Email: romka@ut.ee * WEB: http://romka.demonews.com * - * Eric Lassauge (March-16-2002) + * Eric Lassauge (May-25-2004) * http://lassauge.free.fr/linux.html * * REVISION HISTORY: + * E.Lassauge - 25-May-2004: + * - added more texture + * - random texture init * */ @@ -41,11 +44,25 @@ static const char sccsid[] = "@(#)tunnel_draw.c 5.02 2002/03/16 xlockmore"; #include #include -#include -#include +#ifdef STANDALONE +# ifndef HAVE_JWXYZ +# include +# include +# endif +#endif +#ifdef HAVE_ANDROID +#include +#endif + #include "tunnel_draw.h" +#ifdef STANDALONE /* For NRAND() */ +#include "xlockmoreI.h" /* in xscreensaver distribution */ +#else /* STANDALONE */ +#include "xlock.h" /* in xlockmore distribution */ +#endif /* STANDALONE */ + typedef struct { float x, y, z; /* Point coordinates */ @@ -58,9 +75,7 @@ typedef struct _tnPath } tnPath; -tnPath *path = NULL; - -const cvPoint initpath[]={ +static const cvPoint initpath[]={ {0.000000, 0.000000, 0.000000}, {2.000000, 1.000000, 0.000000}, {4.000000, 0.000000, 0.000000}, @@ -82,22 +97,26 @@ const cvPoint initpath[]={ {-1.000000, -1.000000, -1.000000} }; -/* Camera variables */ -static float cam_t=0; -static tnPath *cam_pos; -static float alpha=0; -/* Tunnel Drawing Variables */ -static int tFlag=0; -static cvPoint prev_points[10]; -static int current_texture; +struct tunnel_state { + + tnPath *path; -/* Modes */ -static float ModeX=0; -static int ModeXFlag=0; + float cam_t; /* Camera variables */ + tnPath *cam_pos; + float alpha; + + int tFlag; /* Tunnel Drawing Variables */ + cvPoint prev_points[10]; + int current_texture; + + float ModeX; /* Modes */ + int ModeXFlag; +}; /*=================== Vector normalization ==================================*/ -void normalize(cvPoint *V) +static void +normalize(cvPoint *V) { float d; @@ -110,16 +129,20 @@ void normalize(cvPoint *V) V->z /= d; } /*=================== C = A x B (Vector multiply) ==========================*/ -void vect_mult(cvPoint *A, cvPoint *B, cvPoint *C) +#if 0 +static void +vect_mult(cvPoint *A, cvPoint *B, cvPoint *C) { /* Vector multiply */ C->x = A->y*B->z - A->z*B->y; C->y = A->z*B->x - A->x*B->z; C->z = A->x*B->y - A->y*B->x; } +#endif /* Catmull-Rom Curve calculations */ -void cvCatmullRom(cvPoint *p, float t, cvPoint *outp) +static void +cvCatmullRom(cvPoint *p, float t, cvPoint *outp) { float t2, t3, t1; @@ -140,7 +163,8 @@ void cvCatmullRom(cvPoint *p, float t, cvPoint *outp) // outp - output point //========================================================================== */ -void RotateAroundLine(cvPoint *p, cvPoint *pp, cvPoint *pl, float a, cvPoint *outp) +static void +RotateAroundLine(cvPoint *p, cvPoint *pp, cvPoint *pl, float a, cvPoint *outp) { cvPoint p1, p2; float l, m, n, ca, sa; @@ -167,7 +191,8 @@ void RotateAroundLine(cvPoint *p, cvPoint *pp, cvPoint *pl, float a, cvPoint *ou /*=================== Load camera and tunnel path ==========================*/ -static void LoadPath(void) +static void +LoadPath(struct tunnel_state *st) { float x, y, z; tnPath *path1=NULL, *path2=NULL; @@ -181,13 +206,14 @@ static void LoadPath(void) z = f->z; f++; - if (path == NULL) + if (st->path == NULL) { - path = (tnPath *)malloc(sizeof(tnPath)); - path1 = path; + st->path = (tnPath *)malloc(sizeof(tnPath)); + path1 = st->path; } else { + if (!path1) path1 = st->path; path2 = (tnPath *)malloc(sizeof(tnPath)); path1->next = path2; path1 = path2; @@ -199,21 +225,26 @@ static void LoadPath(void) path1->p.z = z; } - cam_pos = path; - cam_t = 0; + st->cam_pos = st->path; + st->cam_t = 0; } /*=================== Tunnel Initialization ================================*/ -void InitTunnel(void) +struct tunnel_state * +atunnel_InitTunnel(void) { - LoadPath(); - current_texture = 0; + struct tunnel_state *st = (struct tunnel_state *) calloc (1, sizeof(*st)); + LoadPath(st); + st->current_texture = NRAND(MAX_TEXTURE); + return st; } -void DrawTunnel(int do_texture, int do_light, GLuint *textures) +void +atunnel_DrawTunnel(struct tunnel_state *st, + int do_texture, int do_light, GLuint *textures) { tnPath *p, *p1, *cmpos; - cvPoint op, p4[4], T, ppp, ppp1, op1, op2; + cvPoint op, p4[4], T, ppp, op1, op2; float t; int i, j, k, flag; cvPoint points[10]; @@ -222,13 +253,15 @@ void DrawTunnel(int do_texture, int do_light, GLuint *textures) /* Select current tunnel texture */ if (do_texture) - glBindTexture(GL_TEXTURE_2D, textures[current_texture]); + glBindTexture(GL_TEXTURE_2D, textures[st->current_texture]); - cmpos = cam_pos; + cmpos = st->cam_pos; /* Get current curve */ - if (cam_pos->next->next->next) + if (st->cam_pos->next && + st->cam_pos->next->next && + st->cam_pos->next->next->next) { - p1 = cam_pos; + p1 = st->cam_pos; for (i=0; i<4; i++) { p4[i].x = p1->p.x; @@ -240,20 +273,20 @@ void DrawTunnel(int do_texture, int do_light, GLuint *textures) else { /* End of tunnel */ - ModeX = 1.0; - ModeXFlag = 0; + st->ModeX = 1.0; + st->ModeXFlag = 0; return; }; /* Get current camera position */ - cvCatmullRom(p4, cam_t, &op); + cvCatmullRom(p4, st->cam_t, &op); /* Next camera position */ - cam_t += 0.02f; - if (cam_t >= 1) + st->cam_t += 0.02f; + if (st->cam_t >= 1) { - cam_t = cam_t - 1; - cmpos = cam_pos->next; + st->cam_t = st->cam_t - 1; + cmpos = st->cam_pos->next; } /* Get curve for next camera position */ @@ -271,17 +304,17 @@ void DrawTunnel(int do_texture, int do_light, GLuint *textures) else { /* End of tunnel */ - ModeX = 1.0; - ModeXFlag = 0; + st->ModeX = 1.0; + st->ModeXFlag = 0; return; } /* Get next camera position */ - cvCatmullRom(p4, cam_t, &op1); + cvCatmullRom(p4, st->cam_t, &op1); /* Rotate camera */ - glRotatef(alpha, 0, 0, -1); - alpha += 1; + glRotatef(st->alpha, 0, 0, -1); + st->alpha += 1; /* Set camera position */ gluLookAt(op.x, op.y, op.z, op1.x, op1.y, op1.z, 0, 1, 0); @@ -295,7 +328,7 @@ void DrawTunnel(int do_texture, int do_light, GLuint *textures) glLightfv(GL_LIGHT0, GL_POSITION, light_position); } - p = cam_pos; + p = st->cam_pos; flag = 0; t = 0; k = 0; @@ -317,8 +350,9 @@ void DrawTunnel(int do_texture, int do_light, GLuint *textures) else { /* End of tunnel */ - ModeX = 1.0; - ModeXFlag = 0; + st->ModeX = 1.0; + st->ModeXFlag = 0; + glEnd(); return; } cvCatmullRom(p4, t, &op); @@ -349,17 +383,14 @@ void DrawTunnel(int do_texture, int do_light, GLuint *textures) else { /* End of tunnel */ - ModeX = 1.0; - ModeXFlag = 0; + st->ModeX = 1.0; + st->ModeXFlag = 0; + glEnd(); return; } cvCatmullRom(p4, t, &op1); - ppp1.x = op1.x; - ppp1.y = op1.y; - ppp1.z = op1.z + 0.25; - T.x = op1.x - op.x; T.y = op1.y - op.y; T.z = op1.z - op.z; @@ -374,9 +405,9 @@ void DrawTunnel(int do_texture, int do_light, GLuint *textures) points[i].z = op2.z; if (!flag) { - prev_points[i].x = op2.x; - prev_points[i].y = op2.y; - prev_points[i].z = op2.z; + st->prev_points[i].x = op2.x; + st->prev_points[i].y = op2.y; + st->prev_points[i].z = op2.z; } } @@ -392,40 +423,42 @@ void DrawTunnel(int do_texture, int do_light, GLuint *textures) j = i+1; if (j > 9) j = 0; glNormal3f(0, 0, 1); /* Normal for lighting */ - glTexCoord2f(0, 0); glVertex3f(prev_points[i].x, prev_points[i].y, prev_points[i].z); + glTexCoord2f(0, 0); glVertex3f(st->prev_points[i].x, st->prev_points[i].y, st->prev_points[i].z); glNormal3f(0, 0, 1); glTexCoord2f(1, 0); glVertex3f(points[i].x, points[i].y, points[i].z); glNormal3f(0, 0, 1); glTexCoord2f(1, 1); glVertex3f(points[j].x, points[j].y, points[j].z); glNormal3f(0, 0, 1); - glTexCoord2f(0, 1); glVertex3f(prev_points[j].x, prev_points[j].y, prev_points[j].z); + glTexCoord2f(0, 1); glVertex3f(st->prev_points[j].x, st->prev_points[j].y, st->prev_points[j].z); } /* Save current polygon coordinates for next position */ for (i=0; i<10; i++) { - prev_points[i].x = points[i].x; - prev_points[i].y = points[i].y; - prev_points[i].z = points[i].z; + st->prev_points[i].x = points[i].x; + st->prev_points[i].y = points[i].y; + st->prev_points[i].z = points[i].z; } } glEnd(); - cam_pos = cmpos; + st->cam_pos = cmpos; } /* =================== Show splash screen =================================== */ -void SplashScreen(int do_wire, int do_texture, int do_light) +void +atunnel_SplashScreen(struct tunnel_state *st, + int do_wire, int do_texture, int do_light) { - if (ModeX > 0) + if (st->ModeX > 0) { /* Reset tunnel and camera position */ - if (!ModeXFlag) + if (!st->ModeXFlag) { - cam_pos = path; - cam_t = 0; - tFlag = 0; - ModeXFlag = 1; - current_texture++; - if (current_texture > 2) current_texture = 0; + st->cam_pos = st->path; + st->cam_t = 0; + st->tFlag = 0; + st->ModeXFlag = 1; + st->current_texture++; + if (st->current_texture >= MAX_TEXTURE) st->current_texture = 0; } /* Now we want to draw splash screen */ glLoadIdentity(); @@ -438,7 +471,7 @@ void SplashScreen(int do_wire, int do_texture, int do_light) glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA); glEnable(GL_BLEND); glDisable(GL_TEXTURE_2D); - glColor4f(1, 1, 1, ModeX); + glColor4f(1, 1, 1, st->ModeX); /* Draw splash screen (simply quad) */ glBegin(GL_QUADS); @@ -448,8 +481,8 @@ void SplashScreen(int do_wire, int do_texture, int do_light) glVertex3f(-10, 10, -1); glEnd(); - ModeX -= 0.05; - if (ModeX <= 0) ModeX = 0; + st->ModeX -= 0.05; + if (st->ModeX <= 0) st->ModeX = 0; if (!do_wire) { @@ -469,4 +502,11 @@ void SplashScreen(int do_wire, int do_texture, int do_light) glColor4f(1, 1, 1, 1); } } + +void +atunnel_FreeTunnel(struct tunnel_state *st) +{ + free (st); +} + #endif