f7e1b8648a7f4d092dd59f95a1a2b2dbd877b6cf
[xscreensaver] / hacks / glx / flurry-star.c
1 /*
2
3 Copyright (c) 2002, Calum Robinson
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9 * Redistributions of source code must retain the above copyright notice, this
10   list of conditions and the following disclaimer.
11
12 * Redistributions in binary form must reproduce the above copyright notice,
13   this list of conditions and the following disclaimer in the documentation
14   and/or other materials provided with the distribution.
15
16 * Neither the name of the author nor the names of its contributors may be used
17   to endorse or promote products derived from this software without specific
18   prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 */
32
33 /* Star.c: implementation of the Star class. */
34
35 #include "flurry.h"
36
37 /* Construction/Destruction */
38
39 void InitStar(Star *s)
40 {
41     int i;
42     for (i=0;i<3;i++) {
43         s->position[i] = RandFlt(-10000.0, 10000.0);
44     }
45     s->rotSpeed = RandFlt(0.4, 0.9);
46     s->mystery = RandFlt(0.0, 10.0);
47 }
48
49 #define BIGMYSTERY 1800.0
50 #define MAXANGLES 16384
51
52 void UpdateStar(global_info_t *global, flurry_info_t *flurry, Star *s)
53 {
54     float rotationsPerSecond = (float) (2.0*PI*12.0/MAXANGLES) * s->rotSpeed /* speed control */;
55     double thisPointInRadians;
56     double thisAngle = flurry->fTime*rotationsPerSecond;
57     float cf;
58     double tmpX1,tmpY1,tmpZ1;
59     double tmpX2,tmpY2,tmpZ2;
60     double tmpX3,tmpY3,tmpZ3;
61     double tmpX4,tmpY4,tmpZ4;
62     double rotation;
63     double cr;
64     double sr;
65
66     s->ate = 0;
67     
68     cf = ((float) (cos(7.0*((flurry->fTime)*rotationsPerSecond))+cos(3.0*((flurry->fTime)*rotationsPerSecond))+cos(13.0*((flurry->fTime)*rotationsPerSecond))));
69     cf /= 6.0f;
70     cf += 0.75f; 
71     thisPointInRadians = 2.0 * PI * (double) s->mystery / (double) BIGMYSTERY;
72     
73     s->position[0] = 250.0f * cf * (float) cos(11.0 * (thisPointInRadians + (3.0*thisAngle)));
74     s->position[1] = 250.0f * cf * (float) sin(12.0 * (thisPointInRadians + (4.0*thisAngle)));
75     s->position[2] = 250.0f * (float) cos((23.0 * (thisPointInRadians + (12.0*thisAngle))));
76     
77     rotation = thisAngle*0.501 + 5.01 * (double) s->mystery / (double) BIGMYSTERY;
78     cr = cos(rotation);
79     sr = sin(rotation);
80     tmpX1 = s->position[0] * cr - s->position[1] * sr;
81     tmpY1 = s->position[1] * cr + s->position[0] * sr;
82     tmpZ1 = s->position[2];
83     
84     tmpX2 = tmpX1 * cr - tmpZ1 * sr;
85     tmpY2 = tmpY1;
86     tmpZ2 = tmpZ1 * cr + tmpX1 * sr;
87     
88     tmpX3 = tmpX2;
89     tmpY3 = tmpY2 * cr - tmpZ2 * sr;
90     tmpZ3 = tmpZ2 * cr + tmpY2 * sr + seraphDistance;
91     
92     rotation = thisAngle*2.501 + 85.01 * (double) s->mystery / (double) BIGMYSTERY;
93     cr = cos(rotation);
94     sr = sin(rotation);
95     tmpX4 = tmpX3 * cr - tmpY3 * sr;
96     tmpY4 = tmpY3 * cr + tmpX3 * sr;
97     tmpZ4 = tmpZ3;
98     
99     s->position[0] = (float) tmpX4;
100     s->position[1] = (float) tmpY4;
101     s->position[2] = (float) tmpZ4;
102 }