17 #undef CORRECT_NORMAL_MATRIX
18 #ifdef CORRECT_NORMAL_MATRIX
19 #include <cgmath/cgmath.h>
22 #define ENABLE_ZBUFFER
25 typedef float g3d_matrix[16];
29 #define IMM_VBUF_SIZE 256
31 #define NORMALIZE(v) \
33 float len = sqrt((v)[0] * (v)[0] + (v)[1] * (v)[1] + (v)[2] * (v)[2]); \
35 float s = 1.0 / len; \
42 enum {LT_POS, LT_DIR};
59 g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
60 int mtop[G3D_NUM_MATRICES];
66 struct light lt[MAX_LIGHTS];
74 g3d_pixel clear_color;
79 int imm_numv, imm_pcount;
80 struct g3d_vertex imm_curv;
81 struct g3d_vertex imm_vbuf[IMM_VBUF_SIZE];
84 static void calc_grad(struct g3d_vertex *v);
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);
91 static struct g3d_state *st;
92 static const float idmat[] = {
101 if(!(st = calloc(1, sizeof *st))) {
102 fprintf(stderr, "failed to allocate G3D context\n");
110 void g3d_destroy(void)
112 #ifdef ENABLE_ZBUFFER
122 #ifdef ENABLE_ZBUFFER
125 memset(st, 0, sizeof *st);
127 st->opt = G3D_CLIP_FRUSTUM;
128 st->polymode = POLYFILL_FLAT;
130 for(i=0; i<G3D_NUM_MATRICES; i++) {
135 for(i=0; i<MAX_LIGHTS; i++) {
136 g3d_light_dir(i, 0, 0, 1);
137 g3d_light_energy(i, 1);
139 g3d_light_ambient(0.1);
143 st->clear_depth = 0xffffff;
146 void g3d_framebuffer(int width, int height, void *pixels)
148 static int max_height;
150 #ifdef ENABLE_ZBUFFER
151 static int max_npixels;
152 int npixels = width * height;
154 if(npixels > max_npixels) {
156 pfill_zbuf = malloc(npixels * sizeof *pfill_zbuf);
157 max_npixels = npixels;
161 if(height > max_height) {
162 polyfill_fbheight(height);
169 pfill_fb.pixels = pixels;
170 pfill_fb.width = width;
171 pfill_fb.height = height;
173 g3d_viewport(0, 0, width, height);
176 /* set the framebuffer pointer, without resetting the size */
177 void g3d_framebuffer_addr(void *pixels)
179 pfill_fb.pixels = pixels;
182 void g3d_viewport(int x, int y, int w, int h)
190 void g3d_clear_color(unsigned char r, unsigned char g, unsigned char b)
192 st->clear_color = find_color(r, g, b);
195 void g3d_clear_depth(float z)
197 int iz = (int)(z * (float)0xffffff);
199 if(iz > 0xffffff) iz = 0xffffff;
200 st->clear_depth = iz;
203 void g3d_clear(unsigned int mask)
205 if(mask & G3D_COLOR_BUFFER_BIT) {
206 memset(pfill_fb.pixels, st->clear_color, pfill_fb.width * pfill_fb.height);
208 if(mask & G3D_DEPTH_BUFFER_BIT) {
209 memset32(pfill_zbuf, st->clear_depth, pfill_fb.width * pfill_fb.height);
213 void g3d_enable(unsigned int opt)
218 void g3d_disable(unsigned int opt)
223 void g3d_setopt(unsigned int opt, unsigned int mask)
225 st->opt = (st->opt & ~mask) | (opt & mask);
228 unsigned int g3d_getopt(unsigned int mask)
230 return st->opt & mask;
233 void g3d_front_face(unsigned int order)
235 st->frontface = order;
238 void g3d_polygon_mode(int pmode)
240 st->polymode = pmode;
243 int g3d_get_polygon_mode(void)
248 void g3d_matrix_mode(int mmode)
253 void g3d_load_identity(void)
255 int top = st->mtop[st->mmode];
256 memcpy(st->mat[st->mmode][top], idmat, 16 * sizeof(float));
259 void g3d_load_matrix(const float *m)
261 int top = st->mtop[st->mmode];
262 memcpy(st->mat[st->mmode][top], m, 16 * sizeof(float));
265 #define M(i,j) (((i) << 2) + (j))
266 void g3d_mult_matrix(const float *m2)
268 int i, j, top = st->mtop[st->mmode];
270 float *dest = st->mat[st->mmode][top];
272 memcpy(m1, dest, sizeof m1);
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)];
284 void g3d_push_matrix(void)
286 int top = st->mtop[st->mmode];
287 if(top >= STACK_SIZE) {
288 fprintf(stderr, "g3d_push_matrix overflow\n");
291 memcpy(st->mat[st->mmode][top + 1], st->mat[st->mmode][top], 16 * sizeof(float));
292 st->mtop[st->mmode] = top + 1;
295 void g3d_pop_matrix(void)
297 if(st->mtop[st->mmode] <= 0) {
298 fprintf(stderr, "g3d_pop_matrix underflow\n");
301 --st->mtop[st->mmode];
304 void g3d_translate(float x, float y, float z)
306 float m[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
313 void g3d_rotate(float deg, float x, float y, float z)
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;
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;
339 void g3d_scale(float x, float y, float z)
349 void g3d_ortho(float left, float right, float bottom, float top, float znear, float zfar)
353 float dx = right - left;
354 float dy = top - bottom;
355 float dz = zfar - znear;
360 m[12] = -(right + left) / dx;
361 m[13] = -(top + bottom) / dy;
362 m[14] = -(zfar + znear) / dz;
368 void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
372 float dx = right - left;
373 float dy = top - bottom;
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;
381 m[0] = 2.0 * nr / dx;
382 m[5] = 2.0 * nr / dy;
392 void g3d_perspective(float vfov_deg, float aspect, float znear, float zfar)
396 float vfov = M_PI * vfov_deg / 180.0f;
397 float s = 1.0f / tan(vfov * 0.5f);
398 float range = znear - zfar;
402 m[10] = (znear + zfar) / range;
404 m[14] = 2.0f * znear * zfar / range;
409 const float *g3d_get_matrix(int which, float *m)
411 int top = st->mtop[which];
414 memcpy(m, st->mat[which][top], 16 * sizeof(float));
416 return st->mat[which][top];
419 void g3d_light_pos(int idx, float x, float y, float z)
421 int mvtop = st->mtop[G3D_MODELVIEW];
423 st->lt[idx].type = LT_POS;
428 xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &st->lt[idx].x);
431 void g3d_light_dir(int idx, float x, float y, float z)
433 int mvtop = st->mtop[G3D_MODELVIEW];
435 st->lt[idx].type = LT_DIR;
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);
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;
450 xform4_vec3(st->norm_mat, &st->lt[idx].x);
452 NORMALIZE(&st->lt[idx].x);
455 void g3d_light_energy(int idx, float val)
457 st->lt[idx].energy = val;
460 void g3d_light_ambient(float val)
465 void g3d_mtl_diffuse(float diffuse)
467 st->mtl.kd = diffuse;
470 void g3d_mtl_specular(float spec)
475 void g3d_mtl_shininess(float shin)
480 static inline int calc_shift(unsigned int x)
490 static inline int calc_mask(unsigned int x)
495 void g3d_set_texture(int xsz, int ysz, void *pixels)
497 pfill_tex.pixels = pixels;
498 pfill_tex.width = xsz;
499 pfill_tex.height = ysz;
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);
507 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
509 g3d_draw_indexed(prim, varr, varr_size, 0, 0);
512 #define NEED_NORMALS (st->opt & (G3D_LIGHTING | G3D_TEXTURE_GEN))
514 void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
515 const uint16_t *iarr, int iarr_size)
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;
524 tmpv = alloca(prim * 6 * sizeof *tmpv);
526 /* calc the normal matrix */
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);
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;
538 nfaces = (iarr ? iarr_size : varr_size) / prim;
540 for(j=0; j<nfaces; j++) {
541 vnum = prim; /* reset vnum for each iteration */
543 for(i=0; i<vnum; i++) {
544 v[i] = iarr ? varr[*iarr++] : *varr++;
546 xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
549 xform3_vec3(st->norm_mat, &v[i].nx);
550 if(st->opt & G3D_LIGHTING) {
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;
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];
566 xform4_vec3(st->mat[G3D_PROJECTION][ptop], &v[i].x);
570 if(st->opt & G3D_CLIP_FRUSTUM) {
572 memcpy(tmpv, v, vnum * sizeof *v);
574 if(clip_frustum(v, &vnum, tmpv, vnum, i) < 0) {
575 /* polygon completely outside of view volume. discard */
584 for(i=0; i<vnum; i++) {
588 #ifdef ENABLE_ZBUFFER
589 if(st->opt & G3D_DEPTH_TEST) {
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];
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);
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 */
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;
625 if(!(sign ^ st->frontface)) {
626 continue; /* back-facing */
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;
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);
650 *dest++ = G3D_PACK_RGB(r, g, b);
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);
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);
666 fill_mode = st->polymode;
667 if(st->opt & G3D_TEXTURE_2D) {
668 fill_mode |= POLYFILL_TEX_BIT;
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;
677 #ifdef ENABLE_ZBUFFER
678 if(st->opt & G3D_DEPTH_TEST) {
679 fill_mode |= POLYFILL_ZBUF_BIT;
687 polyfill(fill_mode, pvtri);
688 if(--num_tri == 0) break;
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
703 (dx ? (d##attr##12 * dy02 - d##attr##02 * dy12) / dx : 0)
705 (dy ? (d##attr##12 * dx02 - d##attr##02 * dx12) / dy : 0)
707 static void calc_grad(struct g3d_vertex *v)
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;
716 float dx = dx12 * dy02 - dx02 * dy12;
717 float dy = dx02 * dy12 - dx12 * dy02;
719 if(st->polymode == POLYFILL_GOURAUD) {
721 pgrad.dldx = cround64(DFDX(l) * 4096.0f);
722 pgrad.dldy = cround64(DFDY(l) * 4096.0f);
723 if(st->opt & G3D_ALPHA_BLEND) {
725 pgrad.dadx = cround64(DFDX(a) * 4096.0f);
726 pgrad.dady = cround64(DFDY(a) * 4096.0f);
730 if(st->opt & G3D_DEPTH_TEST) {
732 pgrad.dzdx = cround64(DFDX(z) * 8388607.5f);
733 pgrad.dzdy = cround64(DFDY(z) * 8388607.5f);
736 if(st->opt & G3D_TEXTURE_2D) {
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);
746 void g3d_begin(int prim)
749 st->imm_pcount = prim;
758 static void imm_flush(void)
760 int numv = st->imm_numv;
762 g3d_draw_indexed(st->imm_prim, st->imm_vbuf, numv, 0, 0);
765 void g3d_vertex(float x, float y, float z)
767 struct g3d_vertex *vptr = st->imm_vbuf + st->imm_numv++;
768 *vptr = st->imm_curv;
774 if(!--st->imm_pcount) {
775 if(st->imm_numv >= IMM_VBUF_SIZE - st->imm_prim) {
778 st->imm_pcount = st->imm_prim;
782 void g3d_normal(float x, float y, float z)
789 #define CLAMP(x, a, b) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
790 #define MIN(a, b) ((a) < (b) ? (a) : (b))
792 void g3d_color1b(unsigned char lum)
794 st->imm_curv.l = MIN(lum, 255);
795 st->imm_curv.a = 255;
798 void g3d_color2b(unsigned char lum, unsigned char a)
800 st->imm_curv.l = MIN(lum, 255);
801 st->imm_curv.a = MIN(a, 255);
804 void g3d_color1f(float lum)
806 int ilum = lum * 255.0f;
807 st->imm_curv.l = CLAMP(ilum, 0, 255);
808 st->imm_curv.a = 255;
811 void g3d_color2f(float lum, float a)
813 int ilum = lum * 255.0f;
815 st->imm_curv.l = CLAMP(ilum, 0, 255);
816 st->imm_curv.a = CLAMP(ia, 0, 255);
819 void g3d_texcoord(float u, float v)
825 static __inline void xform4_vec3(const float *mat, float *vec)
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];
836 static __inline void xform3_vec3(const float *mat, float *vec)
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];
845 static void shade(struct g3d_vertex *v)
850 lum = st->ambient * st->mtl.kd;
852 for(i=0; i<MAX_LIGHTS; i++) {
856 if(!(st->opt & (G3D_LIGHT0 << i))) {
860 ldir[0] = st->lt[i].x;
861 ldir[1] = st->lt[i].y;
862 ldir[2] = st->lt[i].z;
864 if(st->lt[i].type != LT_DIR) {
871 if((ndotl = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
875 lum += st->mtl.kd * st->lt[i].energy * ndotl;
877 if(st->opt & G3D_SPECULAR) {
881 if((ndoth = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
884 ndoth = pow(ndoth, st->mtl.shin);
886 lum += st->mtl.ks * st->lt[i].energy * ndoth;
890 ilum = cround64(lum * 255.0);
892 v->l = ilum > 255 ? 255 : ilum;
895 #endif /* !def BUILD_OPENGL */