7 #if defined(__WATCOMC__) || defined(_WIN32) || defined(__DJGPP__)
21 typedef float g3d_matrix[16];
25 #define IMM_VBUF_SIZE 256
27 #define NORMALIZE(v) \
29 float len = sqrt((v)[0] * (v)[0] + (v)[1] * (v)[1] + (v)[2] * (v)[2]); \
31 float s = 1.0 / len; \
38 enum {LT_POS, LT_DIR};
56 g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
57 int mtop[G3D_NUM_MATRICES];
63 struct light lt[MAX_LIGHTS];
73 int imm_numv, imm_pcount;
74 struct g3d_vertex imm_curv;
75 struct g3d_vertex imm_vbuf[IMM_VBUF_SIZE];
78 static void imm_flush(void);
79 static __inline void xform4_vec3(const float *mat, float *vec);
80 static __inline void xform3_vec3(const float *mat, float *vec);
81 static void shade(struct g3d_vertex *v);
83 static struct g3d_state *st;
84 static const float idmat[] = {
93 if(!(st = malloc(sizeof *st))) {
94 fprintf(stderr, "failed to allocate G3D context\n");
102 void g3d_destroy(void)
111 memset(st, 0, sizeof *st);
113 st->opt = G3D_CLIP_FRUSTUM;
114 st->polymode = POLYFILL_FLAT;
116 for(i=0; i<G3D_NUM_MATRICES; i++) {
121 for(i=0; i<MAX_LIGHTS; i++) {
122 g3d_light_color(i, 1, 1, 1);
124 g3d_light_ambient(0.1, 0.1, 0.1);
126 g3d_mtl_diffuse(1, 1, 1);
129 void g3d_framebuffer(int width, int height, void *pixels)
131 static int prev_height;
133 if(height > prev_height) {
134 polyfill_fbheight(height);
141 pfill_fb.pixels = pixels;
142 pfill_fb.width = width;
143 pfill_fb.height = height;
145 g3d_viewport(0, 0, width, height);
148 /* set the framebuffer pointer, without resetting the size */
149 void g3d_framebuffer_addr(void *pixels)
152 pfill_fb.pixels = pixels;
155 void g3d_viewport(int x, int y, int w, int h)
163 void g3d_enable(unsigned int opt)
168 void g3d_disable(unsigned int opt)
173 void g3d_setopt(unsigned int opt, unsigned int mask)
175 st->opt = (st->opt & ~mask) | (opt & mask);
178 unsigned int g3d_getopt(unsigned int mask)
180 return st->opt & mask;
183 void g3d_front_face(unsigned int order)
185 st->frontface = order;
188 void g3d_polygon_mode(int pmode)
190 st->polymode = pmode;
193 int g3d_get_polygon_mode(void)
198 void g3d_matrix_mode(int mmode)
203 void g3d_load_identity(void)
205 int top = st->mtop[st->mmode];
206 memcpy(st->mat[st->mmode][top], idmat, 16 * sizeof(float));
209 void g3d_load_matrix(const float *m)
211 int top = st->mtop[st->mmode];
212 memcpy(st->mat[st->mmode][top], m, 16 * sizeof(float));
215 #define M(i,j) (((i) << 2) + (j))
216 void g3d_mult_matrix(const float *m2)
218 int i, j, top = st->mtop[st->mmode];
220 float *dest = st->mat[st->mmode][top];
222 memcpy(m1, dest, sizeof m1);
226 *dest++ = m1[M(0,j)] * m2[M(i,0)] +
227 m1[M(1,j)] * m2[M(i,1)] +
228 m1[M(2,j)] * m2[M(i,2)] +
229 m1[M(3,j)] * m2[M(i,3)];
234 void g3d_push_matrix(void)
236 int top = st->mtop[st->mmode];
237 if(top >= G3D_NUM_MATRICES) {
238 fprintf(stderr, "g3d_push_matrix overflow\n");
241 memcpy(st->mat[st->mmode][top + 1], st->mat[st->mmode][top], 16 * sizeof(float));
242 st->mtop[st->mmode] = top + 1;
245 void g3d_pop_matrix(void)
247 if(st->mtop[st->mmode] <= 0) {
248 fprintf(stderr, "g3d_pop_matrix underflow\n");
251 --st->mtop[st->mmode];
254 void g3d_translate(float x, float y, float z)
256 float m[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
263 void g3d_rotate(float deg, float x, float y, float z)
267 float angle = M_PI * deg / 180.0f;
268 float sina = sin(angle);
269 float cosa = cos(angle);
270 float one_minus_cosa = 1.0f - cosa;
275 m[0] = nxsq + (1.0f - nxsq) * cosa;
276 m[4] = x * y * one_minus_cosa - z * sina;
277 m[8] = x * z * one_minus_cosa + y * sina;
278 m[1] = x * y * one_minus_cosa + z * sina;
279 m[5] = nysq + (1.0 - nysq) * cosa;
280 m[9] = y * z * one_minus_cosa - x * sina;
281 m[2] = x * z * one_minus_cosa - y * sina;
282 m[6] = y * z * one_minus_cosa + x * sina;
283 m[10] = nzsq + (1.0 - nzsq) * cosa;
289 void g3d_scale(float x, float y, float z)
299 void g3d_ortho(float left, float right, float bottom, float top, float znear, float zfar)
303 float dx = right - left;
304 float dy = top - bottom;
305 float dz = zfar - znear;
310 m[12] = -(right + left) / dx;
311 m[13] = -(top + bottom) / dy;
312 m[14] = -(zfar + znear) / dz;
318 void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
322 float dx = right - left;
323 float dy = top - bottom;
326 float a = (right + left) / dx;
327 float b = (top + bottom) / dy;
328 float c = -(fr + nr) / dz;
329 float d = -2.0 * fr * nr / dz;
331 m[0] = 2.0 * nr / dx;
332 m[5] = 2.0 * nr / dy;
342 void g3d_perspective(float vfov_deg, float aspect, float znear, float zfar)
346 float vfov = M_PI * vfov_deg / 180.0f;
347 float s = 1.0f / tan(vfov * 0.5f);
348 float range = znear - zfar;
352 m[10] = (znear + zfar) / range;
354 m[14] = 2.0f * znear * zfar / range;
359 const float *g3d_get_matrix(int which, float *m)
361 int top = st->mtop[which];
364 memcpy(m, st->mat[which][top], 16 * sizeof(float));
366 return st->mat[which][top];
369 void g3d_light_pos(int idx, float x, float y, float z)
371 int mvtop = st->mtop[G3D_MODELVIEW];
373 st->lt[idx].type = LT_POS;
378 xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &st->lt[idx].x);
381 void g3d_light_dir(int idx, float x, float y, float z)
383 int mvtop = st->mtop[G3D_MODELVIEW];
385 st->lt[idx].type = LT_DIR;
390 /* calc the normal matrix */
391 memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
392 st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
394 xform4_vec3(st->norm_mat, &st->lt[idx].x);
396 NORMALIZE(&st->lt[idx].x);
399 void g3d_light_color(int idx, float r, float g, float b)
406 void g3d_light_ambient(float r, float g, float b)
413 void g3d_mtl_diffuse(float r, float g, float b)
420 void g3d_mtl_specular(float r, float g, float b)
427 void g3d_mtl_shininess(float shin)
432 static INLINE int calc_shift(unsigned int x)
442 static INLINE int calc_mask(unsigned int x)
447 void g3d_set_texture(int xsz, int ysz, void *pixels)
449 pfill_tex.pixels = pixels;
450 pfill_tex.width = xsz;
451 pfill_tex.height = ysz;
453 pfill_tex.xshift = calc_shift(xsz);
454 pfill_tex.yshift = calc_shift(ysz);
455 pfill_tex.xmask = calc_mask(xsz);
456 pfill_tex.ymask = calc_mask(ysz);
459 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
461 g3d_draw_indexed(prim, varr, varr_size, 0, 0);
464 void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
465 const uint16_t *iarr, int iarr_size)
467 int i, j, vnum, nfaces, fill_mode;
468 struct pvertex pv[16];
469 struct g3d_vertex v[16];
470 int mvtop = st->mtop[G3D_MODELVIEW];
471 int ptop = st->mtop[G3D_PROJECTION];
472 struct g3d_vertex *tmpv;
474 tmpv = alloca(prim * 6 * sizeof *tmpv);
476 /* calc the normal matrix */
477 memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
478 st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
480 nfaces = (iarr ? iarr_size : varr_size) / prim;
482 for(j=0; j<nfaces; j++) {
483 vnum = prim; /* reset vnum for each iteration */
485 for(i=0; i<vnum; i++) {
486 v[i] = iarr ? varr[*iarr++] : *varr++;
488 xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
489 xform3_vec3(st->norm_mat, &v[i].nx);
491 if(st->opt & G3D_LIGHTING) {
494 if(st->opt & G3D_TEXTURE_GEN) {
495 v[i].u = v[i].nx * 0.5 + 0.5;
496 v[i].v = 0.5 - v[i].ny * 0.5;
498 if(st->opt & G3D_TEXTURE_MAT) {
499 float *mat = st->mat[G3D_TEXTURE][st->mtop[G3D_TEXTURE]];
500 float x = mat[0] * v[i].u + mat[4] * v[i].v + mat[12];
501 float y = mat[1] * v[i].u + mat[5] * v[i].v + mat[13];
502 float w = mat[3] * v[i].u + mat[7] * v[i].v + mat[15];
506 xform4_vec3(st->mat[G3D_PROJECTION][ptop], &v[i].x);
510 if(st->opt & G3D_CLIP_FRUSTUM) {
512 memcpy(tmpv, v, vnum * sizeof *v);
514 if(clip_frustum(v, &vnum, tmpv, vnum, i) < 0) {
515 /* polygon completely outside of view volume. discard */
524 for(i=0; i<vnum; i++) {
528 /*v[i].z /= v[i].w;*/
531 /* viewport transformation */
532 v[i].x = (v[i].x * 0.5f + 0.5f) * (float)st->vport[2] + st->vport[0];
533 v[i].y = (0.5f - v[i].y * 0.5f) * (float)st->vport[3] + st->vport[1];
535 /* convert pos to 24.8 fixed point */
536 pv[i].x = cround64(v[i].x * 256.0f);
537 pv[i].y = cround64(v[i].y * 256.0f);
538 /* convert tex coords to 16.16 fixed point */
539 pv[i].u = cround64(v[i].u * 65536.0f);
540 pv[i].v = cround64(v[i].v * 65536.0f);
541 /* pass the color through as is */
548 /* backface culling */
549 if(vnum > 2 && st->opt & G3D_CULL_FACE) {
550 int32_t ax = pv[1].x - pv[0].x;
551 int32_t ay = pv[1].y - pv[0].y;
552 int32_t bx = pv[2].x - pv[0].x;
553 int32_t by = pv[2].y - pv[0].y;
554 int32_t cross_z = (ax >> 4) * (by >> 4) - (ay >> 4) * (bx >> 4);
555 int sign = (cross_z >> 31) & 1;
557 if(!(sign ^ st->frontface)) {
558 continue; /* back-facing */
564 if(st->opt & (G3D_ALPHA_BLEND | G3D_ADD_BLEND)) {
565 int r, g, b, inv_alpha;
566 g3d_pixel *dest = st->pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
567 if(st->opt & G3D_ALPHA_BLEND) {
568 inv_alpha = 255 - pv[0].a;
569 r = ((int)pv[0].r * pv[0].a + G3D_UNPACK_R(*dest) * inv_alpha) >> 8;
570 g = ((int)pv[0].g * pv[0].a + G3D_UNPACK_G(*dest) * inv_alpha) >> 8;
571 b = ((int)pv[0].b * pv[0].a + G3D_UNPACK_B(*dest) * inv_alpha) >> 8;
573 r = (int)pv[0].r + G3D_UNPACK_R(*dest);
574 g = (int)pv[0].g + G3D_UNPACK_G(*dest);
575 b = (int)pv[0].b + G3D_UNPACK_B(*dest);
580 *dest++ = G3D_PACK_RGB(r, g, b);
582 g3d_pixel *dest = st->pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
583 *dest = G3D_PACK_RGB(pv[0].r, pv[0].g, pv[0].b);
589 g3d_pixel col = G3D_PACK_RGB(pv[0].r, pv[0].g, pv[0].b);
590 draw_line(pv[0].x >> 8, pv[0].y >> 8, pv[1].x >> 8, pv[1].y >> 8, col);
595 fill_mode = st->polymode;
596 if(st->opt & G3D_TEXTURE_2D) {
597 fill_mode |= POLYFILL_TEX_BIT;
599 if(st->opt & G3D_ALPHA_BLEND) {
600 fill_mode |= POLYFILL_ALPHA_BIT;
601 } else if(st->opt & G3D_ADD_BLEND) {
602 fill_mode |= POLYFILL_ADD_BIT;
604 polyfill(fill_mode, pv, vnum);
609 void g3d_begin(int prim)
612 st->imm_pcount = prim;
621 static void imm_flush(void)
623 int numv = st->imm_numv;
625 g3d_draw_indexed(st->imm_prim, st->imm_vbuf, numv, 0, 0);
628 void g3d_vertex(float x, float y, float z)
630 struct g3d_vertex *vptr = st->imm_vbuf + st->imm_numv++;
631 *vptr = st->imm_curv;
637 if(!--st->imm_pcount) {
638 if(st->imm_numv >= IMM_VBUF_SIZE - st->imm_prim) {
641 st->imm_pcount = st->imm_prim;
645 void g3d_normal(float x, float y, float z)
652 #define CLAMP(x, a, b) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
653 #define MIN(a, b) ((a) < (b) ? (a) : (b))
655 void g3d_color3b(unsigned char r, unsigned char g, unsigned char b)
657 st->imm_curv.r = MIN(r, 255);
658 st->imm_curv.g = MIN(g, 255);
659 st->imm_curv.b = MIN(b, 255);
660 st->imm_curv.a = 255;
663 void g3d_color4b(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
665 st->imm_curv.r = MIN(r, 255);
666 st->imm_curv.g = MIN(g, 255);
667 st->imm_curv.b = MIN(b, 255);
668 st->imm_curv.a = MIN(a, 255);
671 void g3d_color3f(float r, float g, float b)
676 st->imm_curv.r = CLAMP(ir, 0, 255);
677 st->imm_curv.g = CLAMP(ig, 0, 255);
678 st->imm_curv.b = CLAMP(ib, 0, 255);
679 st->imm_curv.a = 255;
682 void g3d_color4f(float r, float g, float b, float a)
688 st->imm_curv.r = CLAMP(ir, 0, 255);
689 st->imm_curv.g = CLAMP(ig, 0, 255);
690 st->imm_curv.b = CLAMP(ib, 0, 255);
691 st->imm_curv.a = CLAMP(ia, 0, 255);
694 void g3d_texcoord(float u, float v)
700 static __inline void xform4_vec3(const float *mat, float *vec)
702 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
703 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
704 float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
705 vec[3] = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
711 static __inline void xform3_vec3(const float *mat, float *vec)
713 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2];
714 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2];
715 vec[2] = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2];
720 static void shade(struct g3d_vertex *v)
725 color[0] = st->ambient[0] * st->mtl.kd[0];
726 color[1] = st->ambient[1] * st->mtl.kd[1];
727 color[2] = st->ambient[2] * st->mtl.kd[2];
729 for(i=0; i<MAX_LIGHTS; i++) {
733 if(!(st->opt & (G3D_LIGHT0 << i))) {
737 ldir[0] = st->lt[i].x;
738 ldir[1] = st->lt[i].y;
739 ldir[2] = st->lt[i].z;
741 if(st->lt[i].type != LT_DIR) {
748 if((ndotl = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
752 color[0] += st->mtl.kd[0] * st->lt[i].r * ndotl;
753 color[1] += st->mtl.kd[1] * st->lt[i].g * ndotl;
754 color[2] += st->mtl.kd[2] * st->lt[i].b * ndotl;
757 if(st->opt & G3D_SPECULAR) {
761 if((ndoth = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
764 ndoth = pow(ndoth, st->mtl.shin);
766 color[0] += st->mtl.ks[0] * st->lt[i].r * ndoth;
767 color[1] += st->mtl.ks[1] * st->lt[i].g * ndoth;
768 color[2] += st->mtl.ks[2] * st->lt[i].b * ndoth;
773 r = cround64(color[0] * 255.0);
774 g = cround64(color[1] * 255.0);
775 b = cround64(color[2] * 255.0);
777 v->r = r > 255 ? 255 : r;
778 v->g = g > 255 ? 255 : g;
779 v->b = b > 255 ? 255 : b;
782 #endif /* !def BUILD_OPENGL */