11 typedef float g3d_matrix[16];
13 #define MAX_VBUF_SIZE 256
20 g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
21 int mtop[G3D_NUM_MATRICES];
30 static void xform4_vec3(const float *mat, float *vec);
31 static void xform3_vec3(const float *mat, float *vec);
32 static void shade(struct g3d_vertex *v);
34 static struct g3d_state *st;
35 static const float idmat[] = {
46 if(!(st = calloc(1, sizeof *st))) {
47 fprintf(stderr, "failed to allocate G3D context\n");
50 st->fill_mode = POLYFILL_FLAT;
52 for(i=0; i<G3D_NUM_MATRICES; i++) {
59 void g3d_destroy(void)
64 void g3d_framebuffer(int width, int height, void *pixels)
70 pimg_fb.pixels = pixels;
71 pimg_fb.width = width;
72 pimg_fb.height = height;
75 void g3d_enable(unsigned int opt)
80 void g3d_disable(unsigned int opt)
85 void g3d_setopt(unsigned int opt, unsigned int mask)
87 st->opt = (st->opt & ~mask) | (opt & mask);
90 unsigned int g3d_getopt(unsigned int mask)
92 return st->opt & mask;
95 void g3d_front_face(unsigned int order)
97 st->frontface = order;
100 void g3d_polygon_mode(int pmode)
102 st->fill_mode = pmode;
105 void g3d_matrix_mode(int mmode)
110 void g3d_load_identity(void)
112 int top = st->mtop[st->mmode];
113 memcpy(st->mat[st->mmode][top], idmat, 16 * sizeof(float));
116 void g3d_load_matrix(const float *m)
118 int top = st->mtop[st->mmode];
119 memcpy(st->mat[st->mmode][top], m, 16 * sizeof(float));
122 #define M(i,j) (((i) << 2) + (j))
123 void g3d_mult_matrix(const float *m2)
125 int i, j, top = st->mtop[st->mmode];
127 float *dest = st->mat[st->mmode][top];
129 memcpy(m1, dest, sizeof m1);
133 *dest++ = m1[M(0,j)] * m2[M(i,0)] +
134 m1[M(1,j)] * m2[M(i,1)] +
135 m1[M(2,j)] * m2[M(i,2)] +
136 m1[M(3,j)] * m2[M(i,3)];
141 void g3d_push_matrix(void)
143 int top = st->mtop[st->mmode];
144 if(top >= G3D_NUM_MATRICES) {
145 fprintf(stderr, "g3d_push_matrix overflow\n");
148 memcpy(st->mat[st->mmode][top + 1], st->mat[st->mmode][top], 16 * sizeof(float));
149 st->mtop[st->mmode] = top + 1;
152 void g3d_pop_matrix(void)
154 if(st->mtop[st->mmode] <= 0) {
155 fprintf(stderr, "g3d_pop_matrix underflow\n");
158 --st->mtop[st->mmode];
161 void g3d_translate(float x, float y, float z)
163 float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
170 void g3d_rotate(float deg, float x, float y, float z)
172 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
174 float angle = M_PI * deg / 180.0f;
175 float sina = sin(angle);
176 float cosa = cos(angle);
177 float one_minus_cosa = 1.0f - cosa;
182 m[0] = nxsq + (1.0f - nxsq) * cosa;
183 m[4] = x * y * one_minus_cosa - z * sina;
184 m[8] = x * z * one_minus_cosa + y * sina;
185 m[1] = x * y * one_minus_cosa + z * sina;
186 m[5] = nysq + (1.0 - nysq) * cosa;
187 m[9] = y * z * one_minus_cosa - x * sina;
188 m[2] = x * z * one_minus_cosa - y * sina;
189 m[6] = y * z * one_minus_cosa + x * sina;
190 m[10] = nzsq + (1.0 - nzsq) * cosa;
196 void g3d_scale(float x, float y, float z)
198 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
206 void g3d_ortho(float left, float right, float bottom, float top, float znear, float zfar)
208 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
210 float dx = right - left;
211 float dy = top - bottom;
212 float dz = zfar - znear;
217 m[12] = -(right + left) / dx;
218 m[13] = -(top + bottom) / dy;
219 m[14] = -(zfar + znear) / dz;
224 void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
226 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
228 float dx = right - left;
229 float dy = top - bottom;
232 float a = (right + left) / dx;
233 float b = (top + bottom) / dy;
234 float c = -(fr + nr) / dz;
235 float d = -2.0 * fr * nr / dz;
237 m[0] = 2.0 * nr / dx;
238 m[5] = 2.0 * nr / dy;
248 void g3d_perspective(float vfov_deg, float aspect, float znear, float zfar)
250 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
252 float vfov = M_PI * vfov_deg / 180.0f;
253 float s = 1.0f / tan(vfov * 0.5f);
254 float range = znear - zfar;
258 m[10] = (znear + zfar) / range;
260 m[14] = 2.0f * znear * zfar / range;
265 const float *g3d_get_matrix(int which, float *m)
267 int top = st->mtop[which];
270 memcpy(m, st->mat[which][top], 16 * sizeof(float));
272 return st->mat[which][top];
275 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
277 g3d_draw_indexed(prim, varr, varr_size, 0, 0);
280 void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
281 const int16_t *iarr, int iarr_size)
284 struct pvertex pv[4];
285 struct g3d_vertex v[4];
286 int vnum = prim; /* primitive vertex counts correspond to enum values */
287 int mvtop = st->mtop[G3D_MODELVIEW];
288 int ptop = st->mtop[G3D_PROJECTION];
290 /* calc the normal matrix */
291 memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
292 st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
294 nfaces = (iarr ? iarr_size : varr_size) / vnum;
296 for(j=0; j<nfaces; j++) {
298 for(i=0; i<vnum; i++) {
299 v[i] = iarr ? varr[*iarr++] : *varr++;
301 xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
302 xform3_vec3(st->norm_mat, &v[i].nx);
304 if(st->opt & G3D_LIGHTING) {
307 xform4_vec3(st->mat[G3D_PROJECTION][ptop], &v[i].x);
312 for(i=0; i<vnum; i++) {
316 /*v[i].z /= v[i].w;*/
319 /* viewport transformation */
320 v[i].x = (v[i].x * 0.5f + 0.5f) * (float)st->width;
321 v[i].y = (0.5f - v[i].y * 0.5f) * (float)st->height;
323 /* convert pos to 24.8 fixed point */
324 pv[i].x = (int32_t)(v[i].x * 256.0f);
325 pv[i].y = (int32_t)(v[i].y * 256.0f);
326 /* convert tex coords to 16.16 fixed point */
327 pv[i].u = (int32_t)(v[i].u * 65536.0f);
328 pv[i].v = (int32_t)(v[i].v * 65536.0f);
329 /* pass the color through as is */
335 /* backface culling */
336 if(vnum > 2 && st->opt & G3D_CULL_FACE) {
337 int32_t ax = pv[1].x - pv[0].x;
338 int32_t ay = pv[1].y - pv[0].y;
339 int32_t bx = pv[2].x - pv[0].x;
340 int32_t by = pv[2].y - pv[0].y;
341 int32_t cross_z = ax * (by >> 8) - ay * (bx >> 8);
342 int sign = (cross_z >> 31) & 1;
344 if(!(sign ^ st->frontface)) {
345 continue; /* back-facing */
349 polyfill(st->fill_mode, pv, vnum);
353 static void xform4_vec3(const float *mat, float *vec)
355 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
356 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
357 float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
358 float w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
366 static void xform3_vec3(const float *mat, float *vec)
368 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2];
369 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2];
370 float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2];
377 static void shade(struct g3d_vertex *v)
379 v->r = v->g = v->b = 255;