dos3d

view src/mingl.c @ 17:1e9f0b3616fa

fixed the matrix multiplication order
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 30 Nov 2011 00:04:16 +0200
parents 7b574ba5758e
children 00d84ab1ef26
line source
1 /*
2 256-color 3D graphics hack for real-mode DOS.
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <math.h>
22 #include <limits.h>
23 #include <assert.h>
24 #include "mingl.h"
25 #include "mglimpl.h"
28 #define DOT(a, b) ((a).x * (b).x + (a).y * (b).y + (a).z * (b).z)
30 #define NORMALIZE(v) \
31 do { \
32 float mag = sqrt(DOT(v, v)); \
33 if(fabs(mag) > 1e-6) { \
34 float invmag = 1.0 / mag; \
35 (v).x *= invmag; \
36 (v).y *= invmag; \
37 (v).z *= invmag; \
38 } \
39 } while(0)
41 static void transform(vec4_t *res, vec4_t *v, float *mat);
42 static void transform3(vec3_t *res, vec3_t *v, float *mat);
43 static void vertex_proc(struct vertex *vert);
44 static int calc_shiftmask(int val, int *shiftp, unsigned int *maskp);
46 static struct state st;
47 static struct framebuffer fb;
49 int mgl_init(int width, int height)
50 {
51 int i;
53 st.flags = 0;
54 st.mmode = 0;
56 mgl_front_face(MGL_CCW);
57 mgl_cull_face(MGL_BACK);
59 st.curv.cidx = 0;
60 st.curv.energy = 1.0;
61 st.curv.norm.x = st.curv.norm.y = st.curv.norm.z = 0.0;
63 if(!(fb.pixels = malloc(width * height))) {
64 return -1;
65 }
66 fb.width = width;
67 fb.height = height;
68 fb.zbuf = 0;
70 if(mgl_rast_init(&st, &fb) == -1) {
71 free(fb.pixels);
72 return -1;
73 }
75 st.mtop[0] = st.mtop[1] = 0;
77 mgl_matrix_mode(MGL_MODELVIEW);
78 mgl_load_identity();
79 mgl_matrix_mode(MGL_PROJECTION);
80 mgl_load_identity();
82 /* initial viewport in the size of the framebuffer */
83 st.vp[0] = st.vp[1] = 0;
84 st.vp[2] = width;
85 st.vp[3] = height;
87 st.col_range = 256;
88 for(i=0; i<MAX_LIGHTS; i++) {
89 st.lpos[i].x = st.lpos[i].y = st.lpos[i].w = 0.0f;
90 st.lpos[i].z = 1.0f;
91 st.lint[i] = 0.0f;
92 }
94 return 0;
95 }
97 void mgl_free(void)
98 {
99 int i;
101 mgl_rast_cleanup();
102 free(fb.pixels);
103 fb.pixels = 0;
105 if(fb.zbuf) {
106 for(i=0; i<fb.num_ztiles; i++) {
107 free(fb.zbuf[i]);
108 }
109 free(fb.zbuf);
110 fb.zbuf = 0;
111 }
112 }
114 unsigned char *mgl_framebuffer(void)
115 {
116 return fb.pixels;
117 }
119 void mgl_clear(int cidx)
120 {
121 memset(fb.pixels, cidx, fb.width * fb.height);
122 }
124 void mgl_clear_depth(void)
125 {
126 int i;
128 if(!fb.zbuf) {
129 long num_pixels = (long)fb.width * (long)fb.height;
130 fb.num_ztiles = (num_pixels + ZTILE_SIZE - 1) / ZTILE_SIZE;
132 if(!(fb.zbuf = malloc(fb.num_ztiles * sizeof *fb.zbuf))) {
133 fprintf(stderr, "failed to allocate ztile array\n");
134 abort();
135 }
137 for(i=0; i<fb.num_ztiles; i++) {
138 if(!(fb.zbuf[i] = malloc(ZTILE_SIZE * 2))) {
139 fprintf(stderr, "failed to allocate ztile %d\n", i);
140 abort();
141 }
142 memset(fb.zbuf[i], 0xff, ZTILE_SIZE * 2);
143 }
144 return;
145 }
147 for(i=0; i<fb.num_ztiles; i++) {
148 memset(fb.zbuf[i], 0xff, ZTILE_SIZE * 2);
149 }
150 }
152 void mgl_enable(unsigned int bit)
153 {
154 st.flags |= bit;
155 }
157 void mgl_disable(unsigned int bit)
158 {
159 st.flags &= ~bit;
160 }
162 int mgl_isenabled(unsigned int bit)
163 {
164 return (st.flags & bit) != 0;
165 }
167 void mgl_front_face(int ff)
168 {
169 st.frontface = ff;
170 }
172 void mgl_cull_face(int cf)
173 {
174 st.cullface = cf;
175 }
177 void mgl_color_range(int rng)
178 {
179 st.col_range = rng;
180 }
182 void mgl_light_intensity(int ltidx, float intens)
183 {
184 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
185 st.lint[ltidx] = intens;
186 }
188 void mgl_light_position(int ltidx, float x, float y, float z, float w)
189 {
190 vec4_t pos;
191 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
193 pos.x = x;
194 pos.y = y;
195 pos.z = z;
196 pos.w = w;
197 transform(&st.lpos[ltidx], &pos, st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]]);
199 if(fabs(st.lpos[ltidx].w) < 1e-6) {
200 NORMALIZE(st.lpos[ltidx]);
201 } else {
202 st.lpos[ltidx].x /= st.lpos[ltidx].w;
203 st.lpos[ltidx].y /= st.lpos[ltidx].w;
204 st.lpos[ltidx].z /= st.lpos[ltidx].w;
205 }
206 }
208 void mgl_begin(int prim)
209 {
210 st.prim = prim;
211 st.vidx = 0;
213 st.ord = st.frontface;
214 if(st.cullface == MGL_FRONT) {
215 st.ord = st.frontface == MGL_CCW ? MGL_CW : MGL_CCW;
216 }
218 /* select the correct rasterizer according to state */
219 mgl_rast_prepare();
220 }
222 void mgl_end(void)
223 {
224 }
226 void mgl_vertex2f(float x, float y)
227 {
228 mgl_vertex4f(x, y, 0.0f, 1.0f);
229 }
231 void mgl_vertex3f(float x, float y, float z)
232 {
233 mgl_vertex4f(x, y, z, 1.0f);
234 }
236 void mgl_vertex4f(float x, float y, float z, float w)
237 {
238 st.v[st.vidx].pos.x = x;
239 st.v[st.vidx].pos.y = y;
240 st.v[st.vidx].pos.z = z;
241 st.v[st.vidx].pos.w = w;
242 st.v[st.vidx].cidx = st.curv.cidx;
243 st.v[st.vidx].energy = st.curv.energy;
244 st.v[st.vidx].norm = st.curv.norm;
245 st.v[st.vidx].tc = st.curv.tc;
247 vertex_proc(st.v + st.vidx);
249 if(++st.vidx >= st.prim) {
250 switch(st.prim) {
251 case MGL_POINTS:
252 mgl_draw_point(st.v);
253 break;
254 case MGL_LINES:
255 mgl_draw_line(st.v, st.v + 1);
256 break;
257 case MGL_TRIANGLES:
258 case MGL_QUADS:
259 mgl_draw_poly(st.v, st.prim);
260 break;
261 default:
262 fprintf(stderr, "invalid primitive: %d\n", st.prim);
263 abort();
264 }
265 st.vidx = 0;
266 }
267 }
269 void mgl_color1f(float energy)
270 {
271 st.curv.energy = energy;
272 }
274 void mgl_index(int c)
275 {
276 st.curv.cidx = c;
277 }
279 void mgl_normal(float x, float y, float z)
280 {
281 st.curv.norm.x = x;
282 st.curv.norm.y = y;
283 st.curv.norm.z = z;
284 }
286 void mgl_texcoord2f(float x, float y)
287 {
288 st.curv.tc.x = x;
289 st.curv.tc.y = y;
290 }
292 static void transform(vec4_t *res, vec4_t *v, float *mat)
293 {
294 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z + mat[12] * v->w;
295 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z + mat[13] * v->w;
296 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z + mat[14] * v->w;
297 res->w = mat[3] * v->x + mat[7] * v->y + mat[11] * v->z + mat[15] * v->w;
298 }
300 /* the matrix is 4x4 (16 floats), just ignoring anything out of the 3x3 */
301 static void transform3(vec3_t *res, vec3_t *v, float *mat)
302 {
303 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z;
304 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z;
305 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z;
306 }
308 static void vertex_proc(struct vertex *vert)
309 {
310 vec4_t pview, pclip;
312 float *mvmat = st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]];
313 float *pmat = st.matrix[MGL_PROJECTION][st.mtop[MGL_PROJECTION]];
315 /* modelview transformation */
316 transform(&pview, &vert->pos, mvmat);
318 if(st.flags & MGL_LIGHTING) {
319 if((st.flags & MGL_SMOOTH) || st.vidx == 0) {
320 int i;
321 vec3_t norm;
322 float irrad = 0.0f;
324 transform3(&norm, &vert->norm, mvmat);
326 for(i=0; i<MAX_LIGHTS; i++) {
327 if(st.lint[i] > 1e-6f) {
328 float ndotl;
329 vec3_t ldir;
331 if(st.lpos[i].w == 0.0) {
332 ldir.x = st.lpos[i].x;
333 ldir.y = st.lpos[i].y;
334 ldir.z = st.lpos[i].z;
335 } else {
336 ldir.x = st.lpos[i].x - pview.x;
337 ldir.y = st.lpos[i].y - pview.y;
338 ldir.z = st.lpos[i].z - pview.z;
340 NORMALIZE(ldir);
341 }
343 ndotl = DOT(norm, ldir);
344 if(ndotl < 0.0) {
345 ndotl = 0.0;
346 }
347 irrad += ndotl * st.lint[i];
348 }
349 }
350 vert->energy = irrad;
351 } else {
352 vert->energy = st.v[0].energy;
353 }
354 }
356 transform(&pclip, &pview, pmat);
357 /* TODO clipping in homogenous clip space */
359 if(pclip.w < 1e-6 && pclip.w > -1e-6) {
360 vert->pos.x = vert->pos.y = vert->pos.z = vert->pos.w = 0.0f;
361 return;
362 }
364 /* perspective division */
365 vert->pos.x = pclip.x / pclip.w;
366 vert->pos.y = pclip.y / pclip.w;
367 vert->pos.z = pclip.z / pclip.w;
368 vert->pos.w = pclip.w;
370 /* viewport transformation */
371 vert->pos.x = st.vp[0] + st.vp[2] * (vert->pos.x * 0.5 + 0.5);
372 vert->pos.y = st.vp[1] + st.vp[3] * (-vert->pos.y * 0.5 + 0.5);
373 }
375 void mgl_viewport(int x, int y, int width, int height)
376 {
377 st.vp[0] = x;
378 st.vp[1] = y;
379 st.vp[2] = width;
380 st.vp[3] = height;
381 }
383 void mgl_matrix_mode(int mmode)
384 {
385 st.mmode = mmode;
386 }
388 void mgl_push_matrix(void)
389 {
390 float *topmat;
391 if(st.mtop[st.mmode] >= MATRIX_STACK_SIZE - 1) {
392 fprintf(stderr, "mgl_push_matrix: stack overflow\n");
393 abort();
394 }
396 topmat = st.matrix[st.mmode][st.mtop[st.mmode]];
397 memcpy(topmat + 16, topmat, 16 * sizeof *topmat);
398 st.mmode++;
399 }
401 void mgl_pop_matrix(void)
402 {
403 if(st.mtop[st.mmode] <= 0) {
404 fprintf(stderr, "mgl_pop_matrix: stack underflow\n");
405 abort();
406 }
407 st.mtop[st.mmode]--;
408 }
410 void mgl_load_matrix(float *mat)
411 {
412 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
413 memcpy(dest, mat, 16 * sizeof *dest);
414 }
416 #define M(i,j) (((i) << 2) + (j))
417 void mgl_mult_matrix(float *m2)
418 {
419 int i, j;
420 float m1[16];
421 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
423 memcpy(m1, dest, sizeof m1);
425 for(i=0; i<4; i++) {
426 for(j=0; j<4; j++) {
427 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
428 m1[M(1,j)] * m2[M(i,1)] +
429 m1[M(2,j)] * m2[M(i,2)] +
430 m1[M(3,j)] * m2[M(i,3)];
431 }
432 }
433 }
435 void mgl_load_identity(void)
436 {
437 static float id[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
438 mgl_load_matrix((float*)id);
439 }
441 void mgl_translate(float x, float y, float z)
442 {
443 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
444 xform[12] = x;
445 xform[13] = y;
446 xform[14] = z;
447 mgl_mult_matrix(xform);
448 }
450 void mgl_rotate(float deg, float x, float y, float z)
451 {
452 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
454 float angle = M_PI * deg / 180.0f;
455 float sina = sin(angle);
456 float cosa = cos(angle);
457 float one_minus_cosa = 1.0f - cosa;
458 float nxsq = x * x;
459 float nysq = y * y;
460 float nzsq = z * z;
462 xform[0] = nxsq + (1.0f - nxsq) * cosa;
463 xform[4] = x * y * one_minus_cosa - z * sina;
464 xform[8] = x * z * one_minus_cosa + y * sina;
465 xform[1] = x * y * one_minus_cosa + z * sina;
466 xform[5] = nysq + (1.0 - nysq) * cosa;
467 xform[9] = y * z * one_minus_cosa - x * sina;
468 xform[2] = x * z * one_minus_cosa - y * sina;
469 xform[6] = y * z * one_minus_cosa + x * sina;
470 xform[10] = nzsq + (1.0 - nzsq) * cosa;
472 mgl_mult_matrix(xform);
473 }
475 void mgl_scale(float x, float y, float z)
476 {
477 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
478 xform[0] = x;
479 xform[5] = y;
480 xform[10] = z;
481 mgl_mult_matrix(xform);
482 }
484 void gl_ortho(float left, float right, float bottom, float top, float nr, float fr)
485 {
486 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
488 float dx = right - left;
489 float dy = top - bottom;
490 float dz = fr - nr;
492 float tx = -(right + left) / dx;
493 float ty = -(top + bottom) / dy;
494 float tz = -(fr + nr) / dz;
496 float sx = 2.0 / dx;
497 float sy = 2.0 / dy;
498 float sz = -2.0 / dz;
500 xform[0] = sx;
501 xform[5] = sy;
502 xform[10] = sz;
503 xform[12] = tx;
504 xform[13] = ty;
505 xform[14] = tz;
507 mgl_mult_matrix(xform);
508 }
510 void mgl_frustum(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 a = (right + left) / dx;
519 float b = (top + bottom) / dy;
520 float c = -(fr + nr) / dz;
521 float d = -2.0 * fr * nr / dz;
523 xform[0] = 2.0 * nr / dx;
524 xform[5] = 2.0 * nr / dy;
525 xform[8] = a;
526 xform[9] = b;
527 xform[10] = c;
528 xform[11] = -1.0f;
529 xform[14] = d;
531 mgl_mult_matrix(xform);
532 }
534 void mgl_perspective(float vfov, float aspect, float nr, float fr)
535 {
536 float vfov_rad = M_PI * vfov / 180.0;
537 float x = nr * tan(vfov_rad / 2.0);
538 mgl_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
539 }
541 void mgl_teximage(int width, int height, unsigned char *pixels)
542 {
543 st.tex.width = width;
544 st.tex.height = height;
545 st.tex.pixels = pixels;
547 if(calc_shiftmask(width, &st.tex.xshift, &st.tex.xmask) == -1 ||
548 calc_shiftmask(height, &st.tex.yshift, &st.tex.ymask) == -1) {
549 st.tex.pixels = 0;
550 }
551 }
553 #define MAX_SHIFT 12
554 static int calc_shiftmask(int val, int *shiftp, unsigned int *maskp)
555 {
556 int i;
558 for(i=0; i<MAX_SHIFT; i++) {
559 if((val >> i) == 1) {
560 *shiftp = i;
561 *maskp = ~(UINT_MAX << i);
562 return 0;
563 }
564 }
565 return -1;
566 }