1 /* Juggler3D, Copyright (c) 2005-2008 Brian Apps <brian@jugglesaver.co.uk>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that copyright
6 * notice and this permission notice appear in supporting documentation. No
7 * representations are made about the suitability of this software for any
8 * purpose. It is provided "as is" without express or implied warranty. */
11 #define countof(x) (sizeof((x))/sizeof((*x)))
14 "*delay: 20000\n*showFPS: False\n*wireframe: False\n"
16 # define refresh_juggler3d 0
17 # define release_juggler3d 0
18 #include "xlockmore.h"
19 #include "gltrackball.h"
21 #ifdef USE_GL /* whole file */
23 /* A selection of macros to make functions from math.h return single precision
24 * numbers. Arguably it's better to work at a higher precision and cast it
25 * back but littering the code with casts makes it less readable -- without
26 * the casts you can get tons of warnings from the compiler (particularily
27 * MSVC which enables loss of precision warnings by default) */
29 #define cosf(a) (float)(cos((a)))
30 #define sinf(a) (float)(sin((a)))
31 #define tanf(a) (float)(tan((a)))
32 #define sqrtf(a) (float)(sqrt((a)))
33 #define powf(a, b) (float)(pow((a), (b)))
38 #define max(a, b) ((a) > (b) ? (a) : (b))
39 #define min(a, b) ((a) < (b) ? (a) : (b))
42 /******************************************************************************
44 * The code is broadly split into the following parts:
46 * - Engine. The process of determining the position of the juggler and
47 * objects being juggled at an arbitrary point in time. This is
48 * independent from any drawing code.
49 * - Sites. The process of creating a random site swap pattern or parsing
50 * a Juggle Saver compatible siteswap for use by the engine. For an
51 * introduction to juggling site swaps check out
52 * http://www.jugglingdb.com/
53 * - Rendering. OpenGL drawing code that animates the juggler.
54 * - XScreenSaver. Interface code to get thing working as a GLX hack.
56 *****************************************************************************/
59 /*****************************************************************************
63 *****************************************************************************/
65 /* POS is used to represent the position of a hand when it catches or throws
66 * an object; as well as the orientation of the object. The rotation and
67 * elevation are specified in degrees. These angles are not normalised so that
68 * it is possible to specify how the object spins and rotates as it is thrown
69 * from the 'From' position to the 'To' position.
71 * Because this is the position of the hand some translation is required with
72 * rings and clubs to get the centre of rotation position. */
84 /* An array of THROW_INFOs are configured with each entry corresponding to the
85 * position in the site swap (In fact we double up odd length patterns to ensure
86 * there is left/right symmetry). It allows us to quickly determine where an
87 * object and the hands are at a given time. The information is specified in
88 * terms of throws, and positions where throws aren't present (0's and 2's) are
91 * TotalTime - The count of beats before this object is thrown again. Typically
92 * this is the same as the weight of the throw but where an object is held it
93 * is longer. e.g. the first throw of the site 64242.7. will be 10, 6 for
94 * throw and 4 (two 2's) for the carry.
95 * TimeInAir - The weight of the throw.
96 * PrevThrow - zero based index into array of THROW_INFOs of the previous throw.
97 * e.g. for the throw '8' in the site 345678..... the PrevThrow is 1
99 * FromPos, FromVelocity, ToPos, ToVelocity - The position and speeds at the
100 * start and end of the throw. These are used to generate a spline while
101 * carrying an object and while moving the hand from a throw to a catch.
102 * NextForHand - Number of beats before the hand that throws this object will
103 * throw another object. This is always going to be at least 2. When there
104 * are gaps in the pattern (0's) or holds (2's) NextForHand increases. */
121 /* OBJECT_POSITION works with the array of THROW_INFOs to allow us to determine
122 * exactly where an object or hand is.
124 * TimeOffset - The total number of beats expired when the object was thrown.
125 * ThrowIndex - The zero based index into the THROW_INFO array for the current
127 * ObjectType - One of the OBJECT_XX defines.
128 * TotalTwist - Only relevant for OBJECT_BALL, this is the total amount the ball
129 * has twisted while in the air. When segmented balls are drawn you see a
130 * spinning effect similar to what happens when you juggle beanbags. */
132 #define OBJECT_DEFAULT 0
133 #define OBJECT_BALL 1
134 #define OBJECT_CLUB 2
135 #define OBJECT_RING 3
146 /* PATTERN_INFO is the main structure that holds the information about a
149 * pThrowInfo is an array of ThrowLen elements that describes each throw in the
151 * pObjectInfo gives the current position of all objects at a given instant.
152 * These values are updated as the pattern is animated.
153 * LeftHand and RightHand describe the current positions of each of the
155 * MaxWeight is the maximum weight of the all throws in pThrowInfo.
156 * Height and Alpha are parameters that describe how objects fall under the
157 * influence of gravity. See SetHeightAndAlpha() for the gory details. */
161 THROW_INFO* pThrowInfo;
164 OBJECT_POSITION* pObjectInfo;
167 OBJECT_POSITION LeftHand;
168 OBJECT_POSITION RightHand;
177 /* EXT_SITE_INFO is used to initialise a PATTERN_INFO object using a Juggle
178 * Saver compatible site swap. These contain additional information about the
179 * type of object thrown, the positions of throw and catch etc. */
181 #define HAS_FROM_POS 1
199 /* RENDER_STATE is used to co-ordinate the OpenGL rendering of the juggler and
201 * pPattern - The pattern to be juggled
202 * CameraElev - The elevation angle (in degrees) that the camera is looking
203 * along. 0 is horizontal and a +ve angle is looking down. This value
204 * should be between -90 and +90.
205 * AspectRatio - Window width to height ratio.
206 * DLStart - The number for the first display list created, any others directly
208 * Time - Animation time (in beats)
209 * TranslateAngle - Cumulative translation (in degrees) for the juggling figure.
210 * SpinAngle- Cumulative spin (in degrees) for the juggling figure.
215 PATTERN_INFO* pPattern;
221 float TranslateAngle;
224 trackball_state *trackball;
230 /*****************************************************************************
234 ****************************************************************************
236 * The main purpose of the engine is to work out the exact position of all the
237 * juggling objects and the juggler's hands at any point in time. The motion
238 * of the objects can be split into two parts: in the air and and being carried.
240 * While in the air, the motion is governed by a standard parabolic trajectory.
241 * The only minor complication is that the engine has no fixed concept of
242 * gravity, instead it using a term called Alpha that varies according to the
243 * pattern (see SetHeightAndAlpha).
245 * The motion while an object is carried comes from fitting a spline through the
246 * catch and throw points and maintaining the catch and throw velocities at
247 * each end. In the simplest case this boils down to cubic Bezier spline. The
248 * only wrinkle occurs when a ball is being carried for a long time. The simple
249 * cubic spline maths produces a curve that goes miles away -- here we do a
250 * bit of reparameterisation so things stay within sensible bounds.
251 * (On a related note, this scheme is _much_ simpler than the Juggle Saver
252 * one. Juggle Saver achieves 2nd order continuity and much care is taken
253 * to avoid spline ringing.)
255 * The motion of the hands is identical to the ball carrying code. It uses two
256 * splines: one while an object is being carried; and another when it moves from
257 * the previous throw to the next catch.
260 static const float CARRY_TIME = 0.56f;
261 static const float PI = 3.14159265358979f;
264 /* While a ball is thrown it twists slighty about an axis, this routine gives
265 * the total about of twist for a given ball throw. */
267 static float GetBallTwistAmount(const THROW_INFO* pThrow)
269 if (pThrow->FromPos.x > pThrow->ToPos.x)
270 return 18.0f * powf(pThrow->TimeInAir, 1.5);
272 return -18.0f * powf(pThrow->TimeInAir, 1.5);
276 static float NormaliseAngle(float Ang)
280 int i = (int) (Ang + 180.0f) / 360;
281 return Ang - 360.0f * i;
285 int i = (int)(180.0f - Ang) / 360;
286 return Ang + i * 360.0f;
291 /* The interpolate routine for ball carrying and hand motion. We are given the
292 * start (P0) and end (P1) points and the velocities at these points, the task
293 * is to form a function P(t) such that:
300 static POS InterpolatePosition(
301 const POS* pP0, const POS* pV0, const POS* pP1, const POS* pV1,
305 float a, b, c, d, tt, tc;
307 /* The interpolation is based on a simple cubic that achieves 1st order
308 * continuity at the end points. However the spline can become too long if
309 * the TLen parameter is large. In this case we cap the curve's length (fix
310 * the shape) and then reparameterise time to achieve the continuity
317 /* The reparameterisation tt(t) gives:
318 * tt(0) = 0, tt(TLen) = tc, tt'(0) = 1, tt'(TLen) = 1
319 * and means we can set t = tt(t), TLen = tc and then fall through
320 * to use the normal cubic spline fit.
322 * The reparameterisation is based on two piecewise quadratics, one
323 * that goes from t = 0 to t = TLen / 2 and the other, mirrored in
324 * tt and t that goes from t = TLen / 2 to t = TLen.
325 * Because TLen > tc we can arrange for tt to be unique in the range if
326 * we specify the quadratic in tt. i.e. t = A * tt ^ 2 + B * tt + C.
328 * Considering the first piece and applying initial conditions.
329 * tt = 0 when t = 0 => C = 0
330 * tt' = 1 when t = 0 => B = 1
331 * tt = tc / 2 when t = TLen / 2 => A = 2 * (TLen - tc) / tc^2
333 * writing in terms of t
334 * tt = (-B + (B ^ 2 + 4At) ^ 0.5) / 2A
336 * tt = ((1 + 4At) ^ 0.5 - 1) / 2A */
338 float A = 2.0f * (TLen - tc) / (tc * tc);
341 t = tc - (sqrtf(1.0f + 4.0f * A * (TLen - t)) - 1.0f) / (2.0f * A);
343 t = (sqrtf(1.0f + 4.0f * A * t) - 1.0f) / (2.0f * A);
348 /* The cubic spline takes the form:
349 * P(t) = p0 * a(t) + v0 * b(t) + p1 * c(t) + v1 * d(t)
350 * where p0 is the start point, v0 the start velocity, p1 the end point and
351 * v1 the end velocity. a(t), b(t), c(t) and d(t) are cubics in t.
354 * a(t) = 2 * (t / TLen) ^ 3 - 3 * (t / TLen) ^ 2 + 1
355 * b(t) = t ^ 3 / TLen ^ 2 - 2 * t ^ 2 / TLen + t
356 * c(t) = -2 * (t / TLen) ^ 3 + 3 * (t / TLen) ^ 2
357 * d(t) = t ^ 3 / TLen ^ 2 - t ^ 2 / TLen
359 * statisfy the boundary conditions:
360 * P(0) = p0, P(TLen) = p1, P'(0) = v0 and P'(TLen) = v1 */
364 a = tt * tt * (2.0f * tt - 3.0f) + 1.0f;
365 b = t * tt * (tt - 2.0f) + t;
366 c = tt * tt * (3.0f - 2.0f * tt);
367 d = t * tt * (tt - 1.0f);
369 p.x = a * pP0->x + b * pV0->x + c * pP1->x + d * pV1->x;
370 p.y = a * pP0->y + b * pV0->y + c * pP1->y + d * pV1->y;
371 p.z = a * pP0->z + b * pV0->z + c * pP1->z + d * pV1->z;
373 p.Rot = a * NormaliseAngle(pP0->Rot) + b * pV0->Rot +
374 c * NormaliseAngle(pP1->Rot) + d * pV1->Rot;
375 p.Elev = a * NormaliseAngle(pP0->Elev) + b * pV0->Elev +
376 c * NormaliseAngle(pP1->Elev) + d * pV1->Elev;
382 static POS InterpolateCarry(
383 const THROW_INFO* pThrow, const THROW_INFO* pNext, float t)
385 float CT = CARRY_TIME + pThrow->TotalTime - pThrow->TimeInAir;
386 return InterpolatePosition(&pThrow->ToPos, &pThrow->ToVelocity,
387 &pNext->FromPos, &pNext->FromVelocity, CT, t);
391 /* Determine the position of the hand at a point in time. */
393 static void GetHandPosition(
394 PATTERN_INFO* pPattern, int RightHand, float Time, POS* pPos)
396 OBJECT_POSITION* pObj =
397 RightHand == 0 ? &pPattern->LeftHand : &pPattern->RightHand;
398 THROW_INFO* pLastThrow;
400 /* Upon entry, the throw information for the relevant hand may be out of
401 * sync. Therefore we advance through the pattern if required. */
403 while (pPattern->pThrowInfo[pObj->ThrowIndex].NextForHand + pObj->TimeOffset
406 int w = pPattern->pThrowInfo[pObj->ThrowIndex].NextForHand;
407 pObj->TimeOffset += w;
408 pObj->ThrowIndex = (pObj->ThrowIndex + w) % pPattern->ThrowLen;
411 pLastThrow = &pPattern->pThrowInfo[pObj->ThrowIndex];
413 /* The TimeInAir will only ever be 2 or 0 if no object is ever thrown by
414 * this hand. In normal circumstances, 2's in the site swap are coalesced
415 * and added to TotalTime of the previous throw. 0 is a hole and means that
416 * an object isn't there. In this case we just hold the hand still. */
417 if (pLastThrow->TimeInAir == 2 || pLastThrow->TimeInAir == 0)
419 pPos->x = pLastThrow->FromPos.x;
420 pPos->y = pLastThrow->FromPos.y;
424 /* The hand is either moving to catch the next object or carrying the
425 * next object to its next throw position. The way THROW_INFO is
426 * structured means the relevant information for the object we're going
427 * to catch is held at the point at which it was thrown
428 * (pNextThrownFrom). We can't go straight for it and instead have to
429 * look at the object we've about to throw next and work out where it
432 THROW_INFO* pNextThrow = &pPattern->pThrowInfo[
433 (pObj->ThrowIndex + pLastThrow->NextForHand) % pPattern->ThrowLen];
435 THROW_INFO* pNextThrownFrom =
436 &pPattern->pThrowInfo[pNextThrow->PrevThrow];
438 /* tc is a measure of how long the object we're due to catch is being
439 * carried for. We use this to work out if we've actually caught it at
440 * this moment in time. */
442 float tc = CARRY_TIME +
443 pNextThrownFrom->TotalTime - pNextThrownFrom->TimeInAir;
445 Time -= pObj->TimeOffset;
447 if (Time > pLastThrow->NextForHand - tc)
449 /* carrying this ball to it's new location */
450 *pPos = InterpolateCarry(pNextThrownFrom,
451 pNextThrow, (Time - (pLastThrow->NextForHand - tc)));
455 /* going for next catch */
456 *pPos = InterpolatePosition(
457 &pLastThrow->FromPos, &pLastThrow->FromVelocity,
458 &pNextThrownFrom->ToPos, &pNextThrownFrom->ToVelocity,
459 pLastThrow->NextForHand - tc, Time);
465 static float SinDeg(float AngInDegrees)
467 return sinf(AngInDegrees * PI / 180.0f);
471 static float CosDeg(float AngInDegrees)
473 return cosf(AngInDegrees * PI / 180.0f);
477 /* Offset the specified position to get the centre of the object based on the
478 * the handle length and the current orientation */
480 static void OffsetHandlePosition(const POS* pPos, float HandleLen, POS* pResult)
482 pResult->x = pPos->x + HandleLen * SinDeg(pPos->Rot) * CosDeg(pPos->Elev);
483 pResult->y = pPos->y + HandleLen * SinDeg(pPos->Elev);
484 pResult->z = pPos->z + HandleLen * CosDeg(pPos->Rot) * CosDeg(pPos->Elev);
485 pResult->Elev = pPos->Elev;
486 pResult->Rot = pPos->Rot;
490 static void GetObjectPosition(
491 PATTERN_INFO* pPattern, int Obj, float Time, float HandleLen, POS* pPos)
493 OBJECT_POSITION* pObj = &pPattern->pObjectInfo[Obj];
496 /* Move through the pattern, if required, such that pThrow corresponds to
497 * the current throw for this object. */
499 while (pPattern->pThrowInfo[pObj->ThrowIndex].TotalTime + pObj->TimeOffset
502 int w = pPattern->pThrowInfo[pObj->ThrowIndex].TotalTime;
503 pObj->TimeOffset += w;
504 pObj->TotalTwist = NormaliseAngle(pObj->TotalTwist +
505 GetBallTwistAmount(&pPattern->pThrowInfo[pObj->ThrowIndex]));
507 pObj->ThrowIndex = (pObj->ThrowIndex + w) % pPattern->ThrowLen;
510 pThrow = &pPattern->pThrowInfo[pObj->ThrowIndex];
512 if (pThrow->TimeInAir == 2 || pThrow->TimeInAir == 0)
514 *pPos = pThrow->FromPos;
515 OffsetHandlePosition(pPos, HandleLen, pPos);
519 float tc = pThrow->TimeInAir - CARRY_TIME;
520 float BallTwist = GetBallTwistAmount(pThrow);
521 Time -= pObj->TimeOffset;
530 OffsetHandlePosition(&pThrow->FromPos, HandleLen, &From);
531 OffsetHandlePosition(&pThrow->ToPos, HandleLen, &To);
533 b = (To.y - From.y) / tc + pPattern->Alpha * tc;
535 pPos->x = (1.0f - t) * From.x + t * To.x;
536 pPos->z = (1.0f - t) * From.z + t * To.z;
537 pPos->y = -pPattern->Alpha * Time * Time + b * Time + From.y;
539 if (pObj->ObjectType == OBJECT_BALL)
540 pPos->Rot = pObj->TotalTwist + t * BallTwist;
543 /* We describe the rotation of a club (or ring) with an
544 * elevation and rotation but don't include a twist.
545 * If we ignore twist for the moment, the orientation at a
546 * rotation of r and an elevation of e can be also be expressed
547 * by rotating the object a further 180 degrees and sort of
548 * mirroring the rotation, e.g.:
549 * rot = r + 180 and elev = 180 - e
550 * We can easily show that the maths holds, consider the
551 * x, y ,z position of the end of a unit length club.
552 * y = sin(180 - e) = sin(e)
553 * x = cos(180 - e) * sin(r + 180) = -cos(e) * - sin(r)
554 * z = cos(180 - e) * cos(r + 180) = -cos(e) * - cos(r)
555 * When a club is thrown these two potential interpretations
556 * can produce unexpected results.
557 * The approach we adopt is that we try and minimise the amount
558 * of rotation we give a club -- normally this is what happens
559 * when juggling since it's much easier to spin the club.
561 * When we come to drawing the object the two interpretations
562 * aren't identical, one causes the object to twist a further
563 * 180 about its axis. We avoid the issue by ensuring our
564 * objects have rotational symmetry of order 2 (e.g. we make
565 * sure clubs have an even number of stripes) this makes the two
566 * interpretations appear identical. */
568 float RotAmt = NormaliseAngle(To.Rot - From.Rot);
572 To.Elev += 180 - 2 * NormaliseAngle(To.Elev);
575 else if (RotAmt > 90.0f)
577 To.Elev += 180 - 2 * NormaliseAngle(To.Elev);
581 pPos->Rot = From.Rot + t * RotAmt;
584 pPos->Elev = (1.0f - t) * From.Elev + t * To.Elev;
589 THROW_INFO* pNextThrow = &pPattern->pThrowInfo[
590 (pObj->ThrowIndex + pThrow->TotalTime) % pPattern->ThrowLen];
592 *pPos = InterpolateCarry(pThrow, pNextThrow, Time - tc);
594 if (pObj->ObjectType == OBJECT_BALL)
595 pPos->Rot = pObj->TotalTwist + BallTwist;
597 OffsetHandlePosition(pPos, HandleLen, pPos);
603 /* Alpha is used to represent the acceleration due to gravity (in fact
604 * 2 * Alpha is the acceleration). Alpha is adjusted according to the pattern
605 * being juggled. My preference is to slow down patterns with lots of objects
606 * -- they move too fast in realtime. Also I prefer to see a balance between
607 * the size of the figure and the height of objects thrown -- juggling patterns
608 * with large numbers of objects under real gravity can mean balls are lobbed
609 * severe heights. Adjusting Alpha achieves both these goals.
611 * Basically we pick a height we'd like to see the biggest throw reach and then
612 * adjust Alpha to meet this. */
614 static void SetHeightAndAlpha(PATTERN_INFO* pPattern,
615 const int* Site, const EXT_SITE_INFO* pExtInfo, int Len)
623 for (i = 0; i < Len; i++)
624 MaxW = max(MaxW, Site[i]);
628 for (i = 0; i < Len; i++)
629 MaxW = max(MaxW, pExtInfo[i].Weight);
632 /* H is the ideal max height we'd like our objects to reach. The formula
633 * was developed by trial and error and was simply stolen from Juggle Saver.
634 * Alpha is then calculated from the classic displacement formula:
635 * s = 0.5at^2 + ut (where a = 2 * Alpha)
636 * We know u (the velocity) is zero at the peak, and the object should fall
637 * H units in half the time of biggest throw weight.
638 * Finally we determine the proper height the max throw reaches since this
639 * may not be H because capping may be applied (e.g. for max weights less
642 H = 8.0f * powf(MaxW / 2.0f, 0.8f) + 5.0f;
643 pPattern->Alpha = (2.0f * H) / powf(max(5, MaxW) - CARRY_TIME, 2.0f);
644 pPattern->Height = pPattern->Alpha * powf((MaxW - CARRY_TIME) * 0.5f, 2);
648 /* Where positions and spin info is not specified, generate suitable default
651 static int GetDefaultSpins(int Weight)
664 static void GetDefaultFromPosition(unsigned char Side, int Weight, POS* pPos)
666 if (Weight > 4 && Weight % 2 != 0)
667 pPos->x = Side ? -0.06f : 0.06f;
668 else if (Weight == 0 || Weight == 2)
669 pPos->x = Side ? 1.6f : -1.6f;
671 pPos->x = Side? 0.24f : -0.24f;
673 pPos->y = (Weight == 2 || Weight == 0) ? -0.25f : 0.0f;
675 pPos->Rot = (Weight % 2 == 0 ? -23.5f : 27.0f) * (Side ? -1.0f : 1.0f);
677 pPos->Elev = Weight == 1 ? -30.0f : 0.0f;
682 static void GetDefaultToPosition(unsigned char Side, int Weight, POS* pPos)
685 pPos->x = Side ? -1.0f : 1.0f;
686 else if (Weight % 2 == 0)
687 pPos->x = Side ? 2.8f : -2.8f;
689 pPos->x = Side? -3.1f : 3.1f;
693 pPos->Rot = (Side ? -35.0f : 35.0f) * (Weight % 2 == 0 ? -1.0f : 1.0f);
699 pPos->Elev = 360.0f - 50.0f;
701 pPos->Elev = 720.0f - 50.0f;
703 pPos->Elev = 360.0f * GetDefaultSpins(Weight) - 50.0f;
708 /* Update the members of PATTERN_INFO for a given juggling pattern. The pattern
709 * can come from an ordinary siteswap (Site != NULL) or from a Juggle Saver
710 * compatible pattern that contains, position and object info etc.
711 * We assume that patterns are valid and have at least one object (a site of
712 * zeros is invalid). The ones we generate randomly are safe. */
714 static void InitPatternInfo(PATTERN_INFO* pPattern,
715 const int* Site, const EXT_SITE_INFO* pExtInfo, int Len)
717 /* Double up on the length of the site if it's of an odd length.
718 * This way we can store position information: even indices are on one
719 * side and odds are on the other. */
720 int InfoLen = Len % 2 == 1 ? Len * 2 : Len;
722 THROW_INFO* pInfo = (THROW_INFO*) calloc(InfoLen, sizeof(THROW_INFO));
724 unsigned char* pUsed;
726 pPattern->MaxWeight = 0;
727 pPattern->ThrowLen = InfoLen;
728 pPattern->pThrowInfo = pInfo;
730 SetHeightAndAlpha(pPattern, Site, pExtInfo, Len);
732 /* First pass through we assign the things we know about for sure just by
733 * looking at the throw weight at this position. This includes TimeInAir;
734 * the throw and catch positions; and throw and catch velocities.
735 * Other information, like the total time for the throw (i.e. when the
736 * object is thrown again) relies on how the rest of the pattern is
737 * structured and we defer this task for successive passes and just make
738 * guesses at this stage. */
740 for (i = 0; i < InfoLen; i++)
743 int w = pExtInfo != NULL ? pExtInfo[i % Len].Weight : Site[i % Len];
745 pInfo[i].TotalTime = pInfo[i].TimeInAir = w;
746 pInfo[(w + i) % Len].PrevThrow = i;
748 /* work out where we are throwing this object from and where it's going
751 if (pExtInfo == NULL || (pExtInfo[i % Len].Flags & HAS_FROM_POS) == 0)
752 GetDefaultFromPosition(i % 2, w, &pInfo[i].FromPos);
754 pInfo[i].FromPos = pExtInfo[i % Len].FromPos;
756 if (pExtInfo == NULL || (pExtInfo[i % Len].Flags & HAS_TO_POS) == 0)
757 GetDefaultToPosition(i % 2, w, &pInfo[i].ToPos);
759 pInfo[i].ToPos = pExtInfo[i % Len].ToPos;
761 /* calculate the velocity the object is moving at the start and end
762 * points -- this information is used to interpolate the hand position
763 * and to determine how the object is moved while it's carried to the
764 * next throw position.
766 * The throw motion is governed by a parabola of the form:
767 * y(t) = a * t ^ 2 + b * t + c
768 * Assuming at the start of the throw y(0) = y0; when it's caught
769 * y(t1) = y1; and the accelation is -2.0 * alpha the equation can be
771 * y(t) = -alpha * t ^ 2 + (alpha * t1 + (y1 - y0) / t1) * t + y0
772 * making the velocity:
773 * y'(t) = -2.0 * alpha * t + (alpha * t1 + (y1 - y0) / t1)
774 * To get the y component of velocity first we determine t1, which is
775 * the throw weight minus the time spent carrying the object. Then
776 * perform the relevant substitutions into the above.
777 * (note: y'(t) = y'(0) - 2.0 * alpha * t)
779 * The velocity in the x direction is constant and can be simply
781 * x' = (x1 - x0) / t1
782 * where x0 and x1 are the start and end x-positions respectively.
787 pInfo[i].FromVelocity.y = pPattern->Alpha * t1 +
788 (pInfo[i].ToPos.y - pInfo[i].FromPos.y) / t1;
789 pInfo[i].ToVelocity.y =
790 pInfo[i].FromVelocity.y - 2.0f * pPattern->Alpha * t1;
791 pInfo[i].FromVelocity.x = pInfo[i].ToVelocity.x =
792 (pInfo[i].ToPos.x - pInfo[i].FromPos.x) / t1;
793 pInfo[i].FromVelocity.z = pInfo[i].ToVelocity.z =
794 (pInfo[i].ToPos.z - pInfo[i].FromPos.z) / t1;
795 pInfo[i].FromVelocity.Rot = pInfo[i].ToVelocity.Rot =
796 (pInfo[i].ToPos.Rot - pInfo[i].FromPos.Rot) / t1;
797 pInfo[i].FromVelocity.Elev = pInfo[i].ToVelocity.Elev =
798 (pInfo[i].ToPos.Elev - pInfo[i].FromPos.Elev) / t1;
801 if (pExtInfo != NULL && (pExtInfo[i % Len].Flags & HAS_SNATCH) != 0)
803 pInfo[i].ToVelocity.x = pExtInfo[i % Len].SnatchX;
804 pInfo[i].ToVelocity.y = pExtInfo[i % Len].SnatchY;
807 if (pExtInfo != NULL && (pExtInfo[i % Len].Flags & HAS_SPINS) != 0)
809 pInfo[i].ToPos.Elev = 360.0f * pExtInfo[i % Len].Spins +
810 NormaliseAngle(pInfo[i].ToPos.Elev);
814 if (w > pPattern->MaxWeight)
815 pPattern->MaxWeight = w;
820 /* Now we go through again and work out exactly how long it is before the
821 * object is thrown again (ie. the TotalTime) typically this is the same
822 * as the time in air, however when we have a throw weight of '2' it's
823 * treated as a hold and we increase the total time accordingly. */
825 for (i = 0; i < InfoLen; i++)
827 if (pInfo[i].TimeInAir != 2)
829 int Next = pInfo[i].TimeInAir + i;
830 while (pInfo[Next % InfoLen].TimeInAir == 2)
833 pInfo[i].TotalTime += 2;
836 /* patch up the Prev index. We don't bother to see if this
837 * is different from before since it's always safe to reassign it */
838 pInfo[Next % InfoLen].PrevThrow = i;
842 /* then we work our way through again figuring out where the hand goes to
843 * catch something as soon as it has thrown the current object. */
845 for (i = 0; i < InfoLen; i++)
847 if (pInfo[i].TimeInAir != 0 && pInfo[i].TimeInAir != 2)
849 /* what we're trying to calculate is how long the hand that threw
850 * the current object has to wait before it throws another.
851 * Typically this is two beats later. However '0' in the site swap
852 * represents a gap in a catch, and '2' represents a hold. We skip
853 * over these until we reach the point where a ball is actually
856 while (pInfo[(i + Wait) % InfoLen].TimeInAir == 2 ||
857 pInfo[(i + Wait) % InfoLen].TimeInAir == 0)
861 pInfo[i].NextForHand = Wait;
865 /* Be careful to ensure the current weight isn't one we're trying
866 * to step over; otherwise we could potentially end up in an
867 * infinite loop. The value we assign may end up being used
868 * in patterns with infinite gaps (e.g. 60) or infinite holds
869 * (e.g. 62) in both cases, setting a wait of 2 ensures things
870 * are well behaved. */
871 pInfo[i].NextForHand = 2;
875 /* Now work out the starting positions for the objects. To do this we
876 * unweave the initial throws so we can pick out the individual threads. */
878 pUsed = (unsigned char*)
879 malloc(sizeof(unsigned char) * pPattern->MaxWeight);
880 pPattern->Objects = Objects;
881 pPattern->pObjectInfo = (OBJECT_POSITION*) calloc(
882 Objects, sizeof(OBJECT_POSITION));
884 for (i = 0; i < pPattern->MaxWeight; i++)
887 for (i = 0; i < pPattern->MaxWeight; i++)
889 int w = pInfo[i % InfoLen].TimeInAir;
890 if (pUsed[i] == 0 && w != 0)
893 pPattern->pObjectInfo[Objects].TimeOffset = i;
894 pPattern->pObjectInfo[Objects].ThrowIndex = i % InfoLen;
895 pPattern->pObjectInfo[Objects].TotalTwist = 0.0f;
897 if (pExtInfo != NULL &&
898 pExtInfo[i % Len].ObjectType != OBJECT_DEFAULT)
900 pPattern->pObjectInfo[Objects].ObjectType =
901 pExtInfo[i % Len].ObjectType;
905 pPattern->pObjectInfo[Objects].ObjectType = (1 + random() % 3);
909 if (w + i < pPattern->MaxWeight)
914 pPattern->LeftHand.TimeOffset = pPattern->LeftHand.ThrowIndex = 0;
915 pPattern->RightHand.TimeOffset = pPattern->RightHand.ThrowIndex = 1;
921 static void ReleasePatternInfo(PATTERN_INFO* pPattern)
923 free(pPattern->pObjectInfo);
924 free(pPattern->pThrowInfo);
928 /*****************************************************************************
932 ****************************************************************************/
934 /* Generate a random site swap. We assume that MaxWeight >= ObjCount and
935 * Len >= MaxWeight. */
937 static int* Generate(int Len, int MaxWeight, int ObjCount)
939 int* Weight = (int*) calloc(Len, sizeof(int));
940 int* Used = (int*) calloc(Len, sizeof(int));
941 int* Options = (int*) calloc(MaxWeight + 1, sizeof(int));
945 for (i = 0; i < Len; i++)
946 Weight[i] = Used[i] = -1;
948 /* Pick out a unique the starting position for each object. -2 is put in
949 * the Used array to signify this is a starting position. */
954 for (j = 0; j < MaxWeight; j++)
957 Options[nOpts++] = j;
960 Used[Options[random() % nOpts]] = -2;
964 /* Now work our way through the pattern moving throws into an available
965 * landing positions. */
966 for (i = 0; i < Len; i++)
970 /* patch up holes in the pattern to zeros */
976 /* Work out the possible places where a throw can land and pick a
977 * weight at random. */
981 for (j = 0 ; j <= MaxWeight; j++)
983 if (Used[(i + j) % Len] == -1)
984 Options[nOpts++] = j;
987 w = Options[random() % nOpts];
990 /* For starting throws make position available for a throw to land.
991 * Because Len >= MaxWeight these positions will only be filled when
992 * a throw wraps around the end of the site swap and therefore we
993 * can guarantee the all the object threads will be tied up. */
997 Used[(i + w) % Len] = 1;
1007 /* Routines to parse the Juggle Saver patterns. These routines are a bit yucky
1008 * and make the big assumption that the patterns are well formed. This is fine
1009 * as it stands because only 'good' ones are used but if the code is ever
1010 * extended to read arbitrary patterns (say from a file) then these routines
1011 * need to be beefed up. */
1013 /* The position text looks something like (x,y,z[,rot[,elev]])
1014 * where the stuff in square brackets is optional */
1016 static unsigned char ParsePositionText(const char** ppch, POS* pPos)
1018 const char* pch = *ppch;
1027 Nums[2] = &pPos->Rot;
1028 Nums[3] = &pPos->Elev;
1039 for (i = 0; OK && i < 4; i++)
1044 while (*pch != ',' && *pch != '\0' && *pch != ')' && *pch != ' ')
1048 if (szTemp[0] != '\0')
1049 *Nums[i] = (float) atof(szTemp);
1058 else if (*pch == ')')
1081 static EXT_SITE_INFO* ParsePattern(const char* Site, int* pLen)
1083 const char* pch = Site;
1085 EXT_SITE_INFO* pInfo = NULL;
1086 unsigned char OK = 1;
1088 while (OK && *pch != 0)
1093 while (*pch == ' ') pch++;
1098 Info.Weight = *pch >= 'A' ? *pch + 10 - 'A' : *pch - '0';
1100 /* parse object type */
1104 while (*pch == ' ') pch++;
1106 if (*pch == 'b' || *pch == 'B')
1108 Info.ObjectType = OBJECT_BALL;
1111 else if (*pch == 'c' || *pch == 'C')
1113 Info.ObjectType = OBJECT_CLUB;
1116 else if (*pch == 'r' || *pch == 'R')
1118 Info.ObjectType = OBJECT_RING;
1121 else if (*pch == 'd' || *pch == 'D')
1123 Info.ObjectType = OBJECT_DEFAULT;
1128 Info.ObjectType = OBJECT_DEFAULT;
1132 /* Parse from position */
1135 while (*pch == ' ') pch++;
1139 GetDefaultFromPosition(Len % 2, Info.Weight, &Info.FromPos);
1140 Info.Flags |= HAS_FROM_POS;
1141 OK = ParsePositionText(&pch, &Info.FromPos);
1145 /* Parse to position */
1148 while (*pch == ' ') pch++;
1152 GetDefaultToPosition(Len % 2, Info.Weight, &Info.ToPos);
1153 Info.Flags |= HAS_TO_POS;
1154 OK = ParsePositionText(&pch, &Info.ToPos);
1161 while (*pch == ' ') pch++;
1166 Info.Flags |= HAS_SNATCH;
1167 OK = ParsePositionText(&pch, &Snatch);
1168 Info.SnatchX = Snatch.x;
1169 Info.SnatchY = Snatch.y;
1176 while (*pch == ' ') pch++;
1182 while (*pch >= '0' && *pch <= '9')
1185 Info.Spins = Info.Spins * 10 + *pch - '0';
1190 Info.Spins = GetDefaultSpins(Info.Weight);
1192 Info.Flags |= HAS_SPINS;
1198 pInfo = (EXT_SITE_INFO*) malloc(sizeof(EXT_SITE_INFO));
1200 pInfo = (EXT_SITE_INFO*) realloc(pInfo, (Len + 1) * sizeof(EXT_SITE_INFO));
1207 if (!OK && pInfo != NULL)
1219 /*****************************************************************************
1221 * Juggle Saver Patterns
1223 *****************************************************************************
1225 * This is a selection of some of the more interesting patterns from taken
1226 * from the Juggle Saver sites.txt file. I've only used patterns that I
1227 * originally created.
1230 static const char* PatternText[] =
1232 "9b@(-2.5,0,-70,40)>(2.5,0,70)*2 1b@(1,0,10)>(-1,0,-10)",
1234 "3B@(1,-0.4)>(2,4.2)/(-2,1)3B@(-1.8,4.4)>(-2.1,0)",
1236 "7c@(-2,0,-20)>(1.2,0,-5)7c@(2,0,20)>(-1.2,0,5)",
1238 "3b@(-0.5,0)>(1.5,0) 3b@(0.5,0)>(-1.5,0) 3r@(-2.5,3,-90,80)>(2,1,90,30)"
1239 "3b@(0.5,0)>(-1.5,0) 3b@(-0.5,0)>(1.5,0) 3r@(2.5,3,90,80)>(-2,1,-90,30)",
1241 "5c@(2,1.9,10)>(-1,1,10)5c@(2,1.8,10)>(-0.5,1.6,10)/(5,-1)"
1242 "5c@(1.6,0.2,10)>(0,-1,10)/(9,-2)5c@(-2,1.9,-10)>(1,1,-10)"
1243 "5c@(-2,1.8,-10)>(0.5,1.6,-10)/(-5,-1)5@(-1.6,0.2,-10)>(0,-1,-10)/(-9,-2)",
1245 "3c@(-1.5,0,0)>(-1.5,1,0)3c@(1.5,-0.2,0)>(1.5,-0.1,0)3c@(0,-0.5,0)>(0,1,0)"
1246 "3@(-1.5,2,0)>(-1.5,-1,0)3@(1.5,0,0)>(1.5,1,0)3@(0,0,0)>(0,-0.5,0)",
1248 "9c@(-2.5,0,-70,40)>(2.5,0,70)*2 1c@(1,0,10)>(-1,0,-10)*0",
1250 "3c@(2,0.5,60,0)>(1.5,4,60,80)/(-6,-12)"
1251 "3c@(-2,0.5,-60,0)>(-1.5,4,-60,80)/(6,-12)",
1253 "3c@(-0.2,0)>(1,0)3c@(0.2,0)>(-1,0)3c@(-2.5,2,-85,30)>(2.5,2,85,40)*2 "
1254 "3@(0.2,0)>(-1,0) 3@(-0.2,0)>(1,0) 3@(2.5,2,85,30)>(-2.5,2,-85,40)*2",
1256 "3c@(-0.5,-0.5,20,-30)>(2.6,4.3,60,60)/(0,1)*1 "
1257 "3c@(1.6,5.6,60,80)>(-2.6,0,-80)*0",
1259 "5c@(-0.3,0,10)>(1.2,0,10) 5c@(0.3,0,-10)>(-1.2,0,-10)"
1260 "5c@(-0.3,0,10)>(1.2,0,10) 5c@(0.3,0,-10)>(-1.2,0,-10)"
1261 "5c@(-3,3.5,-65,80)>(3,2.5,65) 5c@(0.3,0,-10)>(-1.2,0,-10)"
1262 "5@(-0.3,0,10)>(1.2,0,10) 5@(0.3,0,-10)>(-1.2,0,-10)"
1263 "5@(-0.3,0,10)>(1.2,0,10)5@(3,3.5,65,80)>(-3,2.5,-65)"
1267 /*****************************************************************************
1271 *****************************************************************************/
1273 static const float FOV = 70.0f;
1274 static const float BodyCol[] = {0.6f, 0.6f, 0.45f, 1.0f};
1275 static const float HandleCol[] = {0.45f, 0.45f, 0.45f, 1.0f};
1276 static const float LightPos[] = {0.0f, 200.0f, 400.0f, 1.0f};
1277 static const float LightDiff[] = {1.0f, 1.0f, 1.0f, 0.0f};
1278 static const float LightAmb[] = {0.02f, 0.02f, 0.02f, 0.0f};
1279 static const float ShoulderPos[3] = {0.95f, 2.1f, 1.7f};
1280 static const float DiffCol[] = {1.0f, 0.0f, 0.0f, 1.0f};
1281 static const float SpecCol[] = {1.0f, 1.0f, 1.0f, 1.0f};
1283 static const float BallRad = 0.34f;
1284 static const float UArmLen = 1.9f;
1285 static const float LArmLen = 2.3f;
1291 #define DL_FOREARM 4
1292 #define DL_UPPERARM 5
1294 static const float AltCols[][4] =
1296 {0.0f, 0.7f, 0.0f, 1.0f},
1297 {0.0f, 0.0f, 0.9f, 1.0f},
1298 {0.0f, 0.9f, 0.9f, 1.0f},
1299 {0.45f, 0.0f, 0.9f, 1.0f},
1300 {0.9f, 0.45f, 0.0f, 1.0f},
1301 {0.0f, 0.45f, 0.9f, 1.0f},
1302 {0.9f, 0.0f, 0.9f, 1.0f},
1303 {0.9f, 0.9f, 0.0f, 1.0f},
1304 {0.9f, 0.0f, 0.45f, 1.0f},
1305 {0.45f, 0.15f, 0.6f, 1.0f},
1306 {0.9f, 0.0f, 0.0f, 1.0f},
1307 {0.0f, 0.9f, 0.45f, 1.0f},
1310 static const float Cols[][4] =
1312 {0.9f, 0.0f, 0.0f, 1.0f}, /* 0 */
1313 {0.0f, 0.7f, 0.0f, 1.0f}, /* 1 */
1314 {0.0f, 0.0f, 0.9f, 1.0f}, /* 2 */
1315 {0.0f, 0.9f, 0.9f, 1.0f}, /* 3 */
1316 {0.9f, 0.0f, 0.9f, 1.0f}, /* 4 */
1317 {0.9f, 0.9f, 0.0f, 1.0f}, /* 5 */
1318 {0.9f, 0.45f, 0.0f, 1.0f}, /* 6 */
1319 {0.9f, 0.0f, 0.45f, 1.0f}, /* 7 */
1320 {0.45f, 0.9f, 0.0f, 1.0f}, /* 8 */
1321 {0.0f, 0.9f, 0.45f, 1.0f}, /* 9 */
1322 {0.45f, 0.0f, 0.9f, 1.0f}, /* 10 */
1323 {0.0f, 0.45f, 0.9f, 1.0f}, /* 11 */
1326 static int InitGLDisplayLists(void);
1329 static void InitGLSettings(RENDER_STATE* pState, int WireFrame)
1331 memset(pState, 0, sizeof(RENDER_STATE));
1333 pState->trackball = gltrackball_init ();
1336 glPolygonMode(GL_FRONT, GL_LINE);
1338 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
1340 glLightfv(GL_LIGHT0, GL_POSITION, LightPos);
1341 glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiff);
1342 glLightfv(GL_LIGHT0, GL_AMBIENT, LightAmb);
1344 glEnable(GL_SMOOTH);
1345 glEnable(GL_LIGHTING);
1346 glEnable(GL_LIGHT0);
1348 glDepthFunc(GL_LESS);
1349 glEnable(GL_DEPTH_TEST);
1351 glCullFace(GL_BACK);
1352 glEnable(GL_CULL_FACE);
1354 pState->DLStart = InitGLDisplayLists();
1358 static void SetCamera(RENDER_STATE* pState)
1360 /* Try to work out a sensible place to put the camera so that more or less
1361 * the whole juggling pattern fits into the screen. We assume that the
1362 * pattern is height limited (i.e. if we get the height right then the width
1363 * will be OK). This is a pretty good assumption given that the screen
1364 * tends to wider than high, and that a juggling pattern is normally much
1367 * If I could draw a diagram here then it would be much easier to
1368 * understand but my ASCII-art skills just aren't up to it.
1370 * Basically we estimate a bounding volume for the juggler and objects
1371 * throughout the pattern. We don't fully account for the fact that the
1372 * juggler moves across the stage in an epicyclic-like motion and instead
1373 * use the near and far planes in x-y (with z = +/- w). We also
1374 * assume that the scene is centred at x=0, this reduces our task to finding
1375 * a bounding rectangle. Finally we need to make an estimate of the
1376 * height - for this we work out the max height of a standard throw or max
1377 * weight from the pattern; we then do a bit of adjustment to account for
1378 * a throw occurring at non-zero y values.
1380 * Next we work out the best way to fit this rectangle into the perspective
1381 * transform. Based on the angle of elevation (+ve angle looks down) and
1382 * the FOV we can work out whether it's the near or far corners that are
1383 * the extreme points. And then trace back from them to find the eye
1388 float ElevRad = pState->CameraElev * PI / 180.0f;
1399 const PATTERN_INFO* pPattern = pState->pPattern;
1401 glMatrixMode(GL_PROJECTION);
1404 for (i = 0; i < pPattern->ThrowLen; i++)
1405 H = max(H, pPattern->pThrowInfo[i].FromPos.y);
1407 H += pPattern->Height;
1409 ElevRad = pState->CameraElev * PI / 180.0f;
1411 /* ta is the angle from a point on the top of the bounding area to the eye
1412 * similarly ba is the angle from a point on the bottom. */
1413 ta = (pState->CameraElev - (FOV - 10.0f) / 2.0f) * PI / 180.0f;
1414 ba = (pState->CameraElev + (FOV - 10.0f) / 2.0f) * PI / 180.0f;
1416 /* tz and bz hold the z location of the top and bottom extreme points.
1417 * For the top, if the angle to the eye location is positive then the
1418 * extreme point is with far z corner (the camera looks in -ve z).
1419 * The logic is reserved for the bottom. */
1420 tz = ta >= 0.0f ? -w : w;
1421 bz = ba >= 0.0f ? w : -w;
1426 /* Solve of the eye location by using a bit of geometry.
1427 * We know the eye lies on intersection of two lines. One comes from the
1428 * top and other from the bottom. Giving two equations:
1429 * ez = tz + a * cos(ta) = bz + b * cos(ba)
1430 * ey = ty + a * sin(ta) = by + b * sin(ba)
1431 * We don't bother to solve for b and use Crammer's rule to get
1432 * | bz-tz -cos(ba) |
1433 * | by-ty -sin(ba) |
1434 * a = ----------------------
1435 * | cos(ta) -cos(ba) |
1436 * | sin(ta) -sin(ba) |
1438 d = cosf(ba) * sinf(ta) - cosf(ta) * sinf(ba);
1439 a = (cosf(ba) * (by - ty) - sinf(ba) * (bz - tz)) / d;
1441 ey = ty + a * sinf(ta);
1442 ez = tz + a * cosf(ta);
1444 /* now work back from the eye point to get the lookat location */
1446 cy = ey - ez * tanf(ElevRad);
1448 /* use the distance from the eye to the scene centre to get a measure
1449 * of what the far clipping should be. We then add on a bit more to be
1451 d = sqrtf(ez * ez + (cy - ey) * (cy - ey));
1453 gluPerspective(FOV, pState->AspectRatio, 0.1f, d + 20.0f);
1454 gluLookAt(0.0, ey, ez, 0.0, cy, cz, 0.0, 1.0, 0.0);
1456 glMatrixMode(GL_MODELVIEW);
1460 static void ResizeGL(RENDER_STATE* pState, int w, int h)
1462 glViewport(0, 0, w, h);
1463 pState->AspectRatio = (float) w / h;
1468 /* Determine the angle at the vertex of a triangle given the length of the
1471 static double CosineRule(double a, double b, double c)
1473 double cosang = (a * a + b * b - c * c) / (2 * a * b);
1474 /* If lengths don't form a proper triangle return something sensible.
1475 * This typically happens with patterns where the juggler reaches too
1476 * far to get hold of an object. */
1477 if (cosang < -1.0 || cosang > 1.0)
1480 return 180.0 * acos(cosang) / PI;
1484 /* Spheres for the balls are generated by subdividing each triangle face into
1485 * four smaller triangles. We start with an octahedron (8 sides) and repeat the
1486 * process a number of times. The result is a mesh that can be split into four
1487 * panels (like beanbags) and is smoother than the normal stacks and slices
1490 static void InterpolateVertex(
1491 const float* v1, const float* v2, float t, float* result)
1493 result[0] = v1[0] * (1.0f - t) + v2[0] * t;
1494 result[1] = v1[1] * (1.0f - t) + v2[1] * t;
1495 result[2] = v1[2] * (1.0f - t) + v2[2] * t;
1499 static void SetGLVertex(const float* v, float rad)
1501 float Len = sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
1503 if (Len >= 1.0e-10f)
1505 glNormal3f(v[0] / Len, v[1] / Len, v[2] / Len);
1506 glVertex3f(rad * v[0] / Len, rad * v[1] / Len, rad * v[2] / Len);
1513 static void SphereSegment(
1514 const float* v1, const float* v2, const float* v3, float r, int Levels)
1518 for (i = 0; i < Levels; i++)
1520 float A[3], B[3], C[3], D[3];
1522 InterpolateVertex(v3, v1, (float) i / Levels, D);
1523 InterpolateVertex(v3, v1, (float)(i + 1) / Levels, A);
1524 InterpolateVertex(v3, v2, (float)(i + 1) / Levels, B);
1525 InterpolateVertex(v3, v2, (float) i / Levels, C);
1527 glBegin(GL_TRIANGLE_STRIP);
1532 for (j = 1; j <= i; j++)
1536 InterpolateVertex(B, A, (float) j / (i + 1), v);
1539 InterpolateVertex(C, D, (float) j / i, v);
1550 /* OK, this function is a bit of misnomer, it only draws half a sphere. Indeed
1551 * it draws two panels and allows us to colour this one way, then draw the
1552 * same shape again rotated 90 degrees in a different colour. Resulting in what
1553 * looks like a four-panel beanbag in two complementary colours. */
1555 static void DrawSphere(float rad)
1558 float v1[3], v2[3], v3[3];
1560 v1[0] = 1.0f, v1[1] = 0.0f; v1[2] = 0.0f;
1561 v2[0] = 0.0f, v2[1] = 1.0f; v2[2] = 0.0f;
1562 v3[0] = 0.0f, v3[1] = 0.0f; v3[2] = 1.0f;
1563 SphereSegment(v1, v2, v3, rad, Levels);
1566 SphereSegment(v2, v1, v3, rad, Levels);
1568 v1[0] = v3[2] = -1.0f;
1569 SphereSegment(v2, v1, v3, rad, Levels);
1572 SphereSegment(v1, v2, v3, rad, Levels);
1576 static void DrawRing(void)
1578 const int Facets = 22;
1579 const float w = 0.1f;
1580 GLUquadric* pQuad = gluNewQuadric();
1581 glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
1582 glTranslatef(0.0f, 0.0f, -w / 2.0f);
1584 gluCylinder(pQuad, 1.0f, 1.0f, w, Facets, 1);
1585 gluQuadricOrientation(pQuad, GLU_INSIDE);
1587 gluCylinder(pQuad, 0.7f, 0.7f, w, Facets, 1);
1588 gluQuadricOrientation(pQuad, GLU_OUTSIDE);
1590 glTranslatef(0.0f, 0.0f, w);
1591 gluDisk(pQuad, 0.7, 1.0f, Facets, 1);
1593 glRotatef(180.0f, 0.0f, 1.0f, 0.0f);
1594 glTranslatef(0.0f, 0.0f, w);
1595 gluDisk(pQuad, 0.7, 1.0f, Facets, 1);
1597 gluDeleteQuadric(pQuad);
1601 /* The club follows a 'circus club' design i.e. it has stripes running down the
1602 * body. The club is draw such that the one stripe uses the current material
1603 * and the second stripe the standard silver colour. */
1605 static void DrawClub(void)
1607 const float r[4] = {0.06f, 0.1f, 0.34f, 0.34f / 2.0f};
1608 const float z[4] = {-0.4f, 0.6f, 1.35f, 2.1f};
1614 na[0] = (float) atan((r[1] - r[0]) / (z[1] - z[0]));
1615 na[1] = (float) atan((r[2] - r[1]) / (z[2] - z[1]));
1616 na[2] = (float) atan((r[3] - r[1]) / (z[3] - z[1]));
1617 na[3] = (float) atan((r[3] - r[2]) / (z[3] - z[2]));
1619 for (i = 0; i < n; i += 2)
1621 float a1 = i * PI * 2.0f / n;
1622 float a2 = (i + 1) * PI * 2.0f / n;
1624 glBegin(GL_TRIANGLE_STRIP);
1625 for (j = 1; j < 4; j++)
1627 glNormal3f(cosf(na[j]) * cosf(a1),
1628 cosf(na[j]) * sinf(a1), sinf(na[j]));
1630 glVertex3f(r[j] * cosf(a1), r[j] * sinf(a1), z[j]);
1632 glNormal3f(cosf(na[j]) * cosf(a2),
1633 cosf(na[j]) * sinf(a2), sinf(na[j]));
1635 glVertex3f(r[j] * cosf(a2), r[j] * sinf(a2), z[j]);
1640 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, HandleCol);
1642 for (i = 1; i < n; i += 2)
1644 float a1 = i * PI * 2.0f / n;
1645 float a2 = (i + 1) * PI * 2.0f / n;
1647 glBegin(GL_TRIANGLE_STRIP);
1648 for (j = 1; j < 4; j++)
1650 glNormal3f(cosf(na[j]) * cosf(a1),
1651 cosf(na[j]) * sinf(a1), sinf(na[j]));
1653 glVertex3f(r[j] * cosf(a1), r[j] * sinf(a1), z[j]);
1655 glNormal3f(cosf(na[j]) * cosf(a2),
1656 cosf(na[j]) * sinf(a2), sinf(na[j]));
1658 glVertex3f(r[j] * cosf(a2), r[j] * sinf(a2), z[j]);
1663 pQuad = gluNewQuadric();
1664 glTranslatef(0.0f, 0.0f, z[0]);
1665 gluCylinder(pQuad, r[0], r[1], z[1] - z[0], n, 1);
1667 glTranslatef(0.0f, 0.0f, z[3] - z[0]);
1668 gluDisk(pQuad, 0.0, r[3], n, 1);
1669 glRotatef(180.0f, 0.0f, 1.0f, 0.0f);
1670 glTranslatef(0.0f, 0.0f, z[3] - z[0]);
1671 gluDisk(pQuad, 0.0, r[0], n, 1);
1672 gluDeleteQuadric(pQuad);
1676 /* In total 6 display lists are used. There are created based on the DL_
1677 * constants defined earlier. The function returns the index of the first
1678 * display list, all others can be calculated based on an offset from there. */
1680 static int InitGLDisplayLists(void)
1682 int s = glGenLists(6);
1685 glNewList(s + DL_BALL, GL_COMPILE);
1686 DrawSphere(BallRad);
1689 glNewList(s + DL_CLUB, GL_COMPILE);
1693 glNewList(s + DL_RING, GL_COMPILE);
1697 pQuad = gluNewQuadric();
1698 gluQuadricNormals(pQuad, GLU_SMOOTH);
1700 glNewList(s + DL_TORSO, GL_COMPILE);
1702 glTranslatef(ShoulderPos[0], ShoulderPos[1], -ShoulderPos[2]);
1703 glRotatef(-90.0f, 0.0f, 1.0f, 0.0f);
1704 gluCylinder(pQuad, 0.3, 0.3, ShoulderPos[0] * 2, 18, 1);
1708 glTranslatef(0.0f, -1.0f, -ShoulderPos[2]);
1709 glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
1710 gluCylinder(pQuad, 0.3, 0.3, ShoulderPos[1] + 1.0f, 18, 1);
1711 glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
1712 gluDisk(pQuad, 0.0, 0.3, 18, 1);
1717 glTranslatef(0.0f, ShoulderPos[1] + 1.0f, -ShoulderPos[2]);
1718 glRotatef(-30.0f, 1.0f, 0.0f, 0.0f);
1719 gluCylinder(pQuad, 0.5, 0.5, 0.3, 15, 1);
1722 glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
1723 glRotatef(180.0f, 0.0f, 0.0f, 1.0f);
1724 gluDisk(pQuad, 0.0, 0.5, 15, 1);
1727 glTranslatef(0.0f, 0.0f, .3f);
1728 gluDisk(pQuad, 0.0, 0.5, 15, 1);
1732 glNewList(s + DL_UPPERARM, GL_COMPILE);
1733 gluQuadricNormals(pQuad, GLU_SMOOTH);
1734 gluQuadricDrawStyle(pQuad, GLU_FILL);
1735 gluSphere(pQuad, 0.3, 12, 8);
1737 gluCylinder(pQuad, 0.3, 0.3, UArmLen, 12, 1);
1738 glTranslatef(0.0f, 0.0f, UArmLen);
1739 gluSphere(pQuad, 0.3, 12, 8);
1742 glNewList(s + DL_FOREARM, GL_COMPILE);
1743 gluCylinder(pQuad, 0.3, 0.3 / 2.0f, LArmLen, 12, 1);
1744 glTranslatef(0.0f, 0.0f, LArmLen);
1745 gluDisk(pQuad, 0, 0.3 / 2.0f, 18, 1);
1748 gluDeleteQuadric(pQuad);
1753 /* Drawing the arm requires connecting the upper and fore arm between the
1754 * shoulder and hand position. Thinking about things kinematically by treating
1755 * the shoulder and elbow as ball joints then, provided the arm can stretch far
1756 * enough, there's a infnite number of ways to position the elbow. Basically
1757 * it's possible to fix and hand and shoulder and then rotate the elbow a full
1758 * 360 degrees. Clearly human anatomy isn't like this and picking a natural
1759 * elbow position can be complex. We chicken out and assume that poking the
1760 * elbow out by 20 degrees from the lowest position gives a reasonably looking
1763 static void DrawArm(RENDER_STATE* pState, float TimePos, int Left)
1766 float x, y, len, len2, ang, ang2;
1768 GetHandPosition(pState->pPattern, Left, TimePos, &Pos);
1770 x = Pos.x + (Left ? -ShoulderPos[0] : ShoulderPos[0]);
1771 y = Pos.y - ShoulderPos[1];
1774 len = sqrtf(x * x + y * y + ShoulderPos[2] * ShoulderPos[2]);
1775 len2 = sqrtf(x * x + ShoulderPos[2] * ShoulderPos[2]);
1777 ang = (float) CosineRule(UArmLen, len, LArmLen);
1778 ang2 = (float) CosineRule(UArmLen, LArmLen, len);
1780 if (ang == 0.0 && ang2 == 0)
1785 glTranslatef(Left ? ShoulderPos[0] : -ShoulderPos[0], ShoulderPos[1],
1787 glRotatef((float)(180.0f * asin(x / len2) / 3.14f), 0.0f, 1.0f, 0.0f);
1788 glRotatef((float)(-180.f * asin(y / len) / 3.14), 1.0f, 0.0f, 0.0f);
1789 glRotatef(Left ? 20.0f : -20.0f, 0.0f, 0.0f, 1.0f);
1790 glRotatef((float) ang, 1.0f, 0.0f, 0.0f);
1791 glCallList(DL_UPPERARM + pState->DLStart);
1793 glRotatef((float)(ang2 - 180.0), 1.0f, 0.0f, 0.f);
1794 glCallList(DL_FOREARM + pState->DLStart);
1799 static void DrawGLScene(RENDER_STATE* pState)
1801 float Time = pState->Time;
1802 int nCols = sizeof(Cols) / sizeof(Cols[0]);
1805 PATTERN_INFO* pPattern = pState->pPattern;
1807 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
1809 glMatrixMode(GL_MODELVIEW);
1811 glTranslatef(5.0f * sinf(pState->TranslateAngle), 0.0f, 0.0f);
1813 gltrackball_rotate (pState->trackball);
1815 glRotatef(pState->SpinAngle, 0.0f, 1.0f, 0.0f);
1816 glTranslatef(0.0, 0.0, -1.0f);
1818 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, DiffCol);
1819 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, SpecCol);
1820 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 60.0f);
1822 for (i = 0; i < pPattern->Objects; i++)
1826 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Cols[i % nCols]);
1829 switch (pPattern->pObjectInfo[i].ObjectType)
1832 GetObjectPosition(pPattern, i, Time, 1.0f, &ObjPos);
1833 glTranslatef(ObjPos.x, ObjPos.y, ObjPos.z);
1834 glRotatef(ObjPos.Rot, 0.0f, 1.0f, 0.0f);
1835 glRotatef(ObjPos.Elev, -1.0f, 0.0f, 0.0f);
1836 glTranslatef(0.0f, 0.0f, -1.0f);
1837 glCallList(DL_CLUB + pState->DLStart);
1841 GetObjectPosition(pPattern, i, Time, 1.0f, &ObjPos);
1842 glTranslatef(ObjPos.x, ObjPos.y, ObjPos.z);
1843 glRotatef(ObjPos.Rot, 0.0f, 1.0f, 0.0f);
1844 glRotatef(ObjPos.Elev, -1.0f, 0.0f, 0.0f);
1845 glCallList(DL_RING + pState->DLStart);
1849 GetObjectPosition(pPattern, i, Time, 0.0f, &ObjPos);
1850 glTranslatef(ObjPos.x, ObjPos.y, ObjPos.z);
1851 glRotatef(ObjPos.Rot, 0.6963f, 0.6963f, 0.1742f);
1852 glCallList(DL_BALL + pState->DLStart);
1853 glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
1854 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE,
1855 AltCols[i % nCols]);
1856 glCallList(DL_BALL + pState->DLStart);
1863 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, BodyCol);
1864 glCallList(DL_TORSO + pState->DLStart);
1865 DrawArm(pState, Time, 1);
1866 DrawArm(pState, Time, 0);
1870 static int RandInRange(int Min, int Max)
1872 return Min + random() % (1 + Max - Min);
1876 static void UpdatePattern(
1877 RENDER_STATE* pState, int MinBalls, int MaxBalls,
1878 int MinHeightInc, int MaxHeightInc)
1880 if (pState->pPattern != NULL)
1881 ReleasePatternInfo(pState->pPattern);
1883 pState->pPattern = (PATTERN_INFO*) malloc(sizeof(PATTERN_INFO));
1885 if ((random() % 3) == 1)
1888 int n = random() % (sizeof(PatternText) / sizeof(PatternText[0]));
1889 EXT_SITE_INFO* pExtInfo = ParsePattern(PatternText[n], &ExtSiteLen);
1890 InitPatternInfo(pState->pPattern, NULL, pExtInfo, ExtSiteLen);
1896 int ballcount, maxweight;
1897 const int RandPatternLen = 1500;
1899 ballcount = RandInRange(MinBalls, MaxBalls);
1900 maxweight = ballcount + RandInRange(MinHeightInc, MaxHeightInc);
1902 pRand = Generate(RandPatternLen, maxweight, ballcount);
1903 InitPatternInfo(pState->pPattern, pRand, NULL, RandPatternLen);
1907 pState->CameraElev = 50.0f - random() % 90;
1908 pState->TranslateAngle = random() % 360;
1909 pState->SpinAngle = random() % 360;
1910 pState->Time = 50.0f;
1915 /*******************************************************************************
1917 * XScreenSaver Configuration
1919 ******************************************************************************/
1923 GLXContext* glxContext;
1924 RENDER_STATE RenderState;
1925 float CurrentFrameRate;
1926 unsigned FramesSinceSync;
1927 unsigned LastSyncTime;
1931 static JUGGLER3D_CONFIG* pConfigInfo = NULL;
1932 static int MaxObjects;
1933 static int MinObjects;
1934 static int MaxHeightInc;
1935 static int MinHeightInc;
1936 static float SpinSpeed;
1937 static float TranslateSpeed;
1938 static float JuggleSpeed;
1940 static XrmOptionDescRec Options[] =
1942 {"-spin", ".spinSpeed", XrmoptionSepArg, 0},
1943 {"-trans", ".translateSpeed", XrmoptionSepArg, 0},
1944 {"-speed", ".juggleSpeed", XrmoptionSepArg, 0},
1945 {"-maxobjs", ".maxObjs", XrmoptionSepArg, 0},
1946 {"-minobjs", ".minObjs", XrmoptionSepArg, 0},
1947 {"-maxhinc", ".maxHInc", XrmoptionSepArg, 0},
1948 {"-minhinc", ".minHInc", XrmoptionSepArg, 0},
1952 static argtype Vars[] =
1954 {&MaxObjects, "maxObjs", "MaxObjs", "8", t_Int},
1955 {&MinObjects, "minObjs", "MinObjs", "3", t_Int},
1956 {&MaxHeightInc, "maxHInc", "MaxHInc", "6", t_Int},
1957 {&MinHeightInc, "minHInc", "MinHInc", "2", t_Int},
1958 {&JuggleSpeed, "juggleSpeed", "JuggleSpeed", "2.2", t_Float},
1959 {&TranslateSpeed, "translateSpeed", "TranslateSpeed", "0.1", t_Float},
1960 {&SpinSpeed, "spinSpeed", "SpinSpeed", "20.0", t_Float},
1964 ENTRYPOINT ModeSpecOpt juggler3d_opts = {countof(Options), Options, countof(Vars), Vars};
1967 ENTRYPOINT void reshape_juggler3d(ModeInfo *mi, int width, int height)
1969 JUGGLER3D_CONFIG* pConfig = &pConfigInfo[MI_SCREEN(mi)];
1970 ResizeGL(&pConfig->RenderState, width, height);
1974 ENTRYPOINT void init_juggler3d(ModeInfo* mi)
1976 JUGGLER3D_CONFIG* pConfig;
1978 if (pConfigInfo == NULL)
1980 /* Apply suitable bounds checks to the input parameters */
1981 MaxObjects = max(3, min(MaxObjects, 36));
1982 MinObjects = max(3, min(MinObjects, MaxObjects));
1984 MaxHeightInc = max(1, min(MaxHeightInc, 32));
1985 MinHeightInc = max(1, min(MinHeightInc, MaxHeightInc));
1987 pConfigInfo = (JUGGLER3D_CONFIG*) calloc(
1988 MI_NUM_SCREENS(mi), sizeof(JUGGLER3D_CONFIG));
1989 if (pConfigInfo == NULL)
1991 fprintf(stderr, "%s: out of memory\n", progname);
1996 pConfig = &pConfigInfo[MI_SCREEN(mi)];
1997 pConfig->glxContext = init_GL(mi);
1998 pConfig->CurrentFrameRate = 0.0f;
1999 pConfig->FramesSinceSync = 0;
2000 pConfig->LastSyncTime = 0;
2001 InitGLSettings(&pConfig->RenderState, MI_IS_WIREFRAME(mi));
2003 UpdatePattern(&pConfig->RenderState, MinObjects, MaxObjects,
2004 MinHeightInc, MaxHeightInc);
2006 reshape_juggler3d(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
2010 ENTRYPOINT void draw_juggler3d(ModeInfo* mi)
2012 JUGGLER3D_CONFIG* pConfig = &pConfigInfo[MI_SCREEN(mi)];
2013 Display* pDisplay = MI_DISPLAY(mi);
2014 Window hwnd = MI_WINDOW(mi);
2016 if (pConfig->glxContext == NULL)
2019 glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(pConfig->glxContext));
2021 /* While drawing, keep track of the rendering speed so we can adjust the
2022 * animation speed so things appear consistent. The basis of the this
2023 * code comes from the frame rate counter (fps.c) but has been modified
2024 * so that it reports the initial frame rate earlier (after 0.02 secs
2025 * instead of 1 sec). */
2027 if (pConfig->FramesSinceSync >= 1 * (int) pConfig->CurrentFrameRate)
2029 struct timeval tvnow;
2032 # ifdef GETTIMEOFDAY_TWO_ARGS
2033 struct timezone tzp;
2034 gettimeofday(&tvnow, &tzp);
2036 gettimeofday(&tvnow);
2039 now = (unsigned) (tvnow.tv_sec * 1000000 + tvnow.tv_usec);
2040 if (pConfig->FramesSinceSync == 0)
2042 pConfig->LastSyncTime = now;
2046 unsigned Delta = now - pConfig->LastSyncTime;
2049 pConfig->LastSyncTime = now;
2050 pConfig->CurrentFrameRate =
2051 (pConfig->FramesSinceSync * 1.0e6f) / Delta;
2052 pConfig->FramesSinceSync = 0;
2057 pConfig->FramesSinceSync++;
2059 if (pConfig->RenderState.Time > 150.0f)
2061 UpdatePattern(&pConfig->RenderState, MinObjects, MaxObjects,
2062 MinHeightInc, MaxHeightInc);
2064 DrawGLScene(&pConfig->RenderState);
2066 if (pConfig->CurrentFrameRate > 1.0e-6f)
2068 pConfig->RenderState.Time += JuggleSpeed / pConfig->CurrentFrameRate;
2069 pConfig->RenderState.SpinAngle += SpinSpeed / pConfig->CurrentFrameRate;
2070 pConfig->RenderState.TranslateAngle +=
2071 TranslateSpeed / pConfig->CurrentFrameRate;
2078 glXSwapBuffers(pDisplay, hwnd);
2082 ENTRYPOINT Bool juggler3d_handle_event(ModeInfo* mi, XEvent* pEvent)
2084 JUGGLER3D_CONFIG* pConfig = &pConfigInfo[MI_SCREEN(mi)];
2085 RENDER_STATE* pState = &pConfig->RenderState;
2087 if (pEvent->xany.type == ButtonPress &&
2088 pEvent->xbutton.button == Button1)
2090 pState->button_down_p = True;
2091 gltrackball_start (pState->trackball,
2092 pEvent->xbutton.x, pEvent->xbutton.y,
2093 MI_WIDTH (mi), MI_HEIGHT (mi));
2096 else if (pEvent->xany.type == ButtonRelease &&
2097 pEvent->xbutton.button == Button1)
2099 pState->button_down_p = False;
2102 else if (pEvent->xany.type == ButtonPress &&
2103 (pEvent->xbutton.button == Button4 ||
2104 pEvent->xbutton.button == Button5 ||
2105 pEvent->xbutton.button == Button6 ||
2106 pEvent->xbutton.button == Button7))
2108 gltrackball_mousewheel (pState->trackball, pEvent->xbutton.button, 2,
2109 !pEvent->xbutton.state);
2112 else if (pEvent->xany.type == MotionNotify &&
2113 pState->button_down_p)
2115 gltrackball_track (pState->trackball,
2116 pEvent->xmotion.x, pEvent->xmotion.y,
2117 MI_WIDTH (mi), MI_HEIGHT (mi));
2120 else if (pEvent->xany.type == KeyPress)
2124 int count = XLookupString(&pEvent->xkey, str, 20, &Key, 0);
2128 UpdatePattern(&pConfig->RenderState, MinObjects, MaxObjects,
2129 MinHeightInc, MaxHeightInc);
2136 XSCREENSAVER_MODULE ("Juggler3D", juggler3d)