6 #if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__DJGPP__)
20 typedef float g3d_matrix[16];
24 #define IMM_VBUF_SIZE 256
26 #define NORMALIZE(v) \
28 float len = sqrt((v)[0] * (v)[0] + (v)[1] * (v)[1] + (v)[2] * (v)[2]); \
30 float s = 1.0 / len; \
37 enum {LT_POS, LT_DIR};
55 g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
56 int mtop[G3D_NUM_MATRICES];
62 struct light lt[MAX_LIGHTS];
72 int imm_numv, imm_pcount;
73 struct g3d_vertex imm_curv;
74 struct g3d_vertex imm_vbuf[IMM_VBUF_SIZE];
77 static void imm_flush(void);
78 static __inline void xform4_vec3(const float *mat, float *vec);
79 static __inline void xform3_vec3(const float *mat, float *vec);
80 static void shade(struct g3d_vertex *v);
82 static struct g3d_state *st;
83 static const float idmat[] = {
94 if(!(st = calloc(1, sizeof *st))) {
95 fprintf(stderr, "failed to allocate G3D context\n");
98 st->opt = G3D_CLIP_FRUSTUM;
99 st->polymode = POLYFILL_FLAT;
101 for(i=0; i<G3D_NUM_MATRICES; i++) {
106 for(i=0; i<MAX_LIGHTS; i++) {
107 g3d_light_color(i, 1, 1, 1);
109 g3d_light_ambient(0.1, 0.1, 0.1);
111 g3d_mtl_diffuse(1, 1, 1);
115 void g3d_destroy(void)
120 void g3d_framebuffer(int width, int height, void *pixels)
126 pfill_fb.pixels = pixels;
127 pfill_fb.width = width;
128 pfill_fb.height = height;
130 g3d_viewport(0, 0, width, height);
133 /* set the framebuffer pointer, without resetting the size */
134 void g3d_framebuffer_addr(void *pixels)
137 pfill_fb.pixels = pixels;
140 void g3d_viewport(int x, int y, int w, int h)
148 void g3d_enable(unsigned int opt)
153 void g3d_disable(unsigned int opt)
158 void g3d_setopt(unsigned int opt, unsigned int mask)
160 st->opt = (st->opt & ~mask) | (opt & mask);
163 unsigned int g3d_getopt(unsigned int mask)
165 return st->opt & mask;
168 void g3d_front_face(unsigned int order)
170 st->frontface = order;
173 void g3d_polygon_mode(int pmode)
175 st->polymode = pmode;
178 void g3d_matrix_mode(int mmode)
183 void g3d_load_identity(void)
185 int top = st->mtop[st->mmode];
186 memcpy(st->mat[st->mmode][top], idmat, 16 * sizeof(float));
189 void g3d_load_matrix(const float *m)
191 int top = st->mtop[st->mmode];
192 memcpy(st->mat[st->mmode][top], m, 16 * sizeof(float));
195 #define M(i,j) (((i) << 2) + (j))
196 void g3d_mult_matrix(const float *m2)
198 int i, j, top = st->mtop[st->mmode];
200 float *dest = st->mat[st->mmode][top];
202 memcpy(m1, dest, sizeof m1);
206 *dest++ = m1[M(0,j)] * m2[M(i,0)] +
207 m1[M(1,j)] * m2[M(i,1)] +
208 m1[M(2,j)] * m2[M(i,2)] +
209 m1[M(3,j)] * m2[M(i,3)];
214 void g3d_push_matrix(void)
216 int top = st->mtop[st->mmode];
217 if(top >= G3D_NUM_MATRICES) {
218 fprintf(stderr, "g3d_push_matrix overflow\n");
221 memcpy(st->mat[st->mmode][top + 1], st->mat[st->mmode][top], 16 * sizeof(float));
222 st->mtop[st->mmode] = top + 1;
225 void g3d_pop_matrix(void)
227 if(st->mtop[st->mmode] <= 0) {
228 fprintf(stderr, "g3d_pop_matrix underflow\n");
231 --st->mtop[st->mmode];
234 void g3d_translate(float x, float y, float z)
236 float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
243 void g3d_rotate(float deg, float x, float y, float z)
245 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
247 float angle = M_PI * deg / 180.0f;
248 float sina = sin(angle);
249 float cosa = cos(angle);
250 float one_minus_cosa = 1.0f - cosa;
255 m[0] = nxsq + (1.0f - nxsq) * cosa;
256 m[4] = x * y * one_minus_cosa - z * sina;
257 m[8] = x * z * one_minus_cosa + y * sina;
258 m[1] = x * y * one_minus_cosa + z * sina;
259 m[5] = nysq + (1.0 - nysq) * cosa;
260 m[9] = y * z * one_minus_cosa - x * sina;
261 m[2] = x * z * one_minus_cosa - y * sina;
262 m[6] = y * z * one_minus_cosa + x * sina;
263 m[10] = nzsq + (1.0 - nzsq) * cosa;
269 void g3d_scale(float x, float y, float z)
271 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
279 void g3d_ortho(float left, float right, float bottom, float top, float znear, float zfar)
281 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
283 float dx = right - left;
284 float dy = top - bottom;
285 float dz = zfar - znear;
290 m[12] = -(right + left) / dx;
291 m[13] = -(top + bottom) / dy;
292 m[14] = -(zfar + znear) / dz;
297 void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
299 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
301 float dx = right - left;
302 float dy = top - bottom;
305 float a = (right + left) / dx;
306 float b = (top + bottom) / dy;
307 float c = -(fr + nr) / dz;
308 float d = -2.0 * fr * nr / dz;
310 m[0] = 2.0 * nr / dx;
311 m[5] = 2.0 * nr / dy;
321 void g3d_perspective(float vfov_deg, float aspect, float znear, float zfar)
323 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
325 float vfov = M_PI * vfov_deg / 180.0f;
326 float s = 1.0f / tan(vfov * 0.5f);
327 float range = znear - zfar;
331 m[10] = (znear + zfar) / range;
333 m[14] = 2.0f * znear * zfar / range;
338 const float *g3d_get_matrix(int which, float *m)
340 int top = st->mtop[which];
343 memcpy(m, st->mat[which][top], 16 * sizeof(float));
345 return st->mat[which][top];
348 void g3d_light_pos(int idx, float x, float y, float z)
350 int mvtop = st->mtop[G3D_MODELVIEW];
352 st->lt[idx].type = LT_POS;
357 xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &st->lt[idx].x);
360 void g3d_light_dir(int idx, float x, float y, float z)
362 int mvtop = st->mtop[G3D_MODELVIEW];
364 st->lt[idx].type = LT_DIR;
369 /* calc the normal matrix */
370 memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
371 st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
373 xform4_vec3(st->norm_mat, &st->lt[idx].x);
375 NORMALIZE(&st->lt[idx].x);
378 void g3d_light_color(int idx, float r, float g, float b)
385 void g3d_light_ambient(float r, float g, float b)
392 void g3d_mtl_diffuse(float r, float g, float b)
399 void g3d_mtl_specular(float r, float g, float b)
406 void g3d_mtl_shininess(float shin)
411 static INLINE int calc_shift(unsigned int x)
421 static INLINE int calc_mask(unsigned int x)
426 void g3d_set_texture(int xsz, int ysz, void *pixels)
428 pfill_tex.pixels = pixels;
429 pfill_tex.width = xsz;
430 pfill_tex.height = ysz;
432 pfill_tex.xshift = calc_shift(xsz);
433 pfill_tex.yshift = calc_shift(ysz);
434 pfill_tex.xmask = calc_mask(xsz);
435 pfill_tex.ymask = calc_mask(ysz);
438 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
440 g3d_draw_indexed(prim, varr, varr_size, 0, 0);
443 void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
444 const uint16_t *iarr, int iarr_size)
446 int i, j, vnum, nfaces, fill_mode;
447 struct pvertex pv[16];
448 struct g3d_vertex v[16];
449 int mvtop = st->mtop[G3D_MODELVIEW];
450 int ptop = st->mtop[G3D_PROJECTION];
451 struct g3d_vertex *tmpv;
453 tmpv = alloca(prim * 6 * sizeof *tmpv);
455 /* calc the normal matrix */
456 memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
457 st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
459 nfaces = (iarr ? iarr_size : varr_size) / prim;
461 for(j=0; j<nfaces; j++) {
462 vnum = prim; /* reset vnum for each iteration */
464 for(i=0; i<vnum; i++) {
465 v[i] = iarr ? varr[*iarr++] : *varr++;
467 xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
468 xform3_vec3(st->norm_mat, &v[i].nx);
470 if(st->opt & G3D_LIGHTING) {
473 if(st->opt & G3D_TEXTURE_GEN) {
474 v[i].u = v[i].nx * 0.5 + 0.5;
475 v[i].v = v[i].ny * 0.5 + 0.5;
477 if(st->opt & G3D_TEXTURE_MAT) {
478 float *mat = st->mat[G3D_TEXTURE][st->mtop[G3D_TEXTURE]];
479 float x = mat[0] * v[i].u + mat[4] * v[i].v + mat[12];
480 float y = mat[1] * v[i].u + mat[5] * v[i].v + mat[13];
481 float w = mat[3] * v[i].u + mat[7] * v[i].v + mat[15];
485 xform4_vec3(st->mat[G3D_PROJECTION][ptop], &v[i].x);
489 if(st->opt & G3D_CLIP_FRUSTUM) {
491 memcpy(tmpv, v, vnum * sizeof *v);
493 if(clip_frustum(v, &vnum, tmpv, vnum, i) < 0) {
494 /* polygon completely outside of view volume. discard */
503 for(i=0; i<vnum; i++) {
507 /*v[i].z /= v[i].w;*/
510 /* viewport transformation */
511 v[i].x = (v[i].x * 0.5f + 0.5f) * (float)st->vport[2] + st->vport[0];
512 v[i].y = (0.5f - v[i].y * 0.5f) * (float)st->vport[3] + st->vport[1];
514 /* convert pos to 24.8 fixed point */
515 pv[i].x = cround64(v[i].x * 256.0f);
516 pv[i].y = cround64(v[i].y * 256.0f);
517 /* convert tex coords to 16.16 fixed point */
518 pv[i].u = cround64(v[i].u * 65536.0f);
519 pv[i].v = cround64(v[i].v * 65536.0f);
520 /* pass the color through as is */
527 /* backface culling */
528 if(vnum > 2 && st->opt & G3D_CULL_FACE) {
529 int32_t ax = pv[1].x - pv[0].x;
530 int32_t ay = pv[1].y - pv[0].y;
531 int32_t bx = pv[2].x - pv[0].x;
532 int32_t by = pv[2].y - pv[0].y;
533 int32_t cross_z = (ax >> 4) * (by >> 4) - (ay >> 4) * (bx >> 4);
534 int sign = (cross_z >> 31) & 1;
536 if(!(sign ^ st->frontface)) {
537 continue; /* back-facing */
543 if(st->opt & G3D_BLEND) {
545 int inv_alpha = 255 - pv[0].a;
546 g3d_pixel *dest = st->pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
547 r = ((int)pv[0].r * pv[0].a + G3D_UNPACK_R(*dest) * inv_alpha) >> 8;
548 g = ((int)pv[0].g * pv[0].a + G3D_UNPACK_G(*dest) * inv_alpha) >> 8;
549 b = ((int)pv[0].b * pv[0].a + G3D_UNPACK_B(*dest) * inv_alpha) >> 8;
550 *dest++ = G3D_PACK_RGB(r, g, b);
552 g3d_pixel *dest = st->pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
553 *dest = G3D_PACK_RGB(pv[0].r, pv[0].g, pv[0].b);
559 g3d_pixel col = G3D_PACK_RGB(pv[0].r, pv[0].g, pv[0].b);
560 draw_line(pv[0].x >> 8, pv[0].y >> 8, pv[1].x >> 8, pv[1].y >> 8, col);
565 fill_mode = st->polymode;
566 if(st->opt & G3D_TEXTURE_2D) {
567 fill_mode |= POLYFILL_TEX_BIT;
569 if(st->opt & G3D_BLEND) {
570 fill_mode |= POLYFILL_BLEND_BIT;
572 polyfill(fill_mode, pv, vnum);
577 void g3d_begin(int prim)
580 st->imm_pcount = prim;
589 static void imm_flush(void)
591 int numv = st->imm_numv;
593 g3d_draw_indexed(st->imm_prim, st->imm_vbuf, numv, 0, 0);
596 void g3d_vertex(float x, float y, float z)
598 struct g3d_vertex *vptr = st->imm_vbuf + st->imm_numv++;
599 *vptr = st->imm_curv;
605 if(!--st->imm_pcount) {
606 if(st->imm_numv >= IMM_VBUF_SIZE - st->imm_prim) {
609 st->imm_pcount = st->imm_prim;
613 void g3d_normal(float x, float y, float z)
620 #define CLAMP(x, a, b) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
621 #define MIN(a, b) ((a) < (b) ? (a) : (b))
623 void g3d_color3b(unsigned char r, unsigned char g, unsigned char b)
625 st->imm_curv.r = MIN(r, 255);
626 st->imm_curv.g = MIN(g, 255);
627 st->imm_curv.b = MIN(b, 255);
628 st->imm_curv.a = 255;
631 void g3d_color4b(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
633 st->imm_curv.r = MIN(r, 255);
634 st->imm_curv.g = MIN(g, 255);
635 st->imm_curv.b = MIN(b, 255);
636 st->imm_curv.a = MIN(a, 255);
639 void g3d_color3f(float r, float g, float b)
644 st->imm_curv.r = CLAMP(ir, 0, 255);
645 st->imm_curv.g = CLAMP(ig, 0, 255);
646 st->imm_curv.b = CLAMP(ib, 0, 255);
647 st->imm_curv.a = 255;
650 void g3d_color4f(float r, float g, float b, float a)
656 st->imm_curv.r = CLAMP(ir, 0, 255);
657 st->imm_curv.g = CLAMP(ig, 0, 255);
658 st->imm_curv.b = CLAMP(ib, 0, 255);
659 st->imm_curv.a = CLAMP(ia, 0, 255);
662 void g3d_texcoord(float u, float v)
668 static __inline void xform4_vec3(const float *mat, float *vec)
670 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
671 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
672 float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
673 vec[3] = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
679 static __inline void xform3_vec3(const float *mat, float *vec)
681 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2];
682 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2];
683 vec[2] = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2];
688 static void shade(struct g3d_vertex *v)
693 color[0] = st->ambient[0] * st->mtl.kd[0];
694 color[1] = st->ambient[1] * st->mtl.kd[1];
695 color[2] = st->ambient[2] * st->mtl.kd[2];
697 for(i=0; i<MAX_LIGHTS; i++) {
701 if(!(st->opt & (G3D_LIGHT0 << i))) {
705 ldir[0] = st->lt[i].x;
706 ldir[1] = st->lt[i].y;
707 ldir[2] = st->lt[i].z;
709 if(st->lt[i].type != LT_DIR) {
716 if((ndotl = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
720 color[0] += st->mtl.kd[0] * st->lt[i].r * ndotl;
721 color[1] += st->mtl.kd[1] * st->lt[i].g * ndotl;
722 color[2] += st->mtl.kd[2] * st->lt[i].b * ndotl;
725 if(st->opt & G3D_SPECULAR) {
729 if((ndoth = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
732 ndoth = pow(ndoth, st->mtl.shin);
734 color[0] += st->mtl.ks[0] * st->lt[i].r * ndoth;
735 color[1] += st->mtl.ks[1] * st->lt[i].g * ndoth;
736 color[2] += st->mtl.ks[2] * st->lt[i].b * ndoth;
741 r = cround64(color[0] * 255.0);
742 g = cround64(color[1] * 255.0);
743 b = cround64(color[2] * 255.0);
745 v->r = r > 255 ? 255 : r;
746 v->g = g > 255 ? 255 : g;
747 v->b = b > 255 ? 255 : b;