rev |
line source |
nuclear@0
|
1 /*
|
nuclear@0
|
2 256-color 3D graphics hack for real-mode DOS.
|
nuclear@0
|
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@0
|
4
|
nuclear@0
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@0
|
6 it under the terms of the GNU General Public License as published by
|
nuclear@0
|
7 the Free Software Foundation, either version 3 of the License, or
|
nuclear@0
|
8 (at your option) any later version.
|
nuclear@0
|
9
|
nuclear@0
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@0
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@0
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@0
|
13 GNU General Public License for more details.
|
nuclear@0
|
14
|
nuclear@0
|
15 You should have received a copy of the GNU General Public License
|
nuclear@0
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
nuclear@0
|
17 */
|
nuclear@0
|
18 #include <stdio.h>
|
nuclear@0
|
19 #include <stdlib.h>
|
nuclear@0
|
20 #include <string.h>
|
nuclear@0
|
21 #include <math.h>
|
nuclear@0
|
22 #include <assert.h>
|
nuclear@0
|
23 #include "mingl.h"
|
nuclear@0
|
24 #include "mglimpl.h"
|
nuclear@0
|
25
|
nuclear@0
|
26 #define DOT(a, b) ((a).x * (b).x + (a).y * (b).y + (a).z * (b).z)
|
nuclear@0
|
27
|
nuclear@0
|
28 static void transform(vec4_t *res, vec4_t *v, float *mat);
|
nuclear@0
|
29 static void transform3(vec3_t *res, vec3_t *v, float *mat);
|
nuclear@0
|
30 static void vertex_proc(struct vertex *vert);
|
nuclear@0
|
31
|
nuclear@0
|
32 static struct state st;
|
nuclear@0
|
33 static struct framebuffer fb;
|
nuclear@0
|
34
|
nuclear@0
|
35 int mgl_init(int width, int height)
|
nuclear@0
|
36 {
|
nuclear@0
|
37 int i;
|
nuclear@0
|
38
|
nuclear@0
|
39 st.flags = 0;
|
nuclear@0
|
40 st.mmode = 0;
|
nuclear@0
|
41
|
nuclear@0
|
42 mgl_front_face(MGL_CCW);
|
nuclear@0
|
43 mgl_cull_face(MGL_BACK);
|
nuclear@0
|
44
|
nuclear@0
|
45 st.curv.cidx = 0;
|
nuclear@0
|
46 st.curv.energy = 1.0;
|
nuclear@0
|
47 st.curv.norm.x = st.curv.norm.y = st.curv.norm.z = 0.0;
|
nuclear@0
|
48
|
nuclear@0
|
49 if(!(fb.pixels = malloc(width * height))) {
|
nuclear@0
|
50 return -1;
|
nuclear@0
|
51 }
|
nuclear@0
|
52 fb.width = width;
|
nuclear@0
|
53 fb.height = height;
|
nuclear@0
|
54 fb.zbuf = 0;
|
nuclear@0
|
55
|
nuclear@0
|
56 if(mgl_rast_init(&st, &fb) == -1) {
|
nuclear@0
|
57 free(fb.pixels);
|
nuclear@0
|
58 return -1;
|
nuclear@0
|
59 }
|
nuclear@0
|
60
|
nuclear@0
|
61 st.mtop[0] = st.mtop[1] = 0;
|
nuclear@0
|
62
|
nuclear@0
|
63 mgl_matrix_mode(MGL_MODELVIEW);
|
nuclear@0
|
64 mgl_load_identity();
|
nuclear@0
|
65 mgl_matrix_mode(MGL_PROJECTION);
|
nuclear@0
|
66 mgl_load_identity();
|
nuclear@0
|
67
|
nuclear@0
|
68 /* initial viewport in the size of the framebuffer */
|
nuclear@0
|
69 st.vp[0] = st.vp[1] = 0;
|
nuclear@0
|
70 st.vp[2] = width;
|
nuclear@0
|
71 st.vp[3] = height;
|
nuclear@0
|
72
|
nuclear@0
|
73 st.col_range = 256;
|
nuclear@0
|
74 for(i=0; i<MAX_LIGHTS; i++) {
|
nuclear@0
|
75 st.ldir[i].x = st.ldir[i].y = 0.0f;
|
nuclear@0
|
76 st.ldir[i].z = 1.0f;
|
nuclear@0
|
77 st.lint[i] = 0.0f;
|
nuclear@0
|
78 }
|
nuclear@0
|
79
|
nuclear@0
|
80 return 0;
|
nuclear@0
|
81 }
|
nuclear@0
|
82
|
nuclear@0
|
83 void mgl_free(void)
|
nuclear@0
|
84 {
|
nuclear@0
|
85 mgl_rast_cleanup();
|
nuclear@0
|
86 free(fb.pixels);
|
nuclear@0
|
87 }
|
nuclear@0
|
88
|
nuclear@0
|
89 unsigned char *mgl_framebuffer(void)
|
nuclear@0
|
90 {
|
nuclear@0
|
91 return fb.pixels;
|
nuclear@0
|
92 }
|
nuclear@0
|
93
|
nuclear@0
|
94 void mgl_clear(int cidx)
|
nuclear@0
|
95 {
|
nuclear@0
|
96 memset(fb.pixels, cidx, fb.width * fb.height);
|
nuclear@0
|
97 }
|
nuclear@0
|
98
|
nuclear@0
|
99 void mgl_enable(unsigned int bit)
|
nuclear@0
|
100 {
|
nuclear@0
|
101 st.flags |= bit;
|
nuclear@0
|
102 }
|
nuclear@0
|
103
|
nuclear@0
|
104 void mgl_disable(unsigned int bit)
|
nuclear@0
|
105 {
|
nuclear@0
|
106 st.flags &= ~bit;
|
nuclear@0
|
107 }
|
nuclear@0
|
108
|
nuclear@0
|
109 void mgl_front_face(int ff)
|
nuclear@0
|
110 {
|
nuclear@0
|
111 st.frontface = ff;
|
nuclear@0
|
112 }
|
nuclear@0
|
113
|
nuclear@0
|
114 void mgl_cull_face(int cf)
|
nuclear@0
|
115 {
|
nuclear@0
|
116 st.cullface = cf;
|
nuclear@0
|
117 }
|
nuclear@0
|
118
|
nuclear@0
|
119 void mgl_color_range(int rng)
|
nuclear@0
|
120 {
|
nuclear@0
|
121 st.col_range = rng;
|
nuclear@0
|
122 }
|
nuclear@0
|
123
|
nuclear@0
|
124 void mgl_light_intensity(int ltidx, float intens)
|
nuclear@0
|
125 {
|
nuclear@0
|
126 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
|
nuclear@0
|
127 st.lint[ltidx] = intens;
|
nuclear@0
|
128 }
|
nuclear@0
|
129
|
nuclear@0
|
130 void mgl_light_direction(int ltidx, float x, float y, float z)
|
nuclear@0
|
131 {
|
nuclear@0
|
132 vec3_t dir;
|
nuclear@0
|
133 float mag;
|
nuclear@0
|
134 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
|
nuclear@0
|
135
|
nuclear@0
|
136 dir.x = x;
|
nuclear@0
|
137 dir.y = y;
|
nuclear@0
|
138 dir.z = z;
|
nuclear@0
|
139 transform3(&st.ldir[ltidx], &dir, st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]]);
|
nuclear@0
|
140
|
nuclear@0
|
141 mag = sqrt(DOT(st.ldir[ltidx], st.ldir[ltidx]));
|
nuclear@0
|
142 if(fabs(mag) < 1e-6) {
|
nuclear@0
|
143 mag = 1.0f;
|
nuclear@0
|
144 }
|
nuclear@0
|
145 st.ldir[ltidx].x /= mag;
|
nuclear@0
|
146 st.ldir[ltidx].y /= mag;
|
nuclear@0
|
147 st.ldir[ltidx].z /= mag;
|
nuclear@0
|
148 }
|
nuclear@0
|
149
|
nuclear@0
|
150 void mgl_begin(int prim)
|
nuclear@0
|
151 {
|
nuclear@0
|
152 st.prim = prim;
|
nuclear@0
|
153 st.vidx = 0;
|
nuclear@0
|
154
|
nuclear@0
|
155 st.ord = st.frontface;
|
nuclear@0
|
156 if(st.cullface == MGL_FRONT) {
|
nuclear@0
|
157 st.ord = st.frontface == MGL_CCW ? MGL_CW : MGL_CCW;
|
nuclear@0
|
158 }
|
nuclear@0
|
159
|
nuclear@0
|
160 /* select the correct rasterizer according to state */
|
nuclear@0
|
161 mgl_rast_prepare();
|
nuclear@0
|
162 }
|
nuclear@0
|
163
|
nuclear@0
|
164 void mgl_end(void)
|
nuclear@0
|
165 {
|
nuclear@0
|
166 }
|
nuclear@0
|
167
|
nuclear@0
|
168 void mgl_vertex2f(float x, float y)
|
nuclear@0
|
169 {
|
nuclear@0
|
170 mgl_vertex4f(x, y, 0.0f, 1.0f);
|
nuclear@0
|
171 }
|
nuclear@0
|
172
|
nuclear@0
|
173 void mgl_vertex3f(float x, float y, float z)
|
nuclear@0
|
174 {
|
nuclear@0
|
175 mgl_vertex4f(x, y, z, 1.0f);
|
nuclear@0
|
176 }
|
nuclear@0
|
177
|
nuclear@0
|
178 void mgl_vertex4f(float x, float y, float z, float w)
|
nuclear@0
|
179 {
|
nuclear@0
|
180 st.v[st.vidx].pos.x = x;
|
nuclear@0
|
181 st.v[st.vidx].pos.y = y;
|
nuclear@0
|
182 st.v[st.vidx].pos.z = z;
|
nuclear@0
|
183 st.v[st.vidx].pos.w = w;
|
nuclear@0
|
184 st.v[st.vidx].cidx = st.curv.cidx;
|
nuclear@0
|
185 st.v[st.vidx].energy = st.curv.energy;
|
nuclear@0
|
186 st.v[st.vidx].norm = st.curv.norm;
|
nuclear@0
|
187 st.v[st.vidx].tc = st.curv.tc;
|
nuclear@0
|
188
|
nuclear@0
|
189 vertex_proc(st.v + st.vidx);
|
nuclear@0
|
190
|
nuclear@0
|
191 if(++st.vidx >= st.prim) {
|
nuclear@0
|
192 switch(st.prim) {
|
nuclear@0
|
193 case MGL_POINTS:
|
nuclear@0
|
194 mgl_draw_point(st.v);
|
nuclear@0
|
195 break;
|
nuclear@0
|
196 case MGL_LINES:
|
nuclear@0
|
197 mgl_draw_line(st.v, st.v + 1);
|
nuclear@0
|
198 break;
|
nuclear@0
|
199 case MGL_TRIANGLES:
|
nuclear@0
|
200 case MGL_QUADS:
|
nuclear@0
|
201 mgl_draw_poly(st.v, st.prim);
|
nuclear@0
|
202 break;
|
nuclear@0
|
203 default:
|
nuclear@0
|
204 fprintf(stderr, "invalid primitive: %d\n", st.prim);
|
nuclear@0
|
205 abort();
|
nuclear@0
|
206 }
|
nuclear@0
|
207 st.vidx = 0;
|
nuclear@0
|
208 }
|
nuclear@0
|
209 }
|
nuclear@0
|
210
|
nuclear@0
|
211 void mgl_color1f(float energy)
|
nuclear@0
|
212 {
|
nuclear@0
|
213 st.curv.energy = energy;
|
nuclear@0
|
214 }
|
nuclear@0
|
215
|
nuclear@0
|
216 void mgl_index(int c)
|
nuclear@0
|
217 {
|
nuclear@0
|
218 st.curv.cidx = c;
|
nuclear@0
|
219 }
|
nuclear@0
|
220
|
nuclear@0
|
221 void mgl_normal(float x, float y, float z)
|
nuclear@0
|
222 {
|
nuclear@0
|
223 st.curv.norm.x = x;
|
nuclear@0
|
224 st.curv.norm.y = y;
|
nuclear@0
|
225 st.curv.norm.z = z;
|
nuclear@0
|
226 }
|
nuclear@0
|
227
|
nuclear@0
|
228 void mgl_texcoord2f(float x, float y)
|
nuclear@0
|
229 {
|
nuclear@0
|
230 st.curv.tc.x = x;
|
nuclear@0
|
231 st.curv.tc.y = y;
|
nuclear@0
|
232 }
|
nuclear@0
|
233
|
nuclear@0
|
234 static void transform(vec4_t *res, vec4_t *v, float *mat)
|
nuclear@0
|
235 {
|
nuclear@0
|
236 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z + mat[12] * v->w;
|
nuclear@0
|
237 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z + mat[13] * v->w;
|
nuclear@0
|
238 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z + mat[14] * v->w;
|
nuclear@0
|
239 res->w = mat[3] * v->x + mat[7] * v->y + mat[11] * v->z + mat[15] * v->w;
|
nuclear@0
|
240 }
|
nuclear@0
|
241
|
nuclear@0
|
242 /* the matrix is 4x4 (16 floats), just ignoring anything out of the 3x3 */
|
nuclear@0
|
243 static void transform3(vec3_t *res, vec3_t *v, float *mat)
|
nuclear@0
|
244 {
|
nuclear@0
|
245 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z;
|
nuclear@0
|
246 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z;
|
nuclear@0
|
247 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z;
|
nuclear@0
|
248 }
|
nuclear@0
|
249
|
nuclear@0
|
250 static void vertex_proc(struct vertex *vert)
|
nuclear@0
|
251 {
|
nuclear@0
|
252 vec4_t pview, pclip;
|
nuclear@0
|
253
|
nuclear@0
|
254 float *mvmat = st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]];
|
nuclear@0
|
255 float *pmat = st.matrix[MGL_PROJECTION][st.mtop[MGL_PROJECTION]];
|
nuclear@0
|
256
|
nuclear@0
|
257 /* modelview transformation */
|
nuclear@0
|
258 transform(&pview, &vert->pos, mvmat);
|
nuclear@0
|
259
|
nuclear@0
|
260 if(st.flags & MGL_LIGHTING) {
|
nuclear@0
|
261 if((st.flags & MGL_SMOOTH) || st.vidx == 0) {
|
nuclear@0
|
262 int i;
|
nuclear@0
|
263 vec3_t norm;
|
nuclear@0
|
264 float irrad = 0.0f;
|
nuclear@0
|
265
|
nuclear@0
|
266 transform3(&norm, &vert->norm, mvmat);
|
nuclear@0
|
267
|
nuclear@0
|
268 for(i=0; i<MAX_LIGHTS; i++) {
|
nuclear@0
|
269 if(st.lint[i] > 1e-6f) {
|
nuclear@0
|
270 float ndotl = DOT(norm, st.ldir[i]);
|
nuclear@0
|
271 if(ndotl < 0.0) {
|
nuclear@0
|
272 ndotl = 0.0;
|
nuclear@0
|
273 }
|
nuclear@0
|
274 irrad += ndotl * st.lint[i];
|
nuclear@0
|
275 }
|
nuclear@0
|
276 }
|
nuclear@0
|
277 vert->energy = irrad;
|
nuclear@0
|
278 } else {
|
nuclear@0
|
279 vert->energy = st.v[0].energy;
|
nuclear@0
|
280 }
|
nuclear@0
|
281 }
|
nuclear@0
|
282
|
nuclear@0
|
283 transform(&pclip, &pview, pmat);
|
nuclear@0
|
284 /* TODO clipping in homogenous clip space */
|
nuclear@0
|
285
|
nuclear@0
|
286 if(pclip.w < 1e-6 && pclip.w > -1e-6) {
|
nuclear@0
|
287 vert->pos.x = vert->pos.y = vert->pos.z = vert->pos.w = 0.0f;
|
nuclear@0
|
288 return;
|
nuclear@0
|
289 }
|
nuclear@0
|
290
|
nuclear@0
|
291 /* perspective division */
|
nuclear@0
|
292 vert->pos.x = pclip.x / pclip.w;
|
nuclear@0
|
293 vert->pos.y = pclip.y / pclip.w;
|
nuclear@0
|
294 vert->pos.z = pclip.z / pclip.w;
|
nuclear@0
|
295 vert->pos.w = pclip.w;
|
nuclear@0
|
296
|
nuclear@0
|
297 /* viewport transformation */
|
nuclear@0
|
298 vert->pos.x = st.vp[0] + st.vp[2] * (vert->pos.x * 0.5 + 0.5);
|
nuclear@0
|
299 vert->pos.y = st.vp[1] + st.vp[3] * (-vert->pos.y * 0.5 + 0.5);
|
nuclear@0
|
300 }
|
nuclear@0
|
301
|
nuclear@0
|
302 void mgl_viewport(int x, int y, int width, int height)
|
nuclear@0
|
303 {
|
nuclear@0
|
304 st.vp[0] = x;
|
nuclear@0
|
305 st.vp[1] = y;
|
nuclear@0
|
306 st.vp[2] = width;
|
nuclear@0
|
307 st.vp[3] = height;
|
nuclear@0
|
308 }
|
nuclear@0
|
309
|
nuclear@0
|
310 void mgl_matrix_mode(int mmode)
|
nuclear@0
|
311 {
|
nuclear@0
|
312 st.mmode = mmode;
|
nuclear@0
|
313 }
|
nuclear@0
|
314
|
nuclear@0
|
315 void mgl_push_matrix(void)
|
nuclear@0
|
316 {
|
nuclear@0
|
317 float *topmat;
|
nuclear@0
|
318 if(st.mtop[st.mmode] >= MATRIX_STACK_SIZE - 1) {
|
nuclear@0
|
319 fprintf(stderr, "mgl_push_matrix: stack overflow\n");
|
nuclear@0
|
320 abort();
|
nuclear@0
|
321 }
|
nuclear@0
|
322
|
nuclear@0
|
323 topmat = st.matrix[st.mmode][st.mtop[st.mmode]];
|
nuclear@0
|
324 memcpy(topmat + 16, topmat, 16 * sizeof *topmat);
|
nuclear@0
|
325 st.mmode++;
|
nuclear@0
|
326 }
|
nuclear@0
|
327
|
nuclear@0
|
328 void mgl_pop_matrix(void)
|
nuclear@0
|
329 {
|
nuclear@0
|
330 if(st.mtop[st.mmode] <= 0) {
|
nuclear@0
|
331 fprintf(stderr, "mgl_pop_matrix: stack underflow\n");
|
nuclear@0
|
332 abort();
|
nuclear@0
|
333 }
|
nuclear@0
|
334 st.mtop[st.mmode]--;
|
nuclear@0
|
335 }
|
nuclear@0
|
336
|
nuclear@0
|
337 void mgl_load_matrix(float *mat)
|
nuclear@0
|
338 {
|
nuclear@0
|
339 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
|
nuclear@0
|
340 memcpy(dest, mat, 16 * sizeof *dest);
|
nuclear@0
|
341 }
|
nuclear@0
|
342
|
nuclear@0
|
343 #define M(i,j) (((j) << 2) + (i))
|
nuclear@0
|
344 void mgl_mult_matrix(float *m2)
|
nuclear@0
|
345 {
|
nuclear@0
|
346 int i, j;
|
nuclear@0
|
347 float m1[16];
|
nuclear@0
|
348 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
|
nuclear@0
|
349
|
nuclear@0
|
350 memcpy(m1, dest, sizeof m1);
|
nuclear@0
|
351
|
nuclear@0
|
352 for(i=0; i<4; i++) {
|
nuclear@0
|
353 for(j=0; j<4; j++) {
|
nuclear@0
|
354 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
|
nuclear@0
|
355 m1[M(1,j)] * m2[M(i,1)] +
|
nuclear@0
|
356 m1[M(2,j)] * m2[M(i,2)] +
|
nuclear@0
|
357 m1[M(3,j)] * m2[M(i,3)];
|
nuclear@0
|
358 }
|
nuclear@0
|
359 }
|
nuclear@0
|
360 }
|
nuclear@0
|
361
|
nuclear@0
|
362 void mgl_load_identity(void)
|
nuclear@0
|
363 {
|
nuclear@0
|
364 static float id[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
365 mgl_load_matrix((float*)id);
|
nuclear@0
|
366 }
|
nuclear@0
|
367
|
nuclear@0
|
368 void mgl_translate(float x, float y, float z)
|
nuclear@0
|
369 {
|
nuclear@0
|
370 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
371 xform[12] = x;
|
nuclear@0
|
372 xform[13] = y;
|
nuclear@0
|
373 xform[14] = z;
|
nuclear@0
|
374 mgl_mult_matrix(xform);
|
nuclear@0
|
375 }
|
nuclear@0
|
376
|
nuclear@0
|
377 void mgl_rotate(float deg, float x, float y, float z)
|
nuclear@0
|
378 {
|
nuclear@0
|
379 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
380
|
nuclear@0
|
381 float angle = M_PI * deg / 180.0f;
|
nuclear@0
|
382 float sina = sin(angle);
|
nuclear@0
|
383 float cosa = cos(angle);
|
nuclear@0
|
384 float one_minus_cosa = 1.0f - cosa;
|
nuclear@0
|
385 float nxsq = x * x;
|
nuclear@0
|
386 float nysq = y * y;
|
nuclear@0
|
387 float nzsq = z * z;
|
nuclear@0
|
388
|
nuclear@0
|
389 xform[0] = nxsq + (1.0f - nxsq) * cosa;
|
nuclear@0
|
390 xform[4] = x * y * one_minus_cosa - z * sina;
|
nuclear@0
|
391 xform[8] = x * z * one_minus_cosa + y * sina;
|
nuclear@0
|
392 xform[1] = x * y * one_minus_cosa + z * sina;
|
nuclear@0
|
393 xform[5] = nysq + (1.0 - nysq) * cosa;
|
nuclear@0
|
394 xform[9] = y * z * one_minus_cosa - x * sina;
|
nuclear@0
|
395 xform[2] = x * z * one_minus_cosa - y * sina;
|
nuclear@0
|
396 xform[6] = y * z * one_minus_cosa + x * sina;
|
nuclear@0
|
397 xform[10] = nzsq + (1.0 - nzsq) * cosa;
|
nuclear@0
|
398
|
nuclear@0
|
399 mgl_mult_matrix(xform);
|
nuclear@0
|
400 }
|
nuclear@0
|
401
|
nuclear@0
|
402 void mgl_scale(float x, float y, float z)
|
nuclear@0
|
403 {
|
nuclear@0
|
404 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
405 xform[0] = x;
|
nuclear@0
|
406 xform[5] = y;
|
nuclear@0
|
407 xform[10] = z;
|
nuclear@0
|
408 mgl_mult_matrix(xform);
|
nuclear@0
|
409 }
|
nuclear@0
|
410
|
nuclear@0
|
411 void gl_ortho(float left, float right, float bottom, float top, float nr, float fr)
|
nuclear@0
|
412 {
|
nuclear@0
|
413 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
414
|
nuclear@0
|
415 float dx = right - left;
|
nuclear@0
|
416 float dy = top - bottom;
|
nuclear@0
|
417 float dz = fr - nr;
|
nuclear@0
|
418
|
nuclear@0
|
419 float tx = -(right + left) / dx;
|
nuclear@0
|
420 float ty = -(top + bottom) / dy;
|
nuclear@0
|
421 float tz = -(fr + nr) / dz;
|
nuclear@0
|
422
|
nuclear@0
|
423 float sx = 2.0 / dx;
|
nuclear@0
|
424 float sy = 2.0 / dy;
|
nuclear@0
|
425 float sz = -2.0 / dz;
|
nuclear@0
|
426
|
nuclear@0
|
427 xform[0] = sx;
|
nuclear@0
|
428 xform[5] = sy;
|
nuclear@0
|
429 xform[10] = sz;
|
nuclear@0
|
430 xform[12] = tx;
|
nuclear@0
|
431 xform[13] = ty;
|
nuclear@0
|
432 xform[14] = tz;
|
nuclear@0
|
433
|
nuclear@0
|
434 mgl_mult_matrix(xform);
|
nuclear@0
|
435 }
|
nuclear@0
|
436
|
nuclear@0
|
437 void mgl_frustum(float left, float right, float bottom, float top, float nr, float fr)
|
nuclear@0
|
438 {
|
nuclear@0
|
439 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
440
|
nuclear@0
|
441 float dx = right - left;
|
nuclear@0
|
442 float dy = top - bottom;
|
nuclear@0
|
443 float dz = fr - nr;
|
nuclear@0
|
444
|
nuclear@0
|
445 float a = (right + left) / dx;
|
nuclear@0
|
446 float b = (top + bottom) / dy;
|
nuclear@0
|
447 float c = -(fr + nr) / dz;
|
nuclear@0
|
448 float d = -2.0 * fr * nr / dz;
|
nuclear@0
|
449
|
nuclear@0
|
450 xform[0] = 2.0 * nr / dx;
|
nuclear@0
|
451 xform[5] = 2.0 * nr / dy;
|
nuclear@0
|
452 xform[8] = a;
|
nuclear@0
|
453 xform[9] = b;
|
nuclear@0
|
454 xform[10] = c;
|
nuclear@0
|
455 xform[11] = -1.0f;
|
nuclear@0
|
456 xform[14] = d;
|
nuclear@0
|
457
|
nuclear@0
|
458 mgl_mult_matrix(xform);
|
nuclear@0
|
459 }
|
nuclear@0
|
460
|
nuclear@0
|
461 void mgl_perspective(float vfov, float aspect, float nr, float fr)
|
nuclear@0
|
462 {
|
nuclear@0
|
463 float vfov_rad = M_PI * vfov / 180.0;
|
nuclear@0
|
464 float x = nr * tan(vfov_rad / 2.0);
|
nuclear@0
|
465 mgl_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
|
nuclear@0
|
466 }
|