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