crude metaballs
[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                 fill_mode = st->polymode;
632                 if(st->opt & G3D_TEXTURE_2D) {
633                         fill_mode |= POLYFILL_TEX_BIT;
634                 }
635                 /*
636                 if(st->opt & G3D_ALPHA_BLEND) {
637                    fill_mode |= POLYFILL_ALPHA_BIT;
638                 } else if(st->opt & G3D_ADD_BLEND) {
639                    fill_mode |= POLYFILL_ADD_BIT;
640                 }
641                 */
642 #ifdef ENABLE_ZBUFFER
643                 if(st->opt & G3D_DEPTH_TEST) {
644                         fill_mode |= POLYFILL_ZBUF_BIT;
645                 }
646 #endif
647                 num_tri = vnum - 2;
648                 vtri = v;
649                 pvtri = pv;
650                 for(;;) {
651                         calc_grad(vtri);
652                         polyfill(fill_mode, pvtri);
653                         if(--num_tri == 0) break;
654                         vtri[1] = vtri[0];
655                         pvtri[1] = pvtri[0];
656                         vtri++;
657                         pvtri++;
658                 }
659         }
660 }
661
662 #define ATTR_DELTAS(attr) \
663         float d##attr##02 = v[0].attr - v[2].attr; \
664         float d##attr##12 = v[1].attr - v[2].attr
665
666 #define DFDX(attr)      \
667         (dx ? (d##attr##12 * dy02 - d##attr##02 * dy12) / dx : 0)
668 #define DFDY(attr)      \
669         (dy ? (d##attr##12 * dx02 - d##attr##02 * dx12) / dy : 0)
670
671 static void calc_grad(struct g3d_vertex *v)
672 {
673         /*float dx01 = v[0].x - v[1].x;*/
674         float dx02 = v[0].x - v[2].x;
675         float dx12 = v[1].x - v[2].x;
676         /*float dy01 = v[0].y - v[1].y;*/
677         float dy02 = v[0].y - v[2].y;
678         float dy12 = v[1].y - v[2].y;
679
680         float dx = dx12 * dy02 - dx02 * dy12;
681         float dy = dx02 * dy12 - dx12 * dy02;
682
683         if(st->polymode == POLYFILL_GOURAUD) {
684                 ATTR_DELTAS(l);
685                 pgrad.dldx = cround64(DFDX(l) * 4096.0f);
686                 pgrad.dldy = cround64(DFDY(l) * 4096.0f);
687                 if(st->opt & G3D_ALPHA_BLEND) {
688                         ATTR_DELTAS(a);
689                         pgrad.dadx = cround64(DFDX(a) * 4096.0f);
690                         pgrad.dady = cround64(DFDY(a) * 4096.0f);
691                 }
692         }
693
694         if(st->opt & G3D_DEPTH_TEST) {
695                 ATTR_DELTAS(z);
696                 pgrad.dzdx = cround64(DFDX(z) * 8388607.5f);
697                 pgrad.dzdy = cround64(DFDY(z) * 8388607.5f);
698         }
699
700         if(st->opt & G3D_TEXTURE_2D) {
701                 ATTR_DELTAS(u);
702                 ATTR_DELTAS(v);
703                 pgrad.dudx = cround64(DFDX(u) * 65536.0f);
704                 pgrad.dudy = cround64(DFDY(u) * 65536.0f);
705                 pgrad.dvdx = cround64(DFDX(v) * 65536.0f);
706                 pgrad.dvdy = cround64(DFDY(v) * 65536.0f);
707         }
708 }
709
710 void g3d_begin(int prim)
711 {
712         st->imm_prim = prim;
713         st->imm_pcount = prim;
714         st->imm_numv = 0;
715 }
716
717 void g3d_end(void)
718 {
719         imm_flush();
720 }
721
722 static void imm_flush(void)
723 {
724         int numv = st->imm_numv;
725         st->imm_numv = 0;
726         g3d_draw_indexed(st->imm_prim, st->imm_vbuf, numv, 0, 0);
727 }
728
729 void g3d_vertex(float x, float y, float z)
730 {
731         struct g3d_vertex *vptr = st->imm_vbuf + st->imm_numv++;
732         *vptr = st->imm_curv;
733         vptr->x = x;
734         vptr->y = y;
735         vptr->z = z;
736         vptr->w = 1.0f;
737
738         if(!--st->imm_pcount) {
739                 if(st->imm_numv >= IMM_VBUF_SIZE - st->imm_prim) {
740                         imm_flush();
741                 }
742                 st->imm_pcount = st->imm_prim;
743         }
744 }
745
746 void g3d_normal(float x, float y, float z)
747 {
748         st->imm_curv.nx = x;
749         st->imm_curv.ny = y;
750         st->imm_curv.nz = z;
751 }
752
753 #define CLAMP(x, a, b)  ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
754 #define MIN(a, b)               ((a) < (b) ? (a) : (b))
755
756 void g3d_color1b(unsigned char lum)
757 {
758         st->imm_curv.l = MIN(lum, 255);
759         st->imm_curv.a = 255;
760 }
761
762 void g3d_color2b(unsigned char lum, unsigned char a)
763 {
764         st->imm_curv.l = MIN(lum, 255);
765         st->imm_curv.a = MIN(a, 255);
766 }
767
768 void g3d_color1f(float lum)
769 {
770         int ilum = lum * 255.0f;
771         st->imm_curv.l = CLAMP(ilum, 0, 255);
772         st->imm_curv.a = 255;
773 }
774
775 void g3d_color2f(float lum, float a)
776 {
777         int ilum = lum * 255.0f;
778         int ia = a * 255.0f;
779         st->imm_curv.l = CLAMP(ilum, 0, 255);
780         st->imm_curv.a = CLAMP(ia, 0, 255);
781 }
782
783 void g3d_texcoord(float u, float v)
784 {
785         st->imm_curv.u = u;
786         st->imm_curv.v = v;
787 }
788
789 static __inline void xform4_vec3(const float *mat, float *vec)
790 {
791         float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
792         float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
793         float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
794         vec[3] = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
795         vec[2] = z;
796         vec[1] = y;
797         vec[0] = x;
798 }
799
800 static __inline void xform3_vec3(const float *mat, float *vec)
801 {
802         float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2];
803         float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2];
804         vec[2] = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2];
805         vec[1] = y;
806         vec[0] = x;
807 }
808
809 static void shade(struct g3d_vertex *v)
810 {
811         int i, ilum;
812         float lum;
813
814         lum = st->ambient * st->mtl.kd;
815
816         for(i=0; i<MAX_LIGHTS; i++) {
817                 float ldir[3];
818                 float ndotl;
819
820                 if(!(st->opt & (G3D_LIGHT0 << i))) {
821                         continue;
822                 }
823
824                 ldir[0] = st->lt[i].x;
825                 ldir[1] = st->lt[i].y;
826                 ldir[2] = st->lt[i].z;
827
828                 if(st->lt[i].type != LT_DIR) {
829                         ldir[0] -= v->x;
830                         ldir[1] -= v->y;
831                         ldir[2] -= v->z;
832                         NORMALIZE(ldir);
833                 }
834
835                 if((ndotl = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
836                         ndotl = 0.0f;
837                 }
838
839                 lum += st->mtl.kd * st->lt[i].energy * ndotl;
840
841                 if(st->opt & G3D_SPECULAR) {
842                         float ndoth;
843                         ldir[2] += 1.0f;
844                         NORMALIZE(ldir);
845                         if((ndoth = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
846                                 ndoth = 0.0f;
847                         }
848                         ndoth = pow(ndoth, st->mtl.shin);
849
850                         lum += st->mtl.ks * st->lt[i].energy * ndoth;
851                 }
852         }
853
854         ilum = cround64(lum * 255.0);
855
856         v->l = ilum > 255 ? 255 : ilum;
857 }
858
859 #endif  /* !def BUILD_OPENGL */