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);
97 glColor3fv(mat_color);
99 /* jwz: in wireframe mode, color tops and sides the same. */
100 mat_color[0] = 0.156863;
101 mat_color[1] = 0.156863;
102 mat_color[2] = 0.392157;
103 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_color);
110 LayGround(int sx, int sy, int sz, int width, int height, sp_instance * si)
113 GLenum begin_polygon;
116 begin_polygon = GL_LINE_LOOP;
118 begin_polygon = GL_POLYGON;
120 if (!si->wireframe) {
122 glCallList(si->TopsSides); /* Render the tops */
123 glNormal3f(0.0, 1.0, 0.0);
125 for (h = 0; h < height; ++h) {
129 for (w = 0; w < width; ++w) {
130 glBegin(begin_polygon);
132 glVertex3i(x, y, z - 1);
133 glVertex3i(x + 1, y, z - 1);
134 glVertex3i(x + 1, y, z);
136 glBegin(begin_polygon);
137 glVertex3i(x + 1, y - 1, z);
138 glVertex3i(x + 1, y - 1, z - 1);
139 glVertex3i(x + 2, y - 1, z - 1);
140 glVertex3i(x + 2, y - 1, z);
148 glCallList(si->TopsSides + 1); /* Render the sides */
150 glNormal3f(0.0, 0.0, 1.0);
152 for (h = 0; h < height; ++h) {
156 for (w = 0; w < width; ++w) {
157 glBegin(begin_polygon);
159 glVertex3i(x + 1, y, z);
160 glVertex3i(x + 1, y - 1, z);
161 glVertex3i(x, y - 1, z);
163 glBegin(begin_polygon);
164 glVertex3i(x + 1, y - 1, z);
165 glVertex3i(x + 2, y - 1, z);
166 glVertex3i(x + 2, y - 2, z);
167 glVertex3i(x + 1, y - 2, z);
169 * PURIFY 4.0.1 reports an unitialized memory read on the next line when using
170 * MesaGL 2.2 and -mono. This has been fixed in MesaGL 2.3 and later. */
177 /* Render the other sides */
179 glNormal3f(1.0, 0.0, 0.0);
181 for (h = 0; h < height; ++h) {
185 for (w = 0; w < width; ++w) {
186 glBegin(begin_polygon);
187 glVertex3i(x + 1, y, z);
188 glVertex3i(x + 1, y, z - 1);
189 glVertex3i(x + 1, y - 1, z - 1);
190 glVertex3i(x + 1, y - 1, z);
192 glBegin(begin_polygon);
193 glVertex3i(x + 2, y - 1, z);
194 glVertex3i(x + 2, y - 1, z - 1);
195 glVertex3i(x + 2, y - 2, z - 1);
196 glVertex3i(x + 2, y - 2, z);
205 glCallList(si->TopsSides); /* Render the tops */
207 for (h = 0; h < height; ++h) {
211 for (w = 0; w < width; ++w) {
212 glBegin(begin_polygon);
214 glVertex3i(x, y, z - 1);
215 glVertex3i(x + 1, y, z - 1);
216 glVertex3i(x + 1, y, z);
218 glBegin(begin_polygon);
219 glVertex3i(x + 1, y - 1, z);
220 glVertex3i(x + 1, y - 1, z - 1);
221 glVertex3i(x + 2, y - 1, z - 1);
222 glVertex3i(x + 2, y - 1, z);
231 #define RESET_SPROINGIE (-30 + myrand(28))
234 AdvanceSproingie(int t, sp_instance * si)
236 int g_higher, g_back, t2;
237 struct sPosColor *thisSproingie = &(si->positions[t]);
238 struct sPosColor *S2 = &(si->positions[0]);
240 if (thisSproingie->life > 0) {
241 if ((++(thisSproingie->frame)) > 11) {
242 if (thisSproingie->frame >= BOOM_FRAME) {
243 if ((thisSproingie->r -= 0.08) < 0.0)
244 thisSproingie->r = 0.0;
245 if ((thisSproingie->g -= 0.08) < 0.0)
246 thisSproingie->g = 0.0;
247 if ((thisSproingie->b -= 0.08) < 0.0)
248 thisSproingie->b = 0.0;
249 if ((--(thisSproingie->life)) < 1) {
250 thisSproingie->life = RESET_SPROINGIE;
254 thisSproingie->x += 1;
255 thisSproingie->y -= 2;
256 thisSproingie->z += 1;
257 thisSproingie->frame = 0;
259 for (t2 = 0; t2 < si->maxsproingies; ++t2) {
260 if ((t2 != t) && (thisSproingie->x == S2->x) &&
261 (thisSproingie->y == S2->y) && (thisSproingie->z == S2->z) &&
262 (S2->life > 10) && (S2->frame < 6)) {
264 if (thisSproingie->life > S2->life) {
268 if (thisSproingie->life > 10) {
269 thisSproingie->life = 10;
270 thisSproingie->frame = BOOM_FRAME;
271 if ((thisSproingie->r += 0.5) > 1.0)
272 thisSproingie->r = 1.0;
273 if ((thisSproingie->g += 0.5) > 1.0)
274 thisSproingie->g = 1.0;
275 if ((thisSproingie->b += 0.5) > 1.0)
276 thisSproingie->b = 1.0;
285 if (!((thisSproingie->life == 10) &&
286 (thisSproingie->frame > 0) &&
287 (thisSproingie->frame < BOOM_FRAME))) {
288 if ((--(thisSproingie->life)) < 1) {
289 thisSproingie->life = RESET_SPROINGIE;
290 } else if (thisSproingie->life < 9) {
291 thisSproingie->frame -= 2;
293 } /* else wait here for frame 0 to come about. */
294 } else if (++(thisSproingie->life) >= 0) {
296 g_higher = -3 + myrand(5);
297 g_back = -2 + myrand(5);
299 g_higher = -2 + myrand(3);
300 g_back = -1 + myrand(3);
306 thisSproingie->x = (-g_higher - g_back);
307 thisSproingie->y = (g_higher << 1);
308 thisSproingie->z = (g_back - g_higher);
309 thisSproingie->life = 40 + myrand(200);
310 thisSproingie->frame = -10;
311 thisSproingie->r = (GLfloat) (40 + myrand(200)) / 255.0;
312 thisSproingie->g = (GLfloat) (40 + myrand(200)) / 255.0;
313 thisSproingie->b = (GLfloat) (40 + myrand(200)) / 255.0;
315 for (t2 = 0; t2 < si->maxsproingies; ++t2) {
316 if ((t2 != t) && (thisSproingie->x == S2->x) &&
317 (thisSproingie->y == S2->y) && (thisSproingie->z == S2->z) &&
318 (S2->life > 10) && (S2->frame < 0)) {
319 /* If one is already being born, just wait. */
320 thisSproingie->life = -1;
328 NextSproingie(int screen)
330 sp_instance *si = &si_list[screen];
332 struct sPosColor *thisSproingie = &(si->positions[0]);
334 if (++si->sframe > 11) {
336 for (t = 0; t < si->maxsproingies; ++t) {
337 thisSproingie->x -= 1;
338 thisSproingie->y += 2;
339 thisSproingie->z -= 1;
343 for (t = 0; t < si->maxsproingies; ++t) {
344 AdvanceSproingie(t, si);
347 if (si->target_count < 0) { /* track to current target */
348 if (si->target_rx < si->rotx)
350 else if (si->target_rx > si->rotx)
353 if (si->target_ry < si->roty)
355 else if (si->target_ry > si->roty)
358 ddx = (si->target_dist - si->dist) / 8;
361 else if (si->target_dist < si->dist)
363 else if (si->target_dist > si->dist)
366 if ((si->target_rx == si->rotx) && (si->target_ry == si->roty) &&
367 (si->target_dist == si->dist)) {
368 si->target_count = T_COUNT;
369 if (si->target_dist <= 32)
370 si->target_count >>= 2;
372 } else if (--si->target_count < 0) { /* make up new target */
373 si->target_rx = myrand(100) - 35;
374 si->target_ry = -myrand(90);
375 si->target_dist = 32 << myrand(2); /* could be 32, 64, or 128, (previously or 256) */
377 if (si->target_dist >= si->dist) /* no duplicate distances */
378 si->target_dist <<= 1;
380 /* Otherwise just hang loose for a while here */
389 for (t = 0; t < maxsproingies; ++t) {
390 if (positions[t].life > 0)
393 (void) printf("RotX: %d, RotY: %d, Dist: %d. Targets: X %d, Y %d, D %d. Visible: %d\n",
394 rotx, roty, dist, target_rx, target_ry, target_dist, count);
402 for (t = 0; t < maxsproingies; ++t) {
406 positions[t].life = -2;
407 positions[t].frame = 0;
414 if (dist < (1 << 16 << 4))
428 rotx = (rotx + 5) % 360;
434 rotx = (rotx - 5) % 360;
440 roty = (roty + 5) % 360;
446 roty = (roty - 5) % 360;
452 rotx = (rotx + 45) % 360;
458 rotx = (rotx - 45) % 360;
464 roty = (roty + 45) % 360;
470 roty = (roty - 45) % 360;
476 RenderSproingie(int t, sp_instance * si)
478 GLfloat scale, pointsize, mat_color[4] =
479 {0.0, 0.0, 0.0, 1.0};
480 GLdouble clipplane[4] =
481 {0.0, 1.0, 0.0, 0.0};
482 struct sPosColor *thisSproingie = &(si->positions[t]);
484 if (thisSproingie->life < 1)
490 mat_color[0] = thisSproingie->r;
491 mat_color[1] = thisSproingie->g;
492 mat_color[2] = thisSproingie->b;
494 glColor3fv(mat_color);
496 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_color);
499 if (thisSproingie->frame < 0) {
500 glEnable(GL_CLIP_PLANE0);
501 glTranslatef((GLfloat) (thisSproingie->x),
502 (GLfloat) (thisSproingie->y) +
503 ((GLfloat) (thisSproingie->frame) / 9.0),
504 (GLfloat) (thisSproingie->z));
505 clipplane[3] = ((GLdouble) (thisSproingie->frame) / 9.0) +
506 (si->wireframe ? 0.0 : 0.1);
507 glClipPlane(GL_CLIP_PLANE0, clipplane);
508 glCallList(si->sproingies[0]);
509 glDisable(GL_CLIP_PLANE0);
510 } else if (thisSproingie->frame >= BOOM_FRAME) {
511 glTranslatef((GLfloat) (thisSproingie->x) + 0.5,
512 (GLfloat) (thisSproingie->y) + 0.5,
513 (GLfloat) (thisSproingie->z) - 0.5);
514 scale = (GLfloat) (1 << (thisSproingie->frame - BOOM_FRAME));
515 glScalef(scale, scale, scale);
516 if (!si->wireframe) {
518 glColor3fv(mat_color);
519 glDisable(GL_LIGHTING);
521 pointsize = (GLfloat) ((BOOM_FRAME + 8) - thisSproingie->frame) -
523 glPointSize((pointsize < 1.0) ? 1.0 : pointsize);
525 * PURIFY 4.0.1 reports an unitialized memory read on the next line when using
526 * MesaGL 2.2. This has been tracked to MesaGL 2.2 src/points.c line 313. */
527 glCallList(si->SproingieBoom);
529 if (!si->wireframe) {
530 glEnable(GL_LIGHTING);
532 } else if (thisSproingie->frame > 5) {
533 glTranslatef((GLfloat) (thisSproingie->x + 1),
534 (GLfloat) (thisSproingie->y - 1), (GLfloat) (thisSproingie->z - 1));
535 glRotatef((GLfloat) - 90.0, 0.0, 1.0, 0.0);
536 glCallList(si->sproingies[thisSproingie->frame - 6]);
538 glTranslatef((GLfloat) (thisSproingie->x), (GLfloat) (thisSproingie->y),
539 (GLfloat) (thisSproingie->z));
540 glCallList(si->sproingies[thisSproingie->frame]);
548 ComputeGround(sp_instance * si)
550 int g_higher, g_back, g_width, g_height;
552 /* higher: x-1, y+2, z-1 */
553 /* back: x-1, y, z+1 */
555 if (si->groundlevel == 0) {
558 } else if (si->groundlevel == 1) {
566 if ((g_higher = si->dist >> 3) < 4)
570 g_height = g_higher << 1;
573 g_higher += (g_higher >> 2);
574 else if (si->rotx > 10)
575 g_higher -= (g_higher >> 2);
578 if (si->dist > 128) {
582 } else if (si->dist > 64) {
585 } else if (si->dist > 32) {
586 /* nothing special */
589 g_higher = g_back = 4;
594 /* startx, starty, startz, width, height */
595 LayGround((-g_higher - g_back), (g_higher << 1), (g_back - g_higher),
596 (g_width), (g_height), si);
600 DisplaySproingies(int screen,int pause)
602 sp_instance *si = &si_list[screen];
605 {8.0, 5.0, -2.0, 0.1};
608 glClear(GL_COLOR_BUFFER_BIT);
610 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
613 glTranslatef(0.0, 0.0, -(GLfloat) (si->dist) / 16.0); /* viewing transform */
614 glRotatef((GLfloat) si->rotx, 1.0, 0.0, 0.0);
615 glRotatef((GLfloat) si->roty, 0.0, 1.0, 0.0);
618 glLightfv(GL_LIGHT0, GL_POSITION, position);
620 #if 0 /* Show light pos */
622 glTranslatef(position[0], position[1], position[2]);
623 glColor3f(1.0, 1.0, 1.0);
624 if (!si->wireframe) {
625 glDisable(GL_LIGHTING);
627 glCallList(si->SproingieBoom);
628 if (!si->wireframe) {
629 glEnable(GL_LIGHTING);
634 glTranslatef((GLfloat) si->sframe * (-1.0 / 12.0) - 0.75,
635 (GLfloat) si->sframe * (2.0 / 12.0) - 0.5,
636 (GLfloat) si->sframe * (-1.0 / 12.0) + 0.75);
641 for (t = 0; t < si->maxsproingies; ++t) {
642 RenderSproingie(t, si);
655 NextSproingieDisplay(int screen,int pause)
657 NextSproingie(screen);
658 if (pause) usleep(pause);
659 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);
674 CleanupSproingies(int screen)
676 sp_instance *si = &si_list[screen];
679 if (si->SproingieBoom) {
680 for (t = 0; t < 6; ++t)
681 glDeleteLists(si->sproingies[t], 1);
683 glDeleteLists(si->TopsSides, 2);
684 glDeleteLists(si->SproingieBoom, 1);
687 si->SproingieBoom = 0;
690 (void) free((void *) (si->positions));
691 si->positions = NULL;
693 if ((active_screens == 0) && si_list) {
694 (void) free((void *) (si_list));
700 InitSproingies(int wfmode, int grnd, int mspr, int screen, int numscreens,
704 {0.2, 0.2, 0.2, 1.0};
706 {10.0, 1.0, 1.0, 10.0};
707 GLfloat mat_diffuse[] =
708 {0.6, 0.6, 0.6, 1.0};
709 GLfloat mat_specular[] =
710 {0.8, 0.8, 0.8, 1.0};
711 GLfloat mat_shininess[] =
717 if (si_list == NULL) {
718 if ((si_list = (sp_instance *) calloc(numscreens,
719 sizeof (sp_instance))) == NULL)
722 si = &si_list[screen];
725 CleanupSproingies(screen);
729 if (mspr >= MAXSPROING)
730 mspr = MAXSPROING - 1;
734 si->dist = (16 << 2);
736 si->target_count = 0;
739 si->wireframe = si->flatshade = 0;
746 si->groundlevel = grnd;
747 si->maxsproingies = mspr;
749 if (si->maxsproingies) {
750 si->positions = (struct sPosColor *) calloc(si->maxsproingies,
751 sizeof (struct sPosColor));
753 if (!(si->positions))
754 si->maxsproingies = 0;
756 for (t = 0; t < si->maxsproingies; ++t) {
757 si->positions[t].x = 0;
758 si->positions[t].y = 0;
759 si->positions[t].z = 0;
760 si->positions[t].life = (-t * ((si->maxsproingies > 19) ? 1 : 4)) - 2;
761 si->positions[t].frame = 0;
764 #if 0 /* Test boom */
765 si->positions[0].x = 0;
766 si->positions[0].y = 0;
767 si->positions[0].z = 0;
768 si->positions[0].life = 10;
769 si->positions[0].frame = BOOM_FRAME;
770 si->positions[0].r = 0.656863;
771 si->positions[0].g = 1.0;
772 si->positions[0].b = 0.656863;
775 if (!(si->TopsSides = build_TopsSides(si->wireframe)))
776 (void) fprintf(stderr, "build_TopsSides\n");
778 if (!(si->sproingies[0] = BuildLWO(si->wireframe, &LWO_s1_1)))
779 (void) fprintf(stderr, "BuildLWO - 1\n");
780 if (!(si->sproingies[1] = BuildLWO(si->wireframe, &LWO_s1_2)))
781 (void) fprintf(stderr, "BuildLWO - 2\n");
782 if (!(si->sproingies[2] = BuildLWO(si->wireframe, &LWO_s1_3)))
783 (void) fprintf(stderr, "BuildLWO - 3\n");
784 if (!(si->sproingies[3] = BuildLWO(si->wireframe, &LWO_s1_4)))
785 (void) fprintf(stderr, "BuildLWO - 4\n");
786 if (!(si->sproingies[4] = BuildLWO(si->wireframe, &LWO_s1_5)))
787 (void) fprintf(stderr, "BuildLWO - 5\n");
788 if (!(si->sproingies[5] = BuildLWO(si->wireframe, &LWO_s1_6)))
789 (void) fprintf(stderr, "BuildLWO - 6\n");
791 if (!(si->SproingieBoom = BuildLWO(si->wireframe, &LWO_s1_b)))
792 (void) fprintf(stderr, "BuildLWO - b\n");
795 glShadeModel(GL_FLAT);
796 glDisable(GL_LIGHTING);
799 glShadeModel(GL_FLAT);
803 glEnable(GL_LIGHTING);
805 glDepthFunc(GL_LEQUAL);
806 glEnable(GL_DEPTH_TEST);
808 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
809 glLightfv(GL_LIGHT0, GL_POSITION, position);
811 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
812 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
813 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
815 /* glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE); */
817 glEnable(GL_CULL_FACE);
820 /* glEnable(GL_NORMALIZE); */
826 /* End of sproingies.c */