~ubuntu-branches/ubuntu/gutsy/rss-glx/gutsy

« back to all changes in this revision

Viewing changes to reallyslick/cpp_src/starBurst.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2005-11-30 18:21:27 UTC
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20051130182127-5iww7elbiyzej1lk
Tags: upstream-0.8.0
ImportĀ upstreamĀ versionĀ 0.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2005  Terence M. Welsh
 
3
 * Ported to Linux by Tugrul Galatali <tugrul@galatali.com>
 
4
 *
 
5
 * This file is part of Hyperspace.
 
6
 *
 
7
 * Hyperspace is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License version 2 as 
 
9
 * published by the Free Software Foundation.
 
10
 *
 
11
 * Hyperspace is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 */
 
20
 
 
21
 
 
22
#include "config.h"
 
23
 
 
24
 
 
25
#include <math.h>
 
26
#ifdef HAVE_GLEW
 
27
#include <GL/glew.h>
 
28
#endif
 
29
#include <GL/gl.h>
 
30
#include <GL/glu.h>
 
31
 
 
32
 
 
33
#include "flare.h"
 
34
#include "starBurst.h"
 
35
#include "wavyNormalCubeMaps.h"
 
36
#include "rsMath/rsMath.h"
 
37
 
 
38
 
 
39
extern int xsize, ysize;
 
40
extern float aspectRatio;
 
41
extern float frameTime;
 
42
extern float camPos[3];
 
43
extern int numAnimTexFrames;
 
44
extern wavyNormalCubeMaps* theWNCM;
 
45
extern unsigned int nebulatex;
 
46
extern unsigned int goo_vp, goo_fp;
 
47
extern int whichTexture;
 
48
extern float depth;
 
49
extern bool dShaders;
 
50
 
 
51
 
 
52
starBurst::starBurst(){
 
53
        int i, j;
 
54
        float vel[3];
 
55
        float normalizer;
 
56
 
 
57
        // initialize stars
 
58
        stars = new stretchedParticle*[SB_NUM_STARS];
 
59
        stars_active = new bool[SB_NUM_STARS];
 
60
        stars_velocity = new float*[SB_NUM_STARS];
 
61
        for(i=0; i<SB_NUM_STARS; i++){
 
62
                stars[i] = new stretchedParticle;
 
63
                stars_active[i] = 0;
 
64
                stars_velocity[i] = new float[3];
 
65
                vel[0] = rsRandf(1.0f) - 0.5f;
 
66
                vel[1] = rsRandf(1.0f) - 0.5f;
 
67
                vel[2] = rsRandf(1.0f) - 0.5f;
 
68
                normalizer = (rsRandf(0.75f) + 0.25f)
 
69
                        / sqrtf(vel[0] * vel[0] + vel[1] * vel[1] + vel[2] * vel[2]);
 
70
                stars_velocity[i][0] = vel[0] * normalizer;
 
71
                stars_velocity[i][1] = vel[1] * normalizer;
 
72
                stars_velocity[i][2] = vel[2] * normalizer;
 
73
        }
 
74
 
 
75
        float xyz[3];
 
76
        float ci, si, cj, sj, cjj, sjj;
 
77
        call_list = glGenLists(1);
 
78
        glNewList(call_list, GL_COMPILE);
 
79
                for(j=0; j<32; j++){
 
80
                        cj = cosf(float(j) * RS_PIx2 / 32.0f);
 
81
                        sj = sinf(float(j) * RS_PIx2 / 32.0f);
 
82
                        cjj = cosf(float(j+1) * RS_PIx2 / 32.0f);
 
83
                        sjj = sinf(float(j+1) * RS_PIx2 / 32.0f);
 
84
                        glBegin(GL_TRIANGLE_STRIP);
 
85
                                for(i=0; i<=32; i++){
 
86
                                        ci = cosf(float(i) * RS_PIx2 / 32.0f);
 
87
                                        si = sinf(float(i) * RS_PIx2 / 32.0f);
 
88
                                        xyz[0] = sj * ci;
 
89
                                        xyz[1] = cj;
 
90
                                        xyz[2] = sj * si;
 
91
                                        glNormal3fv(xyz);
 
92
                                        glVertex3fv(xyz);
 
93
                                        xyz[0] = sjj * ci;
 
94
                                        xyz[1] = cjj;
 
95
                                        xyz[2] = sjj * si;
 
96
                                        glNormal3fv(xyz);
 
97
                                        glVertex3fv(xyz);
 
98
                                }
 
99
                        glEnd();
 
100
                }
 
101
        glEndList();
 
102
 
 
103
        size = 4.0f;
 
104
}
 
105
 
 
106
 
 
107
void starBurst::restart(float* position){
 
108
        int i;
 
109
 
 
110
        for(i=0; i<SB_NUM_STARS; i++){  // don't restart if any star is still active
 
111
                if(stars_active[i])
 
112
                        return;
 
113
        }
 
114
        if(size < 3.0f)  // or if flare hasn't faded out completely
 
115
                return;
 
116
 
 
117
        float color[3];
 
118
        color[0] = rsRandf(1.0f);
 
119
        color[1] = rsRandf(1.0f);
 
120
        color[2] = rsRandf(1.0f);
 
121
        color[rsRandi(3)] = 1.0f;
 
122
        for(i=0; i<SB_NUM_STARS; i++){
 
123
                stars_active[i] = 1;
 
124
                stars[i]->pos[0] = position[0];
 
125
                stars[i]->pos[1] = position[1];
 
126
                stars[i]->pos[2] = position[2];
 
127
                stars[i]->color[0] = color[0];
 
128
                stars[i]->color[1] = color[1];
 
129
                stars[i]->color[2] = color[2];
 
130
        }
 
131
 
 
132
        size = 0.0f;
 
133
        pos[0] = position[0];
 
134
        pos[1] = position[1];
 
135
        pos[2] = position[2];
 
136
}
 
137
 
 
138
 
 
139
void starBurst::drawStars(){
 
140
        int i;
 
141
        float distance;
 
142
 
 
143
        // draw stars
 
144
        glEnable(GL_TEXTURE_2D);
 
145
        glBindTexture(GL_TEXTURE_2D, flaretex[0]);
 
146
        for(i=0; i<SB_NUM_STARS; i++){
 
147
                stars[i]->pos[0] += stars_velocity[i][0] * frameTime;
 
148
                stars[i]->pos[1] += stars_velocity[i][1] * frameTime;
 
149
                stars[i]->pos[2] += stars_velocity[i][2] * frameTime;
 
150
                distance = sqrtf((stars[i]->pos[0] - camPos[0]) * (stars[i]->pos[0] - camPos[0])
 
151
                        + (stars[i]->pos[1] - camPos[1]) * (stars[i]->pos[1] - camPos[1])
 
152
                        + (stars[i]->pos[2] - camPos[2]) * (stars[i]->pos[2] - camPos[2]));
 
153
                if(distance > depth)
 
154
                        stars_active[i] = 0;
 
155
                if(stars_active[i])
 
156
                        stars[i]->draw(camPos);
 
157
        }
 
158
}
 
159
 
 
160
 
 
161
void starBurst::draw(){
 
162
        drawStars();
 
163
        
 
164
        size += frameTime * 0.5f;
 
165
        if(size >= 3.0f)
 
166
                return;
 
167
 
 
168
        // draw flare
 
169
        float brightness = 1.0f - (size * 0.333333f);
 
170
        if(brightness > 0.0f){
 
171
                double p[3];
 
172
                p[0] = pos[0];
 
173
                p[1] = pos[1];
 
174
                p[2] = pos[2];
 
175
                flare(p, 1.0f, 1.0f, 1.0f, brightness);
 
176
        }
 
177
 
 
178
        glMatrixMode(GL_MODELVIEW);
 
179
        glPushMatrix();
 
180
        glTranslatef(pos[0], pos[1], pos[2]);
 
181
        glScalef(size, size, size);
 
182
 
 
183
        // draw sphere
 
184
        glBindTexture(GL_TEXTURE_2D, nebulatex);
 
185
        glEnable(GL_TEXTURE_2D);
 
186
        glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
 
187
        glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
 
188
        glEnable(GL_TEXTURE_GEN_S);
 
189
        glEnable(GL_TEXTURE_GEN_T);
 
190
 
 
191
        glBlendFunc(GL_SRC_ALPHA, GL_ONE);
 
192
        glEnable(GL_BLEND);
 
193
        glColor4f(brightness, brightness, brightness, 1.0f);
 
194
        glCallList(call_list);
 
195
 
 
196
        glDisable(GL_TEXTURE_GEN_S);
 
197
        glDisable(GL_TEXTURE_GEN_T);
 
198
 
 
199
        glPopMatrix();
 
200
}
 
201
 
 
202
 
 
203
#ifdef HAVE_GLEW
 
204
void starBurst::draw(float lerp){
 
205
        drawStars();
 
206
        
 
207
        size += frameTime * 0.5f;
 
208
        if(size >= 3.0f)
 
209
                return;
 
210
 
 
211
        // draw flare
 
212
        float brightness = 1.0f - (size * 0.333333f);
 
213
        if(brightness > 0.0f){
 
214
                double p[3];
 
215
                p[0] = pos[0];
 
216
                p[1] = pos[1];
 
217
                p[2] = pos[2];
 
218
                flare(p, 1.0f, 1.0f, 1.0f, brightness);
 
219
        }
 
220
 
 
221
        glMatrixMode(GL_MODELVIEW);
 
222
        glPushMatrix();
 
223
        glTranslatef(pos[0], pos[1], pos[2]);
 
224
        glScalef(size, size, size);
 
225
 
 
226
        // draw sphere
 
227
        glDisable(GL_TEXTURE_2D);
 
228
        glEnable(GL_TEXTURE_CUBE_MAP_ARB);
 
229
        glActiveTextureARB(GL_TEXTURE2_ARB);
 
230
        glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, nebulatex);
 
231
        glActiveTextureARB(GL_TEXTURE1_ARB);
 
232
        glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, theWNCM->texture[(whichTexture + 1) % numAnimTexFrames]);
 
233
        glActiveTextureARB(GL_TEXTURE0_ARB);
 
234
        glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, theWNCM->texture[whichTexture]);
 
235
        glBindProgramARB(GL_VERTEX_PROGRAM_ARB, goo_vp);
 
236
        glEnable(GL_VERTEX_PROGRAM_ARB);
 
237
        glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, goo_fp);
 
238
        glEnable(GL_FRAGMENT_PROGRAM_ARB);
 
239
 
 
240
        glBlendFunc(GL_SRC_ALPHA, GL_ONE);
 
241
        glEnable(GL_BLEND);
 
242
        glColor4f(brightness, brightness, brightness, lerp);
 
243
        glCallList(call_list);
 
244
 
 
245
        glDisable(GL_TEXTURE_CUBE_MAP_ARB);
 
246
        glDisable(GL_VERTEX_PROGRAM_ARB);
 
247
        glDisable(GL_FRAGMENT_PROGRAM_ARB);
 
248
 
 
249
        glPopMatrix();
 
250
}
 
251
#endif