fixed hairball update and drawing
[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
128                 hball.pos.x = hball.xform[12];
129                 hball.pos.y = hball.xform[13];
130                 hball.pos.z = hball.xform[14];
131         } else {
132                 if(mouse_bmask & MOUSE_BN_MIDDLE) {
133                         hball.pos.x += mouse_dx * 0.05;
134                         hball.pos.y -= mouse_dy * 0.05;
135                 }
136
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;
141         }
142
143         update_hairball(&hball, dt);
144
145         mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
146 }
147
148 static void draw(void)
149 {
150         unsigned long msec;
151         static unsigned long last_swap;
152
153         update();
154
155         memset(fb_pixels, 0, fb_width * fb_height * 2);
156
157         g3d_matrix_mode(G3D_MODELVIEW);
158         g3d_load_identity();
159         g3d_translate(0, 0, -cam_dist);
160         g3d_rotate(cam_phi, 1, 0, 0);
161         g3d_rotate(cam_theta, 0, 1, 0);
162
163         draw_hairball(&hball);
164
165         msec = get_msec();
166         if(msec - last_swap < 16) {
167                 wait_vsync();
168         }
169         if(!opt.vsync) {
170                 wait_vsync();
171         }
172         swap_buffers(fb_pixels);
173         last_swap = get_msec();
174 }
175
176 static void update_hairball(struct hairball *hb, float dt)
177 {
178         int i, j, spawn_count;
179         struct particle pdummy, *prev, *p;
180         static float spawn_acc;
181
182         /* update particles */
183         for(i=0; i<NSPAWNPOS; i++) {
184                 pdummy.next = hb->plist[i];
185                 prev = &pdummy;
186                 while(prev && prev->next) {
187                         p = prev->next;
188                         if(--p->life <= 0) {
189                                 prev->next = p->next;
190                                 pfree(p);
191                                 prev = prev->next;
192                                 continue;
193                         }
194
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;
199
200                         p->r = p->g = p->b = (p->life << 8) / HAIR_LENGTH - 1;
201
202                         prev = p;
203                 }
204
205                 hb->plist[i] = pdummy.next;
206         }
207
208         /* spawn new particles */
209         spawn_acc += dt;
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];
216
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];
220
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;
225
226                         p->next = hb->plist[i];
227                         hb->plist[i] = p;
228                 }
229                 spawn_acc -= 1.0f / SPAWN_RATE;
230         }
231 }
232
233 static void draw_hairball(struct hairball *hb)
234 {
235         int i, col;
236         struct particle *p;
237         vec3_t prevpos;
238         vec3_t start[NSPAWNPOS];
239
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];
244         }
245
246         g3d_begin(G3D_LINES);
247         for(i=0; i<NSPAWNPOS; i++) {
248                 if(start[i].z >= hb->pos.z) continue;
249                 p = hb->plist[i];
250                 prevpos = start[i];
251                 while(p) {
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);
255                         prevpos = p->pos;
256                         p = p->next;
257                 }
258         }
259         g3d_end();
260
261
262         g3d_push_matrix();
263         g3d_mult_matrix(hb->xform);
264         zsort_mesh(&sphmesh);
265         draw_mesh(&sphmesh);
266         g3d_pop_matrix();
267
268         g3d_begin(G3D_LINES);
269         for(i=0; i<NSPAWNPOS; i++) {
270                 if(start[i].z < hb->pos.z) continue;
271                 p = hb->plist[i];
272                 prevpos = start[i];
273                 while(p) {
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);
277                         prevpos = p->pos;
278                         p = p->next;
279                 }
280         }
281         g3d_end();
282
283 }
284
285
286 static struct particle *palloc(void)
287 {
288         return malloc(sizeof(struct particle));
289 }
290
291 void pfree(struct particle *p)
292 {
293         free(p);
294 }