dos3d

view src/mingl.c @ 11:0909996838ff

fucked up lighting and enabled key repeat in dosemu
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Nov 2011 05:02:08 +0200
parents bce78aaafc68
children 7b574ba5758e
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 <assert.h>
23 #include "mingl.h"
24 #include "mglimpl.h"
27 #define DOT(a, b) ((a).x * (b).x + (a).y * (b).y + (a).z * (b).z)
29 #define NORMALIZE(v) \
30 do { \
31 float mag = sqrt(DOT(v, v)); \
32 if(fabs(mag) > 1e-6) { \
33 float invmag = 1.0 / mag; \
34 (v).x *= invmag; \
35 (v).y *= invmag; \
36 (v).z *= invmag; \
37 } \
38 } while(0)
40 static void transform(vec4_t *res, vec4_t *v, float *mat);
41 static void transform3(vec3_t *res, vec3_t *v, float *mat);
42 static void vertex_proc(struct vertex *vert);
43 static int calc_shiftmask(int val, int *shiftp, unsigned int *maskp);
45 static struct state st;
46 static struct framebuffer fb;
48 int mgl_init(int width, int height)
49 {
50 int i;
52 st.flags = 0;
53 st.mmode = 0;
55 mgl_front_face(MGL_CCW);
56 mgl_cull_face(MGL_BACK);
58 st.curv.cidx = 0;
59 st.curv.energy = 1.0;
60 st.curv.norm.x = st.curv.norm.y = st.curv.norm.z = 0.0;
62 if(!(fb.pixels = malloc(width * height))) {
63 return -1;
64 }
65 fb.width = width;
66 fb.height = height;
67 fb.zbuf = 0;
69 if(mgl_rast_init(&st, &fb) == -1) {
70 free(fb.pixels);
71 return -1;
72 }
74 st.mtop[0] = st.mtop[1] = 0;
76 mgl_matrix_mode(MGL_MODELVIEW);
77 mgl_load_identity();
78 mgl_matrix_mode(MGL_PROJECTION);
79 mgl_load_identity();
81 /* initial viewport in the size of the framebuffer */
82 st.vp[0] = st.vp[1] = 0;
83 st.vp[2] = width;
84 st.vp[3] = height;
86 st.col_range = 256;
87 for(i=0; i<MAX_LIGHTS; i++) {
88 st.lpos[i].x = st.lpos[i].y = st.lpos[i].w = 0.0f;
89 st.lpos[i].z = 1.0f;
90 st.lint[i] = 0.0f;
91 }
93 return 0;
94 }
96 void mgl_free(void)
97 {
98 int i;
100 mgl_rast_cleanup();
101 free(fb.pixels);
102 fb.pixels = 0;
104 if(fb.zbuf) {
105 for(i=0; i<fb.num_ztiles; i++) {
106 free(fb.zbuf[i]);
107 }
108 free(fb.zbuf);
109 fb.zbuf = 0;
110 }
111 }
113 unsigned char *mgl_framebuffer(void)
114 {
115 return fb.pixels;
116 }
118 void mgl_clear(int cidx)
119 {
120 memset(fb.pixels, cidx, fb.width * fb.height);
121 }
123 void mgl_clear_depth(void)
124 {
125 int i;
127 if(!fb.zbuf) {
128 long num_pixels = (long)fb.width * (long)fb.height;
129 fb.num_ztiles = (num_pixels + ZTILE_SIZE - 1) / ZTILE_SIZE;
131 if(!(fb.zbuf = malloc(fb.num_ztiles * sizeof *fb.zbuf))) {
132 fprintf(stderr, "failed to allocate ztile array\n");
133 abort();
134 }
136 for(i=0; i<fb.num_ztiles; i++) {
137 if(!(fb.zbuf[i] = malloc(ZTILE_SIZE * 2))) {
138 fprintf(stderr, "failed to allocate ztile %d\n", i);
139 abort();
140 }
141 memset(fb.zbuf[i], 0xff, ZTILE_SIZE * 2);
142 }
143 return;
144 }
146 for(i=0; i<fb.num_ztiles; i++) {
147 memset(fb.zbuf[i], 0xff, ZTILE_SIZE * 2);
148 }
149 }
151 void mgl_enable(unsigned int bit)
152 {
153 st.flags |= bit;
154 }
156 void mgl_disable(unsigned int bit)
157 {
158 st.flags &= ~bit;
159 }
161 int mgl_isenabled(unsigned int bit)
162 {
163 return (st.flags & bit) != 0;
164 }
166 void mgl_front_face(int ff)
167 {
168 st.frontface = ff;
169 }
171 void mgl_cull_face(int cf)
172 {
173 st.cullface = cf;
174 }
176 void mgl_color_range(int rng)
177 {
178 st.col_range = rng;
179 }
181 void mgl_light_intensity(int ltidx, float intens)
182 {
183 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
184 st.lint[ltidx] = intens;
185 }
187 void mgl_light_position(int ltidx, float x, float y, float z, float w)
188 {
189 vec4_t pos;
190 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
192 pos.x = x;
193 pos.y = y;
194 pos.z = z;
195 pos.w = w;
196 transform(&st.lpos[ltidx], &pos, st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]]);
198 if(fabs(st.lpos[ltidx].w) < 1e-6) {
199 st.lpos[ltidx].w = 1.0;
201 NORMALIZE(st.lpos[ltidx]);
202 } else {
203 st.lpos[ltidx].x /= st.lpos[ltidx].w;
204 st.lpos[ltidx].y /= st.lpos[ltidx].w;
205 st.lpos[ltidx].z /= st.lpos[ltidx].w;
206 }
207 }
209 void mgl_begin(int prim)
210 {
211 st.prim = prim;
212 st.vidx = 0;
214 st.ord = st.frontface;
215 if(st.cullface == MGL_FRONT) {
216 st.ord = st.frontface == MGL_CCW ? MGL_CW : MGL_CCW;
217 }
219 /* select the correct rasterizer according to state */
220 mgl_rast_prepare();
221 }
223 void mgl_end(void)
224 {
225 }
227 void mgl_vertex2f(float x, float y)
228 {
229 mgl_vertex4f(x, y, 0.0f, 1.0f);
230 }
232 void mgl_vertex3f(float x, float y, float z)
233 {
234 mgl_vertex4f(x, y, z, 1.0f);
235 }
237 void mgl_vertex4f(float x, float y, float z, float w)
238 {
239 st.v[st.vidx].pos.x = x;
240 st.v[st.vidx].pos.y = y;
241 st.v[st.vidx].pos.z = z;
242 st.v[st.vidx].pos.w = w;
243 st.v[st.vidx].cidx = st.curv.cidx;
244 st.v[st.vidx].energy = st.curv.energy;
245 st.v[st.vidx].norm = st.curv.norm;
246 st.v[st.vidx].tc = st.curv.tc;
248 vertex_proc(st.v + st.vidx);
250 if(++st.vidx >= st.prim) {
251 switch(st.prim) {
252 case MGL_POINTS:
253 mgl_draw_point(st.v);
254 break;
255 case MGL_LINES:
256 mgl_draw_line(st.v, st.v + 1);
257 break;
258 case MGL_TRIANGLES:
259 case MGL_QUADS:
260 mgl_draw_poly(st.v, st.prim);
261 break;
262 default:
263 fprintf(stderr, "invalid primitive: %d\n", st.prim);
264 abort();
265 }
266 st.vidx = 0;
267 }
268 }
270 void mgl_color1f(float energy)
271 {
272 st.curv.energy = energy;
273 }
275 void mgl_index(int c)
276 {
277 st.curv.cidx = c;
278 }
280 void mgl_normal(float x, float y, float z)
281 {
282 st.curv.norm.x = x;
283 st.curv.norm.y = y;
284 st.curv.norm.z = z;
285 }
287 void mgl_texcoord2f(float x, float y)
288 {
289 st.curv.tc.x = x;
290 st.curv.tc.y = y;
291 }
293 static void transform(vec4_t *res, vec4_t *v, float *mat)
294 {
295 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z + mat[12] * v->w;
296 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z + mat[13] * v->w;
297 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z + mat[14] * v->w;
298 res->w = mat[3] * v->x + mat[7] * v->y + mat[11] * v->z + mat[15] * v->w;
299 }
301 /* the matrix is 4x4 (16 floats), just ignoring anything out of the 3x3 */
302 static void transform3(vec3_t *res, vec3_t *v, float *mat)
303 {
304 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z;
305 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z;
306 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z;
307 }
309 static void vertex_proc(struct vertex *vert)
310 {
311 vec4_t pview, pclip;
313 float *mvmat = st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]];
314 float *pmat = st.matrix[MGL_PROJECTION][st.mtop[MGL_PROJECTION]];
316 /* modelview transformation */
317 transform(&pview, &vert->pos, mvmat);
319 if(st.flags & MGL_LIGHTING) {
320 if((st.flags & MGL_SMOOTH) || st.vidx == 0) {
321 int i;
322 vec3_t norm;
323 float irrad = 0.0f;
325 transform3(&norm, &vert->norm, mvmat);
327 for(i=0; i<MAX_LIGHTS; i++) {
328 if(st.lint[i] > 1e-6f) {
329 float ndotl;
330 vec3_t ldir;
332 if(st.lpos[i].w == 0.0) {
333 ldir.x = st.lpos[i].x;
334 ldir.y = st.lpos[i].y;
335 ldir.z = st.lpos[i].z;
336 } else {
337 ldir.x = st.lpos[i].x - pview.x;
338 ldir.y = st.lpos[i].y - pview.y;
339 ldir.z = st.lpos[i].z - pview.z;
341 NORMALIZE(ldir);
342 }
344 ndotl = DOT(norm, ldir);
345 if(ndotl < 0.0) {
346 ndotl = 0.0;
347 }
348 irrad += ndotl * st.lint[i];
349 }
350 }
351 vert->energy = irrad;
352 } else {
353 vert->energy = st.v[0].energy;
354 }
355 }
357 transform(&pclip, &pview, pmat);
358 /* TODO clipping in homogenous clip space */
360 if(pclip.w < 1e-6 && pclip.w > -1e-6) {
361 vert->pos.x = vert->pos.y = vert->pos.z = vert->pos.w = 0.0f;
362 return;
363 }
365 /* perspective division */
366 vert->pos.x = pclip.x / pclip.w;
367 vert->pos.y = pclip.y / pclip.w;
368 vert->pos.z = pclip.z / pclip.w;
369 vert->pos.w = pclip.w;
371 /* viewport transformation */
372 vert->pos.x = st.vp[0] + st.vp[2] * (vert->pos.x * 0.5 + 0.5);
373 vert->pos.y = st.vp[1] + st.vp[3] * (-vert->pos.y * 0.5 + 0.5);
374 }
376 void mgl_viewport(int x, int y, int width, int height)
377 {
378 st.vp[0] = x;
379 st.vp[1] = y;
380 st.vp[2] = width;
381 st.vp[3] = height;
382 }
384 void mgl_matrix_mode(int mmode)
385 {
386 st.mmode = mmode;
387 }
389 void mgl_push_matrix(void)
390 {
391 float *topmat;
392 if(st.mtop[st.mmode] >= MATRIX_STACK_SIZE - 1) {
393 fprintf(stderr, "mgl_push_matrix: stack overflow\n");
394 abort();
395 }
397 topmat = st.matrix[st.mmode][st.mtop[st.mmode]];
398 memcpy(topmat + 16, topmat, 16 * sizeof *topmat);
399 st.mmode++;
400 }
402 void mgl_pop_matrix(void)
403 {
404 if(st.mtop[st.mmode] <= 0) {
405 fprintf(stderr, "mgl_pop_matrix: stack underflow\n");
406 abort();
407 }
408 st.mtop[st.mmode]--;
409 }
411 void mgl_load_matrix(float *mat)
412 {
413 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
414 memcpy(dest, mat, 16 * sizeof *dest);
415 }
417 #define M(i,j) (((j) << 2) + (i))
418 void mgl_mult_matrix(float *m2)
419 {
420 int i, j;
421 float m1[16];
422 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
424 memcpy(m1, dest, sizeof m1);
426 for(i=0; i<4; i++) {
427 for(j=0; j<4; j++) {
428 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
429 m1[M(1,j)] * m2[M(i,1)] +
430 m1[M(2,j)] * m2[M(i,2)] +
431 m1[M(3,j)] * m2[M(i,3)];
432 }
433 }
434 }
436 void mgl_load_identity(void)
437 {
438 static float id[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
439 mgl_load_matrix((float*)id);
440 }
442 void mgl_translate(float x, float y, float z)
443 {
444 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
445 xform[12] = x;
446 xform[13] = y;
447 xform[14] = z;
448 mgl_mult_matrix(xform);
449 }
451 void mgl_rotate(float deg, float x, float y, float z)
452 {
453 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
455 float angle = M_PI * deg / 180.0f;
456 float sina = sin(angle);
457 float cosa = cos(angle);
458 float one_minus_cosa = 1.0f - cosa;
459 float nxsq = x * x;
460 float nysq = y * y;
461 float nzsq = z * z;
463 xform[0] = nxsq + (1.0f - nxsq) * cosa;
464 xform[4] = x * y * one_minus_cosa - z * sina;
465 xform[8] = x * z * one_minus_cosa + y * sina;
466 xform[1] = x * y * one_minus_cosa + z * sina;
467 xform[5] = nysq + (1.0 - nysq) * cosa;
468 xform[9] = y * z * one_minus_cosa - x * sina;
469 xform[2] = x * z * one_minus_cosa - y * sina;
470 xform[6] = y * z * one_minus_cosa + x * sina;
471 xform[10] = nzsq + (1.0 - nzsq) * cosa;
473 mgl_mult_matrix(xform);
474 }
476 void mgl_scale(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};
479 xform[0] = x;
480 xform[5] = y;
481 xform[10] = z;
482 mgl_mult_matrix(xform);
483 }
485 void gl_ortho(float left, float right, float bottom, float top, float nr, float fr)
486 {
487 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
489 float dx = right - left;
490 float dy = top - bottom;
491 float dz = fr - nr;
493 float tx = -(right + left) / dx;
494 float ty = -(top + bottom) / dy;
495 float tz = -(fr + nr) / dz;
497 float sx = 2.0 / dx;
498 float sy = 2.0 / dy;
499 float sz = -2.0 / dz;
501 xform[0] = sx;
502 xform[5] = sy;
503 xform[10] = sz;
504 xform[12] = tx;
505 xform[13] = ty;
506 xform[14] = tz;
508 mgl_mult_matrix(xform);
509 }
511 void mgl_frustum(float left, float right, float bottom, float top, float nr, float fr)
512 {
513 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
515 float dx = right - left;
516 float dy = top - bottom;
517 float dz = fr - nr;
519 float a = (right + left) / dx;
520 float b = (top + bottom) / dy;
521 float c = -(fr + nr) / dz;
522 float d = -2.0 * fr * nr / dz;
524 xform[0] = 2.0 * nr / dx;
525 xform[5] = 2.0 * nr / dy;
526 xform[8] = a;
527 xform[9] = b;
528 xform[10] = c;
529 xform[11] = -1.0f;
530 xform[14] = d;
532 mgl_mult_matrix(xform);
533 }
535 void mgl_perspective(float vfov, float aspect, float nr, float fr)
536 {
537 float vfov_rad = M_PI * vfov / 180.0;
538 float x = nr * tan(vfov_rad / 2.0);
539 mgl_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
540 }
542 void mgl_teximage(int width, int height, unsigned char *pixels)
543 {
544 st.tex.width = width;
545 st.tex.height = height;
546 st.tex.pixels = pixels;
548 if(calc_shiftmask(width, &st.tex.xshift, &st.tex.xmask) == -1 ||
549 calc_shiftmask(height, &st.tex.yshift, &st.tex.ymask) == -1) {
550 st.tex.pixels = 0;
551 }
552 }
554 #define MAX_SHIFT 12
555 static int calc_shiftmask(int val, int *shiftp, unsigned int *maskp)
556 {
557 int i;
559 for(i=0; i<MAX_SHIFT; i++) {
560 if((val >> i) == 1) {
561 *shiftp = i;
562 *maskp = ~(0xffff << i);
563 return 0;
564 }
565 }
566 return -1;
567 }