deepstone

view src/mingl.c @ 13:7b574ba5758e

fixed the directional light
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Nov 2011 05:12:33 +0200
parents 0909996838ff
children 1e9f0b3616fa
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 NORMALIZE(st.lpos[ltidx]);
200 } else {
201 st.lpos[ltidx].x /= st.lpos[ltidx].w;
202 st.lpos[ltidx].y /= st.lpos[ltidx].w;
203 st.lpos[ltidx].z /= st.lpos[ltidx].w;
204 }
205 }
207 void mgl_begin(int prim)
208 {
209 st.prim = prim;
210 st.vidx = 0;
212 st.ord = st.frontface;
213 if(st.cullface == MGL_FRONT) {
214 st.ord = st.frontface == MGL_CCW ? MGL_CW : MGL_CCW;
215 }
217 /* select the correct rasterizer according to state */
218 mgl_rast_prepare();
219 }
221 void mgl_end(void)
222 {
223 }
225 void mgl_vertex2f(float x, float y)
226 {
227 mgl_vertex4f(x, y, 0.0f, 1.0f);
228 }
230 void mgl_vertex3f(float x, float y, float z)
231 {
232 mgl_vertex4f(x, y, z, 1.0f);
233 }
235 void mgl_vertex4f(float x, float y, float z, float w)
236 {
237 st.v[st.vidx].pos.x = x;
238 st.v[st.vidx].pos.y = y;
239 st.v[st.vidx].pos.z = z;
240 st.v[st.vidx].pos.w = w;
241 st.v[st.vidx].cidx = st.curv.cidx;
242 st.v[st.vidx].energy = st.curv.energy;
243 st.v[st.vidx].norm = st.curv.norm;
244 st.v[st.vidx].tc = st.curv.tc;
246 vertex_proc(st.v + st.vidx);
248 if(++st.vidx >= st.prim) {
249 switch(st.prim) {
250 case MGL_POINTS:
251 mgl_draw_point(st.v);
252 break;
253 case MGL_LINES:
254 mgl_draw_line(st.v, st.v + 1);
255 break;
256 case MGL_TRIANGLES:
257 case MGL_QUADS:
258 mgl_draw_poly(st.v, st.prim);
259 break;
260 default:
261 fprintf(stderr, "invalid primitive: %d\n", st.prim);
262 abort();
263 }
264 st.vidx = 0;
265 }
266 }
268 void mgl_color1f(float energy)
269 {
270 st.curv.energy = energy;
271 }
273 void mgl_index(int c)
274 {
275 st.curv.cidx = c;
276 }
278 void mgl_normal(float x, float y, float z)
279 {
280 st.curv.norm.x = x;
281 st.curv.norm.y = y;
282 st.curv.norm.z = z;
283 }
285 void mgl_texcoord2f(float x, float y)
286 {
287 st.curv.tc.x = x;
288 st.curv.tc.y = y;
289 }
291 static void transform(vec4_t *res, vec4_t *v, float *mat)
292 {
293 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z + mat[12] * v->w;
294 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z + mat[13] * v->w;
295 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z + mat[14] * v->w;
296 res->w = mat[3] * v->x + mat[7] * v->y + mat[11] * v->z + mat[15] * v->w;
297 }
299 /* the matrix is 4x4 (16 floats), just ignoring anything out of the 3x3 */
300 static void transform3(vec3_t *res, vec3_t *v, float *mat)
301 {
302 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z;
303 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z;
304 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z;
305 }
307 static void vertex_proc(struct vertex *vert)
308 {
309 vec4_t pview, pclip;
311 float *mvmat = st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]];
312 float *pmat = st.matrix[MGL_PROJECTION][st.mtop[MGL_PROJECTION]];
314 /* modelview transformation */
315 transform(&pview, &vert->pos, mvmat);
317 if(st.flags & MGL_LIGHTING) {
318 if((st.flags & MGL_SMOOTH) || st.vidx == 0) {
319 int i;
320 vec3_t norm;
321 float irrad = 0.0f;
323 transform3(&norm, &vert->norm, mvmat);
325 for(i=0; i<MAX_LIGHTS; i++) {
326 if(st.lint[i] > 1e-6f) {
327 float ndotl;
328 vec3_t ldir;
330 if(st.lpos[i].w == 0.0) {
331 ldir.x = st.lpos[i].x;
332 ldir.y = st.lpos[i].y;
333 ldir.z = st.lpos[i].z;
334 } else {
335 ldir.x = st.lpos[i].x - pview.x;
336 ldir.y = st.lpos[i].y - pview.y;
337 ldir.z = st.lpos[i].z - pview.z;
339 NORMALIZE(ldir);
340 }
342 ndotl = DOT(norm, ldir);
343 if(ndotl < 0.0) {
344 ndotl = 0.0;
345 }
346 irrad += ndotl * st.lint[i];
347 }
348 }
349 vert->energy = irrad;
350 } else {
351 vert->energy = st.v[0].energy;
352 }
353 }
355 transform(&pclip, &pview, pmat);
356 /* TODO clipping in homogenous clip space */
358 if(pclip.w < 1e-6 && pclip.w > -1e-6) {
359 vert->pos.x = vert->pos.y = vert->pos.z = vert->pos.w = 0.0f;
360 return;
361 }
363 /* perspective division */
364 vert->pos.x = pclip.x / pclip.w;
365 vert->pos.y = pclip.y / pclip.w;
366 vert->pos.z = pclip.z / pclip.w;
367 vert->pos.w = pclip.w;
369 /* viewport transformation */
370 vert->pos.x = st.vp[0] + st.vp[2] * (vert->pos.x * 0.5 + 0.5);
371 vert->pos.y = st.vp[1] + st.vp[3] * (-vert->pos.y * 0.5 + 0.5);
372 }
374 void mgl_viewport(int x, int y, int width, int height)
375 {
376 st.vp[0] = x;
377 st.vp[1] = y;
378 st.vp[2] = width;
379 st.vp[3] = height;
380 }
382 void mgl_matrix_mode(int mmode)
383 {
384 st.mmode = mmode;
385 }
387 void mgl_push_matrix(void)
388 {
389 float *topmat;
390 if(st.mtop[st.mmode] >= MATRIX_STACK_SIZE - 1) {
391 fprintf(stderr, "mgl_push_matrix: stack overflow\n");
392 abort();
393 }
395 topmat = st.matrix[st.mmode][st.mtop[st.mmode]];
396 memcpy(topmat + 16, topmat, 16 * sizeof *topmat);
397 st.mmode++;
398 }
400 void mgl_pop_matrix(void)
401 {
402 if(st.mtop[st.mmode] <= 0) {
403 fprintf(stderr, "mgl_pop_matrix: stack underflow\n");
404 abort();
405 }
406 st.mtop[st.mmode]--;
407 }
409 void mgl_load_matrix(float *mat)
410 {
411 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
412 memcpy(dest, mat, 16 * sizeof *dest);
413 }
415 #define M(i,j) (((j) << 2) + (i))
416 void mgl_mult_matrix(float *m2)
417 {
418 int i, j;
419 float m1[16];
420 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
422 memcpy(m1, dest, sizeof m1);
424 for(i=0; i<4; i++) {
425 for(j=0; j<4; j++) {
426 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
427 m1[M(1,j)] * m2[M(i,1)] +
428 m1[M(2,j)] * m2[M(i,2)] +
429 m1[M(3,j)] * m2[M(i,3)];
430 }
431 }
432 }
434 void mgl_load_identity(void)
435 {
436 static float id[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
437 mgl_load_matrix((float*)id);
438 }
440 void mgl_translate(float x, float y, float z)
441 {
442 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
443 xform[12] = x;
444 xform[13] = y;
445 xform[14] = z;
446 mgl_mult_matrix(xform);
447 }
449 void mgl_rotate(float deg, float x, float y, float z)
450 {
451 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
453 float angle = M_PI * deg / 180.0f;
454 float sina = sin(angle);
455 float cosa = cos(angle);
456 float one_minus_cosa = 1.0f - cosa;
457 float nxsq = x * x;
458 float nysq = y * y;
459 float nzsq = z * z;
461 xform[0] = nxsq + (1.0f - nxsq) * cosa;
462 xform[4] = x * y * one_minus_cosa - z * sina;
463 xform[8] = x * z * one_minus_cosa + y * sina;
464 xform[1] = x * y * one_minus_cosa + z * sina;
465 xform[5] = nysq + (1.0 - nysq) * cosa;
466 xform[9] = y * z * one_minus_cosa - x * sina;
467 xform[2] = x * z * one_minus_cosa - y * sina;
468 xform[6] = y * z * one_minus_cosa + x * sina;
469 xform[10] = nzsq + (1.0 - nzsq) * cosa;
471 mgl_mult_matrix(xform);
472 }
474 void mgl_scale(float x, float y, float z)
475 {
476 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
477 xform[0] = x;
478 xform[5] = y;
479 xform[10] = z;
480 mgl_mult_matrix(xform);
481 }
483 void gl_ortho(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 tx = -(right + left) / dx;
492 float ty = -(top + bottom) / dy;
493 float tz = -(fr + nr) / dz;
495 float sx = 2.0 / dx;
496 float sy = 2.0 / dy;
497 float sz = -2.0 / dz;
499 xform[0] = sx;
500 xform[5] = sy;
501 xform[10] = sz;
502 xform[12] = tx;
503 xform[13] = ty;
504 xform[14] = tz;
506 mgl_mult_matrix(xform);
507 }
509 void mgl_frustum(float left, float right, float bottom, float top, float nr, float fr)
510 {
511 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
513 float dx = right - left;
514 float dy = top - bottom;
515 float dz = fr - nr;
517 float a = (right + left) / dx;
518 float b = (top + bottom) / dy;
519 float c = -(fr + nr) / dz;
520 float d = -2.0 * fr * nr / dz;
522 xform[0] = 2.0 * nr / dx;
523 xform[5] = 2.0 * nr / dy;
524 xform[8] = a;
525 xform[9] = b;
526 xform[10] = c;
527 xform[11] = -1.0f;
528 xform[14] = d;
530 mgl_mult_matrix(xform);
531 }
533 void mgl_perspective(float vfov, float aspect, float nr, float fr)
534 {
535 float vfov_rad = M_PI * vfov / 180.0;
536 float x = nr * tan(vfov_rad / 2.0);
537 mgl_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
538 }
540 void mgl_teximage(int width, int height, unsigned char *pixels)
541 {
542 st.tex.width = width;
543 st.tex.height = height;
544 st.tex.pixels = pixels;
546 if(calc_shiftmask(width, &st.tex.xshift, &st.tex.xmask) == -1 ||
547 calc_shiftmask(height, &st.tex.yshift, &st.tex.ymask) == -1) {
548 st.tex.pixels = 0;
549 }
550 }
552 #define MAX_SHIFT 12
553 static int calc_shiftmask(int val, int *shiftp, unsigned int *maskp)
554 {
555 int i;
557 for(i=0; i<MAX_SHIFT; i++) {
558 if((val >> i) == 1) {
559 *shiftp = i;
560 *maskp = ~(0xffff << i);
561 return 0;
562 }
563 }
564 return -1;
565 }