deepstone

annotate src/mingl.c @ 30:a8d42276b5ad

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