disabled dep-files when building on dos, because it goes into an infinite loop
[dosdemo] / src / hairball.c
index 40ce53f..89c45c3 100644 (file)
@@ -1,21 +1,32 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <float.h>
 #include "demo.h"
 #include "3dgfx.h"
 #include "vmath.h"
 #include "screen.h"
 #include "util.h"
+#include "cfgopt.h"
+#include "mesh.h"
 
-#define NSPAWNPOS      128
+
+#ifdef MSDOS
+#include "dos/gfx.h"   /* for wait_vsync assembly macro */
+#else
+void wait_vsync(void);
+#endif
+
+#define NSPAWNPOS      64
 #define SPAWN_RATE     16.0f
-#define GRAV           (-6.0f)
-#define HAIR_LENGTH 32
+//#define GRAV         (-6.0f)
+#define GRAV           0.0f
+#define HAIR_LENGTH 20
 
 struct particle {
        vec3_t pos;
        vec3_t vel;
-       float r, g, b, a;
+       int r, g, b, a;
        int life;
 
        struct particle *next;
@@ -50,9 +61,12 @@ static struct screen scr = {
 
 static long start_time;
 
-static vec3_t *spawnpos, *spawndir;
+static float cam_theta, cam_phi = 15;
+static float cam_dist = 8;
 
+static vec3_t *spawnpos, *spawndir;
 static struct hairball hball;
+static struct g3d_mesh sphmesh;
 
 struct screen *hairball_screen(void)
 {
@@ -61,7 +75,9 @@ struct screen *hairball_screen(void)
 
 static int init(void)
 {
-       int i;
+       int i, j, numpt = 0;
+
+       gen_sphere_mesh(&sphmesh, 1.0f, 12, 6);
 
        hball.xform[0] = hball.xform[5] = hball.xform[10] = hball.xform[15] = 1.0f;
        hball.rot.w = 1.0f;
@@ -72,8 +88,30 @@ static int init(void)
                return -1;
        }
 
-       for(i=0; i<NSPAWNPOS; i++) {
-               spawnpos[i] = spawndir[i] = sphrand(1.0f);
+       while(numpt < NSPAWNPOS) {
+               float best_dist = -1.0f;
+               for(i=0; i<100; i++) {
+                       vec3_t pos = sphrand(1.0f);
+
+                       float mindist = FLT_MAX;
+                       for(j=0; j<numpt; j++) {
+                               float dx = pos.x - spawnpos[i].x;
+                               float dy = pos.y - spawnpos[i].y;
+                               float dz = pos.z - spawnpos[i].z;
+                               float dsq = dx * dx + dy * dy + dz * dz;
+                               if(dsq < mindist) {
+                                       mindist = dsq;
+                               }
+                       }
+
+                       if(mindist > best_dist) {
+                               spawnpos[numpt] = pos;
+                               best_dist = mindist;
+                       }
+               }
+
+               spawndir[numpt] = spawnpos[numpt];
+               ++numpt;
        }
 
        return 0;
@@ -106,34 +144,56 @@ static void update(void)
        prev_mx = mouse_x;
        prev_my = mouse_y;
 
-       if(mouse_bmask) {
-               hball.pos.x += mouse_dx * 0.05;
-               hball.pos.y -= mouse_dy * 0.05;
-       }
 
-       hball.rot = quat_rotate(hball.rot, dt * 2.0f, 0, 1, 0);
+       if(opt.sball) {
+               memcpy(hball.xform, sball_matrix, 16 * sizeof(float));
 
-       quat_to_mat(hball.xform, hball.rot);
-       hball.xform[12] = hball.pos.x;
-       hball.xform[13] = hball.pos.y;
-       hball.xform[14] = hball.pos.z;
+               hball.pos.x = hball.xform[12];
+               hball.pos.y = hball.xform[13];
+               hball.pos.z = hball.xform[14];
+       } else {
+               if(mouse_bmask & MOUSE_BN_MIDDLE) {
+                       hball.pos.x += mouse_dx * 0.05;
+                       hball.pos.y -= mouse_dy * 0.05;
+               }
+
+               quat_to_mat(hball.xform, hball.rot);
+               hball.xform[12] = hball.pos.x;
+               hball.xform[13] = hball.pos.y;
+               hball.xform[14] = hball.pos.z;
+       }
 
        update_hairball(&hball, dt);
+
+       mouse_orbit_update(&cam_theta, &cam_phi, &cam_dist);
 }
 
 static void draw(void)
 {
+       unsigned long msec;
+       static unsigned long last_swap;
+
        update();
 
        memset(fb_pixels, 0, fb_width * fb_height * 2);
 
        g3d_matrix_mode(G3D_MODELVIEW);
        g3d_load_identity();
-       g3d_translate(0, 0, -8);
+       g3d_translate(0, 0, -cam_dist);
+       g3d_rotate(cam_phi, 1, 0, 0);
+       g3d_rotate(cam_theta, 0, 1, 0);
 
        draw_hairball(&hball);
 
+       msec = get_msec();
+       if(msec - last_swap < 16) {
+               wait_vsync();
+       }
+       if(!opt.vsync) {
+               wait_vsync();
+       }
        swap_buffers(fb_pixels);
+       last_swap = get_msec();
 }
 
 static void update_hairball(struct hairball *hb, float dt)
@@ -159,6 +219,9 @@ static void update_hairball(struct hairball *hb, float dt)
                        p->pos.y += p->vel.y * dt;
                        p->pos.z += p->vel.z * dt;
                        p->vel.y += GRAV * dt;
+
+                       p->r = p->g = p->b = (p->life << 8) / HAIR_LENGTH - 1;
+
                        prev = p;
                }
 
@@ -172,14 +235,16 @@ static void update_hairball(struct hairball *hb, float dt)
                        struct particle *p = palloc();
                        float *mat = hb->xform;
                        vec3_t pos = spawnpos[i];
+                       vec3_t dir = spawndir[i];
 
                        p->pos.x = mat[0] * pos.x + mat[4] * pos.y + mat[8] * pos.z + mat[12];
                        p->pos.y = mat[1] * pos.x + mat[5] * pos.y + mat[9] * pos.z + mat[13];
                        p->pos.z = mat[2] * pos.x + mat[6] * pos.y + mat[10] * pos.z + mat[14];
 
-                       p->vel = spawndir[i];
+                       p->vel.x = mat[0] * dir.x + mat[4] * dir.y + mat[8] * dir.z;
+                       p->vel.y = mat[1] * dir.x + mat[5] * dir.y + mat[9] * dir.z;
+                       p->vel.z = mat[2] * dir.x + mat[6] * dir.y + mat[10] * dir.z;
                        p->life = HAIR_LENGTH;
-                       /* TODO color */
 
                        p->next = hb->plist[i];
                        hb->plist[i] = p;
@@ -190,21 +255,46 @@ static void update_hairball(struct hairball *hb, float dt)
 
 static void draw_hairball(struct hairball *hb)
 {
-       int i, j, col;
+       int i, col;
        struct particle *p;
        vec3_t prevpos;
+       vec3_t start[NSPAWNPOS];
+
+       for(i=0; i<NSPAWNPOS; i++) {
+               start[i].x = hb->xform[0] * spawnpos[i].x + hb->xform[4] * spawnpos[i].y + hb->xform[8] * spawnpos[i].z + hb->xform[12];
+               start[i].y = hb->xform[1] * spawnpos[i].x + hb->xform[5] * spawnpos[i].y + hb->xform[9] * spawnpos[i].z + hb->xform[13];
+               start[i].z = hb->xform[2] * spawnpos[i].x + hb->xform[6] * spawnpos[i].y + hb->xform[10] * spawnpos[i].z + hb->xform[14];
+       }
 
        g3d_begin(G3D_LINES);
        for(i=0; i<NSPAWNPOS; i++) {
+               if(start[i].z >= hb->pos.z) continue;
                p = hb->plist[i];
-               prevpos.x = hb->xform[0] * spawnpos[i].x + hb->xform[4] * spawnpos[i].y + hb->xform[8] * spawnpos[i].z + hb->xform[12];
-               prevpos.y = hb->xform[1] * spawnpos[i].x + hb->xform[5] * spawnpos[i].y + hb->xform[9] * spawnpos[i].z + hb->xform[13];
-               prevpos.z = hb->xform[2] * spawnpos[i].x + hb->xform[6] * spawnpos[i].y + hb->xform[10] * spawnpos[i].z + hb->xform[14];
+               prevpos = start[i];
+               while(p) {
+                       g3d_color3b(p->r, p->g, p->b);
+                       g3d_vertex(prevpos.x, prevpos.y, prevpos.z);
+                       g3d_vertex(p->pos.x, p->pos.y, p->pos.z);
+                       prevpos = p->pos;
+                       p = p->next;
+               }
+       }
+       g3d_end();
+
+
+       g3d_push_matrix();
+       g3d_mult_matrix(hb->xform);
+       zsort_mesh(&sphmesh);
+       draw_mesh(&sphmesh);
+       g3d_pop_matrix();
 
-               j = 0;
+       g3d_begin(G3D_LINES);
+       for(i=0; i<NSPAWNPOS; i++) {
+               if(start[i].z < hb->pos.z) continue;
+               p = hb->plist[i];
+               prevpos = start[i];
                while(p) {
-                       col = 255 - j++ * (HAIR_LENGTH - 1);
-                       g3d_color3b(col, col, col);
+                       g3d_color3b(p->r, p->g, p->b);
                        g3d_vertex(prevpos.x, prevpos.y, prevpos.z);
                        g3d_vertex(p->pos.x, p->pos.y, p->pos.z);
                        prevpos = p->pos;
@@ -212,6 +302,7 @@ static void draw_hairball(struct hairball *hb)
                }
        }
        g3d_end();
+
 }