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->polymode = 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 /* set the framebuffer pointer, without resetting the size */
121 void g3d_framebuffer_addr(void *pixels)
124 pfill_fb.pixels = pixels;
127 void g3d_viewport(int x, int y, int w, int h)
135 void g3d_enable(unsigned int opt)
140 void g3d_disable(unsigned int opt)
145 void g3d_setopt(unsigned int opt, unsigned int mask)
147 st->opt = (st->opt & ~mask) | (opt & mask);
150 unsigned int g3d_getopt(unsigned int mask)
152 return st->opt & mask;
155 void g3d_front_face(unsigned int order)
157 st->frontface = order;
160 void g3d_polygon_mode(int pmode)
162 st->polymode = pmode;
165 void g3d_matrix_mode(int mmode)
170 void g3d_load_identity(void)
172 int top = st->mtop[st->mmode];
173 memcpy(st->mat[st->mmode][top], idmat, 16 * sizeof(float));
176 void g3d_load_matrix(const float *m)
178 int top = st->mtop[st->mmode];
179 memcpy(st->mat[st->mmode][top], m, 16 * sizeof(float));
182 #define M(i,j) (((i) << 2) + (j))
183 void g3d_mult_matrix(const float *m2)
185 int i, j, top = st->mtop[st->mmode];
187 float *dest = st->mat[st->mmode][top];
189 memcpy(m1, dest, sizeof m1);
193 *dest++ = m1[M(0,j)] * m2[M(i,0)] +
194 m1[M(1,j)] * m2[M(i,1)] +
195 m1[M(2,j)] * m2[M(i,2)] +
196 m1[M(3,j)] * m2[M(i,3)];
201 void g3d_push_matrix(void)
203 int top = st->mtop[st->mmode];
204 if(top >= G3D_NUM_MATRICES) {
205 fprintf(stderr, "g3d_push_matrix overflow\n");
208 memcpy(st->mat[st->mmode][top + 1], st->mat[st->mmode][top], 16 * sizeof(float));
209 st->mtop[st->mmode] = top + 1;
212 void g3d_pop_matrix(void)
214 if(st->mtop[st->mmode] <= 0) {
215 fprintf(stderr, "g3d_pop_matrix underflow\n");
218 --st->mtop[st->mmode];
221 void g3d_translate(float x, float y, float z)
223 float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
230 void g3d_rotate(float deg, float x, float y, float z)
232 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
234 float angle = M_PI * deg / 180.0f;
235 float sina = sin(angle);
236 float cosa = cos(angle);
237 float one_minus_cosa = 1.0f - cosa;
242 m[0] = nxsq + (1.0f - nxsq) * cosa;
243 m[4] = x * y * one_minus_cosa - z * sina;
244 m[8] = x * z * one_minus_cosa + y * sina;
245 m[1] = x * y * one_minus_cosa + z * sina;
246 m[5] = nysq + (1.0 - nysq) * cosa;
247 m[9] = y * z * one_minus_cosa - x * sina;
248 m[2] = x * z * one_minus_cosa - y * sina;
249 m[6] = y * z * one_minus_cosa + x * sina;
250 m[10] = nzsq + (1.0 - nzsq) * cosa;
256 void g3d_scale(float x, float y, float z)
258 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
266 void g3d_ortho(float left, float right, float bottom, float top, float znear, float zfar)
268 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
270 float dx = right - left;
271 float dy = top - bottom;
272 float dz = zfar - znear;
277 m[12] = -(right + left) / dx;
278 m[13] = -(top + bottom) / dy;
279 m[14] = -(zfar + znear) / dz;
284 void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
286 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
288 float dx = right - left;
289 float dy = top - bottom;
292 float a = (right + left) / dx;
293 float b = (top + bottom) / dy;
294 float c = -(fr + nr) / dz;
295 float d = -2.0 * fr * nr / dz;
297 m[0] = 2.0 * nr / dx;
298 m[5] = 2.0 * nr / dy;
308 void g3d_perspective(float vfov_deg, float aspect, float znear, float zfar)
310 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
312 float vfov = M_PI * vfov_deg / 180.0f;
313 float s = 1.0f / tan(vfov * 0.5f);
314 float range = znear - zfar;
318 m[10] = (znear + zfar) / range;
320 m[14] = 2.0f * znear * zfar / range;
325 const float *g3d_get_matrix(int which, float *m)
327 int top = st->mtop[which];
330 memcpy(m, st->mat[which][top], 16 * sizeof(float));
332 return st->mat[which][top];
335 void g3d_light_pos(int idx, float x, float y, float z)
337 int mvtop = st->mtop[G3D_MODELVIEW];
343 xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &st->lt[idx].x);
346 void g3d_light_color(int idx, float r, float g, float b)
353 void g3d_light_ambient(float r, float g, float b)
360 void g3d_mtl_diffuse(float r, float g, float b)
367 void g3d_mtl_specular(float r, float g, float b)
374 void g3d_mtl_shininess(float shin)
379 static INLINE int calc_shift(unsigned int x)
389 static INLINE int calc_mask(unsigned int x)
394 void g3d_set_texture(int xsz, int ysz, void *pixels)
396 pfill_tex.pixels = pixels;
397 pfill_tex.width = xsz;
398 pfill_tex.height = ysz;
400 pfill_tex.xshift = calc_shift(xsz);
401 pfill_tex.yshift = calc_shift(ysz);
402 pfill_tex.xmask = calc_mask(xsz);
403 pfill_tex.ymask = calc_mask(ysz);
406 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
408 g3d_draw_indexed(prim, varr, varr_size, 0, 0);
411 void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
412 const uint16_t *iarr, int iarr_size)
414 int i, j, vnum, nfaces, fill_mode;
415 struct pvertex pv[16];
416 struct g3d_vertex v[16];
417 int mvtop = st->mtop[G3D_MODELVIEW];
418 int ptop = st->mtop[G3D_PROJECTION];
419 struct g3d_vertex *tmpv;
421 tmpv = alloca(prim * 6 * sizeof *tmpv);
423 /* calc the normal matrix */
424 memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
425 st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
427 nfaces = (iarr ? iarr_size : varr_size) / prim;
429 for(j=0; j<nfaces; j++) {
430 vnum = prim; /* reset vnum for each iteration */
432 for(i=0; i<vnum; i++) {
433 v[i] = iarr ? varr[*iarr++] : *varr++;
435 xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
436 xform3_vec3(st->norm_mat, &v[i].nx);
438 if(st->opt & G3D_LIGHTING) {
441 if(st->opt & G3D_TEXTURE_GEN) {
442 v[i].u = v[i].nx * 0.5 + 0.5;
443 v[i].v = v[i].ny * 0.5 + 0.5;
445 if(st->opt & G3D_TEXTURE_MAT) {
446 float *mat = st->mat[G3D_TEXTURE][st->mtop[G3D_TEXTURE]];
447 float x = mat[0] * v[i].u + mat[4] * v[i].v + mat[12];
448 float y = mat[1] * v[i].u + mat[5] * v[i].v + mat[13];
449 float w = mat[3] * v[i].u + mat[7] * v[i].v + mat[15];
453 xform4_vec3(st->mat[G3D_PROJECTION][ptop], &v[i].x);
457 if(st->opt & G3D_CLIP_FRUSTUM) {
459 memcpy(tmpv, v, vnum * sizeof *v);
461 if(clip_frustum(v, &vnum, tmpv, vnum, i) < 0) {
462 /* polygon completely outside of view volume. discard */
471 for(i=0; i<vnum; i++) {
475 /*v[i].z /= v[i].w;*/
478 /* viewport transformation */
479 v[i].x = (v[i].x * 0.5f + 0.5f) * (float)st->vport[2] + st->vport[0];
480 v[i].y = (0.5f - v[i].y * 0.5f) * (float)st->vport[3] + st->vport[1];
482 /* convert pos to 24.8 fixed point */
483 pv[i].x = cround64(v[i].x * 256.0f);
484 pv[i].y = cround64(v[i].y * 256.0f);
485 /* convert tex coords to 16.16 fixed point */
486 pv[i].u = cround64(v[i].u * 65536.0f);
487 pv[i].v = cround64(v[i].v * 65536.0f);
488 /* pass the color through as is */
495 /* backface culling */
496 if(vnum > 2 && st->opt & G3D_CULL_FACE) {
497 int32_t ax = pv[1].x - pv[0].x;
498 int32_t ay = pv[1].y - pv[0].y;
499 int32_t bx = pv[2].x - pv[0].x;
500 int32_t by = pv[2].y - pv[0].y;
501 int32_t cross_z = (ax >> 4) * (by >> 4) - (ay >> 4) * (bx >> 4);
502 int sign = (cross_z >> 31) & 1;
504 if(!(sign ^ st->frontface)) {
505 continue; /* back-facing */
511 if(st->opt & G3D_BLEND) {
513 int inv_alpha = 255 - pv[0].a;
514 uint32_t *dest = st->pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
515 r = ((int)pv[0].r * pv[0].a + UNPACK_R32(*dest) * inv_alpha) >> 8;
516 g = ((int)pv[0].g * pv[0].a + UNPACK_G32(*dest) * inv_alpha) >> 8;
517 b = ((int)pv[0].b * pv[0].a + UNPACK_B32(*dest) * inv_alpha) >> 8;
518 *dest++ = PACK_RGB16(r, g, b);
520 uint32_t *dest = st->pixels + (pv[0].y >> 8) * st->width + (pv[0].x >> 8);
521 *dest = PACK_RGB32(pv[0].r, pv[0].g, pv[0].b);
527 uint32_t col = PACK_RGB32(pv[0].r, pv[0].g, pv[0].b);
528 draw_line(pv[0].x >> 8, pv[0].y >> 8, pv[1].x >> 8, pv[1].y >> 8, col);
533 fill_mode = st->polymode;
534 if(st->opt & G3D_TEXTURE_2D) {
535 fill_mode |= POLYFILL_TEX_BIT;
537 if(st->opt & G3D_BLEND) {
538 fill_mode |= POLYFILL_BLEND_BIT;
540 polyfill(fill_mode, pv, vnum);
545 void g3d_begin(int prim)
548 st->imm_pcount = prim;
557 static void imm_flush(void)
559 int numv = st->imm_numv;
561 g3d_draw_indexed(st->imm_prim, st->imm_vbuf, numv, 0, 0);
564 void g3d_vertex(float x, float y, float z)
566 struct g3d_vertex *vptr = st->imm_vbuf + st->imm_numv++;
567 *vptr = st->imm_curv;
573 if(!--st->imm_pcount) {
574 if(st->imm_numv >= IMM_VBUF_SIZE - st->imm_prim) {
577 st->imm_pcount = st->imm_prim;
581 void g3d_normal(float x, float y, float z)
588 void g3d_color3b(unsigned char r, unsigned char g, unsigned char b)
593 st->imm_curv.a = 255;
596 void g3d_color4b(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
604 void g3d_color3f(float r, float g, float b)
609 st->imm_curv.r = ir > 255 ? 255 : ir;
610 st->imm_curv.g = ig > 255 ? 255 : ig;
611 st->imm_curv.b = ib > 255 ? 255 : ib;
612 st->imm_curv.a = 255;
615 void g3d_color4f(float r, float g, float b, float a)
621 st->imm_curv.r = ir > 255 ? 255 : ir;
622 st->imm_curv.g = ig > 255 ? 255 : ig;
623 st->imm_curv.b = ib > 255 ? 255 : ib;
624 st->imm_curv.a = ia > 255 ? 255 : ia;
627 void g3d_texcoord(float u, float v)
633 static void xform4_vec3(const float *mat, float *vec)
635 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
636 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
637 float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
638 float w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
646 static void xform3_vec3(const float *mat, float *vec)
648 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2];
649 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2];
650 float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2];
657 #define NORMALIZE(v) \
659 float len = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); \
661 float s = 1.0 / len; \
668 static void shade(struct g3d_vertex *v)
673 color[0] = st->ambient[0] * st->mtl.kd[0];
674 color[1] = st->ambient[1] * st->mtl.kd[1];
675 color[2] = st->ambient[2] * st->mtl.kd[2];
677 for(i=0; i<MAX_LIGHTS; i++) {
681 if(!(st->opt & (G3D_LIGHT0 << i))) {
685 ldir[0] = st->lt[i].x - v->x;
686 ldir[1] = st->lt[i].y - v->y;
687 ldir[2] = st->lt[i].z - v->z;
690 if((ndotl = v->nx * ldir[0] + v->ny * ldir[1] + v->nz * ldir[2]) < 0.0f) {
694 color[0] += st->mtl.kd[0] * st->lt[i].r * ndotl;
695 color[1] += st->mtl.kd[1] * st->lt[i].g * ndotl;
696 color[2] += st->mtl.kd[2] * st->lt[i].b * ndotl;
699 r = cround64(color[0] * 255.0);
700 g = cround64(color[1] * 255.0);
701 b = cround64(color[2] * 255.0);
703 v->r = r > 255 ? 255 : r;
704 v->g = g > 255 ? 255 : g;
705 v->b = b > 255 ? 255 : b;