19 static float wind[] = {-0.0, 0.0, 0.01};
32 float vx, vy, vz; /* velocity */
38 struct particle *plist;
43 struct vec2 pos, size;
51 static int init(void);
52 static void destroy(void);
53 static void start(long trans_time);
54 static void draw(void);
56 int init_emitter(struct emitter *em, int num, unsigned char *map, int xsz, int ysz);
57 void update_particles(struct emitter *em, float dt);
58 void draw_particles(struct emitter *em);
60 int init_vfield_load(struct vfield *vf, const char *fname);
61 void vfield_eval(struct vfield *vf, float x, float y, struct vec2 *dir);
64 static struct screen scr = {
72 static struct emitter em;
73 static struct vfield vfield;
74 static struct g3d_vertex *varr;
75 static long start_time;
77 static float cam_theta, cam_phi = 25;
78 static float cam_dist = 3;
80 struct screen *greets_screen(void)
89 unsigned char *pixels;
91 if(!(pixels = img_load_pixels("data/greets1.png", &xsz, &ysz, IMG_FMT_GREY8))) {
92 fprintf(stderr, "failed to load particle spawn map\n");
96 init_emitter(&em, PCOUNT, pixels, xsz, ysz);
97 img_free_pixels(pixels);
99 if(!(varr = malloc(PCOUNT * sizeof *varr))) {
100 perror("failed to allocate particle vertex buffer\n");
104 if(init_vfield_load(&vfield, "data/vfield1") == -1) {
111 static void destroy(void)
115 static void start(long trans_time)
117 g3d_matrix_mode(G3D_PROJECTION);
119 g3d_perspective(50.0, 1.3333333, 0.5, 100.0);
121 start_time = time_msec;
124 static void update(void)
126 static long prev_msec;
127 static int prev_mx, prev_my;
128 static unsigned int prev_bmask;
130 long msec = time_msec - start_time;
131 float dt = (msec - prev_msec) / 1000.0f;
135 if((mouse_bmask ^ prev_bmask) == 0) {
136 int dx = mouse_x - prev_mx;
137 int dy = mouse_y - prev_my;
140 if(mouse_bmask & 1) {
141 cam_theta += dx * 1.0;
144 if(cam_phi < -90) cam_phi = -90;
145 if(cam_phi > 90) cam_phi = 90;
147 if(mouse_bmask & 4) {
148 cam_dist += dy * 0.5;
150 if(cam_dist < 0) cam_dist = 0;
157 prev_bmask = mouse_bmask;
159 update_particles(&em, dt);
162 static void draw(void)
166 memset(fb_pixels, 0, fb_width * fb_height * 2);
168 g3d_matrix_mode(G3D_MODELVIEW);
170 g3d_translate(0, 0, -cam_dist);
172 g3d_mult_matrix(sball_matrix);
174 g3d_rotate(cam_phi, 1, 0, 0);
175 g3d_rotate(cam_theta, 0, 1, 0);
180 swap_buffers(fb_pixels);
185 int init_emitter(struct emitter *em, int num, unsigned char *map, int xsz, int ysz)
188 float aspect = (float)xsz / (float)ysz;
191 if(!(em->plist = malloc(num * sizeof *em->plist))) {
197 for(i=0; i<num; i++) {
201 } while(map[y * xsz + x] < 128);
203 p->x = (float)x / (float)xsz - 0.5;
204 p->y = -(float)y / (float)xsz + 0.5 / aspect;
205 p->z = ((float)i / (float)num * 2.0 - 1.0) * 0.005;
206 p->r = p->g = p->b = 0.9;
207 p->vx = p->vy = p->vz = 0.0f;
214 void update_particles(struct emitter *em, float dt)
218 struct particle *p = em->plist;
219 struct g3d_vertex *v = varr;
221 for(i=0; i<em->pcount; i++) {
222 vfield_eval(&vfield, p->x, p->y, &accel);
223 p->x += p->vx * DRAG * dt;
224 p->y += p->vy * DRAG * dt;
225 p->z += p->vz * DRAG * dt;
226 p->vx += (wind[0] + accel.x * FORCE) * dt;
227 p->vy += (wind[1] + accel.y * FORCE) * dt;
228 p->vz += (wind[2] + p->z * ZBIAS) * dt;
230 if(p->life < 0.0f) p->life = 0.0f;
236 v->r = v->g = v->b = 200;
237 v->a = cround64(p->life * 255.0 / MAX_LIFE);
244 void draw_particles(struct emitter *em)
246 g3d_enable(G3D_BLEND);
247 g3d_draw(G3D_POINTS, varr, PCOUNT);
248 g3d_disable(G3D_BLEND);
252 int init_vfield_load(struct vfield *vf, const char *fname)
257 if(!(fp = fopen(fname, "rb"))) {
258 fprintf(stderr, "failed to open vector field: %s\n", fname);
261 if(fread(&vf->width, sizeof vf->width, 1, fp) < 1 ||
262 fread(&vf->height, sizeof vf->height, 1, fp) < 1) {
263 fprintf(stderr, "init_vfield_load: unexpected end of file while reading header\n");
268 /* assume xsz is pow2 otherwise fuck you */
276 if(!(vf->v = malloc(vf->width * vf->height * sizeof *vf->v))) {
277 fprintf(stderr, "failed to allocate %dx%d vector field\n", vf->width, vf->height);
281 if(fread(vf->v, sizeof *vf->v, vf->width * vf->height, fp) < vf->width * vf->height) {
282 fprintf(stderr, "init_vfield_load: unexpected end of file while reading %dx%d vector field\n",
283 vf->width, vf->height);
289 vf->pos.x = vf->pos.y = 0;
290 vf->size.x = vf->size.y = 1;
294 void vfield_eval(struct vfield *vf, float x, float y, struct vec2 *dir)
298 struct vec2 *p1, *p2;
299 struct vec2 left, right;
301 x = ((x - vf->pos.x) / vf->size.x + 0.5f) * vf->width;
302 y = ((y - vf->pos.y) / vf->size.y + 0.5f) * vf->height;
308 if(x > vf->width - 2) x = vf->width - 2;
309 if(y > vf->height - 2) y = vf->height - 2;
317 p1 = vf->v + (py << vf->xshift) + px;
320 left.x = p1->x + (p2->x - p1->x) * ty;
321 left.y = p1->y + (p2->y - p1->y) * ty;
324 right.x = p1->x + (p2->x - p1->x) * ty;
325 right.y = p1->y + (p2->y - p1->y) * ty;
327 dir->x = left.x + (right.x - left.x) * tx;
328 dir->y = left.y + (right.y - left.y) * ty;
330 dir->x += ((float)rand() / RAND_MAX - 0.5) * 0.7;
331 dir->y += ((float)rand() / RAND_MAX - 0.5) * 0.7;