texture mapping and shading LUTs
[metatoy] / src / game.c
index 61e236b..3d96161 100644 (file)
@@ -7,40 +7,58 @@
 #include "mesh.h"
 #include "metasurf.h"
 #include "util.h"
-
-#define BBOX_SIZE              10.0f
-#define BBOX_HSZ               (BBOX_SIZE / 2.0f)
-#define VOX_RES                        30
-#define VOX_STEP               (BBOX_SIZE / (float)VOX_RES)
+#include "cgmath/cgmath.h"
+#include "metaobj.h"
+
+#define BBOX_XSZ               16
+#define BBOX_YSZ               15
+#define BBOX_ZSZ               10
+#define VOX_RES                        24
+
+#define BBOX_HXSZ              (BBOX_XSZ / 2.0f)
+#define BBOX_HYSZ              (BBOX_YSZ / 2.0f)
+#define BBOX_HZSZ              (BBOX_ZSZ / 2.0f)
+#define VOX_XRES               (VOX_RES * BBOX_XSZ / BBOX_ZSZ)
+#define VOX_YRES               (VOX_RES * BBOX_YSZ / BBOX_ZSZ)
+#define VOX_ZRES               VOX_RES
+#define VOX_XSTEP              (BBOX_XSZ / (float)VOX_XRES)
+#define VOX_YSTEP              (BBOX_YSZ / (float)VOX_YRES)
+#define VOX_ZSTEP              (BBOX_ZSZ / (float)VOX_ZRES)
 
 #define VBUF_MAX_TRIS  256
 #define VBUF_SIZE              (VBUF_MAX_TRIS * 3)
 
-struct mball {
-       float energy;
-       float x, y, z;
-};
-
-static struct g3d_mesh mesh;
 static struct g3d_vertex *vbuf;
 static struct metasurface *msurf;
-static struct mball *balls;
-static int num_balls;
+static struct mobject **mobj;
+
+#define NUM_OBJ                2
+static int num_mobj, cur_obj;
+static int grabbed;
+
+static int mousebn[3];
+static int mousex, mousey;
+static float cam_theta, cam_phi;
+
+extern unsigned char textures_img[];
+extern unsigned char textures_cmap[];
+extern unsigned char textures_slut[];
+
 
 static void update(float tsec);
 static void draw_metaballs(void);
 
+
 int game_init(void)
 {
-       int i;
-
        init_colormgr();
+       load_colormap(0, 256, textures_cmap, textures_slut);
 
        g3d_init();
-       g3d_framebuffer(320, 200, framebuf);
-       g3d_viewport(0, 0, 320, 200);
+       g3d_framebuffer(FB_WIDTH, FB_HEIGHT, framebuf);
+       g3d_viewport(0, 0, FB_WIDTH, FB_HEIGHT);
 
-       g3d_clear_color(0, 0, 0);
+       g3d_clear_color(0);
 
        g3d_matrix_mode(G3D_PROJECTION);
        g3d_load_identity();
@@ -50,28 +68,26 @@ int game_init(void)
        g3d_enable(G3D_DEPTH_TEST);
        g3d_enable(G3D_LIGHTING);
        g3d_enable(G3D_LIGHT0);
+       g3d_light_ambient(0.2);
 
        g3d_polygon_mode(G3D_GOURAUD);
 
-       gen_torus_mesh(&mesh, 2.0, 0.7, 24, 12);
-
        if(!(msurf = msurf_create())) {
                return -1;
        }
        msurf_set_threshold(msurf, 8);
        msurf_set_inside(msurf, MSURF_GREATER);
-       msurf_set_bounds(msurf, -BBOX_HSZ, -BBOX_HSZ, -BBOX_HSZ, BBOX_HSZ, BBOX_HSZ, BBOX_HSZ);
-       msurf_set_resolution(msurf, VOX_RES, VOX_RES, VOX_RES);
+       msurf_set_bounds(msurf, -BBOX_HXSZ, -BBOX_HYSZ, -BBOX_HZSZ, BBOX_HXSZ, BBOX_HYSZ, BBOX_HZSZ);
+       msurf_set_resolution(msurf, VOX_XRES, VOX_YRES, VOX_ZRES);
        msurf_enable(msurf, MSURF_NORMALIZE);
 
        vbuf = malloc_nf(VBUF_SIZE * sizeof *vbuf);
 
-       num_balls = 1;
-       balls = calloc_nf(num_balls, sizeof *balls);
-
-       for(i=0; i<num_balls; i++) {
-               balls[i].energy = 20;
-       }
+       num_mobj = NUM_OBJ;
+       mobj = malloc(num_mobj * sizeof *mobj);
+       mobj[0] = metaobj_sgi();
+       mobj[1] = metaobj_sflake();
+       cur_obj = 1;
        return 0;
 }
 
@@ -81,33 +97,27 @@ void game_shutdown(void)
 
 static void update(float tsec)
 {
-       int i, j, k, n;
-       float x, y, z, dx, dy, dz, dsq, energy;
+       int i, j, k;
+       float energy;
+       cgm_vec3 pos;
        float *vox = msurf_voxels(msurf);
 
-       for(i=0; i<num_balls; i++) {
-               balls[i].y = sin(tsec) * 5.0f;
-       }
+       mobj[cur_obj]->update(mobj[cur_obj], tsec);
 
-       for(i=0; i<VOX_RES; i++) {
-               z = -BBOX_HSZ + i * VOX_STEP;
-               for(j=0; j<VOX_RES; j++) {
-                       y = -BBOX_HSZ + j * VOX_STEP;
-                       for(k=0; k<VOX_RES; k++) {
-                               x = -BBOX_HSZ + k * VOX_STEP;
+       for(i=0; i<VOX_ZRES; i++) {
+               pos.z = -BBOX_HZSZ + i * VOX_ZSTEP;
+               for(j=0; j<VOX_YRES; j++) {
+                       pos.y = -BBOX_HYSZ + j * VOX_YSTEP;
+                       for(k=0; k<VOX_XRES; k++) {
+                               pos.x = -BBOX_HXSZ + k * VOX_XSTEP;
 
                                /* initialize with the vertical distance for the pool */
-                               energy = 5.0 / (y + BBOX_HSZ * 0.98);
+                               energy = 5.0 / (pos.y + BBOX_HYSZ);
+                               /*energy += 5.0 / (pos.x + BBOX_HXSZ);
+                               energy += 5.0 / (BBOX_HXSZ - pos.x);*/
 
-                               /* add the contribution of the balls */
-                               for(n=0; n<num_balls; n++) {
-                                       dx = x - balls[n].x;
-                                       dy = y - balls[n].y;
-                                       dz = z - balls[n].z;
-                                       dsq = dx * dx + dy * dy + dz * dz;
+                               energy += mobj[cur_obj]->eval(mobj[cur_obj], &pos);
 
-                                       energy += balls[n].energy / dsq;
-                               }
                                *vox++ = energy;
                        }
                }
@@ -127,12 +137,17 @@ void game_draw(void)
 
        g3d_matrix_mode(G3D_MODELVIEW);
        g3d_load_identity();
-       g3d_translate(0, 0, -15);
-       /*g3d_rotate(tsec * 50.0f, 1, 0, 0);
-       g3d_rotate(tsec * 30.0f, 0, 0, 1);
-
-       draw_mesh(&mesh);*/
+       g3d_translate(0, 1, -14);
+       g3d_rotate(cam_phi, 1, 0, 0);
+       g3d_rotate(cam_theta, 0, 1, 0);
+
+       g3d_disable(G3D_LIGHTING);
+       g3d_enable(G3D_TEXTURE_2D);
+       g3d_enable(G3D_TEXTURE_GEN);
+       g3d_set_texture(32, 32, textures_img);
        draw_metaballs();
+       g3d_disable(G3D_TEXTURE_GEN);
+       g3d_enable(G3D_LIGHTING);
 
        game_swap_buffers();
 }
@@ -183,8 +198,38 @@ void game_keyboard(int key, int press)
 
 void game_mouse(int bn, int press, int x, int y)
 {
+       mousebn[bn] = press;
+       mousex = x;
+       mousey = y;
+
+       if(bn == 0) {
+               if(press && !grabbed) {
+                       grabbed = 1;
+               } else if(!press && grabbed) {
+                       grabbed = 0;
+               }
+       }
 }
 
 void game_motion(int x, int y)
 {
+       int dx = x - mousex;
+       int dy = y - mousey;
+       mousex = x;
+       mousey = y;
+
+       if((dx | dy) == 0) return;
+
+       if(mousebn[0]) {
+               if(grabbed) {
+                       mobj[cur_obj]->pos.x += dx * 0.1;
+                       mobj[cur_obj]->pos.y -= dy * 0.1;
+               }
+       }
+       if(mousebn[2]) {
+               cam_theta += (float)dx * (0.6f * 1.333333333f);
+               cam_phi += (float)dy * 0.6f;
+               if(cam_phi < -90) cam_phi = -90;
+               if(cam_phi > 90) cam_phi = 90;
+       }
 }