added lighting
[dosdemo] / src / 3dgfx.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <assert.h>
6 #include "3dgfx.h"
7 #include "polyfill.h"
8 #include "inttypes.h"
9
10 #define STACK_SIZE      8
11 typedef float g3d_matrix[16];
12
13 #define MAX_VBUF_SIZE   256
14 #define MAX_LIGHTS              4
15
16 struct light {
17         float x, y, z;
18         float r, g, b;
19 };
20
21 struct material {
22         float kd[3];
23         float ks[3];
24         float shin;
25 };
26
27 struct g3d_state {
28         unsigned int opt;
29         int frontface;
30         int fill_mode;
31
32         g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
33         int mtop[G3D_NUM_MATRICES];
34         int mmode;
35
36         g3d_matrix norm_mat;
37
38         float ambient[3];
39         struct light lt[MAX_LIGHTS];
40         struct material mtl;
41
42         int width, height;
43         void *pixels;
44 };
45
46 static void xform4_vec3(const float *mat, float *vec);
47 static void xform3_vec3(const float *mat, float *vec);
48 static void shade(struct g3d_vertex *v);
49
50 static struct g3d_state *st;
51 static const float idmat[] = {
52         1, 0, 0, 0,
53         0, 1, 0, 0,
54         0, 0, 1, 0,
55         0, 0, 0, 1
56 };
57
58 int g3d_init(void)
59 {
60         int i;
61
62         if(!(st = calloc(1, sizeof *st))) {
63                 fprintf(stderr, "failed to allocate G3D context\n");
64                 return -1;
65         }
66         st->fill_mode = POLYFILL_FLAT;
67
68         for(i=0; i<G3D_NUM_MATRICES; i++) {
69                 g3d_matrix_mode(i);
70                 g3d_load_identity();
71         }
72
73         for(i=0; i<MAX_LIGHTS; i++) {
74                 g3d_light_color(i, 1, 1, 1);
75         }
76         g3d_light_ambient(0.1, 0.1, 0.1);
77
78         g3d_mtl_diffuse(1, 1, 1);
79         return 0;
80 }
81
82 void g3d_destroy(void)
83 {
84         free(st);
85 }
86
87 void g3d_framebuffer(int width, int height, void *pixels)
88 {
89         st->width = width;
90         st->height = height;
91         st->pixels = pixels;
92
93         pimg_fb.pixels = pixels;
94         pimg_fb.width = width;
95         pimg_fb.height = height;
96 }
97
98 void g3d_enable(unsigned int opt)
99 {
100         st->opt |= opt;
101 }
102
103 void g3d_disable(unsigned int opt)
104 {
105         st->opt &= ~opt;
106 }
107
108 void g3d_setopt(unsigned int opt, unsigned int mask)
109 {
110         st->opt = (st->opt & ~mask) | (opt & mask);
111 }
112
113 unsigned int g3d_getopt(unsigned int mask)
114 {
115         return st->opt & mask;
116 }
117
118 void g3d_front_face(unsigned int order)
119 {
120         st->frontface = order;
121 }
122
123 void g3d_polygon_mode(int pmode)
124 {
125         st->fill_mode = pmode;
126 }
127
128 void g3d_matrix_mode(int mmode)
129 {
130         st->mmode = mmode;
131 }
132
133 void g3d_load_identity(void)
134 {
135         int top = st->mtop[st->mmode];
136         memcpy(st->mat[st->mmode][top], idmat, 16 * sizeof(float));
137 }
138
139 void g3d_load_matrix(const float *m)
140 {
141         int top = st->mtop[st->mmode];
142         memcpy(st->mat[st->mmode][top], m, 16 * sizeof(float));
143 }
144
145 #define M(i,j)  (((i) << 2) + (j))
146 void g3d_mult_matrix(const float *m2)
147 {
148         int i, j, top = st->mtop[st->mmode];
149         float m1[16];
150         float *dest = st->mat[st->mmode][top];
151
152         memcpy(m1, dest, sizeof m1);
153
154         for(i=0; i<4; i++) {
155                 for(j=0; j<4; j++) {
156                         *dest++ = m1[M(0,j)] * m2[M(i,0)] +
157                                 m1[M(1,j)] * m2[M(i,1)] +
158                                 m1[M(2,j)] * m2[M(i,2)] +
159                                 m1[M(3,j)] * m2[M(i,3)];
160                 }
161         }
162 }
163
164 void g3d_push_matrix(void)
165 {
166         int top = st->mtop[st->mmode];
167         if(top >= G3D_NUM_MATRICES) {
168                 fprintf(stderr, "g3d_push_matrix overflow\n");
169                 return;
170         }
171         memcpy(st->mat[st->mmode][top + 1], st->mat[st->mmode][top], 16 * sizeof(float));
172         st->mtop[st->mmode] = top + 1;
173 }
174
175 void g3d_pop_matrix(void)
176 {
177         if(st->mtop[st->mmode] <= 0) {
178                 fprintf(stderr, "g3d_pop_matrix underflow\n");
179                 return;
180         }
181         --st->mtop[st->mmode];
182 }
183
184 void g3d_translate(float x, float y, float z)
185 {
186         float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
187         m[12] = x;
188         m[13] = y;
189         m[14] = z;
190         g3d_mult_matrix(m);
191 }
192
193 void g3d_rotate(float deg, float x, float y, float z)
194 {
195         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
196
197         float angle = M_PI * deg / 180.0f;
198         float sina = sin(angle);
199         float cosa = cos(angle);
200         float one_minus_cosa = 1.0f - cosa;
201         float nxsq = x * x;
202         float nysq = y * y;
203         float nzsq = z * z;
204
205         m[0] = nxsq + (1.0f - nxsq) * cosa;
206         m[4] = x * y * one_minus_cosa - z * sina;
207         m[8] = x * z * one_minus_cosa + y * sina;
208         m[1] = x * y * one_minus_cosa + z * sina;
209         m[5] = nysq + (1.0 - nysq) * cosa;
210         m[9] = y * z * one_minus_cosa - x * sina;
211         m[2] = x * z * one_minus_cosa - y * sina;
212         m[6] = y * z * one_minus_cosa + x * sina;
213         m[10] = nzsq + (1.0 - nzsq) * cosa;
214         m[15] = 1.0f;
215
216         g3d_mult_matrix(m);
217 }
218
219 void g3d_scale(float x, float y, float z)
220 {
221         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
222         m[0] = x;
223         m[5] = y;
224         m[10] = z;
225         m[15] = 1.0f;
226         g3d_mult_matrix(m);
227 }
228
229 void g3d_ortho(float left, float right, float bottom, float top, float znear, float zfar)
230 {
231         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
232
233         float dx = right - left;
234         float dy = top - bottom;
235         float dz = zfar - znear;
236
237         m[0] = 2.0 / dx;
238         m[5] = 2.0 / dy;
239         m[10] = -2.0 / dz;
240         m[12] = -(right + left) / dx;
241         m[13] = -(top + bottom) / dy;
242         m[14] = -(zfar + znear) / dz;
243
244         g3d_mult_matrix(m);
245 }
246
247 void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
248 {
249         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
250
251         float dx = right - left;
252         float dy = top - bottom;
253         float dz = fr - nr;
254
255         float a = (right + left) / dx;
256         float b = (top + bottom) / dy;
257         float c = -(fr + nr) / dz;
258         float d = -2.0 * fr * nr / dz;
259
260         m[0] = 2.0 * nr / dx;
261         m[5] = 2.0 * nr / dy;
262         m[8] = a;
263         m[9] = b;
264         m[10] = c;
265         m[11] = -1.0f;
266         m[14] = d;
267
268         g3d_mult_matrix(m);
269 }
270
271 void g3d_perspective(float vfov_deg, float aspect, float znear, float zfar)
272 {
273         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
274
275         float vfov = M_PI * vfov_deg / 180.0f;
276         float s = 1.0f / tan(vfov * 0.5f);
277         float range = znear - zfar;
278
279         m[0] = s / aspect;
280         m[5] = s;
281         m[10] = (znear + zfar) / range;
282         m[11] = -1.0f;
283         m[14] = 2.0f * znear * zfar / range;
284
285         g3d_mult_matrix(m);
286 }
287
288 const float *g3d_get_matrix(int which, float *m)
289 {
290         int top = st->mtop[which];
291
292         if(m) {
293                 memcpy(m, st->mat[which][top], 16 * sizeof(float));
294         }
295         return st->mat[which][top];
296 }
297
298 void g3d_light_pos(int idx, float x, float y, float z)
299 {
300         int mvtop = st->mtop[G3D_MODELVIEW];
301
302         st->lt[idx].x = x;
303         st->lt[idx].y = y;
304         st->lt[idx].z = z;
305
306         xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &st->lt[idx].x);
307 }
308
309 void g3d_light_color(int idx, float r, float g, float b)
310 {
311         st->lt[idx].r = r;
312         st->lt[idx].g = g;
313         st->lt[idx].b = b;
314 }
315
316 void g3d_light_ambient(float r, float g, float b)
317 {
318         st->ambient[0] = r;
319         st->ambient[1] = g;
320         st->ambient[2] = b;
321 }
322
323 void g3d_mtl_diffuse(float r, float g, float b)
324 {
325         st->mtl.kd[0] = r;
326         st->mtl.kd[1] = g;
327         st->mtl.kd[2] = b;
328 }
329
330 void g3d_mtl_specular(float r, float g, float b)
331 {
332         st->mtl.ks[0] = r;
333         st->mtl.ks[1] = g;
334         st->mtl.ks[2] = b;
335 }
336
337 void g3d_mtl_shininess(float shin)
338 {
339         st->mtl.shin = shin;
340 }
341
342 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
343 {
344         g3d_draw_indexed(prim, varr, varr_size, 0, 0);
345 }
346
347 void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
348                 const int16_t *iarr, int iarr_size)
349 {
350         int i, j, nfaces;
351         struct pvertex pv[4];
352         struct g3d_vertex v[4];
353         int vnum = prim; /* primitive vertex counts correspond to enum values */
354         int mvtop = st->mtop[G3D_MODELVIEW];
355         int ptop = st->mtop[G3D_PROJECTION];
356
357         /* calc the normal matrix */
358         memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
359         st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
360
361         nfaces = (iarr ? iarr_size : varr_size) / vnum;
362
363         for(j=0; j<nfaces; j++) {
364
365                 for(i=0; i<vnum; i++) {
366                         v[i] = iarr ? varr[*iarr++] : *varr++;
367
368                         xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
369                         xform3_vec3(st->norm_mat, &v[i].nx);
370
371                         if(st->opt & G3D_LIGHTING) {
372                                 shade(v + i);
373                         }
374                         xform4_vec3(st->mat[G3D_PROJECTION][ptop], &v[i].x);
375                 }
376
377                 /* TODO clipping */
378
379                 for(i=0; i<vnum; i++) {
380                         if(v[i].w != 0.0f) {
381                                 v[i].x /= v[i].w;
382                                 v[i].y /= v[i].w;
383                                 /*v[i].z /= v[i].w;*/
384                         }
385
386                         /* viewport transformation */
387                         v[i].x = (v[i].x * 0.5f + 0.5f) * (float)st->width;
388                         v[i].y = (0.5f - v[i].y * 0.5f) * (float)st->height;
389
390                         /* convert pos to 24.8 fixed point */
391                         pv[i].x = (int32_t)(v[i].x * 256.0f);
392                         pv[i].y = (int32_t)(v[i].y * 256.0f);
393                         /* convert tex coords to 16.16 fixed point */
394                         pv[i].u = (int32_t)(v[i].u * 65536.0f);
395                         pv[i].v = (int32_t)(v[i].v * 65536.0f);
396                         /* pass the color through as is */
397                         pv[i].r = v[i].r;
398                         pv[i].g = v[i].g;
399                         pv[i].b = v[i].b;
400                 }
401
402                 /* backface culling */
403                 if(vnum > 2 && st->opt & G3D_CULL_FACE) {
404                         int32_t ax = pv[1].x - pv[0].x;
405                         int32_t ay = pv[1].y - pv[0].y;
406                         int32_t bx = pv[2].x - pv[0].x;
407                         int32_t by = pv[2].y - pv[0].y;
408                         int32_t cross_z = ax * (by >> 8) - ay * (bx >> 8);
409                         int sign = (cross_z >> 31) & 1;
410
411                         if(!(sign ^ st->frontface)) {
412                                 continue;       /* back-facing */
413                         }
414                 }
415
416                 polyfill(st->fill_mode, pv, vnum);
417         }
418 }
419
420 static void xform4_vec3(const float *mat, float *vec)
421 {
422         float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
423         float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
424         float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
425         float w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
426
427         vec[0] = x;
428         vec[1] = y;
429         vec[2] = z;
430         vec[3] = w;
431 }
432
433 static void xform3_vec3(const float *mat, float *vec)
434 {
435         float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2];
436         float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2];
437         float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2];
438
439         vec[0] = x;
440         vec[1] = y;
441         vec[2] = z;
442 }
443
444 #define NORMALIZE(v) \
445         do { \
446                 float len = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); \
447                 if(len != 0.0) { \
448                         float s = 1.0 / len; \
449                         v[0] *= s; \
450                         v[1] *= s; \
451                         v[2] *= s; \
452                 } \
453         } while(0)
454
455 static void shade(struct g3d_vertex *v)
456 {
457         int i, r, g, b;
458         float color[3];
459
460         color[0] = st->ambient[0] * st->mtl.kd[0];
461         color[1] = st->ambient[1] * st->mtl.kd[1];
462         color[2] = st->ambient[2] * st->mtl.kd[2];
463
464         for(i=0; i<MAX_LIGHTS; i++) {
465                 float ldir[3];
466                 float ndotl;
467
468                 if(!(st->opt & (G3D_LIGHT0 << i))) {
469                         continue;
470                 }
471
472                 ldir[0] = st->lt[i].x - v->x;
473                 ldir[1] = st->lt[i].y - v->y;
474                 ldir[2] = st->lt[i].z - v->z;
475                 NORMALIZE(ldir);
476
477                 if((ndotl = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
478                         ndotl = 0.0f;
479                 }
480
481                 color[0] += st->mtl.kd[0] * st->lt[i].r * ndotl;
482                 color[1] += st->mtl.kd[1] * st->lt[i].g * ndotl;
483                 color[2] += st->mtl.kd[2] * st->lt[i].b * ndotl;
484         }
485
486         r = color[0] * 255.0;
487         g = color[1] * 255.0;
488         b = color[2] * 255.0;
489
490         v->r = r > 255 ? 255 : r;
491         v->g = g > 255 ? 255 : g;
492         v->b = b > 255 ? 255 : b;
493 }