deepstone
view src/mingl.c @ 9:bce78aaafc68
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 26 Nov 2011 03:59:48 +0200 |
parents | 0e781cc43178 |
children | 0909996838ff |
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 static void transform(vec4_t *res, vec4_t *v, float *mat);
30 static void transform3(vec3_t *res, vec3_t *v, float *mat);
31 static void vertex_proc(struct vertex *vert);
32 static int calc_shiftmask(int val, int *shiftp, unsigned int *maskp);
34 static struct state st;
35 static struct framebuffer fb;
37 int mgl_init(int width, int height)
38 {
39 int i;
41 st.flags = 0;
42 st.mmode = 0;
44 mgl_front_face(MGL_CCW);
45 mgl_cull_face(MGL_BACK);
47 st.curv.cidx = 0;
48 st.curv.energy = 1.0;
49 st.curv.norm.x = st.curv.norm.y = st.curv.norm.z = 0.0;
51 if(!(fb.pixels = malloc(width * height))) {
52 return -1;
53 }
54 fb.width = width;
55 fb.height = height;
56 fb.zbuf = 0;
58 if(mgl_rast_init(&st, &fb) == -1) {
59 free(fb.pixels);
60 return -1;
61 }
63 st.mtop[0] = st.mtop[1] = 0;
65 mgl_matrix_mode(MGL_MODELVIEW);
66 mgl_load_identity();
67 mgl_matrix_mode(MGL_PROJECTION);
68 mgl_load_identity();
70 /* initial viewport in the size of the framebuffer */
71 st.vp[0] = st.vp[1] = 0;
72 st.vp[2] = width;
73 st.vp[3] = height;
75 st.col_range = 256;
76 for(i=0; i<MAX_LIGHTS; i++) {
77 st.ldir[i].x = st.ldir[i].y = 0.0f;
78 st.ldir[i].z = 1.0f;
79 st.lint[i] = 0.0f;
80 }
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 bit)
141 {
142 st.flags |= bit;
143 }
145 void mgl_disable(unsigned int bit)
146 {
147 st.flags &= ~bit;
148 }
150 int mgl_isenabled(unsigned int bit)
151 {
152 return (st.flags & bit) != 0;
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_color_range(int rng)
166 {
167 st.col_range = rng;
168 }
170 void mgl_light_intensity(int ltidx, float intens)
171 {
172 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
173 st.lint[ltidx] = intens;
174 }
176 void mgl_light_direction(int ltidx, float x, float y, float z)
177 {
178 vec3_t dir;
179 float mag;
180 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
182 dir.x = x;
183 dir.y = y;
184 dir.z = z;
185 transform3(&st.ldir[ltidx], &dir, st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]]);
187 mag = sqrt(DOT(st.ldir[ltidx], st.ldir[ltidx]));
188 if(fabs(mag) < 1e-6) {
189 mag = 1.0f;
190 }
191 st.ldir[ltidx].x /= mag;
192 st.ldir[ltidx].y /= mag;
193 st.ldir[ltidx].z /= mag;
194 }
196 void mgl_begin(int prim)
197 {
198 st.prim = prim;
199 st.vidx = 0;
201 st.ord = st.frontface;
202 if(st.cullface == MGL_FRONT) {
203 st.ord = st.frontface == MGL_CCW ? MGL_CW : MGL_CCW;
204 }
206 /* select the correct rasterizer according to state */
207 mgl_rast_prepare();
208 }
210 void mgl_end(void)
211 {
212 }
214 void mgl_vertex2f(float x, float y)
215 {
216 mgl_vertex4f(x, y, 0.0f, 1.0f);
217 }
219 void mgl_vertex3f(float x, float y, float z)
220 {
221 mgl_vertex4f(x, y, z, 1.0f);
222 }
224 void mgl_vertex4f(float x, float y, float z, float w)
225 {
226 st.v[st.vidx].pos.x = x;
227 st.v[st.vidx].pos.y = y;
228 st.v[st.vidx].pos.z = z;
229 st.v[st.vidx].pos.w = w;
230 st.v[st.vidx].cidx = st.curv.cidx;
231 st.v[st.vidx].energy = st.curv.energy;
232 st.v[st.vidx].norm = st.curv.norm;
233 st.v[st.vidx].tc = st.curv.tc;
235 vertex_proc(st.v + st.vidx);
237 if(++st.vidx >= st.prim) {
238 switch(st.prim) {
239 case MGL_POINTS:
240 mgl_draw_point(st.v);
241 break;
242 case MGL_LINES:
243 mgl_draw_line(st.v, st.v + 1);
244 break;
245 case MGL_TRIANGLES:
246 case MGL_QUADS:
247 mgl_draw_poly(st.v, st.prim);
248 break;
249 default:
250 fprintf(stderr, "invalid primitive: %d\n", st.prim);
251 abort();
252 }
253 st.vidx = 0;
254 }
255 }
257 void mgl_color1f(float energy)
258 {
259 st.curv.energy = energy;
260 }
262 void mgl_index(int c)
263 {
264 st.curv.cidx = c;
265 }
267 void mgl_normal(float x, float y, float z)
268 {
269 st.curv.norm.x = x;
270 st.curv.norm.y = y;
271 st.curv.norm.z = z;
272 }
274 void mgl_texcoord2f(float x, float y)
275 {
276 st.curv.tc.x = x;
277 st.curv.tc.y = y;
278 }
280 static void transform(vec4_t *res, vec4_t *v, float *mat)
281 {
282 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z + mat[12] * v->w;
283 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z + mat[13] * v->w;
284 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z + mat[14] * v->w;
285 res->w = mat[3] * v->x + mat[7] * v->y + mat[11] * v->z + mat[15] * v->w;
286 }
288 /* the matrix is 4x4 (16 floats), just ignoring anything out of the 3x3 */
289 static void transform3(vec3_t *res, vec3_t *v, float *mat)
290 {
291 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z;
292 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z;
293 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z;
294 }
296 static void vertex_proc(struct vertex *vert)
297 {
298 vec4_t pview, pclip;
300 float *mvmat = st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]];
301 float *pmat = st.matrix[MGL_PROJECTION][st.mtop[MGL_PROJECTION]];
303 /* modelview transformation */
304 transform(&pview, &vert->pos, mvmat);
306 if(st.flags & MGL_LIGHTING) {
307 if((st.flags & MGL_SMOOTH) || st.vidx == 0) {
308 int i;
309 vec3_t norm;
310 float irrad = 0.0f;
312 transform3(&norm, &vert->norm, mvmat);
314 for(i=0; i<MAX_LIGHTS; i++) {
315 if(st.lint[i] > 1e-6f) {
316 float ndotl = DOT(norm, st.ldir[i]);
317 if(ndotl < 0.0) {
318 ndotl = 0.0;
319 }
320 irrad += ndotl * st.lint[i];
321 }
322 }
323 vert->energy = irrad;
324 } else {
325 vert->energy = st.v[0].energy;
326 }
327 }
329 transform(&pclip, &pview, pmat);
330 /* TODO clipping in homogenous clip space */
332 if(pclip.w < 1e-6 && pclip.w > -1e-6) {
333 vert->pos.x = vert->pos.y = vert->pos.z = vert->pos.w = 0.0f;
334 return;
335 }
337 /* perspective division */
338 vert->pos.x = pclip.x / pclip.w;
339 vert->pos.y = pclip.y / pclip.w;
340 vert->pos.z = pclip.z / pclip.w;
341 vert->pos.w = pclip.w;
343 /* viewport transformation */
344 vert->pos.x = st.vp[0] + st.vp[2] * (vert->pos.x * 0.5 + 0.5);
345 vert->pos.y = st.vp[1] + st.vp[3] * (-vert->pos.y * 0.5 + 0.5);
346 }
348 void mgl_viewport(int x, int y, int width, int height)
349 {
350 st.vp[0] = x;
351 st.vp[1] = y;
352 st.vp[2] = width;
353 st.vp[3] = height;
354 }
356 void mgl_matrix_mode(int mmode)
357 {
358 st.mmode = mmode;
359 }
361 void mgl_push_matrix(void)
362 {
363 float *topmat;
364 if(st.mtop[st.mmode] >= MATRIX_STACK_SIZE - 1) {
365 fprintf(stderr, "mgl_push_matrix: stack overflow\n");
366 abort();
367 }
369 topmat = st.matrix[st.mmode][st.mtop[st.mmode]];
370 memcpy(topmat + 16, topmat, 16 * sizeof *topmat);
371 st.mmode++;
372 }
374 void mgl_pop_matrix(void)
375 {
376 if(st.mtop[st.mmode] <= 0) {
377 fprintf(stderr, "mgl_pop_matrix: stack underflow\n");
378 abort();
379 }
380 st.mtop[st.mmode]--;
381 }
383 void mgl_load_matrix(float *mat)
384 {
385 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
386 memcpy(dest, mat, 16 * sizeof *dest);
387 }
389 #define M(i,j) (((j) << 2) + (i))
390 void mgl_mult_matrix(float *m2)
391 {
392 int i, j;
393 float m1[16];
394 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
396 memcpy(m1, dest, sizeof m1);
398 for(i=0; i<4; i++) {
399 for(j=0; j<4; j++) {
400 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
401 m1[M(1,j)] * m2[M(i,1)] +
402 m1[M(2,j)] * m2[M(i,2)] +
403 m1[M(3,j)] * m2[M(i,3)];
404 }
405 }
406 }
408 void mgl_load_identity(void)
409 {
410 static float id[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
411 mgl_load_matrix((float*)id);
412 }
414 void mgl_translate(float x, float y, float z)
415 {
416 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
417 xform[12] = x;
418 xform[13] = y;
419 xform[14] = z;
420 mgl_mult_matrix(xform);
421 }
423 void mgl_rotate(float deg, float x, float y, float z)
424 {
425 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
427 float angle = M_PI * deg / 180.0f;
428 float sina = sin(angle);
429 float cosa = cos(angle);
430 float one_minus_cosa = 1.0f - cosa;
431 float nxsq = x * x;
432 float nysq = y * y;
433 float nzsq = z * z;
435 xform[0] = nxsq + (1.0f - nxsq) * cosa;
436 xform[4] = x * y * one_minus_cosa - z * sina;
437 xform[8] = x * z * one_minus_cosa + y * sina;
438 xform[1] = x * y * one_minus_cosa + z * sina;
439 xform[5] = nysq + (1.0 - nysq) * cosa;
440 xform[9] = y * z * one_minus_cosa - x * sina;
441 xform[2] = x * z * one_minus_cosa - y * sina;
442 xform[6] = y * z * one_minus_cosa + x * sina;
443 xform[10] = nzsq + (1.0 - nzsq) * cosa;
445 mgl_mult_matrix(xform);
446 }
448 void mgl_scale(float x, float y, float z)
449 {
450 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
451 xform[0] = x;
452 xform[5] = y;
453 xform[10] = z;
454 mgl_mult_matrix(xform);
455 }
457 void gl_ortho(float left, float right, float bottom, float top, float nr, float fr)
458 {
459 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
461 float dx = right - left;
462 float dy = top - bottom;
463 float dz = fr - nr;
465 float tx = -(right + left) / dx;
466 float ty = -(top + bottom) / dy;
467 float tz = -(fr + nr) / dz;
469 float sx = 2.0 / dx;
470 float sy = 2.0 / dy;
471 float sz = -2.0 / dz;
473 xform[0] = sx;
474 xform[5] = sy;
475 xform[10] = sz;
476 xform[12] = tx;
477 xform[13] = ty;
478 xform[14] = tz;
480 mgl_mult_matrix(xform);
481 }
483 void mgl_frustum(float left, float right, float bottom, float top, float nr, float fr)
484 {
485 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
487 float dx = right - left;
488 float dy = top - bottom;
489 float dz = fr - nr;
491 float a = (right + left) / dx;
492 float b = (top + bottom) / dy;
493 float c = -(fr + nr) / dz;
494 float d = -2.0 * fr * nr / dz;
496 xform[0] = 2.0 * nr / dx;
497 xform[5] = 2.0 * nr / dy;
498 xform[8] = a;
499 xform[9] = b;
500 xform[10] = c;
501 xform[11] = -1.0f;
502 xform[14] = d;
504 mgl_mult_matrix(xform);
505 }
507 void mgl_perspective(float vfov, float aspect, float nr, float fr)
508 {
509 float vfov_rad = M_PI * vfov / 180.0;
510 float x = nr * tan(vfov_rad / 2.0);
511 mgl_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
512 }
514 void mgl_teximage(int width, int height, unsigned char *pixels)
515 {
516 st.tex.width = width;
517 st.tex.height = height;
518 st.tex.pixels = pixels;
520 if(calc_shiftmask(width, &st.tex.xshift, &st.tex.xmask) == -1 ||
521 calc_shiftmask(height, &st.tex.yshift, &st.tex.ymask) == -1) {
522 st.tex.pixels = 0;
523 }
524 }
526 #define MAX_SHIFT 12
527 static int calc_shiftmask(int val, int *shiftp, unsigned int *maskp)
528 {
529 int i;
531 for(i=0; i<MAX_SHIFT; i++) {
532 if((val >> i) == 1) {
533 *shiftp = i;
534 *maskp = ~(0xffff << i);
535 return 0;
536 }
537 }
538 return -1;
539 }