dos3d

view src/mingl.c @ 21:00d84ab1ef26

switched to wacom
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Sep 2013 18:17:55 +0300
parents 1e9f0b3616fa
children f2c2e45e8edd
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(struct vertex *vert);
28 static int calc_shiftmask(int val, int *shiftp, unsigned int *maskp);
30 static struct state st;
31 static struct framebuffer fb;
33 int mgl_init(int width, int height)
34 {
35 int i;
37 st.flags = 0;
38 st.mmode = 0;
40 mgl_front_face(MGL_CCW);
41 mgl_cull_face(MGL_BACK);
43 st.curv.cidx = 0;
44 st.curv.energy = 1.0;
45 st.curv.norm.x = st.curv.norm.y = st.curv.norm.z = 0.0;
47 if(!(fb.pixels = malloc(width * height))) {
48 return -1;
49 }
50 fb.width = width;
51 fb.height = height;
52 fb.zbuf = 0;
54 if(mgl_rast_init(&st, &fb) == -1) {
55 free(fb.pixels);
56 return -1;
57 }
59 st.mtop[0] = st.mtop[1] = 0;
61 mgl_matrix_mode(MGL_MODELVIEW);
62 mgl_load_identity();
63 mgl_matrix_mode(MGL_PROJECTION);
64 mgl_load_identity();
66 /* initial viewport in the size of the framebuffer */
67 st.vp[0] = st.vp[1] = 0;
68 st.vp[2] = width;
69 st.vp[3] = height;
71 st.col_range = 256;
72 for(i=0; i<MAX_LIGHTS; i++) {
73 st.lpos[i].x = st.lpos[i].y = st.lpos[i].w = 0.0f;
74 st.lpos[i].z = 1.0f;
75 st.lint[i] = 0.0f;
76 }
78 return 0;
79 }
81 void mgl_free(void)
82 {
83 int i;
85 mgl_rast_cleanup();
86 free(fb.pixels);
87 fb.pixels = 0;
89 if(fb.zbuf) {
90 for(i=0; i<fb.num_ztiles; i++) {
91 free(fb.zbuf[i]);
92 }
93 free(fb.zbuf);
94 fb.zbuf = 0;
95 }
96 }
98 unsigned char *mgl_framebuffer(void)
99 {
100 return fb.pixels;
101 }
103 void mgl_clear(int cidx)
104 {
105 memset(fb.pixels, cidx, fb.width * fb.height);
106 }
108 void mgl_clear_depth(void)
109 {
110 int i;
112 if(!fb.zbuf) {
113 long num_pixels = (long)fb.width * (long)fb.height;
114 fb.num_ztiles = (num_pixels + ZTILE_SIZE - 1) / ZTILE_SIZE;
116 if(!(fb.zbuf = malloc(fb.num_ztiles * sizeof *fb.zbuf))) {
117 fprintf(stderr, "failed to allocate ztile array\n");
118 abort();
119 }
121 for(i=0; i<fb.num_ztiles; i++) {
122 if(!(fb.zbuf[i] = malloc(ZTILE_SIZE * 2))) {
123 fprintf(stderr, "failed to allocate ztile %d\n", i);
124 abort();
125 }
126 memset(fb.zbuf[i], 0xff, ZTILE_SIZE * 2);
127 }
128 return;
129 }
131 for(i=0; i<fb.num_ztiles; i++) {
132 memset(fb.zbuf[i], 0xff, ZTILE_SIZE * 2);
133 }
134 }
136 void mgl_enable(unsigned int bit)
137 {
138 st.flags |= bit;
139 }
141 void mgl_disable(unsigned int bit)
142 {
143 st.flags &= ~bit;
144 }
146 int mgl_isenabled(unsigned int bit)
147 {
148 return (st.flags & bit) != 0;
149 }
151 void mgl_front_face(int ff)
152 {
153 st.frontface = ff;
154 }
156 void mgl_cull_face(int cf)
157 {
158 st.cullface = cf;
159 }
161 void mgl_color_range(int rng)
162 {
163 st.col_range = rng;
164 }
166 void mgl_light_intensity(int ltidx, float intens)
167 {
168 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
169 st.lint[ltidx] = intens;
170 }
172 void mgl_light_position(int ltidx, float x, float y, float z, float w)
173 {
174 vec4_t pos;
175 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
177 pos.x = x;
178 pos.y = y;
179 pos.z = z;
180 pos.w = w;
181 transform(&st.lpos[ltidx], &pos, st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]]);
183 if(fabs(st.lpos[ltidx].w) < 1e-6) {
184 NORMALIZE(st.lpos[ltidx]);
185 } else {
186 st.lpos[ltidx].x /= st.lpos[ltidx].w;
187 st.lpos[ltidx].y /= st.lpos[ltidx].w;
188 st.lpos[ltidx].z /= st.lpos[ltidx].w;
189 }
190 }
192 void mgl_begin(int prim)
193 {
194 st.prim = prim;
195 st.vidx = 0;
197 st.ord = st.frontface;
198 if(st.cullface == MGL_FRONT) {
199 st.ord = st.frontface == MGL_CCW ? MGL_CW : MGL_CCW;
200 }
202 /* select the correct rasterizer according to state */
203 mgl_rast_prepare();
204 }
206 void mgl_end(void)
207 {
208 }
210 void mgl_vertex2f(float x, float y)
211 {
212 mgl_vertex4f(x, y, 0.0f, 1.0f);
213 }
215 void mgl_vertex3f(float x, float y, float z)
216 {
217 mgl_vertex4f(x, y, z, 1.0f);
218 }
220 void mgl_vertex4f(float x, float y, float z, float w)
221 {
222 st.v[st.vidx].pos.x = x;
223 st.v[st.vidx].pos.y = y;
224 st.v[st.vidx].pos.z = z;
225 st.v[st.vidx].pos.w = w;
226 st.v[st.vidx].cidx = st.curv.cidx;
227 st.v[st.vidx].energy = st.curv.energy;
228 st.v[st.vidx].norm = st.curv.norm;
229 st.v[st.vidx].tc = st.curv.tc;
231 vertex_proc(st.v + st.vidx);
233 if(++st.vidx >= st.prim) {
234 switch(st.prim) {
235 case MGL_POINTS:
236 mgl_draw_point(st.v);
237 break;
238 case MGL_LINES:
239 mgl_draw_line(st.v, st.v + 1);
240 break;
241 case MGL_TRIANGLES:
242 case MGL_QUADS:
243 mgl_draw_poly(st.v, st.prim);
244 break;
245 default:
246 fprintf(stderr, "invalid primitive: %d\n", st.prim);
247 abort();
248 }
249 st.vidx = 0;
250 }
251 }
253 void mgl_color1f(float energy)
254 {
255 st.curv.energy = energy;
256 }
258 void mgl_index(int c)
259 {
260 st.curv.cidx = c;
261 }
263 void mgl_normal(float x, float y, float z)
264 {
265 st.curv.norm.x = x;
266 st.curv.norm.y = y;
267 st.curv.norm.z = z;
268 }
270 void mgl_texcoord2f(float x, float y)
271 {
272 st.curv.tc.x = x;
273 st.curv.tc.y = y;
274 }
276 static void transform(vec4_t *res, vec4_t *v, float *mat)
277 {
278 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z + mat[12] * v->w;
279 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z + mat[13] * v->w;
280 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z + mat[14] * v->w;
281 res->w = mat[3] * v->x + mat[7] * v->y + mat[11] * v->z + mat[15] * v->w;
282 }
284 /* the matrix is 4x4 (16 floats), just ignoring anything out of the 3x3 */
285 static void transform3(vec3_t *res, vec3_t *v, float *mat)
286 {
287 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z;
288 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z;
289 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z;
290 }
292 static void vertex_proc(struct vertex *vert)
293 {
294 vec4_t pview, pclip;
296 float *mvmat = st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]];
297 float *pmat = st.matrix[MGL_PROJECTION][st.mtop[MGL_PROJECTION]];
299 /* modelview transformation */
300 transform(&pview, &vert->pos, mvmat);
302 if(st.flags & MGL_LIGHTING) {
303 if((st.flags & MGL_SMOOTH) || st.vidx == 0) {
304 int i;
305 vec3_t norm;
306 float irrad = 0.0f;
308 transform3(&norm, &vert->norm, mvmat);
310 for(i=0; i<MAX_LIGHTS; i++) {
311 if(st.lint[i] > 1e-6f) {
312 float ndotl;
313 vec3_t ldir;
315 if(st.lpos[i].w == 0.0) {
316 ldir.x = st.lpos[i].x;
317 ldir.y = st.lpos[i].y;
318 ldir.z = st.lpos[i].z;
319 } else {
320 ldir.x = st.lpos[i].x - pview.x;
321 ldir.y = st.lpos[i].y - pview.y;
322 ldir.z = st.lpos[i].z - pview.z;
324 NORMALIZE(ldir);
325 }
327 ndotl = DOT(norm, ldir);
328 if(ndotl < 0.0) {
329 ndotl = 0.0;
330 }
331 irrad += ndotl * st.lint[i];
332 }
333 }
334 vert->energy = irrad;
335 } else {
336 vert->energy = st.v[0].energy;
337 }
338 }
340 transform(&pclip, &pview, pmat);
341 /* TODO clipping in homogenous clip space */
343 if(pclip.w < 1e-6 && pclip.w > -1e-6) {
344 vert->pos.x = vert->pos.y = vert->pos.z = vert->pos.w = 0.0f;
345 return;
346 }
348 /* perspective division */
349 vert->pos.x = pclip.x / pclip.w;
350 vert->pos.y = pclip.y / pclip.w;
351 vert->pos.z = pclip.z / pclip.w;
352 vert->pos.w = pclip.w;
354 /* viewport transformation */
355 vert->pos.x = st.vp[0] + st.vp[2] * (vert->pos.x * 0.5 + 0.5);
356 vert->pos.y = st.vp[1] + st.vp[3] * (-vert->pos.y * 0.5 + 0.5);
357 }
359 void mgl_viewport(int x, int y, int width, int height)
360 {
361 st.vp[0] = x;
362 st.vp[1] = y;
363 st.vp[2] = width;
364 st.vp[3] = height;
365 }
367 void mgl_matrix_mode(int mmode)
368 {
369 st.mmode = mmode;
370 }
372 void mgl_push_matrix(void)
373 {
374 float *topmat;
375 if(st.mtop[st.mmode] >= MATRIX_STACK_SIZE - 1) {
376 fprintf(stderr, "mgl_push_matrix: stack overflow\n");
377 abort();
378 }
380 topmat = st.matrix[st.mmode][st.mtop[st.mmode]];
381 memcpy(topmat + 16, topmat, 16 * sizeof *topmat);
382 st.mmode++;
383 }
385 void mgl_pop_matrix(void)
386 {
387 if(st.mtop[st.mmode] <= 0) {
388 fprintf(stderr, "mgl_pop_matrix: stack underflow\n");
389 abort();
390 }
391 st.mtop[st.mmode]--;
392 }
394 void mgl_load_matrix(float *mat)
395 {
396 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
397 memcpy(dest, mat, 16 * sizeof *dest);
398 }
400 #define M(i,j) (((i) << 2) + (j))
401 void mgl_mult_matrix(float *m2)
402 {
403 int i, j;
404 float m1[16];
405 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
407 memcpy(m1, dest, sizeof m1);
409 for(i=0; i<4; i++) {
410 for(j=0; j<4; j++) {
411 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
412 m1[M(1,j)] * m2[M(i,1)] +
413 m1[M(2,j)] * m2[M(i,2)] +
414 m1[M(3,j)] * m2[M(i,3)];
415 }
416 }
417 }
419 void mgl_load_identity(void)
420 {
421 static float id[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
422 mgl_load_matrix((float*)id);
423 }
425 void mgl_translate(float x, float y, float z)
426 {
427 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
428 xform[12] = x;
429 xform[13] = y;
430 xform[14] = z;
431 mgl_mult_matrix(xform);
432 }
434 void mgl_rotate(float deg, float x, float y, float z)
435 {
436 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
438 float angle = M_PI * deg / 180.0f;
439 float sina = sin(angle);
440 float cosa = cos(angle);
441 float one_minus_cosa = 1.0f - cosa;
442 float nxsq = x * x;
443 float nysq = y * y;
444 float nzsq = z * z;
446 xform[0] = nxsq + (1.0f - nxsq) * cosa;
447 xform[4] = x * y * one_minus_cosa - z * sina;
448 xform[8] = x * z * one_minus_cosa + y * sina;
449 xform[1] = x * y * one_minus_cosa + z * sina;
450 xform[5] = nysq + (1.0 - nysq) * cosa;
451 xform[9] = y * z * one_minus_cosa - x * sina;
452 xform[2] = x * z * one_minus_cosa - y * sina;
453 xform[6] = y * z * one_minus_cosa + x * sina;
454 xform[10] = nzsq + (1.0 - nzsq) * cosa;
456 mgl_mult_matrix(xform);
457 }
459 void mgl_scale(float x, float y, float z)
460 {
461 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
462 xform[0] = x;
463 xform[5] = y;
464 xform[10] = z;
465 mgl_mult_matrix(xform);
466 }
468 void gl_ortho(float left, float right, float bottom, float top, float nr, float fr)
469 {
470 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
472 float dx = right - left;
473 float dy = top - bottom;
474 float dz = fr - nr;
476 float tx = -(right + left) / dx;
477 float ty = -(top + bottom) / dy;
478 float tz = -(fr + nr) / dz;
480 float sx = 2.0 / dx;
481 float sy = 2.0 / dy;
482 float sz = -2.0 / dz;
484 xform[0] = sx;
485 xform[5] = sy;
486 xform[10] = sz;
487 xform[12] = tx;
488 xform[13] = ty;
489 xform[14] = tz;
491 mgl_mult_matrix(xform);
492 }
494 void mgl_frustum(float left, float right, float bottom, float top, float nr, float fr)
495 {
496 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
498 float dx = right - left;
499 float dy = top - bottom;
500 float dz = fr - nr;
502 float a = (right + left) / dx;
503 float b = (top + bottom) / dy;
504 float c = -(fr + nr) / dz;
505 float d = -2.0 * fr * nr / dz;
507 xform[0] = 2.0 * nr / dx;
508 xform[5] = 2.0 * nr / dy;
509 xform[8] = a;
510 xform[9] = b;
511 xform[10] = c;
512 xform[11] = -1.0f;
513 xform[14] = d;
515 mgl_mult_matrix(xform);
516 }
518 void mgl_perspective(float vfov, float aspect, float nr, float fr)
519 {
520 float vfov_rad = M_PI * vfov / 180.0;
521 float x = nr * tan(vfov_rad / 2.0);
522 mgl_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
523 }
525 void mgl_teximage(int width, int height, unsigned char *pixels)
526 {
527 st.tex.width = width;
528 st.tex.height = height;
529 st.tex.pixels = pixels;
531 if(calc_shiftmask(width, &st.tex.xshift, &st.tex.xmask) == -1 ||
532 calc_shiftmask(height, &st.tex.yshift, &st.tex.ymask) == -1) {
533 st.tex.pixels = 0;
534 }
535 }
537 #define MAX_SHIFT 12
538 static int calc_shiftmask(int val, int *shiftp, unsigned int *maskp)
539 {
540 int i;
542 for(i=0; i<MAX_SHIFT; i++) {
543 if((val >> i) == 1) {
544 *shiftp = i;
545 *maskp = ~(UINT_MAX << i);
546 return 0;
547 }
548 }
549 return -1;
550 }