6 #if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__DJGPP__)
20 typedef float g3d_matrix[16];
24 #define IMM_VBUF_SIZE 256
42 g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
43 int mtop[G3D_NUM_MATRICES];
49 struct light lt[MAX_LIGHTS];
59 int imm_numv, imm_pcount;
60 struct g3d_vertex imm_curv;
61 struct g3d_vertex imm_vbuf[IMM_VBUF_SIZE];
64 static void imm_flush(void);
65 static void xform4_vec3(const float *mat, float *vec);
66 static void xform3_vec3(const float *mat, float *vec);
67 static void shade(struct g3d_vertex *v);
69 static struct g3d_state *st;
70 static const float idmat[] = {
81 if(!(st = calloc(1, sizeof *st))) {
82 fprintf(stderr, "failed to allocate G3D context\n");
85 st->opt = G3D_CLIP_FRUSTUM;
86 st->fill_mode = POLYFILL_FLAT;
88 for(i=0; i<G3D_NUM_MATRICES; i++) {
93 for(i=0; i<MAX_LIGHTS; i++) {
94 g3d_light_color(i, 1, 1, 1);
96 g3d_light_ambient(0.1, 0.1, 0.1);
98 g3d_mtl_diffuse(1, 1, 1);
102 void g3d_destroy(void)
107 void g3d_framebuffer(int width, int height, void *pixels)
113 pfill_fb.pixels = pixels;
114 pfill_fb.width = width;
115 pfill_fb.height = height;
117 g3d_viewport(0, 0, width, height);
120 void g3d_viewport(int x, int y, int w, int h)
128 void g3d_enable(unsigned int opt)
133 void g3d_disable(unsigned int opt)
138 void g3d_setopt(unsigned int opt, unsigned int mask)
140 st->opt = (st->opt & ~mask) | (opt & mask);
143 unsigned int g3d_getopt(unsigned int mask)
145 return st->opt & mask;
148 void g3d_front_face(unsigned int order)
150 st->frontface = order;
153 void g3d_polygon_mode(int pmode)
155 st->fill_mode = pmode;
158 void g3d_matrix_mode(int mmode)
163 void g3d_load_identity(void)
165 int top = st->mtop[st->mmode];
166 memcpy(st->mat[st->mmode][top], idmat, 16 * sizeof(float));
169 void g3d_load_matrix(const float *m)
171 int top = st->mtop[st->mmode];
172 memcpy(st->mat[st->mmode][top], m, 16 * sizeof(float));
175 #define M(i,j) (((i) << 2) + (j))
176 void g3d_mult_matrix(const float *m2)
178 int i, j, top = st->mtop[st->mmode];
180 float *dest = st->mat[st->mmode][top];
182 memcpy(m1, dest, sizeof m1);
186 *dest++ = m1[M(0,j)] * m2[M(i,0)] +
187 m1[M(1,j)] * m2[M(i,1)] +
188 m1[M(2,j)] * m2[M(i,2)] +
189 m1[M(3,j)] * m2[M(i,3)];
194 void g3d_push_matrix(void)
196 int top = st->mtop[st->mmode];
197 if(top >= G3D_NUM_MATRICES) {
198 fprintf(stderr, "g3d_push_matrix overflow\n");
201 memcpy(st->mat[st->mmode][top + 1], st->mat[st->mmode][top], 16 * sizeof(float));
202 st->mtop[st->mmode] = top + 1;
205 void g3d_pop_matrix(void)
207 if(st->mtop[st->mmode] <= 0) {
208 fprintf(stderr, "g3d_pop_matrix underflow\n");
211 --st->mtop[st->mmode];
214 void g3d_translate(float x, float y, float z)
216 float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
223 void g3d_rotate(float deg, float x, float y, float z)
225 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
227 float angle = M_PI * deg / 180.0f;
228 float sina = sin(angle);
229 float cosa = cos(angle);
230 float one_minus_cosa = 1.0f - cosa;
235 m[0] = nxsq + (1.0f - nxsq) * cosa;
236 m[4] = x * y * one_minus_cosa - z * sina;
237 m[8] = x * z * one_minus_cosa + y * sina;
238 m[1] = x * y * one_minus_cosa + z * sina;
239 m[5] = nysq + (1.0 - nysq) * cosa;
240 m[9] = y * z * one_minus_cosa - x * sina;
241 m[2] = x * z * one_minus_cosa - y * sina;
242 m[6] = y * z * one_minus_cosa + x * sina;
243 m[10] = nzsq + (1.0 - nzsq) * cosa;
249 void g3d_scale(float x, float y, float z)
251 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
259 void g3d_ortho(float left, float right, float bottom, float top, float znear, float zfar)
261 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
263 float dx = right - left;
264 float dy = top - bottom;
265 float dz = zfar - znear;
270 m[12] = -(right + left) / dx;
271 m[13] = -(top + bottom) / dy;
272 m[14] = -(zfar + znear) / dz;
277 void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
279 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
281 float dx = right - left;
282 float dy = top - bottom;
285 float a = (right + left) / dx;
286 float b = (top + bottom) / dy;
287 float c = -(fr + nr) / dz;
288 float d = -2.0 * fr * nr / dz;
290 m[0] = 2.0 * nr / dx;
291 m[5] = 2.0 * nr / dy;
301 void g3d_perspective(float vfov_deg, float aspect, float znear, float zfar)
303 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
305 float vfov = M_PI * vfov_deg / 180.0f;
306 float s = 1.0f / tan(vfov * 0.5f);
307 float range = znear - zfar;
311 m[10] = (znear + zfar) / range;
313 m[14] = 2.0f * znear * zfar / range;
318 const float *g3d_get_matrix(int which, float *m)
320 int top = st->mtop[which];
323 memcpy(m, st->mat[which][top], 16 * sizeof(float));
325 return st->mat[which][top];
328 void g3d_light_pos(int idx, float x, float y, float z)
330 int mvtop = st->mtop[G3D_MODELVIEW];
336 xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &st->lt[idx].x);
339 void g3d_light_color(int idx, float r, float g, float b)
346 void g3d_light_ambient(float r, float g, float b)
353 void g3d_mtl_diffuse(float r, float g, float b)
360 void g3d_mtl_specular(float r, float g, float b)
367 void g3d_mtl_shininess(float shin)
372 static INLINE int calc_shift(unsigned int x)
382 static INLINE int calc_mask(unsigned int x)
387 void g3d_set_texture(int xsz, int ysz, void *pixels)
389 pfill_tex.pixels = pixels;
390 pfill_tex.width = xsz;
391 pfill_tex.height = ysz;
393 pfill_tex.xshift = calc_shift(xsz);
394 pfill_tex.yshift = calc_shift(ysz);
395 pfill_tex.xmask = calc_mask(xsz);
396 pfill_tex.ymask = calc_mask(ysz);
399 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
401 g3d_draw_indexed(prim, varr, varr_size, 0, 0);
404 void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
405 const uint16_t *iarr, int iarr_size)
407 int i, j, vnum, nfaces;
408 struct pvertex pv[16];
409 struct g3d_vertex v[16];
410 int mvtop = st->mtop[G3D_MODELVIEW];
411 int ptop = st->mtop[G3D_PROJECTION];
412 struct g3d_vertex *tmpv;
414 tmpv = alloca(prim * 6 * sizeof *tmpv);
416 /* calc the normal matrix */
417 memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
418 st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
420 nfaces = (iarr ? iarr_size : varr_size) / prim;
422 for(j=0; j<nfaces; j++) {
423 vnum = prim; /* reset vnum for each iteration */
425 for(i=0; i<vnum; i++) {
426 v[i] = iarr ? varr[*iarr++] : *varr++;
428 xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
429 xform3_vec3(st->norm_mat, &v[i].nx);
431 if(st->opt & G3D_LIGHTING) {
434 if(st->opt & G3D_TEXTURE_GEN) {
435 v[i].u = v[i].nx * 0.5 + 0.5;
436 v[i].v = v[i].ny * 0.5 + 0.5;
438 xform4_vec3(st->mat[G3D_PROJECTION][ptop], &v[i].x);
442 if(st->opt & G3D_CLIP_FRUSTUM) {
444 memcpy(tmpv, v, vnum * sizeof *v);
446 if(clip_frustum(v, &vnum, tmpv, vnum, i) < 0) {
447 /* polygon completely outside of view volume. discard */
456 for(i=0; i<vnum; i++) {
460 /*v[i].z /= v[i].w;*/
463 /* viewport transformation */
464 v[i].x = (v[i].x * 0.5f + 0.5f) * (float)st->vport[2] + st->vport[0];
465 v[i].y = (0.5f - v[i].y * 0.5f) * (float)st->vport[3] + st->vport[1];
467 /* convert pos to 24.8 fixed point */
468 pv[i].x = cround64(v[i].x * 256.0f);
469 pv[i].y = cround64(v[i].y * 256.0f);
470 /* convert tex coords to 16.16 fixed point */
471 pv[i].u = cround64(v[i].u * 65536.0f);
472 pv[i].v = cround64(v[i].v * 65536.0f);
473 /* pass the color through as is */
480 /* backface culling */
481 if(vnum > 2 && st->opt & G3D_CULL_FACE) {
482 int32_t ax = pv[1].x - pv[0].x;
483 int32_t ay = pv[1].y - pv[0].y;
484 int32_t bx = pv[2].x - pv[0].x;
485 int32_t by = pv[2].y - pv[0].y;
486 int32_t cross_z = (ax >> 4) * (by >> 4) - (ay >> 4) * (bx >> 4);
487 int sign = (cross_z >> 31) & 1;
489 if(!(sign ^ st->frontface)) {
490 continue; /* back-facing */
496 if(st->opt & G3D_BLEND) {
498 int inv_alpha = 255 - pv[0].a;
499 uint16_t *dest = st->pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
500 r = ((int)pv[0].r * pv[0].a + UNPACK_R16(*dest) * inv_alpha) >> 8;
501 g = ((int)pv[0].g * pv[0].a + UNPACK_G16(*dest) * inv_alpha) >> 8;
502 b = ((int)pv[0].b * pv[0].a + UNPACK_B16(*dest) * inv_alpha) >> 8;
503 *dest++ = PACK_RGB16(r, g, b);
505 uint16_t *dest = st->pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
506 *dest = PACK_RGB16(pv[0].r, pv[0].g, pv[0].b);
511 /* TODO: draw line */
515 polyfill(st->fill_mode, pv, vnum);
520 void g3d_begin(int prim)
523 st->imm_pcount = prim;
532 static void imm_flush(void)
534 int numv = st->imm_numv;
536 g3d_draw_indexed(st->imm_prim, st->imm_vbuf, numv, 0, 0);
539 void g3d_vertex(float x, float y, float z)
541 struct g3d_vertex *vptr = st->imm_vbuf + st->imm_numv++;
542 *vptr = st->imm_curv;
548 if(!--st->imm_pcount) {
549 if(st->imm_numv >= IMM_VBUF_SIZE - st->imm_prim) {
552 st->imm_pcount = st->imm_prim;
556 void g3d_normal(float x, float y, float z)
563 void g3d_color3b(unsigned char r, unsigned char g, unsigned char b)
568 st->imm_curv.a = 255;
571 void g3d_color4b(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
579 void g3d_color3f(float r, float g, float b)
584 st->imm_curv.r = ir > 255 ? 255 : ir;
585 st->imm_curv.g = ig > 255 ? 255 : ig;
586 st->imm_curv.b = ib > 255 ? 255 : ib;
587 st->imm_curv.a = 255;
590 void g3d_color4f(float r, float g, float b, float a)
596 st->imm_curv.r = ir > 255 ? 255 : ir;
597 st->imm_curv.g = ig > 255 ? 255 : ig;
598 st->imm_curv.b = ib > 255 ? 255 : ib;
599 st->imm_curv.a = ia > 255 ? 255 : ia;
602 void g3d_texcoord(float u, float v)
608 static void xform4_vec3(const float *mat, float *vec)
610 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
611 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
612 float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
613 float w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
621 static void xform3_vec3(const float *mat, float *vec)
623 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2];
624 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2];
625 float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2];
632 #define NORMALIZE(v) \
634 float len = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); \
636 float s = 1.0 / len; \
643 static void shade(struct g3d_vertex *v)
648 color[0] = st->ambient[0] * st->mtl.kd[0];
649 color[1] = st->ambient[1] * st->mtl.kd[1];
650 color[2] = st->ambient[2] * st->mtl.kd[2];
652 for(i=0; i<MAX_LIGHTS; i++) {
656 if(!(st->opt & (G3D_LIGHT0 << i))) {
660 ldir[0] = st->lt[i].x - v->x;
661 ldir[1] = st->lt[i].y - v->y;
662 ldir[2] = st->lt[i].z - v->z;
665 if((ndotl = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
669 color[0] += st->mtl.kd[0] * st->lt[i].r * ndotl;
670 color[1] += st->mtl.kd[1] * st->lt[i].g * ndotl;
671 color[2] += st->mtl.kd[2] * st->lt[i].b * ndotl;
674 r = cround64(color[0] * 255.0);
675 g = cround64(color[1] * 255.0);
676 b = cround64(color[2] * 255.0);
678 v->r = r > 255 ? 255 : r;
679 v->g = g > 255 ? 255 : g;
680 v->b = b > 255 ? 255 : b;