texture mapping and shading LUTs
[metatoy] / src / game.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4 #include "game.h"
5 #include "colormgr.h"
6 #include "3dgfx.h"
7 #include "mesh.h"
8 #include "metasurf.h"
9 #include "util.h"
10 #include "cgmath/cgmath.h"
11 #include "metaobj.h"
12
13 #define BBOX_XSZ                16
14 #define BBOX_YSZ                15
15 #define BBOX_ZSZ                10
16 #define VOX_RES                 24
17
18 #define BBOX_HXSZ               (BBOX_XSZ / 2.0f)
19 #define BBOX_HYSZ               (BBOX_YSZ / 2.0f)
20 #define BBOX_HZSZ               (BBOX_ZSZ / 2.0f)
21 #define VOX_XRES                (VOX_RES * BBOX_XSZ / BBOX_ZSZ)
22 #define VOX_YRES                (VOX_RES * BBOX_YSZ / BBOX_ZSZ)
23 #define VOX_ZRES                VOX_RES
24 #define VOX_XSTEP               (BBOX_XSZ / (float)VOX_XRES)
25 #define VOX_YSTEP               (BBOX_YSZ / (float)VOX_YRES)
26 #define VOX_ZSTEP               (BBOX_ZSZ / (float)VOX_ZRES)
27
28 #define VBUF_MAX_TRIS   256
29 #define VBUF_SIZE               (VBUF_MAX_TRIS * 3)
30
31 static struct g3d_vertex *vbuf;
32 static struct metasurface *msurf;
33 static struct mobject **mobj;
34
35 #define NUM_OBJ         2
36 static int num_mobj, cur_obj;
37 static int grabbed;
38
39 static int mousebn[3];
40 static int mousex, mousey;
41 static float cam_theta, cam_phi;
42
43 extern unsigned char textures_img[];
44 extern unsigned char textures_cmap[];
45 extern unsigned char textures_slut[];
46
47
48 static void update(float tsec);
49 static void draw_metaballs(void);
50
51
52 int game_init(void)
53 {
54         init_colormgr();
55         load_colormap(0, 256, textures_cmap, textures_slut);
56
57         g3d_init();
58         g3d_framebuffer(FB_WIDTH, FB_HEIGHT, framebuf);
59         g3d_viewport(0, 0, FB_WIDTH, FB_HEIGHT);
60
61         g3d_clear_color(0);
62
63         g3d_matrix_mode(G3D_PROJECTION);
64         g3d_load_identity();
65         g3d_perspective(60.0f, 1.33333, 0.5, 500.0);
66
67         g3d_enable(G3D_CULL_FACE);
68         g3d_enable(G3D_DEPTH_TEST);
69         g3d_enable(G3D_LIGHTING);
70         g3d_enable(G3D_LIGHT0);
71         g3d_light_ambient(0.2);
72
73         g3d_polygon_mode(G3D_GOURAUD);
74
75         if(!(msurf = msurf_create())) {
76                 return -1;
77         }
78         msurf_set_threshold(msurf, 8);
79         msurf_set_inside(msurf, MSURF_GREATER);
80         msurf_set_bounds(msurf, -BBOX_HXSZ, -BBOX_HYSZ, -BBOX_HZSZ, BBOX_HXSZ, BBOX_HYSZ, BBOX_HZSZ);
81         msurf_set_resolution(msurf, VOX_XRES, VOX_YRES, VOX_ZRES);
82         msurf_enable(msurf, MSURF_NORMALIZE);
83
84         vbuf = malloc_nf(VBUF_SIZE * sizeof *vbuf);
85
86         num_mobj = NUM_OBJ;
87         mobj = malloc(num_mobj * sizeof *mobj);
88         mobj[0] = metaobj_sgi();
89         mobj[1] = metaobj_sflake();
90         cur_obj = 1;
91         return 0;
92 }
93
94 void game_shutdown(void)
95 {
96 }
97
98 static void update(float tsec)
99 {
100         int i, j, k;
101         float energy;
102         cgm_vec3 pos;
103         float *vox = msurf_voxels(msurf);
104
105         mobj[cur_obj]->update(mobj[cur_obj], tsec);
106
107         for(i=0; i<VOX_ZRES; i++) {
108                 pos.z = -BBOX_HZSZ + i * VOX_ZSTEP;
109                 for(j=0; j<VOX_YRES; j++) {
110                         pos.y = -BBOX_HYSZ + j * VOX_YSTEP;
111                         for(k=0; k<VOX_XRES; k++) {
112                                 pos.x = -BBOX_HXSZ + k * VOX_XSTEP;
113
114                                 /* initialize with the vertical distance for the pool */
115                                 energy = 5.0 / (pos.y + BBOX_HYSZ);
116                                 /*energy += 5.0 / (pos.x + BBOX_HXSZ);
117                                 energy += 5.0 / (BBOX_HXSZ - pos.x);*/
118
119                                 energy += mobj[cur_obj]->eval(mobj[cur_obj], &pos);
120
121                                 *vox++ = energy;
122                         }
123                 }
124         }
125
126         msurf_polygonize(msurf);
127 }
128
129 void game_draw(void)
130 {
131         unsigned long msec = game_getmsec();
132         float tsec = (float)msec / 1000.0f;
133
134         update(tsec);
135
136         g3d_clear(G3D_COLOR_BUFFER_BIT | G3D_DEPTH_BUFFER_BIT);
137
138         g3d_matrix_mode(G3D_MODELVIEW);
139         g3d_load_identity();
140         g3d_translate(0, 1, -14);
141         g3d_rotate(cam_phi, 1, 0, 0);
142         g3d_rotate(cam_theta, 0, 1, 0);
143
144         g3d_disable(G3D_LIGHTING);
145         g3d_enable(G3D_TEXTURE_2D);
146         g3d_enable(G3D_TEXTURE_GEN);
147         g3d_set_texture(32, 32, textures_img);
148         draw_metaballs();
149         g3d_disable(G3D_TEXTURE_GEN);
150         g3d_enable(G3D_LIGHTING);
151
152         game_swap_buffers();
153 }
154
155 static void draw_metaballs(void)
156 {
157         int i, nverts, vbuf_count;
158         float *varr, *narr;
159         struct g3d_vertex *vbptr;
160         static int nfrm;
161
162         nverts = msurf_vertex_count(msurf);
163         varr = msurf_vertices(msurf);
164         narr = msurf_normals(msurf);
165
166         vbptr = vbuf;
167         for(i=0; i<nverts; i++) {
168                 vbuf_count = vbptr - vbuf;
169                 if(vbuf_count >= VBUF_SIZE) {
170                         g3d_draw(G3D_TRIANGLES, vbuf, vbuf_count);
171                         vbptr = vbuf;
172                 }
173                 vbptr->x = varr[0];
174                 vbptr->y = varr[1];
175                 vbptr->z = varr[2];
176                 vbptr->w = 1.0f;
177                 vbptr->nx = narr[0];
178                 vbptr->ny = narr[1];
179                 vbptr->nz = narr[2];
180                 vbptr->w = 1.0f;
181                 vbptr->l = 255;
182                 vbptr++;
183                 varr += 3;
184                 narr += 3;
185         }
186
187         if(vbptr > vbuf) {
188                 g3d_draw(G3D_TRIANGLES, vbuf, vbptr - vbuf);
189         }
190
191         nfrm++;
192 }
193
194 void game_keyboard(int key, int press)
195 {
196         if(key == 27) game_quit();
197 }
198
199 void game_mouse(int bn, int press, int x, int y)
200 {
201         mousebn[bn] = press;
202         mousex = x;
203         mousey = y;
204
205         if(bn == 0) {
206                 if(press && !grabbed) {
207                         grabbed = 1;
208                 } else if(!press && grabbed) {
209                         grabbed = 0;
210                 }
211         }
212 }
213
214 void game_motion(int x, int y)
215 {
216         int dx = x - mousex;
217         int dy = y - mousey;
218         mousex = x;
219         mousey = y;
220
221         if((dx | dy) == 0) return;
222
223         if(mousebn[0]) {
224                 if(grabbed) {
225                         mobj[cur_obj]->pos.x += dx * 0.1;
226                         mobj[cur_obj]->pos.y -= dy * 0.1;
227                 }
228         }
229         if(mousebn[2]) {
230                 cam_theta += (float)dx * (0.6f * 1.333333333f);
231                 cam_phi += (float)dy * 0.6f;
232                 if(cam_phi < -90) cam_phi = -90;
233                 if(cam_phi > 90) cam_phi = 90;
234         }
235 }