deepstone

view src/mingl.c @ 31:d0164235bfd1

changed the linux/mac makefile name to GNUmakefile
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Sep 2013 18:37:07 +0300
parents 00d84ab1ef26
children 17a5107b6fa4
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5 #include <limits.h>
6 #include <assert.h>
7 #include "vmath.h"
8 #include "mingl.h"
9 #include "mglimpl.h"
12 #define DOT(a, b) ((a).x * (b).x + (a).y * (b).y + (a).z * (b).z)
14 #define NORMALIZE(v) \
15 do { \
16 float mag = sqrt(DOT(v, v)); \
17 if(fabs(mag) > 1e-6) { \
18 float invmag = 1.0 / mag; \
19 (v).x *= invmag; \
20 (v).y *= invmag; \
21 (v).z *= invmag; \
22 } \
23 } while(0)
25 static void transform(vec4_t *res, vec4_t *v, float *mat);
26 static void transform3(vec3_t *res, vec3_t *v, float *mat);
27 static void vertex_proc_view(struct vertex *vert);
28 static int vertex_proc_proj(struct vertex *vert);
29 static int calc_shiftmask(int val, int *shiftp, unsigned int *maskp);
31 static struct state st;
32 static struct framebuffer fb;
34 int mgl_init(int width, int height)
35 {
36 int i;
38 st.flags = 0;
39 st.mmode = 0;
41 mgl_front_face(MGL_CCW);
42 mgl_cull_face(MGL_BACK);
44 st.curv.cidx = 0;
45 st.curv.energy = 1.0;
46 st.curv.norm.x = st.curv.norm.y = st.curv.norm.z = 0.0;
48 if(!(fb.pixels = malloc(width * height))) {
49 return -1;
50 }
51 fb.width = width;
52 fb.height = height;
53 fb.zbuf = 0;
55 if(mgl_rast_init(&st, &fb) == -1) {
56 free(fb.pixels);
57 return -1;
58 }
60 st.mtop[0] = st.mtop[1] = 0;
62 mgl_matrix_mode(MGL_MODELVIEW);
63 mgl_load_identity();
64 mgl_matrix_mode(MGL_PROJECTION);
65 mgl_load_identity();
67 /* initial viewport in the size of the framebuffer */
68 st.vp[0] = st.vp[1] = 0;
69 st.vp[2] = width;
70 st.vp[3] = height;
72 st.col_range = 256;
73 for(i=0; i<MAX_LIGHTS; i++) {
74 st.lpos[i].x = st.lpos[i].y = st.lpos[i].w = 0.0f;
75 st.lpos[i].z = 1.0f;
76 st.lint[i] = 0.0f;
77 }
79 st.ambient = 0.1;
81 mgl_clip_init(&st);
82 return 0;
83 }
85 void mgl_free(void)
86 {
87 int i;
89 mgl_rast_cleanup();
90 free(fb.pixels);
91 fb.pixels = 0;
93 if(fb.zbuf) {
94 for(i=0; i<fb.num_ztiles; i++) {
95 free(fb.zbuf[i]);
96 }
97 free(fb.zbuf);
98 fb.zbuf = 0;
99 }
100 }
102 unsigned char *mgl_framebuffer(void)
103 {
104 return fb.pixels;
105 }
107 void mgl_clear(int cidx)
108 {
109 memset(fb.pixels, cidx, fb.width * fb.height);
110 }
112 void mgl_clear_depth(void)
113 {
114 int i;
116 if(!fb.zbuf) {
117 long num_pixels = (long)fb.width * (long)fb.height;
118 fb.num_ztiles = (num_pixels + ZTILE_SIZE - 1) / ZTILE_SIZE;
120 if(!(fb.zbuf = malloc(fb.num_ztiles * sizeof *fb.zbuf))) {
121 fprintf(stderr, "failed to allocate ztile array\n");
122 abort();
123 }
125 for(i=0; i<fb.num_ztiles; i++) {
126 if(!(fb.zbuf[i] = malloc(ZTILE_SIZE * 2))) {
127 fprintf(stderr, "failed to allocate ztile %d\n", i);
128 abort();
129 }
130 memset(fb.zbuf[i], 0xff, ZTILE_SIZE * 2);
131 }
132 return;
133 }
135 for(i=0; i<fb.num_ztiles; i++) {
136 memset(fb.zbuf[i], 0xff, ZTILE_SIZE * 2);
137 }
138 }
140 void mgl_enable(unsigned int what)
141 {
142 st.flags |= (1 << what);
143 }
145 void mgl_disable(unsigned int what)
146 {
147 st.flags &= ~(1 << what);
148 }
150 int mgl_isenabled(unsigned int what)
151 {
152 return IS_ENABLED(st.flags, what);
153 }
155 void mgl_front_face(int ff)
156 {
157 st.frontface = ff;
158 }
160 void mgl_cull_face(int cf)
161 {
162 st.cullface = cf;
163 }
165 void mgl_set_ambient(float amb)
166 {
167 st.ambient = amb;
168 }
170 float mgl_get_ambient(void)
171 {
172 return st.ambient;
173 }
175 void mgl_color_range(int rng)
176 {
177 st.col_range = rng;
178 }
180 void mgl_light_intensity(int ltidx, float intens)
181 {
182 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
183 st.lint[ltidx] = intens;
184 }
186 void mgl_light_position(int ltidx, float x, float y, float z, float w)
187 {
188 vec4_t pos;
189 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
191 pos.x = x;
192 pos.y = y;
193 pos.z = z;
194 pos.w = w;
195 transform(&st.lpos[ltidx], &pos, st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]]);
197 if(fabs(st.lpos[ltidx].w) < 1e-6) {
198 NORMALIZE(st.lpos[ltidx]);
199 } else {
200 st.lpos[ltidx].x /= st.lpos[ltidx].w;
201 st.lpos[ltidx].y /= st.lpos[ltidx].w;
202 st.lpos[ltidx].z /= st.lpos[ltidx].w;
203 }
204 }
206 void mgl_begin(int prim)
207 {
208 st.prim = prim;
209 st.vidx = 0;
211 st.ord = st.frontface;
212 if(st.cullface == MGL_FRONT) {
213 st.ord = st.frontface == MGL_CCW ? MGL_CW : MGL_CCW;
214 }
216 /* select the correct rasterizer according to state */
217 mgl_rast_prepare();
218 }
220 void mgl_end(void)
221 {
222 }
224 void mgl_vertex2f(float x, float y)
225 {
226 mgl_vertex4f(x, y, 0.0f, 1.0f);
227 }
229 void mgl_vertex3f(float x, float y, float z)
230 {
231 mgl_vertex4f(x, y, z, 1.0f);
232 }
234 void mgl_vertex4f(float x, float y, float z, float w)
235 {
236 st.v[st.vidx].pos.x = x;
237 st.v[st.vidx].pos.y = y;
238 st.v[st.vidx].pos.z = z;
239 st.v[st.vidx].pos.w = w;
240 st.v[st.vidx].cidx = st.curv.cidx;
241 st.v[st.vidx].energy = st.curv.energy;
242 st.v[st.vidx].norm = st.curv.norm;
243 st.v[st.vidx].tc = st.curv.tc;
245 /* T&L up to view space, to perform user-clipping */
246 vertex_proc_view(st.v + st.vidx);
248 if(++st.vidx >= st.prim) {
249 st.vidx = 0;
251 switch(st.prim) {
252 case MGL_POINTS:
253 vertex_proc_proj(st.v);
254 mgl_draw_point(st.v);
255 break;
256 case MGL_LINES:
257 vertex_proc_proj(st.v);
258 vertex_proc_proj(st.v + 1);
259 mgl_draw_line(st.v, st.v + 1);
260 break;
261 case MGL_TRIANGLES:
262 case MGL_QUADS:
263 {
264 int nverts = mgl_clip_poly(st.v, st.prim);
265 if(nverts > 0) {
266 int i;
267 /* passed clipping, perform projection for all verts and draw */
268 for(i=0; i<nverts; i++) {
269 if(vertex_proc_proj(st.v + i) == -1) {
270 printf("this shouldn't happen!\n");
271 return;
272 }
273 }
274 mgl_draw_poly(st.v, nverts);
275 }
276 }
277 break;
278 default:
279 fprintf(stderr, "invalid primitive: %d\n", st.prim);
280 abort();
281 }
282 }
283 }
285 void mgl_color1f(float energy)
286 {
287 st.curv.energy = energy;
288 }
290 void mgl_index(int c)
291 {
292 st.curv.cidx = c;
293 }
295 void mgl_normal(float x, float y, float z)
296 {
297 st.curv.norm.x = x;
298 st.curv.norm.y = y;
299 st.curv.norm.z = z;
300 }
302 void mgl_texcoord2f(float x, float y)
303 {
304 st.curv.tc.x = x;
305 st.curv.tc.y = y;
306 }
308 static void transform(vec4_t *res, vec4_t *v, float *mat)
309 {
310 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z + mat[12] * v->w;
311 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z + mat[13] * v->w;
312 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z + mat[14] * v->w;
313 res->w = mat[3] * v->x + mat[7] * v->y + mat[11] * v->z + mat[15] * v->w;
314 }
316 /* the matrix is 4x4 (16 floats), just ignoring anything out of the 3x3 */
317 static void transform3(vec3_t *res, vec3_t *v, float *mat)
318 {
319 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z;
320 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z;
321 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z;
322 }
324 static void vertex_proc_view(struct vertex *vert)
325 {
326 vec4_t pview;
328 float *mvmat = st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]];
330 /* modelview transformation */
331 transform(&pview, &vert->pos, mvmat);
333 if(mgl_isenabled(MGL_LIGHTING)) {
334 if(mgl_isenabled(MGL_SMOOTH) || st.vidx == 0) {
335 int i;
336 vec3_t norm;
337 float irrad = st.ambient;
339 transform3(&norm, &vert->norm, mvmat);
341 for(i=0; i<MAX_LIGHTS; i++) {
342 if(st.lint[i] > 1e-6f) {
343 float ndotl;
344 vec3_t ldir;
346 if(st.lpos[i].w == 0.0) {
347 ldir.x = st.lpos[i].x;
348 ldir.y = st.lpos[i].y;
349 ldir.z = st.lpos[i].z;
350 } else {
351 ldir.x = st.lpos[i].x - pview.x;
352 ldir.y = st.lpos[i].y - pview.y;
353 ldir.z = st.lpos[i].z - pview.z;
355 NORMALIZE(ldir);
356 }
358 ndotl = DOT(norm, ldir);
359 if(ndotl < 0.0) {
360 ndotl = 0.0;
361 }
362 irrad += ndotl * st.lint[i];
363 }
364 }
365 vert->energy = irrad > 1.0 ? 1.0 : irrad;
366 } else {
367 vert->energy = st.v[0].energy;
368 }
369 }
371 vert->pos = pview;
372 }
374 static int vertex_proc_proj(struct vertex *vert)
375 {
376 vec4_t pclip;
378 float *pmat = st.matrix[MGL_PROJECTION][st.mtop[MGL_PROJECTION]];
380 transform(&pclip, &vert->pos, pmat);
381 /* TODO clipping in homogenous clip space */
383 if(pclip.w < 1e-6) {
384 vert->pos.x = vert->pos.y = vert->pos.z = vert->pos.w = 0.0f;
385 return -1;
386 }
388 /* perspective division */
389 vert->pos.x = pclip.x / pclip.w;
390 vert->pos.y = pclip.y / pclip.w;
391 vert->pos.z = pclip.z / pclip.w;
392 vert->pos.w = pclip.w;
394 /* viewport transformation */
395 vert->pos.x = st.vp[0] + st.vp[2] * (vert->pos.x * 0.5 + 0.5);
396 vert->pos.y = st.vp[1] + st.vp[3] * (-vert->pos.y * 0.5 + 0.5);
398 return 0;
399 }
401 void mgl_viewport(int x, int y, int width, int height)
402 {
403 st.vp[0] = x;
404 st.vp[1] = y;
405 st.vp[2] = width;
406 st.vp[3] = height;
407 }
409 void mgl_matrix_mode(int mmode)
410 {
411 st.mmode = mmode;
412 }
414 void mgl_push_matrix(void)
415 {
416 float *topmat;
417 if(st.mtop[st.mmode] >= MATRIX_STACK_SIZE - 1) {
418 fprintf(stderr, "mgl_push_matrix: stack overflow\n");
419 abort();
420 }
422 topmat = st.matrix[st.mmode][st.mtop[st.mmode]];
423 memcpy(topmat + 16, topmat, 16 * sizeof *topmat);
424 st.mmode++;
425 }
427 void mgl_pop_matrix(void)
428 {
429 if(st.mtop[st.mmode] <= 0) {
430 fprintf(stderr, "mgl_pop_matrix: stack underflow\n");
431 abort();
432 }
433 st.mtop[st.mmode]--;
434 }
436 void mgl_load_matrix(float *mat)
437 {
438 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
439 memcpy(dest, mat, 16 * sizeof *dest);
440 }
442 #define M(i,j) (((i) << 2) + (j))
443 void mgl_mult_matrix(float *m2)
444 {
445 int i, j;
446 float m1[16];
447 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
449 memcpy(m1, dest, sizeof m1);
451 for(i=0; i<4; i++) {
452 for(j=0; j<4; j++) {
453 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
454 m1[M(1,j)] * m2[M(i,1)] +
455 m1[M(2,j)] * m2[M(i,2)] +
456 m1[M(3,j)] * m2[M(i,3)];
457 }
458 }
459 }
461 void mgl_load_identity(void)
462 {
463 static float id[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
464 mgl_load_matrix((float*)id);
465 }
467 void mgl_translate(float x, float y, float z)
468 {
469 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
470 xform[12] = x;
471 xform[13] = y;
472 xform[14] = z;
473 mgl_mult_matrix(xform);
474 }
476 void mgl_rotate(float deg, float x, float y, float z)
477 {
478 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
480 float angle = M_PI * deg / 180.0f;
481 float sina = sin(angle);
482 float cosa = cos(angle);
483 float one_minus_cosa = 1.0f - cosa;
484 float nxsq = x * x;
485 float nysq = y * y;
486 float nzsq = z * z;
488 xform[0] = nxsq + (1.0f - nxsq) * cosa;
489 xform[4] = x * y * one_minus_cosa - z * sina;
490 xform[8] = x * z * one_minus_cosa + y * sina;
491 xform[1] = x * y * one_minus_cosa + z * sina;
492 xform[5] = nysq + (1.0 - nysq) * cosa;
493 xform[9] = y * z * one_minus_cosa - x * sina;
494 xform[2] = x * z * one_minus_cosa - y * sina;
495 xform[6] = y * z * one_minus_cosa + x * sina;
496 xform[10] = nzsq + (1.0 - nzsq) * cosa;
498 mgl_mult_matrix(xform);
499 }
501 void mgl_scale(float x, float y, float z)
502 {
503 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
504 xform[0] = x;
505 xform[5] = y;
506 xform[10] = z;
507 mgl_mult_matrix(xform);
508 }
510 void gl_ortho(float left, float right, float bottom, float top, float nr, float fr)
511 {
512 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
514 float dx = right - left;
515 float dy = top - bottom;
516 float dz = fr - nr;
518 float tx = -(right + left) / dx;
519 float ty = -(top + bottom) / dy;
520 float tz = -(fr + nr) / dz;
522 float sx = 2.0 / dx;
523 float sy = 2.0 / dy;
524 float sz = -2.0 / dz;
526 xform[0] = sx;
527 xform[5] = sy;
528 xform[10] = sz;
529 xform[12] = tx;
530 xform[13] = ty;
531 xform[14] = tz;
533 mgl_mult_matrix(xform);
534 }
536 void mgl_frustum(float left, float right, float bottom, float top, float nr, float fr)
537 {
538 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
540 float dx = right - left;
541 float dy = top - bottom;
542 float dz = fr - nr;
544 float a = (right + left) / dx;
545 float b = (top + bottom) / dy;
546 float c = -(fr + nr) / dz;
547 float d = -2.0 * fr * nr / dz;
549 xform[0] = 2.0 * nr / dx;
550 xform[5] = 2.0 * nr / dy;
551 xform[8] = a;
552 xform[9] = b;
553 xform[10] = c;
554 xform[11] = -1.0f;
555 xform[14] = d;
557 mgl_mult_matrix(xform);
558 }
560 void mgl_perspective(float vfov, float aspect, float nr, float fr)
561 {
562 float vfov_rad = M_PI * vfov / 180.0;
563 float x = nr * tan(vfov_rad / 2.0);
564 mgl_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
565 }
567 void mgl_teximage(int width, int height, unsigned char *pixels)
568 {
569 st.tex.width = width;
570 st.tex.height = height;
571 st.tex.pixels = pixels;
573 if(calc_shiftmask(width, &st.tex.xshift, &st.tex.xmask) == -1 ||
574 calc_shiftmask(height, &st.tex.yshift, &st.tex.ymask) == -1) {
575 st.tex.pixels = 0;
576 }
577 }
579 void mgl_clip_plane(int id, float nx, float ny, float nz, float dist)
580 {
581 id -= MGL_CLIP_PLANE0;
583 if(id < 0 || id > MAX_CLIP_PLANES) {
584 return;
585 }
587 st.clip_planes[id].normal.x = nx;
588 st.clip_planes[id].normal.y = ny;
589 st.clip_planes[id].normal.z = nz;
590 NORMALIZE(st.clip_planes[id].normal);
592 st.clip_planes[id].pt.x = st.clip_planes[id].normal.x * dist;
593 st.clip_planes[id].pt.y = st.clip_planes[id].normal.y * dist;
594 st.clip_planes[id].pt.z = st.clip_planes[id].normal.z * dist;
596 printf("set clip plane %d -> n[%f %f %f] p[%f %f %f]\n", id,
597 st.clip_planes[id].normal.x, st.clip_planes[id].normal.y, st.clip_planes[id].normal.z,
598 st.clip_planes[id].pt.x, st.clip_planes[id].pt.y, st.clip_planes[id].pt.z);
600 }
602 #define MAX_SHIFT 12
603 static int calc_shiftmask(int val, int *shiftp, unsigned int *maskp)
604 {
605 int i;
607 for(i=0; i<MAX_SHIFT; i++) {
608 if((val >> i) == 1) {
609 *shiftp = i;
610 *maskp = ~(UINT_MAX << i);
611 return 0;
612 }
613 }
614 return -1;
615 }