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 if(mouse_bmask & MOUSE_BN_MIDDLE) {
129 hball.pos.x += mouse_dx * 0.05;
130 hball.pos.y -= mouse_dy * 0.05;
133 quat_to_mat(hball.xform, hball.rot);
134 hball.xform[12] = hball.pos.x;
135 hball.xform[13] = hball.pos.y;
136 hball.xform[14] = hball.pos.z;
139 update_hairball(&hball, dt);
141 mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
144 static void draw(void)
147 static unsigned long last_swap;
151 memset(fb_pixels, 0, fb_width * fb_height * 2);
153 g3d_matrix_mode(G3D_MODELVIEW);
155 g3d_translate(0, 0, -cam_dist);
156 g3d_rotate(cam_phi, 1, 0, 0);
157 g3d_rotate(cam_theta, 0, 1, 0);
159 draw_hairball(&hball);
162 if(msec - last_swap < 16) {
168 swap_buffers(fb_pixels);
169 last_swap = get_msec();
172 static void update_hairball(struct hairball *hb, float dt)
174 int i, j, spawn_count;
175 struct particle pdummy, *prev, *p;
176 static float spawn_acc;
178 /* update particles */
179 for(i=0; i<NSPAWNPOS; i++) {
180 pdummy.next = hb->plist[i];
182 while(prev && prev->next) {
185 prev->next = p->next;
191 p->pos.x += p->vel.x * dt;
192 p->pos.y += p->vel.y * dt;
193 p->pos.z += p->vel.z * dt;
194 p->vel.y += GRAV * dt;
196 p->r = p->g = p->b = (p->life << 8) / HAIR_LENGTH - 1;
201 hb->plist[i] = pdummy.next;
204 /* spawn new particles */
206 if(spawn_acc >= (1.0f / SPAWN_RATE)) {
207 for(i=0; i<NSPAWNPOS; i++) {
208 struct particle *p = palloc();
209 float *mat = hb->xform;
210 vec3_t pos = spawnpos[i];
212 p->pos.x = mat[0] * pos.x + mat[4] * pos.y + mat[8] * pos.z + mat[12];
213 p->pos.y = mat[1] * pos.x + mat[5] * pos.y + mat[9] * pos.z + mat[13];
214 p->pos.z = mat[2] * pos.x + mat[6] * pos.y + mat[10] * pos.z + mat[14];
216 p->vel = spawndir[i];
217 p->life = HAIR_LENGTH;
219 p->next = hb->plist[i];
222 spawn_acc -= 1.0f / SPAWN_RATE;
226 static void draw_hairball(struct hairball *hb)
231 vec3_t start[NSPAWNPOS];
233 for(i=0; i<NSPAWNPOS; i++) {
234 start[i].x = hb->xform[0] * spawnpos[i].x + hb->xform[4] * spawnpos[i].y + hb->xform[8] * spawnpos[i].z + hb->xform[12];
235 start[i].y = hb->xform[1] * spawnpos[i].x + hb->xform[5] * spawnpos[i].y + hb->xform[9] * spawnpos[i].z + hb->xform[13];
236 start[i].z = hb->xform[2] * spawnpos[i].x + hb->xform[6] * spawnpos[i].y + hb->xform[10] * spawnpos[i].z + hb->xform[14];
239 g3d_begin(G3D_LINES);
240 for(i=0; i<NSPAWNPOS; i++) {
241 if(start[i].z >= hb->pos.z) continue;
245 g3d_color3b(p->r, p->g, p->b);
246 g3d_vertex(prevpos.x, prevpos.y, prevpos.z);
247 g3d_vertex(p->pos.x, p->pos.y, p->pos.z);
256 g3d_mult_matrix(hb->xform);
257 zsort_mesh(&sphmesh);
261 g3d_begin(G3D_LINES);
262 for(i=0; i<NSPAWNPOS; i++) {
263 if(start[i].z < hb->pos.z) continue;
267 g3d_color3b(p->r, p->g, p->b);
268 g3d_vertex(prevpos.x, prevpos.y, prevpos.z);
269 g3d_vertex(p->pos.x, p->pos.y, p->pos.z);
279 static struct particle *palloc(void)
281 return malloc(sizeof(struct particle));
284 void pfree(struct particle *p)