summaryrefslogtreecommitdiffstats
path: root/debian/fireflies/fireflies-2.08/src/scene.cc
blob: 1f8872dd229ddcf39ada9f021cd552d67cb167c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "scene.h"
#include "modes.h"

#include <GL/glu.h>

Vec3f world;
	
#define WIND_WAIT		rand_real(4*tail_length, 8*tail_length)
#define OFFSCREEN_VEC3()	rand_vec3(-2*world[0], 2*world[0])

// max elapse time is 1/10th of a second so we don't get jaggies even if
// the CPU load is high and everything slows down
#define MAX_ELAPSE 0.1

Scene::Scene() : matrix(-1.0)
{
    set_defaults();
}

Scene::~Scene()
{
    GLuint i;
    for (i = 0; i < baits.size(); i++)
	delete baits[i];
    for (i = 0; i < flies.size(); i++)
	delete flies[i];
}

void Scene::set_defaults()
{
    bmodes.clear();
    smodes.clear();

    // all bmodes equally likely
    for (int i = 0; i < NUM_BMODES; i++)
	bmodes.add(i, 10);

    // except these
    bmodes.change(BMODE_NORMAL, 20);
    bmodes.change(BMODE_GLOW, 15);

    // all smodes equally likely
    for (int i = 0; i < NUM_SMODES; i++)
	smodes.add(i, 10);

    // except...
    smodes.change(SMODE_SWARMS, 5);

    fast_forward = 1;
    minbaits = 2;
    maxbaits = 5;
    minflies = 100;
    maxflies = 175;
    fsize = 1.5;
    bspeed = 50.;
    baccel = 600.;
    fspeed = 100.;
    faccel = 300.;
    hue_rate = 15.;
    tail_length = 2.25;
    tail_width = 2.5;
    tail_opaq = 0.6;
    glow_factor = 2.;
    wind_speed = 3.;
    draw_bait = false;
}

void Scene::create()
{
    GLuint i, nbaits, nflies;
    
    curtime = 0.0;
    wind_when = curtime + WIND_WAIT;
    scene_start_mode(-1); // non-existent, just to initialize

    nbaits = (minbaits + maxbaits)/2;
    nflies = (minflies + maxflies)/2;

    baits.reserve(nbaits);
    for (i = 0; i < nbaits; i++) {
	baits.push_back(new Bait());
    }

    add_flies(nflies);

    for (i = 0; i < 3; i++) {
	switch (rand_int(0, 1)) {
	case 0: 	accel[i] = -1; wind[i] = -wind_speed;	break;
	default: 	accel[i] = 1;  wind[i] = wind_speed;	break;
	}
    }
}

void Scene::add_flies(unsigned n)
{
    if (flies.size() >= maxflies)
	return;
    if (flies.size()+n >= maxflies)
	n = maxflies - flies.size();

    // about 3 groups per bait
    int groupsize = (flies.size()+n)/(3*baits.size());
    if (groupsize < 10) // but at least size 10
	groupsize = 10;
    Vec3f where;
    Bait *b = baits[0];
    flies.reserve(flies.size()+n);
    for (unsigned i = 0; i < n; i++) {
	if ((i % groupsize) == 0) {
	    where = OFFSCREEN_VEC3();
	    b = baits[rand_int(0, baits.size()-1)];
	}
	flies.push_back(new Firefly(b, where, world[2]/3));
    }
}

void Scene::rem_flies(unsigned n)
{
    if (flies.size() <= minflies)
	return;
    if (flies.size()-n <= minflies)
	n = flies.size() - minflies;

    Bait *b = baits[rand_int(0, baits.size()-1)];
    vector<Firefly*>::iterator it = flies.begin();
    while (it != flies.end() && n > 0) {
	if ((*it)->bait == b) {
	    delete (*it);
	    it = flies.erase(it);
	    n--;
	}
	else
	    it++;
    }
}

void Scene::resize(int width, int height)
{
    GLfloat aspect = (GLfloat)width / (GLfloat)height;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(80., aspect, 5, 2000);

    world[2] = 50.;
    if (width > height) {
	world[1] = 80.;
	world[0] = world[1] * (width) / height;
    }
    else {
	world[0] = 80.;
	world[1] = world[0] * (height) / width;
    }
    camera.pos = Vec3f(0., 0., 3*world[2]);

    // For some reason this needs to be done everytime we resize, otherwise
    // blending is disabled (and I assume other functions)
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
}

void Scene::apply_camera()
{
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslated(-camera.pos[0], -camera.pos[1], -camera.pos[2]);
    glRotated(camera.rot_angle, camera.rot_axis[0], camera.rot_axis[1], camera.rot_axis[2]);
}

void Scene::draw()
{
#if 0
    glColor4f(0.5f, 0.5f, 0.5f, 1.0f);
    draw_box(-world, world);
#endif

    for (GLuint i = 0; i < baits.size(); i++)
	baits[i]->draw();

    for (GLuint i = 0; i < flies.size(); i++)
	flies[i]->draw();

    vector<Tail*>::iterator it = dead_tails.begin();
    for (; it != dead_tails.end(); it++)
	(*it)->draw();

}

void Scene::elapse(double t)
{
    for (unsigned i = 0; i < fast_forward; i++)
	elapse_once(t);
}

void Scene::elapse_once(double t)
{
    if (t > MAX_ELAPSE)
	t = MAX_ELAPSE;
    // matrix mode?
    if (matrix > 0) {
	matrix += t;
	if (matrix >= mode_when)
	    scene_start_mode(-SMODE_MATRIX);
	else if (matrix-curtime >= 0.5) {
	    Quat q = axis_to_quat(camera.rot_axis, DEG_TO_RAD(camera.rot_angle));
	    Quat dq = axis_to_quat(matrix_axis, 0.7*t);

	    q = q * dq;
	    unitize(q);

	    camera.rot_axis = q.vector();
	    camera.rot_angle = RAD_TO_DEG(2.0*acos(q.scalar()));
	}
	return;
    }

    curtime += t;
    if (curtime >= mode_when)
	scene_start_mode(mode_next);

    // the wind, she's a changin!
    if (curtime >= wind_when) {
	for (int i = 0; i < 3; i++) {
	    if (rand_int(0, 1) == 0)
		accel[i] = -accel[i];
	}
	// next change based on tail length (so we can see a whole cycle of
	// prettiness blow one way before it gets tossed another)
	wind_when = curtime + WIND_WAIT;
    }

    wind += accel*t;
    clamp_vec(wind, wind_speed);

    // elapse, my children
    for (GLuint i = 0; i < baits.size(); i++)
	baits[i]->elapse(t);

    for (GLuint i = 0; i < flies.size(); i++)
	flies[i]->elapse(t);

    vector<Tail*>::iterator it = dead_tails.begin();
    while (it != dead_tails.end()) {
	if ((*it)->elapse(t)) { // he's dead!
	    delete (*it);
	    it = dead_tails.erase(it);
	}
	else it++;
    }
}