deepstone
view src/mingl.c @ 2:8c9a63a902d5
added palman to the dos makefile
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 21 Nov 2011 10:15:37 +0200 |
parents | |
children | 0e781cc43178 |
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"
26 #define DOT(a, b) ((a).x * (b).x + (a).y * (b).y + (a).z * (b).z)
28 static void transform(vec4_t *res, vec4_t *v, float *mat);
29 static void transform3(vec3_t *res, vec3_t *v, float *mat);
30 static void vertex_proc(struct vertex *vert);
32 static struct state st;
33 static struct framebuffer fb;
35 int mgl_init(int width, int height)
36 {
37 int i;
39 st.flags = 0;
40 st.mmode = 0;
42 mgl_front_face(MGL_CCW);
43 mgl_cull_face(MGL_BACK);
45 st.curv.cidx = 0;
46 st.curv.energy = 1.0;
47 st.curv.norm.x = st.curv.norm.y = st.curv.norm.z = 0.0;
49 if(!(fb.pixels = malloc(width * height))) {
50 return -1;
51 }
52 fb.width = width;
53 fb.height = height;
54 fb.zbuf = 0;
56 if(mgl_rast_init(&st, &fb) == -1) {
57 free(fb.pixels);
58 return -1;
59 }
61 st.mtop[0] = st.mtop[1] = 0;
63 mgl_matrix_mode(MGL_MODELVIEW);
64 mgl_load_identity();
65 mgl_matrix_mode(MGL_PROJECTION);
66 mgl_load_identity();
68 /* initial viewport in the size of the framebuffer */
69 st.vp[0] = st.vp[1] = 0;
70 st.vp[2] = width;
71 st.vp[3] = height;
73 st.col_range = 256;
74 for(i=0; i<MAX_LIGHTS; i++) {
75 st.ldir[i].x = st.ldir[i].y = 0.0f;
76 st.ldir[i].z = 1.0f;
77 st.lint[i] = 0.0f;
78 }
80 return 0;
81 }
83 void mgl_free(void)
84 {
85 mgl_rast_cleanup();
86 free(fb.pixels);
87 }
89 unsigned char *mgl_framebuffer(void)
90 {
91 return fb.pixels;
92 }
94 void mgl_clear(int cidx)
95 {
96 memset(fb.pixels, cidx, fb.width * fb.height);
97 }
99 void mgl_enable(unsigned int bit)
100 {
101 st.flags |= bit;
102 }
104 void mgl_disable(unsigned int bit)
105 {
106 st.flags &= ~bit;
107 }
109 void mgl_front_face(int ff)
110 {
111 st.frontface = ff;
112 }
114 void mgl_cull_face(int cf)
115 {
116 st.cullface = cf;
117 }
119 void mgl_color_range(int rng)
120 {
121 st.col_range = rng;
122 }
124 void mgl_light_intensity(int ltidx, float intens)
125 {
126 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
127 st.lint[ltidx] = intens;
128 }
130 void mgl_light_direction(int ltidx, float x, float y, float z)
131 {
132 vec3_t dir;
133 float mag;
134 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
136 dir.x = x;
137 dir.y = y;
138 dir.z = z;
139 transform3(&st.ldir[ltidx], &dir, st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]]);
141 mag = sqrt(DOT(st.ldir[ltidx], st.ldir[ltidx]));
142 if(fabs(mag) < 1e-6) {
143 mag = 1.0f;
144 }
145 st.ldir[ltidx].x /= mag;
146 st.ldir[ltidx].y /= mag;
147 st.ldir[ltidx].z /= mag;
148 }
150 void mgl_begin(int prim)
151 {
152 st.prim = prim;
153 st.vidx = 0;
155 st.ord = st.frontface;
156 if(st.cullface == MGL_FRONT) {
157 st.ord = st.frontface == MGL_CCW ? MGL_CW : MGL_CCW;
158 }
160 /* select the correct rasterizer according to state */
161 mgl_rast_prepare();
162 }
164 void mgl_end(void)
165 {
166 }
168 void mgl_vertex2f(float x, float y)
169 {
170 mgl_vertex4f(x, y, 0.0f, 1.0f);
171 }
173 void mgl_vertex3f(float x, float y, float z)
174 {
175 mgl_vertex4f(x, y, z, 1.0f);
176 }
178 void mgl_vertex4f(float x, float y, float z, float w)
179 {
180 st.v[st.vidx].pos.x = x;
181 st.v[st.vidx].pos.y = y;
182 st.v[st.vidx].pos.z = z;
183 st.v[st.vidx].pos.w = w;
184 st.v[st.vidx].cidx = st.curv.cidx;
185 st.v[st.vidx].energy = st.curv.energy;
186 st.v[st.vidx].norm = st.curv.norm;
187 st.v[st.vidx].tc = st.curv.tc;
189 vertex_proc(st.v + st.vidx);
191 if(++st.vidx >= st.prim) {
192 switch(st.prim) {
193 case MGL_POINTS:
194 mgl_draw_point(st.v);
195 break;
196 case MGL_LINES:
197 mgl_draw_line(st.v, st.v + 1);
198 break;
199 case MGL_TRIANGLES:
200 case MGL_QUADS:
201 mgl_draw_poly(st.v, st.prim);
202 break;
203 default:
204 fprintf(stderr, "invalid primitive: %d\n", st.prim);
205 abort();
206 }
207 st.vidx = 0;
208 }
209 }
211 void mgl_color1f(float energy)
212 {
213 st.curv.energy = energy;
214 }
216 void mgl_index(int c)
217 {
218 st.curv.cidx = c;
219 }
221 void mgl_normal(float x, float y, float z)
222 {
223 st.curv.norm.x = x;
224 st.curv.norm.y = y;
225 st.curv.norm.z = z;
226 }
228 void mgl_texcoord2f(float x, float y)
229 {
230 st.curv.tc.x = x;
231 st.curv.tc.y = y;
232 }
234 static void transform(vec4_t *res, vec4_t *v, float *mat)
235 {
236 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z + mat[12] * v->w;
237 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z + mat[13] * v->w;
238 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z + mat[14] * v->w;
239 res->w = mat[3] * v->x + mat[7] * v->y + mat[11] * v->z + mat[15] * v->w;
240 }
242 /* the matrix is 4x4 (16 floats), just ignoring anything out of the 3x3 */
243 static void transform3(vec3_t *res, vec3_t *v, float *mat)
244 {
245 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z;
246 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z;
247 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z;
248 }
250 static void vertex_proc(struct vertex *vert)
251 {
252 vec4_t pview, pclip;
254 float *mvmat = st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]];
255 float *pmat = st.matrix[MGL_PROJECTION][st.mtop[MGL_PROJECTION]];
257 /* modelview transformation */
258 transform(&pview, &vert->pos, mvmat);
260 if(st.flags & MGL_LIGHTING) {
261 if((st.flags & MGL_SMOOTH) || st.vidx == 0) {
262 int i;
263 vec3_t norm;
264 float irrad = 0.0f;
266 transform3(&norm, &vert->norm, mvmat);
268 for(i=0; i<MAX_LIGHTS; i++) {
269 if(st.lint[i] > 1e-6f) {
270 float ndotl = DOT(norm, st.ldir[i]);
271 if(ndotl < 0.0) {
272 ndotl = 0.0;
273 }
274 irrad += ndotl * st.lint[i];
275 }
276 }
277 vert->energy = irrad;
278 } else {
279 vert->energy = st.v[0].energy;
280 }
281 }
283 transform(&pclip, &pview, pmat);
284 /* TODO clipping in homogenous clip space */
286 if(pclip.w < 1e-6 && pclip.w > -1e-6) {
287 vert->pos.x = vert->pos.y = vert->pos.z = vert->pos.w = 0.0f;
288 return;
289 }
291 /* perspective division */
292 vert->pos.x = pclip.x / pclip.w;
293 vert->pos.y = pclip.y / pclip.w;
294 vert->pos.z = pclip.z / pclip.w;
295 vert->pos.w = pclip.w;
297 /* viewport transformation */
298 vert->pos.x = st.vp[0] + st.vp[2] * (vert->pos.x * 0.5 + 0.5);
299 vert->pos.y = st.vp[1] + st.vp[3] * (-vert->pos.y * 0.5 + 0.5);
300 }
302 void mgl_viewport(int x, int y, int width, int height)
303 {
304 st.vp[0] = x;
305 st.vp[1] = y;
306 st.vp[2] = width;
307 st.vp[3] = height;
308 }
310 void mgl_matrix_mode(int mmode)
311 {
312 st.mmode = mmode;
313 }
315 void mgl_push_matrix(void)
316 {
317 float *topmat;
318 if(st.mtop[st.mmode] >= MATRIX_STACK_SIZE - 1) {
319 fprintf(stderr, "mgl_push_matrix: stack overflow\n");
320 abort();
321 }
323 topmat = st.matrix[st.mmode][st.mtop[st.mmode]];
324 memcpy(topmat + 16, topmat, 16 * sizeof *topmat);
325 st.mmode++;
326 }
328 void mgl_pop_matrix(void)
329 {
330 if(st.mtop[st.mmode] <= 0) {
331 fprintf(stderr, "mgl_pop_matrix: stack underflow\n");
332 abort();
333 }
334 st.mtop[st.mmode]--;
335 }
337 void mgl_load_matrix(float *mat)
338 {
339 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
340 memcpy(dest, mat, 16 * sizeof *dest);
341 }
343 #define M(i,j) (((j) << 2) + (i))
344 void mgl_mult_matrix(float *m2)
345 {
346 int i, j;
347 float m1[16];
348 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
350 memcpy(m1, dest, sizeof m1);
352 for(i=0; i<4; i++) {
353 for(j=0; j<4; j++) {
354 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
355 m1[M(1,j)] * m2[M(i,1)] +
356 m1[M(2,j)] * m2[M(i,2)] +
357 m1[M(3,j)] * m2[M(i,3)];
358 }
359 }
360 }
362 void mgl_load_identity(void)
363 {
364 static float id[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
365 mgl_load_matrix((float*)id);
366 }
368 void mgl_translate(float x, float y, float z)
369 {
370 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
371 xform[12] = x;
372 xform[13] = y;
373 xform[14] = z;
374 mgl_mult_matrix(xform);
375 }
377 void mgl_rotate(float deg, float x, float y, float z)
378 {
379 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
381 float angle = M_PI * deg / 180.0f;
382 float sina = sin(angle);
383 float cosa = cos(angle);
384 float one_minus_cosa = 1.0f - cosa;
385 float nxsq = x * x;
386 float nysq = y * y;
387 float nzsq = z * z;
389 xform[0] = nxsq + (1.0f - nxsq) * cosa;
390 xform[4] = x * y * one_minus_cosa - z * sina;
391 xform[8] = x * z * one_minus_cosa + y * sina;
392 xform[1] = x * y * one_minus_cosa + z * sina;
393 xform[5] = nysq + (1.0 - nysq) * cosa;
394 xform[9] = y * z * one_minus_cosa - x * sina;
395 xform[2] = x * z * one_minus_cosa - y * sina;
396 xform[6] = y * z * one_minus_cosa + x * sina;
397 xform[10] = nzsq + (1.0 - nzsq) * cosa;
399 mgl_mult_matrix(xform);
400 }
402 void mgl_scale(float x, float y, float z)
403 {
404 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
405 xform[0] = x;
406 xform[5] = y;
407 xform[10] = z;
408 mgl_mult_matrix(xform);
409 }
411 void gl_ortho(float left, float right, float bottom, float top, float nr, float fr)
412 {
413 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
415 float dx = right - left;
416 float dy = top - bottom;
417 float dz = fr - nr;
419 float tx = -(right + left) / dx;
420 float ty = -(top + bottom) / dy;
421 float tz = -(fr + nr) / dz;
423 float sx = 2.0 / dx;
424 float sy = 2.0 / dy;
425 float sz = -2.0 / dz;
427 xform[0] = sx;
428 xform[5] = sy;
429 xform[10] = sz;
430 xform[12] = tx;
431 xform[13] = ty;
432 xform[14] = tz;
434 mgl_mult_matrix(xform);
435 }
437 void mgl_frustum(float left, float right, float bottom, float top, float nr, float fr)
438 {
439 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
441 float dx = right - left;
442 float dy = top - bottom;
443 float dz = fr - nr;
445 float a = (right + left) / dx;
446 float b = (top + bottom) / dy;
447 float c = -(fr + nr) / dz;
448 float d = -2.0 * fr * nr / dz;
450 xform[0] = 2.0 * nr / dx;
451 xform[5] = 2.0 * nr / dy;
452 xform[8] = a;
453 xform[9] = b;
454 xform[10] = c;
455 xform[11] = -1.0f;
456 xform[14] = d;
458 mgl_mult_matrix(xform);
459 }
461 void mgl_perspective(float vfov, float aspect, float nr, float fr)
462 {
463 float vfov_rad = M_PI * vfov / 180.0;
464 float x = nr * tan(vfov_rad / 2.0);
465 mgl_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
466 }