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 struct gllist *sproingies[6];
54 struct gllist *SproingieBoom;
56 struct sPosColor *positions;
59 static sp_instance *si_list = NULL;
60 static int active_screens = 0;
62 void SproingieSwap(void);
64 extern struct gllist *s1_1;
65 extern struct gllist *s1_2;
66 extern struct gllist *s1_3;
67 extern struct gllist *s1_4;
68 extern struct gllist *s1_5;
69 extern struct gllist *s1_6;
70 extern struct gllist *s1_b;
75 return ((int) (((float) range) * LRAND() / (MAXRAND)));
79 build_TopsSides(int wireframe)
82 GLfloat mat_color[4] =
85 dl_num = glGenLists(2);
87 return (0); /* 0 means out of display lists. */
90 glNewList(dl_num, GL_COMPILE);
91 mat_color[0] = 0.392157;
92 mat_color[1] = 0.784314;
93 mat_color[2] = 0.941176;
95 glColor3fv(mat_color);
97 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_color);
102 glNewList(dl_num + 1, GL_COMPILE);
104 glColor3fv(mat_color);
106 /* jwz: in wireframe mode, color tops and sides the same. */
107 mat_color[0] = 0.156863;
108 mat_color[1] = 0.156863;
109 mat_color[2] = 0.392157;
110 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_color);
117 LayGround(int sx, int sy, int sz, int width, int height, sp_instance * si)
120 GLenum begin_polygon;
123 begin_polygon = GL_LINE_LOOP;
125 begin_polygon = GL_POLYGON;
127 if (!si->wireframe) {
129 glCallList(si->TopsSides); /* Render the tops */
130 glNormal3f(0.0, 1.0, 0.0);
132 for (h = 0; h < height; ++h) {
136 for (w = 0; w < width; ++w) {
137 glBegin(begin_polygon);
139 glVertex3i(x, y, z - 1);
140 glVertex3i(x + 1, y, z - 1);
141 glVertex3i(x + 1, y, z);
143 glBegin(begin_polygon);
144 glVertex3i(x + 1, y - 1, z);
145 glVertex3i(x + 1, y - 1, z - 1);
146 glVertex3i(x + 2, y - 1, z - 1);
147 glVertex3i(x + 2, y - 1, z);
155 glCallList(si->TopsSides + 1); /* Render the sides */
157 glNormal3f(0.0, 0.0, 1.0);
159 for (h = 0; h < height; ++h) {
163 for (w = 0; w < width; ++w) {
164 glBegin(begin_polygon);
166 glVertex3i(x + 1, y, z);
167 glVertex3i(x + 1, y - 1, z);
168 glVertex3i(x, y - 1, z);
170 glBegin(begin_polygon);
171 glVertex3i(x + 1, y - 1, z);
172 glVertex3i(x + 2, y - 1, z);
173 glVertex3i(x + 2, y - 2, z);
174 glVertex3i(x + 1, y - 2, z);
176 * PURIFY 4.0.1 reports an unitialized memory read on the next line when using
177 * MesaGL 2.2 and -mono. This has been fixed in MesaGL 2.3 and later. */
184 /* Render the other sides */
186 glNormal3f(1.0, 0.0, 0.0);
188 for (h = 0; h < height; ++h) {
192 for (w = 0; w < width; ++w) {
193 glBegin(begin_polygon);
194 glVertex3i(x + 1, y, z);
195 glVertex3i(x + 1, y, z - 1);
196 glVertex3i(x + 1, y - 1, z - 1);
197 glVertex3i(x + 1, y - 1, z);
199 glBegin(begin_polygon);
200 glVertex3i(x + 2, y - 1, z);
201 glVertex3i(x + 2, y - 1, z - 1);
202 glVertex3i(x + 2, y - 2, z - 1);
203 glVertex3i(x + 2, y - 2, z);
212 glCallList(si->TopsSides); /* Render the tops */
214 for (h = 0; h < height; ++h) {
218 for (w = 0; w < width; ++w) {
219 glBegin(begin_polygon);
221 glVertex3i(x, y, z - 1);
222 glVertex3i(x + 1, y, z - 1);
223 glVertex3i(x + 1, y, z);
225 glBegin(begin_polygon);
226 glVertex3i(x + 1, y - 1, z);
227 glVertex3i(x + 1, y - 1, z - 1);
228 glVertex3i(x + 2, y - 1, z - 1);
229 glVertex3i(x + 2, y - 1, z);
238 #define RESET_SPROINGIE (-30 + myrand(28))
241 AdvanceSproingie(int t, sp_instance * si)
243 int g_higher, g_back, t2;
244 struct sPosColor *thisSproingie = &(si->positions[t]);
245 struct sPosColor *S2 = &(si->positions[0]);
247 if (thisSproingie->life > 0) {
248 if ((++(thisSproingie->frame)) > 11) {
249 if (thisSproingie->frame >= BOOM_FRAME) {
250 if ((thisSproingie->r -= 0.08) < 0.0)
251 thisSproingie->r = 0.0;
252 if ((thisSproingie->g -= 0.08) < 0.0)
253 thisSproingie->g = 0.0;
254 if ((thisSproingie->b -= 0.08) < 0.0)
255 thisSproingie->b = 0.0;
256 if ((--(thisSproingie->life)) < 1) {
257 thisSproingie->life = RESET_SPROINGIE;
261 thisSproingie->x += 1;
262 thisSproingie->y -= 2;
263 thisSproingie->z += 1;
264 thisSproingie->frame = 0;
266 for (t2 = 0; t2 < si->maxsproingies; ++t2) {
267 if ((t2 != t) && (thisSproingie->x == S2->x) &&
268 (thisSproingie->y == S2->y) && (thisSproingie->z == S2->z) &&
269 (S2->life > 10) && (S2->frame < 6)) {
271 if (thisSproingie->life > S2->life) {
275 if (thisSproingie->life > 10) {
276 thisSproingie->life = 10;
277 thisSproingie->frame = BOOM_FRAME;
278 if ((thisSproingie->r += 0.5) > 1.0)
279 thisSproingie->r = 1.0;
280 if ((thisSproingie->g += 0.5) > 1.0)
281 thisSproingie->g = 1.0;
282 if ((thisSproingie->b += 0.5) > 1.0)
283 thisSproingie->b = 1.0;
292 if (!((thisSproingie->life == 10) &&
293 (thisSproingie->frame > 0) &&
294 (thisSproingie->frame < BOOM_FRAME))) {
295 if ((--(thisSproingie->life)) < 1) {
296 thisSproingie->life = RESET_SPROINGIE;
297 } else if (thisSproingie->life < 9) {
298 thisSproingie->frame -= 2;
300 } /* else wait here for frame 0 to come about. */
301 } else if (++(thisSproingie->life) >= 0) {
303 g_higher = -3 + myrand(5);
304 g_back = -2 + myrand(5);
306 g_higher = -2 + myrand(3);
307 g_back = -1 + myrand(3);
313 thisSproingie->x = (-g_higher - g_back);
314 thisSproingie->y = (g_higher << 1);
315 thisSproingie->z = (g_back - g_higher);
316 thisSproingie->life = 40 + myrand(200);
317 thisSproingie->frame = -10;
318 thisSproingie->r = (GLfloat) (40 + myrand(200)) / 255.0;
319 thisSproingie->g = (GLfloat) (40 + myrand(200)) / 255.0;
320 thisSproingie->b = (GLfloat) (40 + myrand(200)) / 255.0;
322 for (t2 = 0; t2 < si->maxsproingies; ++t2) {
323 if ((t2 != t) && (thisSproingie->x == S2->x) &&
324 (thisSproingie->y == S2->y) && (thisSproingie->z == S2->z) &&
325 (S2->life > 10) && (S2->frame < 0)) {
326 /* If one is already being born, just wait. */
327 thisSproingie->life = -1;
335 NextSproingie(int screen)
337 sp_instance *si = &si_list[screen];
339 struct sPosColor *thisSproingie = &(si->positions[0]);
341 if (++si->sframe > 11) {
343 for (t = 0; t < si->maxsproingies; ++t) {
344 thisSproingie->x -= 1;
345 thisSproingie->y += 2;
346 thisSproingie->z -= 1;
350 for (t = 0; t < si->maxsproingies; ++t) {
351 AdvanceSproingie(t, si);
354 if (si->target_count < 0) { /* track to current target */
355 if (si->target_rx < si->rotx)
357 else if (si->target_rx > si->rotx)
360 if (si->target_ry < si->roty)
362 else if (si->target_ry > si->roty)
365 ddx = (si->target_dist - si->dist) / 8;
368 else if (si->target_dist < si->dist)
370 else if (si->target_dist > si->dist)
373 if ((si->target_rx == si->rotx) && (si->target_ry == si->roty) &&
374 (si->target_dist == si->dist)) {
375 si->target_count = T_COUNT;
376 if (si->target_dist <= 32)
377 si->target_count >>= 2;
379 } else if (--si->target_count < 0) { /* make up new target */
380 si->target_rx = myrand(100) - 35;
381 si->target_ry = -myrand(90);
382 si->target_dist = 32 << myrand(2); /* could be 32, 64, or 128, (previously or 256) */
384 if (si->target_dist >= si->dist) /* no duplicate distances */
385 si->target_dist <<= 1;
387 /* Otherwise just hang loose for a while here */
396 for (t = 0; t < maxsproingies; ++t) {
397 if (positions[t].life > 0)
400 (void) printf("RotX: %d, RotY: %d, Dist: %d. Targets: X %d, Y %d, D %d. Visible: %d\n",
401 rotx, roty, dist, target_rx, target_ry, target_dist, count);
409 for (t = 0; t < maxsproingies; ++t) {
413 positions[t].life = -2;
414 positions[t].frame = 0;
421 if (dist < (1 << 16 << 4))
435 rotx = (rotx + 5) % 360;
441 rotx = (rotx - 5) % 360;
447 roty = (roty + 5) % 360;
453 roty = (roty - 5) % 360;
459 rotx = (rotx + 45) % 360;
465 rotx = (rotx - 45) % 360;
471 roty = (roty + 45) % 360;
477 roty = (roty - 45) % 360;
483 RenderSproingie(int t, sp_instance * si)
485 GLfloat scale, pointsize, mat_color[4] =
486 {0.0, 0.0, 0.0, 1.0};
487 GLdouble clipplane[4] =
488 {0.0, 1.0, 0.0, 0.0};
489 struct sPosColor *thisSproingie = &(si->positions[t]);
491 if (thisSproingie->life < 1)
497 mat_color[0] = thisSproingie->r;
498 mat_color[1] = thisSproingie->g;
499 mat_color[2] = thisSproingie->b;
501 glColor3fv(mat_color);
503 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_color);
506 if (thisSproingie->frame < 0) {
507 glEnable(GL_CLIP_PLANE0);
508 glTranslatef((GLfloat) (thisSproingie->x),
509 (GLfloat) (thisSproingie->y) +
510 ((GLfloat) (thisSproingie->frame) / 9.0),
511 (GLfloat) (thisSproingie->z));
512 clipplane[3] = ((GLdouble) (thisSproingie->frame) / 9.0) +
513 (si->wireframe ? 0.0 : 0.1);
514 glClipPlane(GL_CLIP_PLANE0, clipplane);
515 /** glCallList(si->sproingies[0]);*/
516 /**/ renderList(si->sproingies[0]);
517 glDisable(GL_CLIP_PLANE0);
518 } else if (thisSproingie->frame >= BOOM_FRAME) {
519 glTranslatef((GLfloat) (thisSproingie->x) + 0.5,
520 (GLfloat) (thisSproingie->y) + 0.5,
521 (GLfloat) (thisSproingie->z) - 0.5);
522 scale = (GLfloat) (1 << (thisSproingie->frame - BOOM_FRAME));
523 glScalef(scale, scale, scale);
524 if (!si->wireframe) {
526 glColor3fv(mat_color);
527 glDisable(GL_LIGHTING);
529 pointsize = (GLfloat) ((BOOM_FRAME + 8) - thisSproingie->frame) -
531 glPointSize((pointsize < 1.0) ? 1.0 : pointsize);
533 * PURIFY 4.0.1 reports an unitialized memory read on the next line when using
534 * MesaGL 2.2. This has been tracked to MesaGL 2.2 src/points.c line 313. */
535 /** glCallList(si->SproingieBoom);*/
536 /**/ renderList(si->SproingieBoom);
538 if (!si->wireframe) {
539 glEnable(GL_LIGHTING);
541 } else if (thisSproingie->frame > 5) {
542 glTranslatef((GLfloat) (thisSproingie->x + 1),
543 (GLfloat) (thisSproingie->y - 1), (GLfloat) (thisSproingie->z - 1));
544 glRotatef((GLfloat) - 90.0, 0.0, 1.0, 0.0);
545 /** glCallList(si->sproingies[thisSproingie->frame - 6]);*/
546 /**/ renderList(si->sproingies[thisSproingie->frame - 6]);
548 glTranslatef((GLfloat) (thisSproingie->x), (GLfloat) (thisSproingie->y),
549 (GLfloat) (thisSproingie->z));
550 /** glCallList(si->sproingies[thisSproingie->frame]);*/
551 /**/ renderList(si->sproingies[thisSproingie->frame]);
559 ComputeGround(sp_instance * si)
561 int g_higher, g_back, g_width, g_height;
563 /* higher: x-1, y+2, z-1 */
564 /* back: x-1, y, z+1 */
566 if (si->groundlevel == 0) {
569 } else if (si->groundlevel == 1) {
577 if ((g_higher = si->dist >> 3) < 4)
581 g_height = g_higher << 1;
584 g_higher += (g_higher >> 2);
585 else if (si->rotx > 10)
586 g_higher -= (g_higher >> 2);
589 if (si->dist > 128) {
593 } else if (si->dist > 64) {
596 } else if (si->dist > 32) {
597 /* nothing special */
600 g_higher = g_back = 4;
605 /* startx, starty, startz, width, height */
606 LayGround((-g_higher - g_back), (g_higher << 1), (g_back - g_higher),
607 (g_width), (g_height), si);
611 DisplaySproingies(int screen,int pause)
613 sp_instance *si = &si_list[screen];
616 {8.0, 5.0, -2.0, 0.1};
619 glClear(GL_COLOR_BUFFER_BIT);
621 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
624 glTranslatef(0.0, 0.0, -(GLfloat) (si->dist) / 16.0); /* viewing transform */
625 glRotatef((GLfloat) si->rotx, 1.0, 0.0, 0.0);
626 glRotatef((GLfloat) si->roty, 0.0, 1.0, 0.0);
629 glLightfv(GL_LIGHT0, GL_POSITION, position);
631 #if 0 /* Show light pos */
633 glTranslatef(position[0], position[1], position[2]);
634 glColor3f(1.0, 1.0, 1.0);
635 if (!si->wireframe) {
636 glDisable(GL_LIGHTING);
638 glCallList(si->SproingieBoom);
639 if (!si->wireframe) {
640 glEnable(GL_LIGHTING);
645 glTranslatef((GLfloat) si->sframe * (-1.0 / 12.0) - 0.75,
646 (GLfloat) si->sframe * (2.0 / 12.0) - 0.5,
647 (GLfloat) si->sframe * (-1.0 / 12.0) + 0.75);
652 for (t = 0; t < si->maxsproingies; ++t) {
653 RenderSproingie(t, si);
666 NextSproingieDisplay(int screen,int pause)
668 NextSproingie(screen);
669 if (pause) usleep(pause);
670 DisplaySproingies(screen,pause);
674 ReshapeSproingies(int w, int h)
676 glViewport(0, 0, w, h);
677 glMatrixMode(GL_PROJECTION);
679 gluPerspective(65.0, (GLfloat) w / (GLfloat) h, 0.1, 2000.0); /* was 200000.0 */
680 glMatrixMode(GL_MODELVIEW);
685 CleanupSproingies(int screen)
687 sp_instance *si = &si_list[screen];
690 if (si->SproingieBoom) {
691 for (t = 0; t < 6; ++t)
692 glDeleteLists(si->sproingies[t], 1);
694 glDeleteLists(si->TopsSides, 2);
695 glDeleteLists(si->SproingieBoom, 1);
698 si->SproingieBoom = 0;
702 glDeleteLists(si->TopsSides, 2);
705 (void) free((void *) (si->positions));
706 si->positions = NULL;
708 if ((active_screens == 0) && si_list) {
709 (void) free((void *) (si_list));
715 InitSproingies(int wfmode, int grnd, int mspr, int screen, int numscreens,
719 {0.2, 0.2, 0.2, 1.0};
721 {10.0, 1.0, 1.0, 10.0};
722 GLfloat mat_diffuse[] =
723 {0.6, 0.6, 0.6, 1.0};
724 GLfloat mat_specular[] =
725 {0.8, 0.8, 0.8, 1.0};
726 GLfloat mat_shininess[] =
732 if (si_list == NULL) {
733 if ((si_list = (sp_instance *) calloc(numscreens,
734 sizeof (sp_instance))) == NULL)
737 si = &si_list[screen];
740 CleanupSproingies(screen);
744 if (mspr >= MAXSPROING)
745 mspr = MAXSPROING - 1;
749 si->dist = (16 << 2);
751 si->target_count = 0;
754 si->wireframe = si->flatshade = 0;
761 si->groundlevel = grnd;
762 si->maxsproingies = mspr;
764 if (si->maxsproingies) {
765 si->positions = (struct sPosColor *) calloc(si->maxsproingies,
766 sizeof (struct sPosColor));
768 if (!(si->positions))
769 si->maxsproingies = 0;
771 for (t = 0; t < si->maxsproingies; ++t) {
772 si->positions[t].x = 0;
773 si->positions[t].y = 0;
774 si->positions[t].z = 0;
775 si->positions[t].life = (-t * ((si->maxsproingies > 19) ? 1 : 4)) - 2;
776 si->positions[t].frame = 0;
779 #if 0 /* Test boom */
780 si->positions[0].x = 0;
781 si->positions[0].y = 0;
782 si->positions[0].z = 0;
783 si->positions[0].life = 10;
784 si->positions[0].frame = BOOM_FRAME;
785 si->positions[0].r = 0.656863;
786 si->positions[0].g = 1.0;
787 si->positions[0].b = 0.656863;
790 if (!(si->TopsSides = build_TopsSides(si->wireframe)))
791 (void) fprintf(stderr, "build_TopsSides\n");
793 if (!(si->sproingies[0] = BuildLWO(si->wireframe, &LWO_s1_1)))
794 (void) fprintf(stderr, "BuildLWO - 1\n");
795 if (!(si->sproingies[1] = BuildLWO(si->wireframe, &LWO_s1_2)))
796 (void) fprintf(stderr, "BuildLWO - 2\n");
797 if (!(si->sproingies[2] = BuildLWO(si->wireframe, &LWO_s1_3)))
798 (void) fprintf(stderr, "BuildLWO - 3\n");
799 if (!(si->sproingies[3] = BuildLWO(si->wireframe, &LWO_s1_4)))
800 (void) fprintf(stderr, "BuildLWO - 4\n");
801 if (!(si->sproingies[4] = BuildLWO(si->wireframe, &LWO_s1_5)))
802 (void) fprintf(stderr, "BuildLWO - 5\n");
803 if (!(si->sproingies[5] = BuildLWO(si->wireframe, &LWO_s1_6)))
804 (void) fprintf(stderr, "BuildLWO - 6\n");
806 if (!(si->SproingieBoom = BuildLWO(si->wireframe, &LWO_s1_b)))
807 (void) fprintf(stderr, "BuildLWO - b\n");
809 si->sproingies[0]=s1_1;
810 si->sproingies[1]=s1_2;
811 si->sproingies[2]=s1_3;
812 si->sproingies[3]=s1_4;
813 si->sproingies[4]=s1_5;
814 si->sproingies[5]=s1_6;
815 si->SproingieBoom=s1_b;
818 glShadeModel(GL_FLAT);
819 glDisable(GL_LIGHTING);
822 glShadeModel(GL_FLAT);
826 glEnable(GL_LIGHTING);
828 glDepthFunc(GL_LEQUAL);
829 glEnable(GL_DEPTH_TEST);
831 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
832 glLightfv(GL_LIGHT0, GL_POSITION, position);
834 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
835 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
836 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
838 /* glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE); */
840 glEnable(GL_CULL_FACE);
843 /* glEnable(GL_NORMALIZE); */
849 /* End of sproingies.c */