49b5f783a28ac315f6c9a7cb6251b2e424f45dd9
[dosdemo] / src / hairball.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "demo.h"
5 #include "3dgfx.h"
6 #include "vmath.h"
7 #include "screen.h"
8 #include "util.h"
9 #include "cfgopt.h"
10 #include "mesh.h"
11
12
13 #ifdef MSDOS
14 #include "dos/gfx.h"    /* for wait_vsync assembly macro */
15 #else
16 void wait_vsync(void);
17 #endif
18
19 #define NSPAWNPOS       64
20 #define SPAWN_RATE      16.0f
21 //#define GRAV          (-6.0f)
22 #define GRAV            0.0f
23 #define HAIR_LENGTH 20
24
25 struct particle {
26         vec3_t pos;
27         vec3_t vel;
28         int r, g, b, a;
29         int life;
30
31         struct particle *next;
32 };
33
34 struct hairball {
35         vec3_t pos;
36         quat_t rot;
37         float xform[16];
38
39         struct particle *plist[NSPAWNPOS];
40 };
41
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);
48
49 static struct particle *palloc(void);
50 void pfree(struct particle *p);
51
52
53 static struct screen scr = {
54         "hairball",
55         init,
56         destroy,
57         start, 0,
58         draw
59 };
60
61 static long start_time;
62
63 static float cam_theta, cam_phi = 15;
64 static float cam_dist = 8;
65
66 static vec3_t *spawnpos, *spawndir;
67 static struct hairball hball;
68 static struct g3d_mesh sphmesh;
69
70 struct screen *hairball_screen(void)
71 {
72         return &scr;
73 }
74
75 static int init(void)
76 {
77         int i;
78
79         gen_sphere_mesh(&sphmesh, 1.0f, 12, 6);
80
81         hball.xform[0] = hball.xform[5] = hball.xform[10] = hball.xform[15] = 1.0f;
82         hball.rot.w = 1.0f;
83
84         if(!(spawnpos = malloc(NSPAWNPOS * sizeof *spawnpos)) ||
85                         !(spawndir = malloc(NSPAWNPOS * sizeof *spawndir))) {
86                 fprintf(stderr, "failed to allocate spawnpos/dir array\n");
87                 return -1;
88         }
89
90         for(i=0; i<NSPAWNPOS; i++) {
91                 spawnpos[i] = spawndir[i] = sphrand(1.0f);
92         }
93
94         return 0;
95 }
96
97 static void destroy(void)
98 {
99 }
100
101 static void start(long trans_time)
102 {
103         g3d_matrix_mode(G3D_PROJECTION);
104         g3d_load_identity();
105         g3d_perspective(50.0, 1.33333, 0.5, 100.0);
106
107         start_time = time_msec;
108 }
109
110 static void update(void)
111 {
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;
117         prev_msec = msec;
118
119         mouse_dx = mouse_x - prev_mx;
120         mouse_dy = mouse_y - prev_my;
121         prev_mx = mouse_x;
122         prev_my = mouse_y;
123
124
125         if(opt.sball) {
126                 memcpy(hball.xform, sball_matrix, 16 * sizeof(float));
127         } else {
128                 if(mouse_bmask & MOUSE_BN_MIDDLE) {
129                         hball.pos.x += mouse_dx * 0.05;
130                         hball.pos.y -= mouse_dy * 0.05;
131                 }
132
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;
137         }
138
139         update_hairball(&hball, dt);
140
141         mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
142 }
143
144 static void draw(void)
145 {
146         unsigned long msec;
147         static unsigned long last_swap;
148
149         update();
150
151         memset(fb_pixels, 0, fb_width * fb_height * 2);
152
153         g3d_matrix_mode(G3D_MODELVIEW);
154         g3d_load_identity();
155         g3d_translate(0, 0, -cam_dist);
156         g3d_rotate(cam_phi, 1, 0, 0);
157         g3d_rotate(cam_theta, 0, 1, 0);
158
159         draw_hairball(&hball);
160
161         msec = get_msec();
162         if(msec - last_swap < 16) {
163                 wait_vsync();
164         }
165         if(!opt.vsync) {
166                 wait_vsync();
167         }
168         swap_buffers(fb_pixels);
169         last_swap = get_msec();
170 }
171
172 static void update_hairball(struct hairball *hb, float dt)
173 {
174         int i, j, spawn_count;
175         struct particle pdummy, *prev, *p;
176         static float spawn_acc;
177
178         /* update particles */
179         for(i=0; i<NSPAWNPOS; i++) {
180                 pdummy.next = hb->plist[i];
181                 prev = &pdummy;
182                 while(prev && prev->next) {
183                         p = prev->next;
184                         if(--p->life <= 0) {
185                                 prev->next = p->next;
186                                 pfree(p);
187                                 prev = prev->next;
188                                 continue;
189                         }
190
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;
195
196                         p->r = p->g = p->b = (p->life << 8) / HAIR_LENGTH - 1;
197
198                         prev = p;
199                 }
200
201                 hb->plist[i] = pdummy.next;
202         }
203
204         /* spawn new particles */
205         spawn_acc += dt;
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];
211
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];
215
216                         p->vel = spawndir[i];
217                         p->life = HAIR_LENGTH;
218
219                         p->next = hb->plist[i];
220                         hb->plist[i] = p;
221                 }
222                 spawn_acc -= 1.0f / SPAWN_RATE;
223         }
224 }
225
226 static void draw_hairball(struct hairball *hb)
227 {
228         int i, col;
229         struct particle *p;
230         vec3_t prevpos;
231         vec3_t start[NSPAWNPOS];
232
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];
237         }
238
239         g3d_begin(G3D_LINES);
240         for(i=0; i<NSPAWNPOS; i++) {
241                 if(start[i].z >= hb->pos.z) continue;
242                 p = hb->plist[i];
243                 prevpos = start[i];
244                 while(p) {
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);
248                         prevpos = p->pos;
249                         p = p->next;
250                 }
251         }
252         g3d_end();
253
254
255         g3d_push_matrix();
256         g3d_mult_matrix(hb->xform);
257         zsort_mesh(&sphmesh);
258         draw_mesh(&sphmesh);
259         g3d_pop_matrix();
260
261         g3d_begin(G3D_LINES);
262         for(i=0; i<NSPAWNPOS; i++) {
263                 if(start[i].z < hb->pos.z) continue;
264                 p = hb->plist[i];
265                 prevpos = start[i];
266                 while(p) {
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);
270                         prevpos = p->pos;
271                         p = p->next;
272                 }
273         }
274         g3d_end();
275
276 }
277
278
279 static struct particle *palloc(void)
280 {
281         return malloc(sizeof(struct particle));
282 }
283
284 void pfree(struct particle *p)
285 {
286         free(p);
287 }