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