deepstone

view src/mingl.c @ 3:0e781cc43178

adding textures
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 21 Nov 2011 10:16:09 +0200
parents f04884489bad
children bce78aaafc68
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 mgl_rast_cleanup();
88 free(fb.pixels);
89 }
91 unsigned char *mgl_framebuffer(void)
92 {
93 return fb.pixels;
94 }
96 void mgl_clear(int cidx)
97 {
98 memset(fb.pixels, cidx, fb.width * fb.height);
99 }
101 void mgl_enable(unsigned int bit)
102 {
103 st.flags |= bit;
104 }
106 void mgl_disable(unsigned int bit)
107 {
108 st.flags &= ~bit;
109 }
111 int mgl_isenabled(unsigned int bit)
112 {
113 return (st.flags & bit) != 0;
114 }
116 void mgl_front_face(int ff)
117 {
118 st.frontface = ff;
119 }
121 void mgl_cull_face(int cf)
122 {
123 st.cullface = cf;
124 }
126 void mgl_color_range(int rng)
127 {
128 st.col_range = rng;
129 }
131 void mgl_light_intensity(int ltidx, float intens)
132 {
133 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
134 st.lint[ltidx] = intens;
135 }
137 void mgl_light_direction(int ltidx, float x, float y, float z)
138 {
139 vec3_t dir;
140 float mag;
141 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
143 dir.x = x;
144 dir.y = y;
145 dir.z = z;
146 transform3(&st.ldir[ltidx], &dir, st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]]);
148 mag = sqrt(DOT(st.ldir[ltidx], st.ldir[ltidx]));
149 if(fabs(mag) < 1e-6) {
150 mag = 1.0f;
151 }
152 st.ldir[ltidx].x /= mag;
153 st.ldir[ltidx].y /= mag;
154 st.ldir[ltidx].z /= mag;
155 }
157 void mgl_begin(int prim)
158 {
159 st.prim = prim;
160 st.vidx = 0;
162 st.ord = st.frontface;
163 if(st.cullface == MGL_FRONT) {
164 st.ord = st.frontface == MGL_CCW ? MGL_CW : MGL_CCW;
165 }
167 /* select the correct rasterizer according to state */
168 mgl_rast_prepare();
169 }
171 void mgl_end(void)
172 {
173 }
175 void mgl_vertex2f(float x, float y)
176 {
177 mgl_vertex4f(x, y, 0.0f, 1.0f);
178 }
180 void mgl_vertex3f(float x, float y, float z)
181 {
182 mgl_vertex4f(x, y, z, 1.0f);
183 }
185 void mgl_vertex4f(float x, float y, float z, float w)
186 {
187 st.v[st.vidx].pos.x = x;
188 st.v[st.vidx].pos.y = y;
189 st.v[st.vidx].pos.z = z;
190 st.v[st.vidx].pos.w = w;
191 st.v[st.vidx].cidx = st.curv.cidx;
192 st.v[st.vidx].energy = st.curv.energy;
193 st.v[st.vidx].norm = st.curv.norm;
194 st.v[st.vidx].tc = st.curv.tc;
196 vertex_proc(st.v + st.vidx);
198 if(++st.vidx >= st.prim) {
199 switch(st.prim) {
200 case MGL_POINTS:
201 mgl_draw_point(st.v);
202 break;
203 case MGL_LINES:
204 mgl_draw_line(st.v, st.v + 1);
205 break;
206 case MGL_TRIANGLES:
207 case MGL_QUADS:
208 mgl_draw_poly(st.v, st.prim);
209 break;
210 default:
211 fprintf(stderr, "invalid primitive: %d\n", st.prim);
212 abort();
213 }
214 st.vidx = 0;
215 }
216 }
218 void mgl_color1f(float energy)
219 {
220 st.curv.energy = energy;
221 }
223 void mgl_index(int c)
224 {
225 st.curv.cidx = c;
226 }
228 void mgl_normal(float x, float y, float z)
229 {
230 st.curv.norm.x = x;
231 st.curv.norm.y = y;
232 st.curv.norm.z = z;
233 }
235 void mgl_texcoord2f(float x, float y)
236 {
237 st.curv.tc.x = x;
238 st.curv.tc.y = y;
239 }
241 static void transform(vec4_t *res, vec4_t *v, float *mat)
242 {
243 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z + mat[12] * v->w;
244 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z + mat[13] * v->w;
245 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z + mat[14] * v->w;
246 res->w = mat[3] * v->x + mat[7] * v->y + mat[11] * v->z + mat[15] * v->w;
247 }
249 /* the matrix is 4x4 (16 floats), just ignoring anything out of the 3x3 */
250 static void transform3(vec3_t *res, vec3_t *v, float *mat)
251 {
252 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z;
253 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z;
254 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z;
255 }
257 static void vertex_proc(struct vertex *vert)
258 {
259 vec4_t pview, pclip;
261 float *mvmat = st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]];
262 float *pmat = st.matrix[MGL_PROJECTION][st.mtop[MGL_PROJECTION]];
264 /* modelview transformation */
265 transform(&pview, &vert->pos, mvmat);
267 if(st.flags & MGL_LIGHTING) {
268 if((st.flags & MGL_SMOOTH) || st.vidx == 0) {
269 int i;
270 vec3_t norm;
271 float irrad = 0.0f;
273 transform3(&norm, &vert->norm, mvmat);
275 for(i=0; i<MAX_LIGHTS; i++) {
276 if(st.lint[i] > 1e-6f) {
277 float ndotl = DOT(norm, st.ldir[i]);
278 if(ndotl < 0.0) {
279 ndotl = 0.0;
280 }
281 irrad += ndotl * st.lint[i];
282 }
283 }
284 vert->energy = irrad;
285 } else {
286 vert->energy = st.v[0].energy;
287 }
288 }
290 transform(&pclip, &pview, pmat);
291 /* TODO clipping in homogenous clip space */
293 if(pclip.w < 1e-6 && pclip.w > -1e-6) {
294 vert->pos.x = vert->pos.y = vert->pos.z = vert->pos.w = 0.0f;
295 return;
296 }
298 /* perspective division */
299 vert->pos.x = pclip.x / pclip.w;
300 vert->pos.y = pclip.y / pclip.w;
301 vert->pos.z = pclip.z / pclip.w;
302 vert->pos.w = pclip.w;
304 /* viewport transformation */
305 vert->pos.x = st.vp[0] + st.vp[2] * (vert->pos.x * 0.5 + 0.5);
306 vert->pos.y = st.vp[1] + st.vp[3] * (-vert->pos.y * 0.5 + 0.5);
307 }
309 void mgl_viewport(int x, int y, int width, int height)
310 {
311 st.vp[0] = x;
312 st.vp[1] = y;
313 st.vp[2] = width;
314 st.vp[3] = height;
315 }
317 void mgl_matrix_mode(int mmode)
318 {
319 st.mmode = mmode;
320 }
322 void mgl_push_matrix(void)
323 {
324 float *topmat;
325 if(st.mtop[st.mmode] >= MATRIX_STACK_SIZE - 1) {
326 fprintf(stderr, "mgl_push_matrix: stack overflow\n");
327 abort();
328 }
330 topmat = st.matrix[st.mmode][st.mtop[st.mmode]];
331 memcpy(topmat + 16, topmat, 16 * sizeof *topmat);
332 st.mmode++;
333 }
335 void mgl_pop_matrix(void)
336 {
337 if(st.mtop[st.mmode] <= 0) {
338 fprintf(stderr, "mgl_pop_matrix: stack underflow\n");
339 abort();
340 }
341 st.mtop[st.mmode]--;
342 }
344 void mgl_load_matrix(float *mat)
345 {
346 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
347 memcpy(dest, mat, 16 * sizeof *dest);
348 }
350 #define M(i,j) (((j) << 2) + (i))
351 void mgl_mult_matrix(float *m2)
352 {
353 int i, j;
354 float m1[16];
355 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
357 memcpy(m1, dest, sizeof m1);
359 for(i=0; i<4; i++) {
360 for(j=0; j<4; j++) {
361 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
362 m1[M(1,j)] * m2[M(i,1)] +
363 m1[M(2,j)] * m2[M(i,2)] +
364 m1[M(3,j)] * m2[M(i,3)];
365 }
366 }
367 }
369 void mgl_load_identity(void)
370 {
371 static float id[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
372 mgl_load_matrix((float*)id);
373 }
375 void mgl_translate(float x, float y, float z)
376 {
377 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
378 xform[12] = x;
379 xform[13] = y;
380 xform[14] = z;
381 mgl_mult_matrix(xform);
382 }
384 void mgl_rotate(float deg, float x, float y, float z)
385 {
386 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
388 float angle = M_PI * deg / 180.0f;
389 float sina = sin(angle);
390 float cosa = cos(angle);
391 float one_minus_cosa = 1.0f - cosa;
392 float nxsq = x * x;
393 float nysq = y * y;
394 float nzsq = z * z;
396 xform[0] = nxsq + (1.0f - nxsq) * cosa;
397 xform[4] = x * y * one_minus_cosa - z * sina;
398 xform[8] = x * z * one_minus_cosa + y * sina;
399 xform[1] = x * y * one_minus_cosa + z * sina;
400 xform[5] = nysq + (1.0 - nysq) * cosa;
401 xform[9] = y * z * one_minus_cosa - x * sina;
402 xform[2] = x * z * one_minus_cosa - y * sina;
403 xform[6] = y * z * one_minus_cosa + x * sina;
404 xform[10] = nzsq + (1.0 - nzsq) * cosa;
406 mgl_mult_matrix(xform);
407 }
409 void mgl_scale(float x, float y, float z)
410 {
411 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
412 xform[0] = x;
413 xform[5] = y;
414 xform[10] = z;
415 mgl_mult_matrix(xform);
416 }
418 void gl_ortho(float left, float right, float bottom, float top, float nr, float fr)
419 {
420 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
422 float dx = right - left;
423 float dy = top - bottom;
424 float dz = fr - nr;
426 float tx = -(right + left) / dx;
427 float ty = -(top + bottom) / dy;
428 float tz = -(fr + nr) / dz;
430 float sx = 2.0 / dx;
431 float sy = 2.0 / dy;
432 float sz = -2.0 / dz;
434 xform[0] = sx;
435 xform[5] = sy;
436 xform[10] = sz;
437 xform[12] = tx;
438 xform[13] = ty;
439 xform[14] = tz;
441 mgl_mult_matrix(xform);
442 }
444 void mgl_frustum(float left, float right, float bottom, float top, float nr, float fr)
445 {
446 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
448 float dx = right - left;
449 float dy = top - bottom;
450 float dz = fr - nr;
452 float a = (right + left) / dx;
453 float b = (top + bottom) / dy;
454 float c = -(fr + nr) / dz;
455 float d = -2.0 * fr * nr / dz;
457 xform[0] = 2.0 * nr / dx;
458 xform[5] = 2.0 * nr / dy;
459 xform[8] = a;
460 xform[9] = b;
461 xform[10] = c;
462 xform[11] = -1.0f;
463 xform[14] = d;
465 mgl_mult_matrix(xform);
466 }
468 void mgl_perspective(float vfov, float aspect, float nr, float fr)
469 {
470 float vfov_rad = M_PI * vfov / 180.0;
471 float x = nr * tan(vfov_rad / 2.0);
472 mgl_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
473 }
475 void mgl_teximage(int width, int height, unsigned char *pixels)
476 {
477 st.tex.width = width;
478 st.tex.height = height;
479 st.tex.pixels = pixels;
481 if(calc_shiftmask(width, &st.tex.xshift, &st.tex.xmask) == -1 ||
482 calc_shiftmask(height, &st.tex.yshift, &st.tex.ymask) == -1) {
483 st.tex.pixels = 0;
484 }
485 }
487 #define MAX_SHIFT 12
488 static int calc_shiftmask(int val, int *shiftp, unsigned int *maskp)
489 {
490 int i;
492 for(i=0; i<MAX_SHIFT; i++) {
493 if((val >> i) == 1) {
494 *shiftp = i;
495 *maskp = ~(0xffff << i);
496 return 0;
497 }
498 }
499 return -1;
500 }