14 #include "dos/gfx.h" /* for wait_vsync assembly macro */
16 void wait_vsync(void);
20 #define SPAWN_RATE 16.0f
21 //#define GRAV (-6.0f)
23 #define HAIR_LENGTH 20
31 struct particle *next;
39 struct particle *plist[NSPAWNPOS];
42 static int init(void);
43 static void destroy(void);
44 static void start(long trans_time);
45 static void draw(void);
46 static void update_hairball(struct hairball *hb, float dt);
47 static void draw_hairball(struct hairball *hb);
49 static struct particle *palloc(void);
50 void pfree(struct particle *p);
53 static struct screen scr = {
61 static long start_time;
63 static float cam_theta, cam_phi = 15;
64 static float cam_dist = 8;
66 static vec3_t *spawnpos, *spawndir;
67 static struct hairball hball;
68 static struct g3d_mesh sphmesh;
70 struct screen *hairball_screen(void)
79 gen_sphere_mesh(&sphmesh, 1.0f, 12, 6);
81 hball.xform[0] = hball.xform[5] = hball.xform[10] = hball.xform[15] = 1.0f;
84 if(!(spawnpos = malloc(NSPAWNPOS * sizeof *spawnpos)) ||
85 !(spawndir = malloc(NSPAWNPOS * sizeof *spawndir))) {
86 fprintf(stderr, "failed to allocate spawnpos/dir array\n");
90 for(i=0; i<NSPAWNPOS; i++) {
91 spawnpos[i] = spawndir[i] = sphrand(1.0f);
97 static void destroy(void)
101 static void start(long trans_time)
103 g3d_matrix_mode(G3D_PROJECTION);
105 g3d_perspective(50.0, 1.33333, 0.5, 100.0);
107 start_time = time_msec;
110 static void update(void)
112 static float prev_mx, prev_my;
113 static long prev_msec;
114 int mouse_dx, mouse_dy;
115 long msec = time_msec - start_time;
116 float dt = (msec - prev_msec) / 1000.0f;
119 mouse_dx = mouse_x - prev_mx;
120 mouse_dy = mouse_y - prev_my;
126 memcpy(hball.xform, sball_matrix, 16 * sizeof(float));
128 hball.pos.x = hball.xform[12];
129 hball.pos.y = hball.xform[13];
130 hball.pos.z = hball.xform[14];
132 if(mouse_bmask & MOUSE_BN_MIDDLE) {
133 hball.pos.x += mouse_dx * 0.05;
134 hball.pos.y -= mouse_dy * 0.05;
137 quat_to_mat(hball.xform, hball.rot);
138 hball.xform[12] = hball.pos.x;
139 hball.xform[13] = hball.pos.y;
140 hball.xform[14] = hball.pos.z;
143 update_hairball(&hball, dt);
145 mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
148 static void draw(void)
151 static unsigned long last_swap;
155 memset(fb_pixels, 0, fb_width * fb_height * 2);
157 g3d_matrix_mode(G3D_MODELVIEW);
159 g3d_translate(0, 0, -cam_dist);
160 g3d_rotate(cam_phi, 1, 0, 0);
161 g3d_rotate(cam_theta, 0, 1, 0);
163 draw_hairball(&hball);
166 if(msec - last_swap < 16) {
172 swap_buffers(fb_pixels);
173 last_swap = get_msec();
176 static void update_hairball(struct hairball *hb, float dt)
178 int i, j, spawn_count;
179 struct particle pdummy, *prev, *p;
180 static float spawn_acc;
182 /* update particles */
183 for(i=0; i<NSPAWNPOS; i++) {
184 pdummy.next = hb->plist[i];
186 while(prev && prev->next) {
189 prev->next = p->next;
195 p->pos.x += p->vel.x * dt;
196 p->pos.y += p->vel.y * dt;
197 p->pos.z += p->vel.z * dt;
198 p->vel.y += GRAV * dt;
200 p->r = p->g = p->b = (p->life << 8) / HAIR_LENGTH - 1;
205 hb->plist[i] = pdummy.next;
208 /* spawn new particles */
210 if(spawn_acc >= (1.0f / SPAWN_RATE)) {
211 for(i=0; i<NSPAWNPOS; i++) {
212 struct particle *p = palloc();
213 float *mat = hb->xform;
214 vec3_t pos = spawnpos[i];
215 vec3_t dir = spawndir[i];
217 p->pos.x = mat[0] * pos.x + mat[4] * pos.y + mat[8] * pos.z + mat[12];
218 p->pos.y = mat[1] * pos.x + mat[5] * pos.y + mat[9] * pos.z + mat[13];
219 p->pos.z = mat[2] * pos.x + mat[6] * pos.y + mat[10] * pos.z + mat[14];
221 p->vel.x = mat[0] * dir.x + mat[4] * dir.y + mat[8] * dir.z;
222 p->vel.y = mat[1] * dir.x + mat[5] * dir.y + mat[9] * dir.z;
223 p->vel.z = mat[2] * dir.x + mat[6] * dir.y + mat[10] * dir.z;
224 p->life = HAIR_LENGTH;
226 p->next = hb->plist[i];
229 spawn_acc -= 1.0f / SPAWN_RATE;
233 static void draw_hairball(struct hairball *hb)
238 vec3_t start[NSPAWNPOS];
240 for(i=0; i<NSPAWNPOS; i++) {
241 start[i].x = hb->xform[0] * spawnpos[i].x + hb->xform[4] * spawnpos[i].y + hb->xform[8] * spawnpos[i].z + hb->xform[12];
242 start[i].y = hb->xform[1] * spawnpos[i].x + hb->xform[5] * spawnpos[i].y + hb->xform[9] * spawnpos[i].z + hb->xform[13];
243 start[i].z = hb->xform[2] * spawnpos[i].x + hb->xform[6] * spawnpos[i].y + hb->xform[10] * spawnpos[i].z + hb->xform[14];
246 g3d_begin(G3D_LINES);
247 for(i=0; i<NSPAWNPOS; i++) {
248 if(start[i].z >= hb->pos.z) continue;
252 g3d_color3b(p->r, p->g, p->b);
253 g3d_vertex(prevpos.x, prevpos.y, prevpos.z);
254 g3d_vertex(p->pos.x, p->pos.y, p->pos.z);
263 g3d_mult_matrix(hb->xform);
264 zsort_mesh(&sphmesh);
268 g3d_begin(G3D_LINES);
269 for(i=0; i<NSPAWNPOS; i++) {
270 if(start[i].z < hb->pos.z) continue;
274 g3d_color3b(p->r, p->g, p->b);
275 g3d_vertex(prevpos.x, prevpos.y, prevpos.z);
276 g3d_vertex(p->pos.x, p->pos.y, p->pos.z);
286 static struct particle *palloc(void)
288 return malloc(sizeof(struct particle));
291 void pfree(struct particle *p)