ac50929839956c644ebfe2a56f0f3da31bb4517c
[metatoy] / src / 3dgfx / 3dgfx.c
1 #ifndef BUILD_OPENGL
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <math.h>
6 #include <assert.h>
7 #include <alloca.h>
8 #include "3dgfx.h"
9 #include "polyfill.h"
10 #include "polyclip.h"
11 #include "inttypes.h"
12 #include "gfxutil.h"
13 #include "util.h"
14 #include "colormgr.h"
15
16
17 #undef CORRECT_NORMAL_MATRIX
18 #ifdef CORRECT_NORMAL_MATRIX
19 #include <cgmath/cgmath.h>
20 #endif
21
22 #define ENABLE_ZBUFFER
23
24 #define STACK_SIZE      16
25 typedef float g3d_matrix[16];
26
27 #define MAX_LIGHTS              4
28
29 #define IMM_VBUF_SIZE   256
30
31 #define NORMALIZE(v) \
32         do { \
33                 float len = sqrt((v)[0] * (v)[0] + (v)[1] * (v)[1] + (v)[2] * (v)[2]); \
34                 if(len != 0.0) { \
35                         float s = 1.0 / len; \
36                         (v)[0] *= s; \
37                         (v)[1] *= s; \
38                         (v)[2] *= s; \
39                 } \
40         } while(0)
41
42 enum {LT_POS, LT_DIR};
43 struct light {
44         int type;
45         float x, y, z;
46         float energy;
47 };
48
49 struct material {
50         float kd, ks;
51         float shin;
52 };
53
54 struct g3d_state {
55         unsigned int opt;
56         int frontface;
57         int polymode;
58
59         g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
60         int mtop[G3D_NUM_MATRICES];
61         int mmode;
62
63         g3d_matrix norm_mat;
64
65         float ambient;
66         struct light lt[MAX_LIGHTS];
67         struct material mtl;
68
69         int width, height;
70         g3d_pixel *pixels;
71
72         int vport[4];
73
74         g3d_pixel clear_color;
75         uint32_t clear_depth;
76
77         /* immediate mode */
78         int imm_prim;
79         int imm_numv, imm_pcount;
80         struct g3d_vertex imm_curv;
81         struct g3d_vertex imm_vbuf[IMM_VBUF_SIZE];
82 };
83
84 static void calc_grad(struct g3d_vertex *v);
85
86 static void imm_flush(void);
87 static __inline void xform4_vec3(const float *mat, float *vec);
88 static __inline void xform3_vec3(const float *mat, float *vec);
89 static void shade(struct g3d_vertex *v);
90
91 static struct g3d_state *st;
92 static const float idmat[] = {
93         1, 0, 0, 0,
94         0, 1, 0, 0,
95         0, 0, 1, 0,
96         0, 0, 0, 1
97 };
98
99 int g3d_init(void)
100 {
101         if(!(st = calloc(1, sizeof *st))) {
102                 fprintf(stderr, "failed to allocate G3D context\n");
103                 return -1;
104         }
105         g3d_reset();
106
107         return 0;
108 }
109
110 void g3d_destroy(void)
111 {
112 #ifdef ENABLE_ZBUFFER
113         free(pfill_zbuf);
114 #endif
115         free(st);
116 }
117
118 void g3d_reset(void)
119 {
120         int i;
121
122 #ifdef ENABLE_ZBUFFER
123         free(pfill_zbuf);
124 #endif
125         memset(st, 0, sizeof *st);
126
127         st->opt = G3D_CLIP_FRUSTUM;
128         st->polymode = POLYFILL_FLAT;
129
130         for(i=0; i<G3D_NUM_MATRICES; i++) {
131                 g3d_matrix_mode(i);
132                 g3d_load_identity();
133         }
134
135         for(i=0; i<MAX_LIGHTS; i++) {
136                 g3d_light_dir(i, 0, 0, 1);
137                 g3d_light_energy(i, 1);
138         }
139         g3d_light_ambient(0.1);
140
141         g3d_mtl_diffuse(1);
142
143         st->clear_depth = 0xffffff;
144 }
145
146 void g3d_framebuffer(int width, int height, void *pixels)
147 {
148         static int max_height;
149
150 #ifdef ENABLE_ZBUFFER
151         static int max_npixels;
152         int npixels = width * height;
153
154         if(npixels > max_npixels) {
155                 free(pfill_zbuf);
156                 pfill_zbuf = malloc(npixels * sizeof *pfill_zbuf);
157                 max_npixels = npixels;
158         }
159 #endif
160
161         if(height > max_height) {
162                 polyfill_fbheight(height);
163                 max_height = height;
164         }
165
166         st->width = width;
167         st->height = height;
168
169         pfill_fb.pixels = pixels;
170         pfill_fb.width = width;
171         pfill_fb.height = height;
172
173         g3d_viewport(0, 0, width, height);
174 }
175
176 /* set the framebuffer pointer, without resetting the size */
177 void g3d_framebuffer_addr(void *pixels)
178 {
179         pfill_fb.pixels = pixels;
180 }
181
182 void g3d_viewport(int x, int y, int w, int h)
183 {
184         st->vport[0] = x;
185         st->vport[1] = y;
186         st->vport[2] = w;
187         st->vport[3] = h;
188 }
189
190 void g3d_clear_color(unsigned char r, unsigned char g, unsigned char b)
191 {
192         st->clear_color = find_color(r, g, b);
193 }
194
195 void g3d_clear_depth(float z)
196 {
197         int iz = (int)(z * (float)0xffffff);
198         if(iz < 0) iz = 0;
199         if(iz > 0xffffff) iz = 0xffffff;
200         st->clear_depth = iz;
201 }
202
203 void g3d_clear(unsigned int mask)
204 {
205         if(mask & G3D_COLOR_BUFFER_BIT) {
206                 memset(pfill_fb.pixels, st->clear_color, pfill_fb.width * pfill_fb.height);
207         }
208         if(mask & G3D_DEPTH_BUFFER_BIT) {
209                 memset32(pfill_zbuf, st->clear_depth, pfill_fb.width * pfill_fb.height);
210         }
211 }
212
213 void g3d_enable(unsigned int opt)
214 {
215         st->opt |= opt;
216 }
217
218 void g3d_disable(unsigned int opt)
219 {
220         st->opt &= ~opt;
221 }
222
223 void g3d_setopt(unsigned int opt, unsigned int mask)
224 {
225         st->opt = (st->opt & ~mask) | (opt & mask);
226 }
227
228 unsigned int g3d_getopt(unsigned int mask)
229 {
230         return st->opt & mask;
231 }
232
233 void g3d_front_face(unsigned int order)
234 {
235         st->frontface = order;
236 }
237
238 void g3d_polygon_mode(int pmode)
239 {
240         st->polymode = pmode;
241 }
242
243 int g3d_get_polygon_mode(void)
244 {
245         return st->polymode;
246 }
247
248 void g3d_matrix_mode(int mmode)
249 {
250         st->mmode = mmode;
251 }
252
253 void g3d_load_identity(void)
254 {
255         int top = st->mtop[st->mmode];
256         memcpy(st->mat[st->mmode][top], idmat, 16 * sizeof(float));
257 }
258
259 void g3d_load_matrix(const float *m)
260 {
261         int top = st->mtop[st->mmode];
262         memcpy(st->mat[st->mmode][top], m, 16 * sizeof(float));
263 }
264
265 #define M(i,j)  (((i) << 2) + (j))
266 void g3d_mult_matrix(const float *m2)
267 {
268         int i, j, top = st->mtop[st->mmode];
269         float m1[16];
270         float *dest = st->mat[st->mmode][top];
271
272         memcpy(m1, dest, sizeof m1);
273
274         for(i=0; i<4; i++) {
275                 for(j=0; j<4; j++) {
276                         *dest++ = m1[M(0,j)] * m2[M(i,0)] +
277                                 m1[M(1,j)] * m2[M(i,1)] +
278                                 m1[M(2,j)] * m2[M(i,2)] +
279                                 m1[M(3,j)] * m2[M(i,3)];
280                 }
281         }
282 }
283
284 void g3d_push_matrix(void)
285 {
286         int top = st->mtop[st->mmode];
287         if(top >= STACK_SIZE) {
288                 fprintf(stderr, "g3d_push_matrix overflow\n");
289                 return;
290         }
291         memcpy(st->mat[st->mmode][top + 1], st->mat[st->mmode][top], 16 * sizeof(float));
292         st->mtop[st->mmode] = top + 1;
293 }
294
295 void g3d_pop_matrix(void)
296 {
297         if(st->mtop[st->mmode] <= 0) {
298                 fprintf(stderr, "g3d_pop_matrix underflow\n");
299                 return;
300         }
301         --st->mtop[st->mmode];
302 }
303
304 void g3d_translate(float x, float y, float z)
305 {
306         float m[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
307         m[12] = x;
308         m[13] = y;
309         m[14] = z;
310         g3d_mult_matrix(m);
311 }
312
313 void g3d_rotate(float deg, float x, float y, float z)
314 {
315         float m[16] = {0};
316
317         float angle = M_PI * deg / 180.0f;
318         float sina = sin(angle);
319         float cosa = cos(angle);
320         float one_minus_cosa = 1.0f - cosa;
321         float nxsq = x * x;
322         float nysq = y * y;
323         float nzsq = z * z;
324
325         m[0] = nxsq + (1.0f - nxsq) * cosa;
326         m[4] = x * y * one_minus_cosa - z * sina;
327         m[8] = x * z * one_minus_cosa + y * sina;
328         m[1] = x * y * one_minus_cosa + z * sina;
329         m[5] = nysq + (1.0 - nysq) * cosa;
330         m[9] = y * z * one_minus_cosa - x * sina;
331         m[2] = x * z * one_minus_cosa - y * sina;
332         m[6] = y * z * one_minus_cosa + x * sina;
333         m[10] = nzsq + (1.0 - nzsq) * cosa;
334         m[15] = 1.0f;
335
336         g3d_mult_matrix(m);
337 }
338
339 void g3d_scale(float x, float y, float z)
340 {
341         float m[16] = {0};
342         m[0] = x;
343         m[5] = y;
344         m[10] = z;
345         m[15] = 1.0f;
346         g3d_mult_matrix(m);
347 }
348
349 void g3d_ortho(float left, float right, float bottom, float top, float znear, float zfar)
350 {
351         float m[16] = {0};
352
353         float dx = right - left;
354         float dy = top - bottom;
355         float dz = zfar - znear;
356
357         m[0] = 2.0 / dx;
358         m[5] = 2.0 / dy;
359         m[10] = -2.0 / dz;
360         m[12] = -(right + left) / dx;
361         m[13] = -(top + bottom) / dy;
362         m[14] = -(zfar + znear) / dz;
363         m[15] = 1.0f;
364
365         g3d_mult_matrix(m);
366 }
367
368 void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
369 {
370         float m[16] = {0};
371
372         float dx = right - left;
373         float dy = top - bottom;
374         float dz = fr - nr;
375
376         float a = (right + left) / dx;
377         float b = (top + bottom) / dy;
378         float c = -(fr + nr) / dz;
379         float d = -2.0 * fr * nr / dz;
380
381         m[0] = 2.0 * nr / dx;
382         m[5] = 2.0 * nr / dy;
383         m[8] = a;
384         m[9] = b;
385         m[10] = c;
386         m[11] = -1.0f;
387         m[14] = d;
388
389         g3d_mult_matrix(m);
390 }
391
392 void g3d_perspective(float vfov_deg, float aspect, float znear, float zfar)
393 {
394         float m[16] = {0};
395
396         float vfov = M_PI * vfov_deg / 180.0f;
397         float s = 1.0f / tan(vfov * 0.5f);
398         float range = znear - zfar;
399
400         m[0] = s / aspect;
401         m[5] = s;
402         m[10] = (znear + zfar) / range;
403         m[11] = -1.0f;
404         m[14] = 2.0f * znear * zfar / range;
405
406         g3d_mult_matrix(m);
407 }
408
409 const float *g3d_get_matrix(int which, float *m)
410 {
411         int top = st->mtop[which];
412
413         if(m) {
414                 memcpy(m, st->mat[which][top], 16 * sizeof(float));
415         }
416         return st->mat[which][top];
417 }
418
419 void g3d_light_pos(int idx, float x, float y, float z)
420 {
421         int mvtop = st->mtop[G3D_MODELVIEW];
422
423         st->lt[idx].type = LT_POS;
424         st->lt[idx].x = x;
425         st->lt[idx].y = y;
426         st->lt[idx].z = z;
427
428         xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &st->lt[idx].x);
429 }
430
431 void g3d_light_dir(int idx, float x, float y, float z)
432 {
433         int mvtop = st->mtop[G3D_MODELVIEW];
434
435         st->lt[idx].type = LT_DIR;
436         st->lt[idx].x = x;
437         st->lt[idx].y = y;
438         st->lt[idx].z = z;
439
440         /* calc the normal matrix */
441 #ifdef CORRECT_NORMAL_MATRIX
442         memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
443         cgm_minverse(st->norm_mat);
444         cgm_mtranspose(st->norm_mat);
445 #else
446         memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
447         st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
448 #endif
449
450         xform4_vec3(st->norm_mat, &st->lt[idx].x);
451
452         NORMALIZE(&st->lt[idx].x);
453 }
454
455 void g3d_light_energy(int idx, float val)
456 {
457         st->lt[idx].energy = val;
458 }
459
460 void g3d_light_ambient(float val)
461 {
462         st->ambient = val;
463 }
464
465 void g3d_mtl_diffuse(float diffuse)
466 {
467         st->mtl.kd = diffuse;
468 }
469
470 void g3d_mtl_specular(float spec)
471 {
472         st->mtl.ks = spec;
473 }
474
475 void g3d_mtl_shininess(float shin)
476 {
477         st->mtl.shin = shin;
478 }
479
480 static inline int calc_shift(unsigned int x)
481 {
482         int res = -1;
483         while(x) {
484                 x >>= 1;
485                 ++res;
486         }
487         return res;
488 }
489
490 static inline int calc_mask(unsigned int x)
491 {
492         return x - 1;
493 }
494
495 void g3d_set_texture(int xsz, int ysz, void *pixels)
496 {
497         pfill_tex.pixels = pixels;
498         pfill_tex.width = xsz;
499         pfill_tex.height = ysz;
500
501         pfill_tex.xshift = calc_shift(xsz);
502         pfill_tex.yshift = calc_shift(ysz);
503         pfill_tex.xmask = calc_mask(xsz);
504         pfill_tex.ymask = calc_mask(ysz);
505 }
506
507 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
508 {
509         g3d_draw_indexed(prim, varr, varr_size, 0, 0);
510 }
511
512 #define NEED_NORMALS    (st->opt & (G3D_LIGHTING | G3D_TEXTURE_GEN))
513
514 void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
515                 const uint16_t *iarr, int iarr_size)
516 {
517         int i, j, vnum, nfaces, fill_mode, num_tri;
518         struct pvertex pv[16], *pvtri;
519         struct g3d_vertex v[16], *vtri;
520         int mvtop = st->mtop[G3D_MODELVIEW];
521         int ptop = st->mtop[G3D_PROJECTION];
522         struct g3d_vertex *tmpv;
523
524         tmpv = alloca(prim * 6 * sizeof *tmpv);
525
526         /* calc the normal matrix */
527         if(NEED_NORMALS) {
528 #ifdef CORRECT_NORMAL_MATRIX
529                 memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
530                 cgm_minverse(st->norm_mat);
531                 cgm_mtranspose(st->norm_mat);
532 #else
533                 memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
534                 st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
535 #endif
536         }
537
538         nfaces = (iarr ? iarr_size : varr_size) / prim;
539
540         for(j=0; j<nfaces; j++) {
541                 vnum = prim;    /* reset vnum for each iteration */
542
543                 for(i=0; i<vnum; i++) {
544                         v[i] = iarr ? varr[*iarr++] : *varr++;
545
546                         xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
547
548                         if(NEED_NORMALS) {
549                                 xform3_vec3(st->norm_mat, &v[i].nx);
550                                 if(st->opt & G3D_LIGHTING) {
551                                         shade(v + i);
552                                 }
553                                 if(st->opt & G3D_TEXTURE_GEN) {
554                                         v[i].u = v[i].nx * 0.5 + 0.5;
555                                         v[i].v = 0.5 - v[i].ny * 0.5;
556                                 }
557                         }
558                         if(st->opt & G3D_TEXTURE_MAT) {
559                                 float *mat = st->mat[G3D_TEXTURE][st->mtop[G3D_TEXTURE]];
560                                 float x = mat[0] * v[i].u + mat[4] * v[i].v + mat[12];
561                                 float y = mat[1] * v[i].u + mat[5] * v[i].v + mat[13];
562                                 float w = mat[3] * v[i].u + mat[7] * v[i].v + mat[15];
563                                 v[i].u = x / w;
564                                 v[i].v = y / w;
565                         }
566                         xform4_vec3(st->mat[G3D_PROJECTION][ptop], &v[i].x);
567                 }
568
569                 /* clipping */
570                 if(st->opt & G3D_CLIP_FRUSTUM) {
571                         for(i=0; i<6; i++) {
572                                 memcpy(tmpv, v, vnum * sizeof *v);
573
574                                 if(clip_frustum(v, &vnum, tmpv, vnum, i) < 0) {
575                                         /* polygon completely outside of view volume. discard */
576                                         vnum = 0;
577                                         break;
578                                 }
579                         }
580
581                         if(!vnum) continue;
582                 }
583
584                 for(i=0; i<vnum; i++) {
585                         if(v[i].w != 0.0f) {
586                                 v[i].x /= v[i].w;
587                                 v[i].y /= v[i].w;
588 #ifdef ENABLE_ZBUFFER
589                                 if(st->opt & G3D_DEPTH_TEST) {
590                                         v[i].z /= v[i].w;
591                                 }
592 #endif
593                         }
594
595                         /* viewport transformation */
596                         v[i].x = (v[i].x * 0.5f + 0.5f) * (float)st->vport[2] + st->vport[0];
597                         v[i].y = (0.5f - v[i].y * 0.5f) * (float)st->vport[3] + st->vport[1];
598
599                         /* convert pos to 24.8 fixed point */
600                         pv[i].x = cround64(v[i].x * 256.0f);
601                         pv[i].y = cround64(v[i].y * 256.0f);
602 #ifdef ENABLE_ZBUFFER
603                         if(st->opt & G3D_DEPTH_TEST) {
604                                 /* after div/w z is in [-1, 1], remap it to [0, 0xffffff] */
605                                 pv[i].z = cround64(v[i].z * 8388607.5f + 8388607.5f);
606                         }
607 #endif
608                         /* convert tex coords to 16.16 fixed point */
609                         pv[i].u = cround64(v[i].u * 65536.0f);
610                         pv[i].v = cround64(v[i].v * 65536.0f);
611                         /* pass the color through as is */
612                         pv[i].l = v[i].l;
613                 }
614
615                 /* backface culling */
616 #if 0   /* TODO fix culling */
617                 if(vnum > 2 && st->opt & G3D_CULL_FACE) {
618                         int32_t ax = pv[1].x - pv[0].x;
619                         int32_t ay = pv[1].y - pv[0].y;
620                         int32_t bx = pv[2].x - pv[0].x;
621                         int32_t by = pv[2].y - pv[0].y;
622                         int32_t cross_z = (ax >> 4) * (by >> 4) - (ay >> 4) * (bx >> 4);
623                         int sign = (cross_z >> 31) & 1;
624
625                         if(!(sign ^ st->frontface)) {
626                                 continue;       /* back-facing */
627                         }
628                 }
629 #endif
630
631                 switch(vnum) {
632                 case 1:
633                         /*
634                         if(st->opt & (G3D_ALPHA_BLEND | G3D_ADD_BLEND)) {
635                                 int r, g, b, inv_alpha;
636                                 g3d_pixel *dest = pfill_fb.pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
637                                 if(st->opt & G3D_ALPHA_BLEND) {
638                                         inv_alpha = 255 - pv[0].a;
639                                         r = ((int)pv[0].r * pv[0].a + G3D_UNPACK_R(*dest) * inv_alpha) >> 8;
640                                         g = ((int)pv[0].g * pv[0].a + G3D_UNPACK_G(*dest) * inv_alpha) >> 8;
641                                         b = ((int)pv[0].b * pv[0].a + G3D_UNPACK_B(*dest) * inv_alpha) >> 8;
642                                 } else {
643                                         r = (int)pv[0].r + G3D_UNPACK_R(*dest);
644                                         g = (int)pv[0].g + G3D_UNPACK_G(*dest);
645                                         b = (int)pv[0].b + G3D_UNPACK_B(*dest);
646                                         if(r > 255) r = 255;
647                                         if(g > 255) g = 255;
648                                         if(b > 255) b = 255;
649                                 }
650                                 *dest++ = G3D_PACK_RGB(r, g, b);
651                         } else {
652                                 g3d_pixel *dest = pfill_fb.pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
653                                 *dest = G3D_PACK_RGB(pv[0].r, pv[0].g, pv[0].b);
654                         }
655                         */
656                         break;
657
658                 case 2:
659                         {
660                                 g3d_pixel col = pv[0].l;
661                                 draw_line(pv[0].x >> 8, pv[0].y >> 8, pv[1].x >> 8, pv[1].y >> 8, col);
662                         }
663                         break;
664
665                 default:
666                         fill_mode = st->polymode;
667                         if(st->opt & G3D_TEXTURE_2D) {
668                                 fill_mode |= POLYFILL_TEX_BIT;
669                         }
670                         /*
671                         if(st->opt & G3D_ALPHA_BLEND) {
672                                 fill_mode |= POLYFILL_ALPHA_BIT;
673                         } else if(st->opt & G3D_ADD_BLEND) {
674                                 fill_mode |= POLYFILL_ADD_BIT;
675                         }
676                         */
677 #ifdef ENABLE_ZBUFFER
678                         if(st->opt & G3D_DEPTH_TEST) {
679                                 fill_mode |= POLYFILL_ZBUF_BIT;
680                         }
681 #endif
682                         num_tri = vnum - 2;
683                         vtri = v;
684                         pvtri = pv;
685                         for(;;) {
686                                 calc_grad(vtri);
687                                 polyfill(fill_mode, pvtri);
688                                 if(--num_tri == 0) break;
689                                 vtri[1] = vtri[0];
690                                 pvtri[1] = pvtri[0];
691                                 vtri++;
692                                 pvtri++;
693                         }
694                 }
695         }
696 }
697
698 #define ATTR_DELTAS(attr) \
699         float d##attr##02 = v[0].attr - v[2].attr; \
700         float d##attr##12 = v[1].attr - v[2].attr
701
702 #define DFDX(attr)      \
703         (dx ? (d##attr##12 * dy02 - d##attr##02 * dy12) / dx : 0)
704 #define DFDY(attr)      \
705         (dy ? (d##attr##12 * dx02 - d##attr##02 * dx12) / dy : 0)
706
707 static void calc_grad(struct g3d_vertex *v)
708 {
709         /*float dx01 = v[0].x - v[1].x;*/
710         float dx02 = v[0].x - v[2].x;
711         float dx12 = v[1].x - v[2].x;
712         /*float dy01 = v[0].y - v[1].y;*/
713         float dy02 = v[0].y - v[2].y;
714         float dy12 = v[1].y - v[2].y;
715
716         float dx = dx12 * dy02 - dx02 * dy12;
717         float dy = dx02 * dy12 - dx12 * dy02;
718
719         if(st->polymode == POLYFILL_GOURAUD) {
720                 ATTR_DELTAS(l);
721                 pgrad.dldx = cround64(DFDX(l) * 4096.0f);
722                 pgrad.dldy = cround64(DFDY(l) * 4096.0f);
723                 if(st->opt & G3D_ALPHA_BLEND) {
724                         ATTR_DELTAS(a);
725                         pgrad.dadx = cround64(DFDX(a) * 4096.0f);
726                         pgrad.dady = cround64(DFDY(a) * 4096.0f);
727                 }
728         }
729
730         if(st->opt & G3D_DEPTH_TEST) {
731                 ATTR_DELTAS(z);
732                 pgrad.dzdx = cround64(DFDX(z) * 8388607.5f);
733                 pgrad.dzdy = cround64(DFDY(z) * 8388607.5f);
734         }
735
736         if(st->opt & G3D_TEXTURE_2D) {
737                 ATTR_DELTAS(u);
738                 ATTR_DELTAS(v);
739                 pgrad.dudx = cround64(DFDX(u) * 65536.0f);
740                 pgrad.dudy = cround64(DFDY(u) * 65536.0f);
741                 pgrad.dvdx = cround64(DFDX(v) * 65536.0f);
742                 pgrad.dvdy = cround64(DFDY(v) * 65536.0f);
743         }
744 }
745
746 void g3d_begin(int prim)
747 {
748         st->imm_prim = prim;
749         st->imm_pcount = prim;
750         st->imm_numv = 0;
751 }
752
753 void g3d_end(void)
754 {
755         imm_flush();
756 }
757
758 static void imm_flush(void)
759 {
760         int numv = st->imm_numv;
761         st->imm_numv = 0;
762         g3d_draw_indexed(st->imm_prim, st->imm_vbuf, numv, 0, 0);
763 }
764
765 void g3d_vertex(float x, float y, float z)
766 {
767         struct g3d_vertex *vptr = st->imm_vbuf + st->imm_numv++;
768         *vptr = st->imm_curv;
769         vptr->x = x;
770         vptr->y = y;
771         vptr->z = z;
772         vptr->w = 1.0f;
773
774         if(!--st->imm_pcount) {
775                 if(st->imm_numv >= IMM_VBUF_SIZE - st->imm_prim) {
776                         imm_flush();
777                 }
778                 st->imm_pcount = st->imm_prim;
779         }
780 }
781
782 void g3d_normal(float x, float y, float z)
783 {
784         st->imm_curv.nx = x;
785         st->imm_curv.ny = y;
786         st->imm_curv.nz = z;
787 }
788
789 #define CLAMP(x, a, b)  ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
790 #define MIN(a, b)               ((a) < (b) ? (a) : (b))
791
792 void g3d_color1b(unsigned char lum)
793 {
794         st->imm_curv.l = MIN(lum, 255);
795         st->imm_curv.a = 255;
796 }
797
798 void g3d_color2b(unsigned char lum, unsigned char a)
799 {
800         st->imm_curv.l = MIN(lum, 255);
801         st->imm_curv.a = MIN(a, 255);
802 }
803
804 void g3d_color1f(float lum)
805 {
806         int ilum = lum * 255.0f;
807         st->imm_curv.l = CLAMP(ilum, 0, 255);
808         st->imm_curv.a = 255;
809 }
810
811 void g3d_color2f(float lum, float a)
812 {
813         int ilum = lum * 255.0f;
814         int ia = a * 255.0f;
815         st->imm_curv.l = CLAMP(ilum, 0, 255);
816         st->imm_curv.a = CLAMP(ia, 0, 255);
817 }
818
819 void g3d_texcoord(float u, float v)
820 {
821         st->imm_curv.u = u;
822         st->imm_curv.v = v;
823 }
824
825 static __inline void xform4_vec3(const float *mat, float *vec)
826 {
827         float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
828         float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
829         float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
830         vec[3] = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
831         vec[2] = z;
832         vec[1] = y;
833         vec[0] = x;
834 }
835
836 static __inline void xform3_vec3(const float *mat, float *vec)
837 {
838         float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2];
839         float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2];
840         vec[2] = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2];
841         vec[1] = y;
842         vec[0] = x;
843 }
844
845 static void shade(struct g3d_vertex *v)
846 {
847         int i, ilum;
848         float lum;
849
850         lum = st->ambient * st->mtl.kd;
851
852         for(i=0; i<MAX_LIGHTS; i++) {
853                 float ldir[3];
854                 float ndotl;
855
856                 if(!(st->opt & (G3D_LIGHT0 << i))) {
857                         continue;
858                 }
859
860                 ldir[0] = st->lt[i].x;
861                 ldir[1] = st->lt[i].y;
862                 ldir[2] = st->lt[i].z;
863
864                 if(st->lt[i].type != LT_DIR) {
865                         ldir[0] -= v->x;
866                         ldir[1] -= v->y;
867                         ldir[2] -= v->z;
868                         NORMALIZE(ldir);
869                 }
870
871                 if((ndotl = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
872                         ndotl = 0.0f;
873                 }
874
875                 lum += st->mtl.kd * st->lt[i].energy * ndotl;
876
877                 if(st->opt & G3D_SPECULAR) {
878                         float ndoth;
879                         ldir[2] += 1.0f;
880                         NORMALIZE(ldir);
881                         if((ndoth = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
882                                 ndoth = 0.0f;
883                         }
884                         ndoth = pow(ndoth, st->mtl.shin);
885
886                         lum += st->mtl.ks * st->lt[i].energy * ndoth;
887                 }
888         }
889
890         ilum = cround64(lum * 255.0);
891
892         v->l = ilum > 255 ? 255 : ilum;
893 }
894
895 #endif  /* !def BUILD_OPENGL */