rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <stdlib.h>
|
nuclear@0
|
3 #include <string.h>
|
nuclear@0
|
4 #include <math.h>
|
nuclear@17
|
5 #include <limits.h>
|
nuclear@0
|
6 #include <assert.h>
|
nuclear@21
|
7 #include "vmath.h"
|
nuclear@0
|
8 #include "mingl.h"
|
nuclear@0
|
9 #include "mglimpl.h"
|
nuclear@0
|
10
|
nuclear@3
|
11
|
nuclear@0
|
12 #define DOT(a, b) ((a).x * (b).x + (a).y * (b).y + (a).z * (b).z)
|
nuclear@0
|
13
|
nuclear@11
|
14 #define NORMALIZE(v) \
|
nuclear@11
|
15 do { \
|
nuclear@11
|
16 float mag = sqrt(DOT(v, v)); \
|
nuclear@11
|
17 if(fabs(mag) > 1e-6) { \
|
nuclear@11
|
18 float invmag = 1.0 / mag; \
|
nuclear@11
|
19 (v).x *= invmag; \
|
nuclear@11
|
20 (v).y *= invmag; \
|
nuclear@11
|
21 (v).z *= invmag; \
|
nuclear@11
|
22 } \
|
nuclear@11
|
23 } while(0)
|
nuclear@11
|
24
|
nuclear@0
|
25 static void transform(vec4_t *res, vec4_t *v, float *mat);
|
nuclear@0
|
26 static void transform3(vec3_t *res, vec3_t *v, float *mat);
|
nuclear@0
|
27 static void vertex_proc(struct vertex *vert);
|
nuclear@3
|
28 static int calc_shiftmask(int val, int *shiftp, unsigned int *maskp);
|
nuclear@0
|
29
|
nuclear@0
|
30 static struct state st;
|
nuclear@0
|
31 static struct framebuffer fb;
|
nuclear@0
|
32
|
nuclear@0
|
33 int mgl_init(int width, int height)
|
nuclear@0
|
34 {
|
nuclear@0
|
35 int i;
|
nuclear@0
|
36
|
nuclear@0
|
37 st.flags = 0;
|
nuclear@0
|
38 st.mmode = 0;
|
nuclear@0
|
39
|
nuclear@0
|
40 mgl_front_face(MGL_CCW);
|
nuclear@0
|
41 mgl_cull_face(MGL_BACK);
|
nuclear@0
|
42
|
nuclear@0
|
43 st.curv.cidx = 0;
|
nuclear@0
|
44 st.curv.energy = 1.0;
|
nuclear@0
|
45 st.curv.norm.x = st.curv.norm.y = st.curv.norm.z = 0.0;
|
nuclear@0
|
46
|
nuclear@0
|
47 if(!(fb.pixels = malloc(width * height))) {
|
nuclear@0
|
48 return -1;
|
nuclear@0
|
49 }
|
nuclear@0
|
50 fb.width = width;
|
nuclear@0
|
51 fb.height = height;
|
nuclear@0
|
52 fb.zbuf = 0;
|
nuclear@0
|
53
|
nuclear@0
|
54 if(mgl_rast_init(&st, &fb) == -1) {
|
nuclear@0
|
55 free(fb.pixels);
|
nuclear@0
|
56 return -1;
|
nuclear@0
|
57 }
|
nuclear@0
|
58
|
nuclear@0
|
59 st.mtop[0] = st.mtop[1] = 0;
|
nuclear@0
|
60
|
nuclear@0
|
61 mgl_matrix_mode(MGL_MODELVIEW);
|
nuclear@0
|
62 mgl_load_identity();
|
nuclear@0
|
63 mgl_matrix_mode(MGL_PROJECTION);
|
nuclear@0
|
64 mgl_load_identity();
|
nuclear@0
|
65
|
nuclear@0
|
66 /* initial viewport in the size of the framebuffer */
|
nuclear@0
|
67 st.vp[0] = st.vp[1] = 0;
|
nuclear@0
|
68 st.vp[2] = width;
|
nuclear@0
|
69 st.vp[3] = height;
|
nuclear@0
|
70
|
nuclear@0
|
71 st.col_range = 256;
|
nuclear@0
|
72 for(i=0; i<MAX_LIGHTS; i++) {
|
nuclear@11
|
73 st.lpos[i].x = st.lpos[i].y = st.lpos[i].w = 0.0f;
|
nuclear@11
|
74 st.lpos[i].z = 1.0f;
|
nuclear@0
|
75 st.lint[i] = 0.0f;
|
nuclear@0
|
76 }
|
nuclear@0
|
77
|
nuclear@0
|
78 return 0;
|
nuclear@0
|
79 }
|
nuclear@0
|
80
|
nuclear@0
|
81 void mgl_free(void)
|
nuclear@0
|
82 {
|
nuclear@9
|
83 int i;
|
nuclear@9
|
84
|
nuclear@0
|
85 mgl_rast_cleanup();
|
nuclear@0
|
86 free(fb.pixels);
|
nuclear@9
|
87 fb.pixels = 0;
|
nuclear@9
|
88
|
nuclear@9
|
89 if(fb.zbuf) {
|
nuclear@9
|
90 for(i=0; i<fb.num_ztiles; i++) {
|
nuclear@9
|
91 free(fb.zbuf[i]);
|
nuclear@9
|
92 }
|
nuclear@9
|
93 free(fb.zbuf);
|
nuclear@9
|
94 fb.zbuf = 0;
|
nuclear@9
|
95 }
|
nuclear@0
|
96 }
|
nuclear@0
|
97
|
nuclear@0
|
98 unsigned char *mgl_framebuffer(void)
|
nuclear@0
|
99 {
|
nuclear@0
|
100 return fb.pixels;
|
nuclear@0
|
101 }
|
nuclear@0
|
102
|
nuclear@0
|
103 void mgl_clear(int cidx)
|
nuclear@0
|
104 {
|
nuclear@0
|
105 memset(fb.pixels, cidx, fb.width * fb.height);
|
nuclear@0
|
106 }
|
nuclear@0
|
107
|
nuclear@9
|
108 void mgl_clear_depth(void)
|
nuclear@9
|
109 {
|
nuclear@9
|
110 int i;
|
nuclear@9
|
111
|
nuclear@9
|
112 if(!fb.zbuf) {
|
nuclear@9
|
113 long num_pixels = (long)fb.width * (long)fb.height;
|
nuclear@9
|
114 fb.num_ztiles = (num_pixels + ZTILE_SIZE - 1) / ZTILE_SIZE;
|
nuclear@9
|
115
|
nuclear@9
|
116 if(!(fb.zbuf = malloc(fb.num_ztiles * sizeof *fb.zbuf))) {
|
nuclear@9
|
117 fprintf(stderr, "failed to allocate ztile array\n");
|
nuclear@9
|
118 abort();
|
nuclear@9
|
119 }
|
nuclear@9
|
120
|
nuclear@9
|
121 for(i=0; i<fb.num_ztiles; i++) {
|
nuclear@9
|
122 if(!(fb.zbuf[i] = malloc(ZTILE_SIZE * 2))) {
|
nuclear@9
|
123 fprintf(stderr, "failed to allocate ztile %d\n", i);
|
nuclear@9
|
124 abort();
|
nuclear@9
|
125 }
|
nuclear@9
|
126 memset(fb.zbuf[i], 0xff, ZTILE_SIZE * 2);
|
nuclear@9
|
127 }
|
nuclear@9
|
128 return;
|
nuclear@9
|
129 }
|
nuclear@9
|
130
|
nuclear@9
|
131 for(i=0; i<fb.num_ztiles; i++) {
|
nuclear@9
|
132 memset(fb.zbuf[i], 0xff, ZTILE_SIZE * 2);
|
nuclear@9
|
133 }
|
nuclear@9
|
134 }
|
nuclear@9
|
135
|
nuclear@0
|
136 void mgl_enable(unsigned int bit)
|
nuclear@0
|
137 {
|
nuclear@0
|
138 st.flags |= bit;
|
nuclear@0
|
139 }
|
nuclear@0
|
140
|
nuclear@0
|
141 void mgl_disable(unsigned int bit)
|
nuclear@0
|
142 {
|
nuclear@0
|
143 st.flags &= ~bit;
|
nuclear@0
|
144 }
|
nuclear@0
|
145
|
nuclear@3
|
146 int mgl_isenabled(unsigned int bit)
|
nuclear@3
|
147 {
|
nuclear@3
|
148 return (st.flags & bit) != 0;
|
nuclear@3
|
149 }
|
nuclear@3
|
150
|
nuclear@0
|
151 void mgl_front_face(int ff)
|
nuclear@0
|
152 {
|
nuclear@0
|
153 st.frontface = ff;
|
nuclear@0
|
154 }
|
nuclear@0
|
155
|
nuclear@0
|
156 void mgl_cull_face(int cf)
|
nuclear@0
|
157 {
|
nuclear@0
|
158 st.cullface = cf;
|
nuclear@0
|
159 }
|
nuclear@0
|
160
|
nuclear@0
|
161 void mgl_color_range(int rng)
|
nuclear@0
|
162 {
|
nuclear@0
|
163 st.col_range = rng;
|
nuclear@0
|
164 }
|
nuclear@0
|
165
|
nuclear@0
|
166 void mgl_light_intensity(int ltidx, float intens)
|
nuclear@0
|
167 {
|
nuclear@0
|
168 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
|
nuclear@0
|
169 st.lint[ltidx] = intens;
|
nuclear@0
|
170 }
|
nuclear@0
|
171
|
nuclear@11
|
172 void mgl_light_position(int ltidx, float x, float y, float z, float w)
|
nuclear@0
|
173 {
|
nuclear@11
|
174 vec4_t pos;
|
nuclear@0
|
175 assert(ltidx >= 0 && ltidx < MAX_LIGHTS);
|
nuclear@0
|
176
|
nuclear@11
|
177 pos.x = x;
|
nuclear@11
|
178 pos.y = y;
|
nuclear@11
|
179 pos.z = z;
|
nuclear@11
|
180 pos.w = w;
|
nuclear@11
|
181 transform(&st.lpos[ltidx], &pos, st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]]);
|
nuclear@0
|
182
|
nuclear@11
|
183 if(fabs(st.lpos[ltidx].w) < 1e-6) {
|
nuclear@11
|
184 NORMALIZE(st.lpos[ltidx]);
|
nuclear@11
|
185 } else {
|
nuclear@11
|
186 st.lpos[ltidx].x /= st.lpos[ltidx].w;
|
nuclear@11
|
187 st.lpos[ltidx].y /= st.lpos[ltidx].w;
|
nuclear@11
|
188 st.lpos[ltidx].z /= st.lpos[ltidx].w;
|
nuclear@0
|
189 }
|
nuclear@0
|
190 }
|
nuclear@0
|
191
|
nuclear@0
|
192 void mgl_begin(int prim)
|
nuclear@0
|
193 {
|
nuclear@0
|
194 st.prim = prim;
|
nuclear@0
|
195 st.vidx = 0;
|
nuclear@0
|
196
|
nuclear@0
|
197 st.ord = st.frontface;
|
nuclear@0
|
198 if(st.cullface == MGL_FRONT) {
|
nuclear@0
|
199 st.ord = st.frontface == MGL_CCW ? MGL_CW : MGL_CCW;
|
nuclear@0
|
200 }
|
nuclear@0
|
201
|
nuclear@0
|
202 /* select the correct rasterizer according to state */
|
nuclear@0
|
203 mgl_rast_prepare();
|
nuclear@0
|
204 }
|
nuclear@0
|
205
|
nuclear@0
|
206 void mgl_end(void)
|
nuclear@0
|
207 {
|
nuclear@0
|
208 }
|
nuclear@0
|
209
|
nuclear@0
|
210 void mgl_vertex2f(float x, float y)
|
nuclear@0
|
211 {
|
nuclear@0
|
212 mgl_vertex4f(x, y, 0.0f, 1.0f);
|
nuclear@0
|
213 }
|
nuclear@0
|
214
|
nuclear@0
|
215 void mgl_vertex3f(float x, float y, float z)
|
nuclear@0
|
216 {
|
nuclear@0
|
217 mgl_vertex4f(x, y, z, 1.0f);
|
nuclear@0
|
218 }
|
nuclear@0
|
219
|
nuclear@0
|
220 void mgl_vertex4f(float x, float y, float z, float w)
|
nuclear@0
|
221 {
|
nuclear@0
|
222 st.v[st.vidx].pos.x = x;
|
nuclear@0
|
223 st.v[st.vidx].pos.y = y;
|
nuclear@0
|
224 st.v[st.vidx].pos.z = z;
|
nuclear@0
|
225 st.v[st.vidx].pos.w = w;
|
nuclear@0
|
226 st.v[st.vidx].cidx = st.curv.cidx;
|
nuclear@0
|
227 st.v[st.vidx].energy = st.curv.energy;
|
nuclear@0
|
228 st.v[st.vidx].norm = st.curv.norm;
|
nuclear@0
|
229 st.v[st.vidx].tc = st.curv.tc;
|
nuclear@0
|
230
|
nuclear@0
|
231 vertex_proc(st.v + st.vidx);
|
nuclear@0
|
232
|
nuclear@0
|
233 if(++st.vidx >= st.prim) {
|
nuclear@0
|
234 switch(st.prim) {
|
nuclear@0
|
235 case MGL_POINTS:
|
nuclear@0
|
236 mgl_draw_point(st.v);
|
nuclear@0
|
237 break;
|
nuclear@0
|
238 case MGL_LINES:
|
nuclear@0
|
239 mgl_draw_line(st.v, st.v + 1);
|
nuclear@0
|
240 break;
|
nuclear@0
|
241 case MGL_TRIANGLES:
|
nuclear@0
|
242 case MGL_QUADS:
|
nuclear@0
|
243 mgl_draw_poly(st.v, st.prim);
|
nuclear@0
|
244 break;
|
nuclear@0
|
245 default:
|
nuclear@0
|
246 fprintf(stderr, "invalid primitive: %d\n", st.prim);
|
nuclear@0
|
247 abort();
|
nuclear@0
|
248 }
|
nuclear@0
|
249 st.vidx = 0;
|
nuclear@0
|
250 }
|
nuclear@0
|
251 }
|
nuclear@0
|
252
|
nuclear@0
|
253 void mgl_color1f(float energy)
|
nuclear@0
|
254 {
|
nuclear@0
|
255 st.curv.energy = energy;
|
nuclear@0
|
256 }
|
nuclear@0
|
257
|
nuclear@0
|
258 void mgl_index(int c)
|
nuclear@0
|
259 {
|
nuclear@0
|
260 st.curv.cidx = c;
|
nuclear@0
|
261 }
|
nuclear@0
|
262
|
nuclear@0
|
263 void mgl_normal(float x, float y, float z)
|
nuclear@0
|
264 {
|
nuclear@0
|
265 st.curv.norm.x = x;
|
nuclear@0
|
266 st.curv.norm.y = y;
|
nuclear@0
|
267 st.curv.norm.z = z;
|
nuclear@0
|
268 }
|
nuclear@0
|
269
|
nuclear@0
|
270 void mgl_texcoord2f(float x, float y)
|
nuclear@0
|
271 {
|
nuclear@0
|
272 st.curv.tc.x = x;
|
nuclear@0
|
273 st.curv.tc.y = y;
|
nuclear@0
|
274 }
|
nuclear@0
|
275
|
nuclear@0
|
276 static void transform(vec4_t *res, vec4_t *v, float *mat)
|
nuclear@0
|
277 {
|
nuclear@0
|
278 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z + mat[12] * v->w;
|
nuclear@0
|
279 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z + mat[13] * v->w;
|
nuclear@0
|
280 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z + mat[14] * v->w;
|
nuclear@0
|
281 res->w = mat[3] * v->x + mat[7] * v->y + mat[11] * v->z + mat[15] * v->w;
|
nuclear@0
|
282 }
|
nuclear@0
|
283
|
nuclear@0
|
284 /* the matrix is 4x4 (16 floats), just ignoring anything out of the 3x3 */
|
nuclear@0
|
285 static void transform3(vec3_t *res, vec3_t *v, float *mat)
|
nuclear@0
|
286 {
|
nuclear@0
|
287 res->x = mat[0] * v->x + mat[4] * v->y + mat[8] * v->z;
|
nuclear@0
|
288 res->y = mat[1] * v->x + mat[5] * v->y + mat[9] * v->z;
|
nuclear@0
|
289 res->z = mat[2] * v->x + mat[6] * v->y + mat[10] * v->z;
|
nuclear@0
|
290 }
|
nuclear@0
|
291
|
nuclear@0
|
292 static void vertex_proc(struct vertex *vert)
|
nuclear@0
|
293 {
|
nuclear@0
|
294 vec4_t pview, pclip;
|
nuclear@0
|
295
|
nuclear@0
|
296 float *mvmat = st.matrix[MGL_MODELVIEW][st.mtop[MGL_MODELVIEW]];
|
nuclear@0
|
297 float *pmat = st.matrix[MGL_PROJECTION][st.mtop[MGL_PROJECTION]];
|
nuclear@0
|
298
|
nuclear@0
|
299 /* modelview transformation */
|
nuclear@0
|
300 transform(&pview, &vert->pos, mvmat);
|
nuclear@0
|
301
|
nuclear@0
|
302 if(st.flags & MGL_LIGHTING) {
|
nuclear@0
|
303 if((st.flags & MGL_SMOOTH) || st.vidx == 0) {
|
nuclear@0
|
304 int i;
|
nuclear@0
|
305 vec3_t norm;
|
nuclear@0
|
306 float irrad = 0.0f;
|
nuclear@0
|
307
|
nuclear@0
|
308 transform3(&norm, &vert->norm, mvmat);
|
nuclear@0
|
309
|
nuclear@0
|
310 for(i=0; i<MAX_LIGHTS; i++) {
|
nuclear@0
|
311 if(st.lint[i] > 1e-6f) {
|
nuclear@11
|
312 float ndotl;
|
nuclear@11
|
313 vec3_t ldir;
|
nuclear@11
|
314
|
nuclear@11
|
315 if(st.lpos[i].w == 0.0) {
|
nuclear@11
|
316 ldir.x = st.lpos[i].x;
|
nuclear@11
|
317 ldir.y = st.lpos[i].y;
|
nuclear@11
|
318 ldir.z = st.lpos[i].z;
|
nuclear@11
|
319 } else {
|
nuclear@11
|
320 ldir.x = st.lpos[i].x - pview.x;
|
nuclear@11
|
321 ldir.y = st.lpos[i].y - pview.y;
|
nuclear@11
|
322 ldir.z = st.lpos[i].z - pview.z;
|
nuclear@11
|
323
|
nuclear@11
|
324 NORMALIZE(ldir);
|
nuclear@11
|
325 }
|
nuclear@11
|
326
|
nuclear@11
|
327 ndotl = DOT(norm, ldir);
|
nuclear@0
|
328 if(ndotl < 0.0) {
|
nuclear@0
|
329 ndotl = 0.0;
|
nuclear@0
|
330 }
|
nuclear@0
|
331 irrad += ndotl * st.lint[i];
|
nuclear@0
|
332 }
|
nuclear@0
|
333 }
|
nuclear@0
|
334 vert->energy = irrad;
|
nuclear@0
|
335 } else {
|
nuclear@0
|
336 vert->energy = st.v[0].energy;
|
nuclear@0
|
337 }
|
nuclear@0
|
338 }
|
nuclear@0
|
339
|
nuclear@0
|
340 transform(&pclip, &pview, pmat);
|
nuclear@0
|
341 /* TODO clipping in homogenous clip space */
|
nuclear@0
|
342
|
nuclear@0
|
343 if(pclip.w < 1e-6 && pclip.w > -1e-6) {
|
nuclear@0
|
344 vert->pos.x = vert->pos.y = vert->pos.z = vert->pos.w = 0.0f;
|
nuclear@0
|
345 return;
|
nuclear@0
|
346 }
|
nuclear@0
|
347
|
nuclear@0
|
348 /* perspective division */
|
nuclear@0
|
349 vert->pos.x = pclip.x / pclip.w;
|
nuclear@0
|
350 vert->pos.y = pclip.y / pclip.w;
|
nuclear@0
|
351 vert->pos.z = pclip.z / pclip.w;
|
nuclear@0
|
352 vert->pos.w = pclip.w;
|
nuclear@0
|
353
|
nuclear@0
|
354 /* viewport transformation */
|
nuclear@0
|
355 vert->pos.x = st.vp[0] + st.vp[2] * (vert->pos.x * 0.5 + 0.5);
|
nuclear@0
|
356 vert->pos.y = st.vp[1] + st.vp[3] * (-vert->pos.y * 0.5 + 0.5);
|
nuclear@0
|
357 }
|
nuclear@0
|
358
|
nuclear@0
|
359 void mgl_viewport(int x, int y, int width, int height)
|
nuclear@0
|
360 {
|
nuclear@0
|
361 st.vp[0] = x;
|
nuclear@0
|
362 st.vp[1] = y;
|
nuclear@0
|
363 st.vp[2] = width;
|
nuclear@0
|
364 st.vp[3] = height;
|
nuclear@0
|
365 }
|
nuclear@0
|
366
|
nuclear@0
|
367 void mgl_matrix_mode(int mmode)
|
nuclear@0
|
368 {
|
nuclear@0
|
369 st.mmode = mmode;
|
nuclear@0
|
370 }
|
nuclear@0
|
371
|
nuclear@0
|
372 void mgl_push_matrix(void)
|
nuclear@0
|
373 {
|
nuclear@0
|
374 float *topmat;
|
nuclear@0
|
375 if(st.mtop[st.mmode] >= MATRIX_STACK_SIZE - 1) {
|
nuclear@0
|
376 fprintf(stderr, "mgl_push_matrix: stack overflow\n");
|
nuclear@0
|
377 abort();
|
nuclear@0
|
378 }
|
nuclear@0
|
379
|
nuclear@0
|
380 topmat = st.matrix[st.mmode][st.mtop[st.mmode]];
|
nuclear@0
|
381 memcpy(topmat + 16, topmat, 16 * sizeof *topmat);
|
nuclear@0
|
382 st.mmode++;
|
nuclear@0
|
383 }
|
nuclear@0
|
384
|
nuclear@0
|
385 void mgl_pop_matrix(void)
|
nuclear@0
|
386 {
|
nuclear@0
|
387 if(st.mtop[st.mmode] <= 0) {
|
nuclear@0
|
388 fprintf(stderr, "mgl_pop_matrix: stack underflow\n");
|
nuclear@0
|
389 abort();
|
nuclear@0
|
390 }
|
nuclear@0
|
391 st.mtop[st.mmode]--;
|
nuclear@0
|
392 }
|
nuclear@0
|
393
|
nuclear@0
|
394 void mgl_load_matrix(float *mat)
|
nuclear@0
|
395 {
|
nuclear@0
|
396 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
|
nuclear@0
|
397 memcpy(dest, mat, 16 * sizeof *dest);
|
nuclear@0
|
398 }
|
nuclear@0
|
399
|
nuclear@17
|
400 #define M(i,j) (((i) << 2) + (j))
|
nuclear@0
|
401 void mgl_mult_matrix(float *m2)
|
nuclear@0
|
402 {
|
nuclear@0
|
403 int i, j;
|
nuclear@0
|
404 float m1[16];
|
nuclear@0
|
405 float *dest = st.matrix[st.mmode][st.mtop[st.mmode]];
|
nuclear@0
|
406
|
nuclear@0
|
407 memcpy(m1, dest, sizeof m1);
|
nuclear@0
|
408
|
nuclear@0
|
409 for(i=0; i<4; i++) {
|
nuclear@0
|
410 for(j=0; j<4; j++) {
|
nuclear@0
|
411 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
|
nuclear@0
|
412 m1[M(1,j)] * m2[M(i,1)] +
|
nuclear@0
|
413 m1[M(2,j)] * m2[M(i,2)] +
|
nuclear@0
|
414 m1[M(3,j)] * m2[M(i,3)];
|
nuclear@0
|
415 }
|
nuclear@0
|
416 }
|
nuclear@0
|
417 }
|
nuclear@0
|
418
|
nuclear@0
|
419 void mgl_load_identity(void)
|
nuclear@0
|
420 {
|
nuclear@0
|
421 static float id[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
422 mgl_load_matrix((float*)id);
|
nuclear@0
|
423 }
|
nuclear@0
|
424
|
nuclear@0
|
425 void mgl_translate(float x, float y, float z)
|
nuclear@0
|
426 {
|
nuclear@0
|
427 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
428 xform[12] = x;
|
nuclear@0
|
429 xform[13] = y;
|
nuclear@0
|
430 xform[14] = z;
|
nuclear@0
|
431 mgl_mult_matrix(xform);
|
nuclear@0
|
432 }
|
nuclear@0
|
433
|
nuclear@0
|
434 void mgl_rotate(float deg, float x, float y, float z)
|
nuclear@0
|
435 {
|
nuclear@0
|
436 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
437
|
nuclear@0
|
438 float angle = M_PI * deg / 180.0f;
|
nuclear@0
|
439 float sina = sin(angle);
|
nuclear@0
|
440 float cosa = cos(angle);
|
nuclear@0
|
441 float one_minus_cosa = 1.0f - cosa;
|
nuclear@0
|
442 float nxsq = x * x;
|
nuclear@0
|
443 float nysq = y * y;
|
nuclear@0
|
444 float nzsq = z * z;
|
nuclear@0
|
445
|
nuclear@0
|
446 xform[0] = nxsq + (1.0f - nxsq) * cosa;
|
nuclear@0
|
447 xform[4] = x * y * one_minus_cosa - z * sina;
|
nuclear@0
|
448 xform[8] = x * z * one_minus_cosa + y * sina;
|
nuclear@0
|
449 xform[1] = x * y * one_minus_cosa + z * sina;
|
nuclear@0
|
450 xform[5] = nysq + (1.0 - nysq) * cosa;
|
nuclear@0
|
451 xform[9] = y * z * one_minus_cosa - x * sina;
|
nuclear@0
|
452 xform[2] = x * z * one_minus_cosa - y * sina;
|
nuclear@0
|
453 xform[6] = y * z * one_minus_cosa + x * sina;
|
nuclear@0
|
454 xform[10] = nzsq + (1.0 - nzsq) * cosa;
|
nuclear@0
|
455
|
nuclear@0
|
456 mgl_mult_matrix(xform);
|
nuclear@0
|
457 }
|
nuclear@0
|
458
|
nuclear@0
|
459 void mgl_scale(float x, float y, float z)
|
nuclear@0
|
460 {
|
nuclear@0
|
461 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
462 xform[0] = x;
|
nuclear@0
|
463 xform[5] = y;
|
nuclear@0
|
464 xform[10] = z;
|
nuclear@0
|
465 mgl_mult_matrix(xform);
|
nuclear@0
|
466 }
|
nuclear@0
|
467
|
nuclear@0
|
468 void gl_ortho(float left, float right, float bottom, float top, float nr, float fr)
|
nuclear@0
|
469 {
|
nuclear@0
|
470 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
471
|
nuclear@0
|
472 float dx = right - left;
|
nuclear@0
|
473 float dy = top - bottom;
|
nuclear@0
|
474 float dz = fr - nr;
|
nuclear@0
|
475
|
nuclear@0
|
476 float tx = -(right + left) / dx;
|
nuclear@0
|
477 float ty = -(top + bottom) / dy;
|
nuclear@0
|
478 float tz = -(fr + nr) / dz;
|
nuclear@0
|
479
|
nuclear@0
|
480 float sx = 2.0 / dx;
|
nuclear@0
|
481 float sy = 2.0 / dy;
|
nuclear@0
|
482 float sz = -2.0 / dz;
|
nuclear@0
|
483
|
nuclear@0
|
484 xform[0] = sx;
|
nuclear@0
|
485 xform[5] = sy;
|
nuclear@0
|
486 xform[10] = sz;
|
nuclear@0
|
487 xform[12] = tx;
|
nuclear@0
|
488 xform[13] = ty;
|
nuclear@0
|
489 xform[14] = tz;
|
nuclear@0
|
490
|
nuclear@0
|
491 mgl_mult_matrix(xform);
|
nuclear@0
|
492 }
|
nuclear@0
|
493
|
nuclear@0
|
494 void mgl_frustum(float left, float right, float bottom, float top, float nr, float fr)
|
nuclear@0
|
495 {
|
nuclear@0
|
496 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
497
|
nuclear@0
|
498 float dx = right - left;
|
nuclear@0
|
499 float dy = top - bottom;
|
nuclear@0
|
500 float dz = fr - nr;
|
nuclear@0
|
501
|
nuclear@0
|
502 float a = (right + left) / dx;
|
nuclear@0
|
503 float b = (top + bottom) / dy;
|
nuclear@0
|
504 float c = -(fr + nr) / dz;
|
nuclear@0
|
505 float d = -2.0 * fr * nr / dz;
|
nuclear@0
|
506
|
nuclear@0
|
507 xform[0] = 2.0 * nr / dx;
|
nuclear@0
|
508 xform[5] = 2.0 * nr / dy;
|
nuclear@0
|
509 xform[8] = a;
|
nuclear@0
|
510 xform[9] = b;
|
nuclear@0
|
511 xform[10] = c;
|
nuclear@0
|
512 xform[11] = -1.0f;
|
nuclear@0
|
513 xform[14] = d;
|
nuclear@0
|
514
|
nuclear@0
|
515 mgl_mult_matrix(xform);
|
nuclear@0
|
516 }
|
nuclear@0
|
517
|
nuclear@0
|
518 void mgl_perspective(float vfov, float aspect, float nr, float fr)
|
nuclear@0
|
519 {
|
nuclear@0
|
520 float vfov_rad = M_PI * vfov / 180.0;
|
nuclear@0
|
521 float x = nr * tan(vfov_rad / 2.0);
|
nuclear@0
|
522 mgl_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
|
nuclear@0
|
523 }
|
nuclear@3
|
524
|
nuclear@3
|
525 void mgl_teximage(int width, int height, unsigned char *pixels)
|
nuclear@3
|
526 {
|
nuclear@3
|
527 st.tex.width = width;
|
nuclear@3
|
528 st.tex.height = height;
|
nuclear@3
|
529 st.tex.pixels = pixels;
|
nuclear@3
|
530
|
nuclear@3
|
531 if(calc_shiftmask(width, &st.tex.xshift, &st.tex.xmask) == -1 ||
|
nuclear@3
|
532 calc_shiftmask(height, &st.tex.yshift, &st.tex.ymask) == -1) {
|
nuclear@3
|
533 st.tex.pixels = 0;
|
nuclear@3
|
534 }
|
nuclear@3
|
535 }
|
nuclear@3
|
536
|
nuclear@3
|
537 #define MAX_SHIFT 12
|
nuclear@3
|
538 static int calc_shiftmask(int val, int *shiftp, unsigned int *maskp)
|
nuclear@3
|
539 {
|
nuclear@3
|
540 int i;
|
nuclear@3
|
541
|
nuclear@3
|
542 for(i=0; i<MAX_SHIFT; i++) {
|
nuclear@3
|
543 if((val >> i) == 1) {
|
nuclear@3
|
544 *shiftp = i;
|
nuclear@17
|
545 *maskp = ~(UINT_MAX << i);
|
nuclear@3
|
546 return 0;
|
nuclear@3
|
547 }
|
nuclear@3
|
548 }
|
nuclear@3
|
549 return -1;
|
nuclear@3
|
550 }
|