1 /* -*- Mode: C; tab-width: 4 -*- */
2 /* sproingies.c - 3D sproingies */
4 #if !defined( lint ) && !defined( SABER )
5 static const char sccsid[] = "@(#)sproingies.c 4.04 97/07/28 xlockmore";
10 * sproingies.c - Copyright 1996 by Ed Mackey, freely distributable.
12 * Permission to use, copy, modify, and distribute this software and its
13 * documentation for any purpose and without fee is hereby granted,
14 * provided that the above copyright notice appear in all copies and that
15 * both that copyright notice and this permission notice appear in
16 * supporting documentation.
18 * This file is provided AS IS with no warranties of any kind. The author
19 * shall have no liability with respect to the infringement of copyrights,
20 * trade secrets or any patents by this file or any part thereof. In no
21 * event will the author be liable for any lost revenue or profits or
22 * other special, indirect and consequential damages.
29 # include "xlockmoreI.h" /* from the xscreensaver distribution */
30 #else /* !STANDALONE */
31 # include "xlock.h" /* from the xlockmore distribution */
32 #endif /* !STANDALONE */
40 #define MAXSPROING 100
45 int x, y, z, frame, life;
50 int rotx, roty, dist, wireframe, flatshade, groundlevel,
52 int sframe, target_rx, target_ry, target_dist, target_count;
53 GLuint sproingies[6], TopsSides, SproingieBoom;
54 struct sPosColor *positions;
57 static sp_instance *si_list = NULL;
58 static int active_screens = 0;
60 void SproingieSwap(void);
62 extern struct lwo LWO_s1_1, LWO_s1_2, LWO_s1_3, LWO_s1_4;
63 extern struct lwo LWO_s1_5, LWO_s1_6, LWO_s1_b;
68 return ((int) (((float) range) * LRAND() / (MAXRAND)));
72 build_TopsSides(int wireframe)
75 GLfloat mat_color[4] =
78 dl_num = glGenLists(2);
80 return (0); /* 0 means out of display lists. */
83 glNewList(dl_num, GL_COMPILE);
84 mat_color[0] = 0.392157;
85 mat_color[1] = 0.784314;
86 mat_color[2] = 0.941176;
88 glColor3fv(mat_color);
90 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_color);
95 glNewList(dl_num + 1, GL_COMPILE);
96 mat_color[0] = 0.156863;
97 mat_color[1] = 0.156863;
98 mat_color[2] = 0.392157;
100 glColor3fv(mat_color);
102 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_color);
109 LayGround(int sx, int sy, int sz, int width, int height, sp_instance * si)
112 GLenum begin_polygon;
115 begin_polygon = GL_LINE_LOOP;
117 begin_polygon = GL_POLYGON;
119 if (!si->wireframe) {
121 glCallList(si->TopsSides); /* Render the tops */
122 glNormal3f(0.0, 1.0, 0.0);
124 for (h = 0; h < height; ++h) {
128 for (w = 0; w < width; ++w) {
129 glBegin(begin_polygon);
131 glVertex3i(x, y, z - 1);
132 glVertex3i(x + 1, y, z - 1);
133 glVertex3i(x + 1, y, z);
135 glBegin(begin_polygon);
136 glVertex3i(x + 1, y - 1, z);
137 glVertex3i(x + 1, y - 1, z - 1);
138 glVertex3i(x + 2, y - 1, z - 1);
139 glVertex3i(x + 2, y - 1, z);
147 glCallList(si->TopsSides + 1); /* Render the sides */
149 glNormal3f(0.0, 0.0, 1.0);
151 for (h = 0; h < height; ++h) {
155 for (w = 0; w < width; ++w) {
156 glBegin(begin_polygon);
158 glVertex3i(x + 1, y, z);
159 glVertex3i(x + 1, y - 1, z);
160 glVertex3i(x, y - 1, z);
162 glBegin(begin_polygon);
163 glVertex3i(x + 1, y - 1, z);
164 glVertex3i(x + 2, y - 1, z);
165 glVertex3i(x + 2, y - 2, z);
166 glVertex3i(x + 1, y - 2, z);
168 * PURIFY 4.0.1 reports an unitialized memory read on the next line when using
169 * MesaGL 2.2 and -mono. This has been fixed in MesaGL 2.3 and later. */
176 /* Render the other sides */
178 glNormal3f(1.0, 0.0, 0.0);
180 for (h = 0; h < height; ++h) {
184 for (w = 0; w < width; ++w) {
185 glBegin(begin_polygon);
186 glVertex3i(x + 1, y, z);
187 glVertex3i(x + 1, y, z - 1);
188 glVertex3i(x + 1, y - 1, z - 1);
189 glVertex3i(x + 1, y - 1, z);
191 glBegin(begin_polygon);
192 glVertex3i(x + 2, y - 1, z);
193 glVertex3i(x + 2, y - 1, z - 1);
194 glVertex3i(x + 2, y - 2, z - 1);
195 glVertex3i(x + 2, y - 2, z);
204 glCallList(si->TopsSides); /* Render the tops */
206 for (h = 0; h < height; ++h) {
210 for (w = 0; w < width; ++w) {
211 glBegin(begin_polygon);
213 glVertex3i(x, y, z - 1);
214 glVertex3i(x + 1, y, z - 1);
215 glVertex3i(x + 1, y, z);
217 glBegin(begin_polygon);
218 glVertex3i(x + 1, y - 1, z);
219 glVertex3i(x + 1, y - 1, z - 1);
220 glVertex3i(x + 2, y - 1, z - 1);
221 glVertex3i(x + 2, y - 1, z);
230 #define RESET_SPROINGIE (-30 + myrand(28))
233 AdvanceSproingie(int t, sp_instance * si)
235 int g_higher, g_back, t2;
236 struct sPosColor *thisSproingie = &(si->positions[t]);
237 struct sPosColor *S2 = &(si->positions[0]);
239 if (thisSproingie->life > 0) {
240 if ((++(thisSproingie->frame)) > 11) {
241 if (thisSproingie->frame >= BOOM_FRAME) {
242 if ((thisSproingie->r -= 0.08) < 0.0)
243 thisSproingie->r = 0.0;
244 if ((thisSproingie->g -= 0.08) < 0.0)
245 thisSproingie->g = 0.0;
246 if ((thisSproingie->b -= 0.08) < 0.0)
247 thisSproingie->b = 0.0;
248 if ((--(thisSproingie->life)) < 1) {
249 thisSproingie->life = RESET_SPROINGIE;
253 thisSproingie->x += 1;
254 thisSproingie->y -= 2;
255 thisSproingie->z += 1;
256 thisSproingie->frame = 0;
258 for (t2 = 0; t2 < si->maxsproingies; ++t2) {
259 if ((t2 != t) && (thisSproingie->x == S2->x) &&
260 (thisSproingie->y == S2->y) && (thisSproingie->z == S2->z) &&
261 (S2->life > 10) && (S2->frame < 6)) {
263 if (thisSproingie->life > S2->life) {
267 if (thisSproingie->life > 10) {
268 thisSproingie->life = 10;
269 thisSproingie->frame = BOOM_FRAME;
270 if ((thisSproingie->r += 0.5) > 1.0)
271 thisSproingie->r = 1.0;
272 if ((thisSproingie->g += 0.5) > 1.0)
273 thisSproingie->g = 1.0;
274 if ((thisSproingie->b += 0.5) > 1.0)
275 thisSproingie->b = 1.0;
284 if (!((thisSproingie->life == 10) &&
285 (thisSproingie->frame > 0) &&
286 (thisSproingie->frame < BOOM_FRAME))) {
287 if ((--(thisSproingie->life)) < 1) {
288 thisSproingie->life = RESET_SPROINGIE;
289 } else if (thisSproingie->life < 9) {
290 thisSproingie->frame -= 2;
292 } /* else wait here for frame 0 to come about. */
293 } else if (++(thisSproingie->life) >= 0) {
295 g_higher = -3 + myrand(5);
296 g_back = -2 + myrand(5);
298 g_higher = -2 + myrand(3);
299 g_back = -1 + myrand(3);
305 thisSproingie->x = (-g_higher - g_back);
306 thisSproingie->y = (g_higher << 1);
307 thisSproingie->z = (g_back - g_higher);
308 thisSproingie->life = 40 + myrand(200);
309 thisSproingie->frame = -10;
310 thisSproingie->r = (GLfloat) (40 + myrand(200)) / 255.0;
311 thisSproingie->g = (GLfloat) (40 + myrand(200)) / 255.0;
312 thisSproingie->b = (GLfloat) (40 + myrand(200)) / 255.0;
314 for (t2 = 0; t2 < si->maxsproingies; ++t2) {
315 if ((t2 != t) && (thisSproingie->x == S2->x) &&
316 (thisSproingie->y == S2->y) && (thisSproingie->z == S2->z) &&
317 (S2->life > 10) && (S2->frame < 0)) {
318 /* If one is already being born, just wait. */
319 thisSproingie->life = -1;
327 NextSproingie(int screen)
329 sp_instance *si = &si_list[screen];
331 struct sPosColor *thisSproingie = &(si->positions[0]);
333 if (++si->sframe > 11) {
335 for (t = 0; t < si->maxsproingies; ++t) {
336 thisSproingie->x -= 1;
337 thisSproingie->y += 2;
338 thisSproingie->z -= 1;
342 for (t = 0; t < si->maxsproingies; ++t) {
343 AdvanceSproingie(t, si);
346 if (si->target_count < 0) { /* track to current target */
347 if (si->target_rx < si->rotx)
349 else if (si->target_rx > si->rotx)
352 if (si->target_ry < si->roty)
354 else if (si->target_ry > si->roty)
357 ddx = (si->target_dist - si->dist) / 8;
360 else if (si->target_dist < si->dist)
362 else if (si->target_dist > si->dist)
365 if ((si->target_rx == si->rotx) && (si->target_ry == si->roty) &&
366 (si->target_dist == si->dist)) {
367 si->target_count = T_COUNT;
368 if (si->target_dist <= 32)
369 si->target_count >>= 2;
371 } else if (--si->target_count < 0) { /* make up new target */
372 si->target_rx = myrand(100) - 35;
373 si->target_ry = -myrand(90);
374 si->target_dist = 32 << myrand(2); /* could be 32, 64, or 128, (previously or 256) */
376 if (si->target_dist >= si->dist) /* no duplicate distances */
377 si->target_dist <<= 1;
379 /* Otherwise just hang loose for a while here */
388 for (t = 0; t < maxsproingies; ++t) {
389 if (positions[t].life > 0)
392 (void) printf("RotX: %d, RotY: %d, Dist: %d. Targets: X %d, Y %d, D %d. Visible: %d\n",
393 rotx, roty, dist, target_rx, target_ry, target_dist, count);
401 for (t = 0; t < maxsproingies; ++t) {
405 positions[t].life = -2;
406 positions[t].frame = 0;
413 if (dist < (1 << 16 << 4))
427 rotx = (rotx + 5) % 360;
433 rotx = (rotx - 5) % 360;
439 roty = (roty + 5) % 360;
445 roty = (roty - 5) % 360;
451 rotx = (rotx + 45) % 360;
457 rotx = (rotx - 45) % 360;
463 roty = (roty + 45) % 360;
469 roty = (roty - 45) % 360;
475 RenderSproingie(int t, sp_instance * si)
477 GLfloat scale, pointsize, mat_color[4] =
478 {0.0, 0.0, 0.0, 1.0};
479 GLdouble clipplane[4] =
480 {0.0, 1.0, 0.0, 0.0};
481 struct sPosColor *thisSproingie = &(si->positions[t]);
483 if (thisSproingie->life < 1)
489 mat_color[0] = thisSproingie->r;
490 mat_color[1] = thisSproingie->g;
491 mat_color[2] = thisSproingie->b;
493 glColor3fv(mat_color);
495 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_color);
498 if (thisSproingie->frame < 0) {
499 glEnable(GL_CLIP_PLANE0);
500 glTranslatef((GLfloat) (thisSproingie->x),
501 (GLfloat) (thisSproingie->y) +
502 ((GLfloat) (thisSproingie->frame) / 9.0),
503 (GLfloat) (thisSproingie->z));
504 clipplane[3] = ((GLdouble) (thisSproingie->frame) / 9.0) +
505 (si->wireframe ? 0.0 : 0.1);
506 glClipPlane(GL_CLIP_PLANE0, clipplane);
507 glCallList(si->sproingies[0]);
508 glDisable(GL_CLIP_PLANE0);
509 } else if (thisSproingie->frame >= BOOM_FRAME) {
510 glTranslatef((GLfloat) (thisSproingie->x) + 0.5,
511 (GLfloat) (thisSproingie->y) + 0.5,
512 (GLfloat) (thisSproingie->z) - 0.5);
513 scale = (GLfloat) (1 << (thisSproingie->frame - BOOM_FRAME));
514 glScalef(scale, scale, scale);
515 if (!si->wireframe) {
517 glColor3fv(mat_color);
518 glDisable(GL_LIGHTING);
520 pointsize = (GLfloat) ((BOOM_FRAME + 8) - thisSproingie->frame) -
522 glPointSize((pointsize < 1.0) ? 1.0 : pointsize);
524 * PURIFY 4.0.1 reports an unitialized memory read on the next line when using
525 * MesaGL 2.2. This has been tracked to MesaGL 2.2 src/points.c line 313. */
526 glCallList(si->SproingieBoom);
528 if (!si->wireframe) {
529 glEnable(GL_LIGHTING);
531 } else if (thisSproingie->frame > 5) {
532 glTranslatef((GLfloat) (thisSproingie->x + 1),
533 (GLfloat) (thisSproingie->y - 1), (GLfloat) (thisSproingie->z - 1));
534 glRotatef((GLfloat) - 90.0, 0.0, 1.0, 0.0);
535 glCallList(si->sproingies[thisSproingie->frame - 6]);
537 glTranslatef((GLfloat) (thisSproingie->x), (GLfloat) (thisSproingie->y),
538 (GLfloat) (thisSproingie->z));
539 glCallList(si->sproingies[thisSproingie->frame]);
547 ComputeGround(sp_instance * si)
549 int g_higher, g_back, g_width, g_height;
551 /* higher: x-1, y+2, z-1 */
552 /* back: x-1, y, z+1 */
554 if (si->groundlevel == 0) {
557 } else if (si->groundlevel == 1) {
565 if ((g_higher = si->dist >> 3) < 4)
569 g_height = g_higher << 1;
572 g_higher += (g_higher >> 2);
573 else if (si->rotx > 10)
574 g_higher -= (g_higher >> 2);
577 if (si->dist > 128) {
581 } else if (si->dist > 64) {
584 } else if (si->dist > 32) {
585 /* nothing special */
588 g_higher = g_back = 4;
593 /* startx, starty, startz, width, height */
594 LayGround((-g_higher - g_back), (g_higher << 1), (g_back - g_higher),
595 (g_width), (g_height), si);
599 DisplaySproingies(int screen,int pause)
601 sp_instance *si = &si_list[screen];
604 {8.0, 5.0, -2.0, 0.1};
607 glClear(GL_COLOR_BUFFER_BIT);
609 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
612 glTranslatef(0.0, 0.0, -(GLfloat) (si->dist) / 16.0); /* viewing transform */
613 glRotatef((GLfloat) si->rotx, 1.0, 0.0, 0.0);
614 glRotatef((GLfloat) si->roty, 0.0, 1.0, 0.0);
617 glLightfv(GL_LIGHT0, GL_POSITION, position);
619 #if 0 /* Show light pos */
621 glTranslatef(position[0], position[1], position[2]);
622 glColor3f(1.0, 1.0, 1.0);
623 if (!si->wireframe) {
624 glDisable(GL_LIGHTING);
626 glCallList(si->SproingieBoom);
627 if (!si->wireframe) {
628 glEnable(GL_LIGHTING);
633 glTranslatef((GLfloat) si->sframe * (-1.0 / 12.0) - 0.75,
634 (GLfloat) si->sframe * (2.0 / 12.0) - 0.5,
635 (GLfloat) si->sframe * (-1.0 / 12.0) + 0.75);
640 for (t = 0; t < si->maxsproingies; ++t) {
641 RenderSproingie(t, si);
654 NextSproingieDisplay(int screen,int pause)
656 NextSproingie(screen);
657 if (pause) usleep(pause);
658 DisplaySproingies(screen,pause);
663 ReshapeSproingies(int w, int h)
665 glViewport(0, 0, w, h);
666 glMatrixMode(GL_PROJECTION);
668 gluPerspective(65.0, (GLfloat) w / (GLfloat) h, 0.1, 2000.0); /* was 200000.0 */
669 glMatrixMode(GL_MODELVIEW);
676 CleanupSproingies(int screen)
678 sp_instance *si = &si_list[screen];
681 if (si->SproingieBoom) {
682 for (t = 0; t < 6; ++t)
683 glDeleteLists(si->sproingies[t], 1);
685 glDeleteLists(si->TopsSides, 2);
686 glDeleteLists(si->SproingieBoom, 1);
689 si->SproingieBoom = 0;
692 (void) free((void *) (si->positions));
693 si->positions = NULL;
695 if ((active_screens == 0) && si_list) {
696 (void) free((void *) (si_list));
702 InitSproingies(int wfmode, int grnd, int mspr, int screen, int numscreens,
706 {0.2, 0.2, 0.2, 1.0};
708 {10.0, 1.0, 1.0, 10.0};
709 GLfloat mat_diffuse[] =
710 {0.6, 0.6, 0.6, 1.0};
711 GLfloat mat_specular[] =
712 {0.8, 0.8, 0.8, 1.0};
713 GLfloat mat_shininess[] =
719 if (si_list == NULL) {
720 if ((si_list = (sp_instance *) calloc(numscreens,
721 sizeof (sp_instance))) == NULL)
724 si = &si_list[screen];
727 CleanupSproingies(screen);
731 if (mspr >= MAXSPROING)
732 mspr = MAXSPROING - 1;
736 si->dist = (16 << 2);
738 si->target_count = 0;
741 si->wireframe = si->flatshade = 0;
748 si->groundlevel = grnd;
749 si->maxsproingies = mspr;
751 if (si->maxsproingies) {
752 si->positions = (struct sPosColor *) calloc(si->maxsproingies,
753 sizeof (struct sPosColor));
755 if (!(si->positions))
756 si->maxsproingies = 0;
758 for (t = 0; t < si->maxsproingies; ++t) {
759 si->positions[t].x = 0;
760 si->positions[t].y = 0;
761 si->positions[t].z = 0;
762 si->positions[t].life = (-t * ((si->maxsproingies > 19) ? 1 : 4)) - 2;
763 si->positions[t].frame = 0;
766 #if 0 /* Test boom */
767 si->positions[0].x = 0;
768 si->positions[0].y = 0;
769 si->positions[0].z = 0;
770 si->positions[0].life = 10;
771 si->positions[0].frame = BOOM_FRAME;
772 si->positions[0].r = 0.656863;
773 si->positions[0].g = 1.0;
774 si->positions[0].b = 0.656863;
777 if (!(si->TopsSides = build_TopsSides(si->wireframe)))
778 (void) fprintf(stderr, "build_TopsSides\n");
780 if (!(si->sproingies[0] = BuildLWO(si->wireframe, &LWO_s1_1)))
781 (void) fprintf(stderr, "BuildLWO - 1\n");
782 if (!(si->sproingies[1] = BuildLWO(si->wireframe, &LWO_s1_2)))
783 (void) fprintf(stderr, "BuildLWO - 2\n");
784 if (!(si->sproingies[2] = BuildLWO(si->wireframe, &LWO_s1_3)))
785 (void) fprintf(stderr, "BuildLWO - 3\n");
786 if (!(si->sproingies[3] = BuildLWO(si->wireframe, &LWO_s1_4)))
787 (void) fprintf(stderr, "BuildLWO - 4\n");
788 if (!(si->sproingies[4] = BuildLWO(si->wireframe, &LWO_s1_5)))
789 (void) fprintf(stderr, "BuildLWO - 5\n");
790 if (!(si->sproingies[5] = BuildLWO(si->wireframe, &LWO_s1_6)))
791 (void) fprintf(stderr, "BuildLWO - 6\n");
793 if (!(si->SproingieBoom = BuildLWO(si->wireframe, &LWO_s1_b)))
794 (void) fprintf(stderr, "BuildLWO - b\n");
797 glShadeModel(GL_FLAT);
798 glDisable(GL_LIGHTING);
801 glShadeModel(GL_FLAT);
805 glEnable(GL_LIGHTING);
807 glDepthFunc(GL_LEQUAL);
808 glEnable(GL_DEPTH_TEST);
810 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
811 glLightfv(GL_LIGHT0, GL_POSITION, position);
813 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
814 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
815 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
817 /* glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE); */
819 glEnable(GL_CULL_FACE);
822 /* glEnable(GL_NORMALIZE); */
828 /* End of sproingies.c */