11 typedef float g3d_matrix[16];
13 #define MAX_VBUF_SIZE 256
19 g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
20 int mtop[G3D_NUM_MATRICES];
29 static void xform4_vec3(const float *mat, float *vec);
30 static void xform3_vec3(const float *mat, float *vec);
31 static void shade(struct g3d_vertex *v);
33 static struct g3d_state *st;
34 static const float idmat[] = {
45 if(!(st = calloc(1, sizeof *st))) {
46 fprintf(stderr, "failed to allocate G3D context\n");
50 for(i=0; i<G3D_NUM_MATRICES; i++) {
57 void g3d_destroy(void)
62 void g3d_framebuffer(int width, int height, void *pixels)
69 void g3d_enable(unsigned int opt)
74 void g3d_disable(unsigned int opt)
79 void g3d_setopt(unsigned int opt, unsigned int mask)
81 st->opt = (st->opt & ~mask) | (opt & mask);
84 unsigned int g3d_getopt(unsigned int mask)
86 return st->opt & mask;
89 void g3d_front_face(unsigned int order)
91 st->frontface = order;
94 void g3d_matrix_mode(int mmode)
99 void g3d_load_identity(void)
101 int top = st->mtop[st->mmode];
102 memcpy(st->mat[st->mmode][top], idmat, 16 * sizeof(float));
105 void g3d_load_matrix(const float *m)
107 int top = st->mtop[st->mmode];
108 memcpy(st->mat[st->mmode][top], m, 16 * sizeof(float));
111 #define M(i,j) (((i) << 2) + (j))
112 void g3d_mult_matrix(const float *m2)
114 int i, j, top = st->mtop[st->mmode];
116 float *dest = st->mat[st->mmode][top];
118 memcpy(m1, dest, sizeof m1);
122 *dest++ = m1[M(0,j)] * m2[M(i,0)] +
123 m1[M(1,j)] * m2[M(i,1)] +
124 m1[M(2,j)] * m2[M(i,2)] +
125 m1[M(3,j)] * m2[M(i,3)];
130 void g3d_push_matrix(void)
132 int top = st->mtop[st->mmode];
133 if(top >= G3D_NUM_MATRICES) {
134 fprintf(stderr, "g3d_push_matrix overflow\n");
137 memcpy(st->mat[st->mmode][top + 1], st->mat[st->mmode][top], 16 * sizeof(float));
138 st->mtop[st->mmode] = top + 1;
141 void g3d_pop_matrix(void)
143 if(st->mtop[st->mmode] <= 0) {
144 fprintf(stderr, "g3d_pop_matrix underflow\n");
147 --st->mtop[st->mmode];
150 void g3d_translate(float x, float y, float z)
152 float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
159 void g3d_rotate(float deg, float x, float y, float z)
161 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
163 float angle = M_PI * deg / 180.0f;
164 float sina = sin(angle);
165 float cosa = cos(angle);
166 float one_minus_cosa = 1.0f - cosa;
171 m[0] = nxsq + (1.0f - nxsq) * cosa;
172 m[4] = x * y * one_minus_cosa - z * sina;
173 m[8] = x * z * one_minus_cosa + y * sina;
174 m[1] = x * y * one_minus_cosa + z * sina;
175 m[5] = nysq + (1.0 - nysq) * cosa;
176 m[9] = y * z * one_minus_cosa - x * sina;
177 m[2] = x * z * one_minus_cosa - y * sina;
178 m[6] = y * z * one_minus_cosa + x * sina;
179 m[10] = nzsq + (1.0 - nzsq) * cosa;
185 void g3d_scale(float x, float y, float z)
187 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
195 void g3d_ortho(float left, float right, float bottom, float top, float znear, float zfar)
197 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
199 float dx = right - left;
200 float dy = top - bottom;
201 float dz = zfar - znear;
206 m[12] = -(right + left) / dx;
207 m[13] = -(top + bottom) / dy;
208 m[14] = -(zfar + znear) / dz;
213 void g3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
215 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
217 float dx = right - left;
218 float dy = top - bottom;
221 float a = (right + left) / dx;
222 float b = (top + bottom) / dy;
223 float c = -(fr + nr) / dz;
224 float d = -2.0 * fr * nr / dz;
226 m[0] = 2.0 * nr / dx;
227 m[5] = 2.0 * nr / dy;
237 void g3d_perspective(float vfov_deg, float aspect, float znear, float zfar)
239 float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
241 float vfov = M_PI * vfov_deg / 180.0f;
242 float s = 1.0f / tan(vfov * 0.5f);
243 float range = znear - zfar;
247 m[10] = (znear + zfar) / range;
249 m[14] = 2.0f * znear * zfar / range;
254 const float *g3d_get_matrix(int which, float *m)
256 int top = st->mtop[which];
259 memcpy(m, st->mat[which][top], 16 * sizeof(float));
261 return st->mat[which][top];
264 void g3d_draw(int prim, const struct g3d_vertex *varr, int varr_size)
266 g3d_draw_indexed(prim, varr, varr_size, 0, 0);
269 void g3d_draw_indexed(int prim, const struct g3d_vertex *varr, int varr_size,
270 const int16_t *iarr, int iarr_size)
273 struct pvertex pv[4];
274 struct g3d_vertex v[4];
275 int vnum = prim; /* primitive vertex counts correspond to enum values */
276 int mvtop = st->mtop[G3D_MODELVIEW];
277 int ptop = st->mtop[G3D_PROJECTION];
279 /* calc the normal matrix */
280 memcpy(st->norm_mat, st->mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
281 st->norm_mat[12] = st->norm_mat[13] = st->norm_mat[14] = 0.0f;
283 nfaces = (iarr ? iarr_size : varr_size) / vnum;
285 for(j=0; j<nfaces; j++) {
287 for(i=0; i<vnum; i++) {
288 v[i] = iarr ? varr[*iarr++] : *varr++;
290 xform4_vec3(st->mat[G3D_MODELVIEW][mvtop], &v[i].x);
291 xform3_vec3(st->norm_mat, &v[i].nx);
293 if(st->opt & G3D_LIGHTING) {
296 xform4_vec3(st->mat[G3D_PROJECTION][ptop], &v[i].x);
301 for(i=0; i<vnum; i++) {
305 /*v[i].z /= v[i].w;*/
308 /* viewport transformation */
309 v[i].x = (v[i].x * 0.5f + 0.5f) * (float)st->width;
310 v[i].y = (0.5f - v[i].y * 0.5f) * (float)st->height;
312 /* convert pos to 24.8 fixed point */
313 pv[i].x = (int32_t)(v[i].x * 256.0f);
314 pv[i].y = (int32_t)(v[i].y * 256.0f);
315 /* convert tex coords to 16.16 fixed point */
316 pv[i].u = (int32_t)(v[i].u * 65536.0f);
317 pv[i].v = (int32_t)(v[i].v * 65536.0f);
318 /* pass the color through as is */
324 /* backface culling */
325 if(vnum > 2 && st->opt & G3D_CULL_FACE) {
326 int32_t ax = pv[1].x - pv[0].x;
327 int32_t ay = pv[1].y - pv[0].y;
328 int32_t bx = pv[2].x - pv[0].x;
329 int32_t by = pv[2].y - pv[0].y;
330 int32_t cross_z = ax * (by >> 8) - ay * (bx >> 8);
331 int sign = (cross_z >> 31) & 1;
333 if(!(sign ^ st->frontface)) {
334 continue; /* back-facing */
338 polyfill_flat(pv, vnum);
342 static void xform4_vec3(const float *mat, float *vec)
344 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
345 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
346 float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
347 float w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
355 static void xform3_vec3(const float *mat, float *vec)
357 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2];
358 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2];
359 float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2];
366 static void shade(struct g3d_vertex *v)
368 v->r = v->g = v->b = 255;