rayzor

annotate src/min3d.c @ 9:70e332156d02

moving along
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 10 Apr 2014 02:31:31 +0300
parents a68dbf80d547
children 79609d482762
rev   line source
nuclear@1 1 #include <stdlib.h>
nuclear@5 2 #include <string.h>
nuclear@5 3 #include <math.h>
nuclear@1 4 #include "min3d.h"
nuclear@1 5 #include "m3dimpl.h"
nuclear@6 6 #include "logger.h"
nuclear@1 7
nuclear@1 8 #ifndef M_PI
nuclear@1 9 #define M_PI 3.141592653
nuclear@1 10 #endif
nuclear@1 11
nuclear@3 12 struct min3d_context *m3dctx;
nuclear@3 13
nuclear@1 14 int m3d_init(void)
nuclear@1 15 {
nuclear@1 16 if(!(m3dctx = malloc(sizeof *m3dctx))) {
nuclear@1 17 return -1;
nuclear@1 18 }
nuclear@1 19 memset(m3dctx, 0, sizeof *m3dctx);
nuclear@1 20
nuclear@1 21 m3d_matrix_mode(M3D_PROJECTION);
nuclear@1 22 m3d_load_identity();
nuclear@1 23 m3d_matrix_mode(M3D_MODELVIEW);
nuclear@1 24 m3d_load_identity();
nuclear@6 25
nuclear@6 26 m3d_color(1, 1, 1);
nuclear@1 27 return 0;
nuclear@1 28 }
nuclear@1 29
nuclear@1 30 void m3d_shutdown(void)
nuclear@1 31 {
nuclear@1 32 free(m3dctx);
nuclear@1 33 }
nuclear@1 34
nuclear@1 35 void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf)
nuclear@1 36 {
nuclear@1 37 m3dctx->cbuf = cbuf;
nuclear@1 38 m3dctx->zbuf = zbuf;
nuclear@6 39
nuclear@9 40 m3d_viewport(0, 0, cbuf->xsz, cbuf->ysz);
nuclear@6 41 }
nuclear@6 42
nuclear@6 43 void m3d_clear_color(float r, float g, float b)
nuclear@6 44 {
nuclear@6 45 m3dctx->clear_color[0] = (int)((r > 1.0 ? 1.0 : r) * 255.0);
nuclear@6 46 m3dctx->clear_color[1] = (int)((g > 1.0 ? 1.0 : g) * 255.0);
nuclear@6 47 m3dctx->clear_color[2] = (int)((b > 1.0 ? 1.0 : b) * 255.0);
nuclear@1 48 }
nuclear@1 49
nuclear@1 50 void m3d_clear(unsigned int bmask)
nuclear@1 51 {
nuclear@9 52 int num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz;
nuclear@1 53 if(bmask & M3D_COLOR_BUFFER_BIT) {
nuclear@9 54 memset(m3dctx->cbuf->pixels, 0, num_pixels * 4);
nuclear@9 55 /*
nuclear@9 56 int i;
nuclear@6 57 unsigned char *ptr = m3dctx->cbuf->pixels;
nuclear@6 58 unsigned char r = m3dctx->clear_color[0];
nuclear@6 59 unsigned char g = m3dctx->clear_color[1];
nuclear@6 60 unsigned char b = m3dctx->clear_color[2];
nuclear@6 61 for(i=0; i<num_pixels; i++) {
nuclear@6 62 *ptr++ = r;
nuclear@6 63 *ptr++ = g;
nuclear@6 64 *ptr++ = b;
nuclear@9 65 }*/
nuclear@1 66 }
nuclear@1 67 if(bmask & M3D_DEPTH_BUFFER_BIT) {
nuclear@1 68 memset(m3dctx->zbuf, 0xff, num_pixels * sizeof *m3dctx->zbuf);
nuclear@1 69 }
nuclear@1 70 }
nuclear@1 71
nuclear@1 72
nuclear@1 73 void m3d_enable(int bit)
nuclear@1 74 {
nuclear@1 75 m3dctx->state |= (1 << bit);
nuclear@1 76 }
nuclear@1 77
nuclear@1 78 void m3d_disable(int bit)
nuclear@1 79 {
nuclear@1 80 m3dctx->state &= ~(1 << bit);
nuclear@1 81 }
nuclear@1 82
nuclear@9 83 void m3d_viewport(int x, int y, int xsz, int ysz)
nuclear@9 84 {
nuclear@9 85 m3dctx->vport[0] = x;
nuclear@9 86 m3dctx->vport[1] = y;
nuclear@9 87 m3dctx->vport[2] = xsz;
nuclear@9 88 m3dctx->vport[3] = ysz;
nuclear@9 89 }
nuclear@9 90
nuclear@1 91
nuclear@1 92 /* matrix stack */
nuclear@1 93 void m3d_matrix_mode(int mode)
nuclear@1 94 {
nuclear@1 95 m3dctx->mmode = mode;
nuclear@1 96 }
nuclear@1 97
nuclear@6 98 void m3d_push_matrix(void)
nuclear@6 99 {
nuclear@6 100 int mm = m3dctx->mmode;
nuclear@6 101 int top = m3dctx->mstack[mm].top;
nuclear@6 102 if(top < MSTACK_SIZE) {
nuclear@6 103 float *cur = m3dctx->mstack[mm].m[top++];
nuclear@6 104 memcpy(m3dctx->mstack[mm].m[top], cur, 16 * sizeof *cur);
nuclear@6 105 m3dctx->mstack[mm].top = top;
nuclear@6 106 }
nuclear@6 107 }
nuclear@6 108
nuclear@6 109 void m3d_pop_matrix(void)
nuclear@6 110 {
nuclear@6 111 int mm = m3dctx->mmode;
nuclear@6 112 if(m3dctx->mstack[mm].top > 0) {
nuclear@6 113 --m3dctx->mstack[mm].top;
nuclear@6 114 }
nuclear@6 115 }
nuclear@6 116
nuclear@1 117 void m3d_load_identity(void)
nuclear@1 118 {
nuclear@1 119 static const float mid[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 120 m3d_load_matrix(mid);
nuclear@1 121 }
nuclear@1 122
nuclear@1 123 void m3d_load_matrix(const float *m)
nuclear@1 124 {
nuclear@1 125 int top = m3dctx->mstack[m3dctx->mmode].top;
nuclear@1 126 memcpy(m3dctx->mstack[m3dctx->mmode].m[top], m, 16 * sizeof *m);
nuclear@1 127 }
nuclear@1 128
nuclear@1 129 #define M(i,j) (((i) << 2) + (j))
nuclear@1 130 void m3d_mult_matrix(const float *m2)
nuclear@1 131 {
nuclear@1 132 int i, j, top = m3dctx->mstack[m3dctx->mmode].top;
nuclear@1 133 float m1[16];
nuclear@1 134 float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
nuclear@1 135
nuclear@1 136 memcpy(m1, dest, sizeof m1);
nuclear@1 137
nuclear@1 138 for(i=0; i<4; i++) {
nuclear@1 139 for(j=0; j<4; j++) {
nuclear@1 140 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
nuclear@1 141 m1[M(1,j)] * m2[M(i,1)] +
nuclear@1 142 m1[M(2,j)] * m2[M(i,2)] +
nuclear@1 143 m1[M(3,j)] * m2[M(i,3)];
nuclear@1 144 }
nuclear@1 145 }
nuclear@1 146 }
nuclear@1 147
nuclear@1 148 void m3d_translate(float x, float y, float z)
nuclear@1 149 {
nuclear@1 150 float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 151 m[12] = x;
nuclear@1 152 m[13] = y;
nuclear@1 153 m[14] = z;
nuclear@1 154 m3d_mult_matrix(m);
nuclear@1 155 }
nuclear@1 156
nuclear@1 157 void m3d_rotate(float deg, float x, float y, float z)
nuclear@1 158 {
nuclear@1 159 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 160
nuclear@1 161 float angle = M_PI * deg / 180.0f;
nuclear@1 162 float sina = sin(angle);
nuclear@1 163 float cosa = cos(angle);
nuclear@1 164 float one_minus_cosa = 1.0f - cosa;
nuclear@1 165 float nxsq = x * x;
nuclear@1 166 float nysq = y * y;
nuclear@1 167 float nzsq = z * z;
nuclear@1 168
nuclear@1 169 xform[0] = nxsq + (1.0f - nxsq) * cosa;
nuclear@1 170 xform[4] = x * y * one_minus_cosa - z * sina;
nuclear@1 171 xform[8] = x * z * one_minus_cosa + y * sina;
nuclear@1 172 xform[1] = x * y * one_minus_cosa + z * sina;
nuclear@1 173 xform[5] = nysq + (1.0 - nysq) * cosa;
nuclear@1 174 xform[9] = y * z * one_minus_cosa - x * sina;
nuclear@1 175 xform[2] = x * z * one_minus_cosa - y * sina;
nuclear@1 176 xform[6] = y * z * one_minus_cosa + x * sina;
nuclear@1 177 xform[10] = nzsq + (1.0 - nzsq) * cosa;
nuclear@1 178
nuclear@1 179 m3d_mult_matrix(xform);
nuclear@1 180 }
nuclear@1 181
nuclear@1 182 void m3d_scale(float x, float y, float z)
nuclear@1 183 {
nuclear@1 184 static float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 185 m[0] = x;
nuclear@1 186 m[5] = y;
nuclear@1 187 m[10] = z;
nuclear@1 188 m3d_mult_matrix(m);
nuclear@1 189 }
nuclear@1 190
nuclear@1 191 void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
nuclear@1 192 {
nuclear@1 193 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 194
nuclear@1 195 float dx = right - left;
nuclear@1 196 float dy = top - bottom;
nuclear@1 197 float dz = fr - nr;
nuclear@1 198
nuclear@1 199 float a = (right + left) / dx;
nuclear@1 200 float b = (top + bottom) / dy;
nuclear@1 201 float c = -(fr + nr) / dz;
nuclear@1 202 float d = -2.0 * fr * nr / dz;
nuclear@1 203
nuclear@1 204 xform[0] = 2.0 * nr / dx;
nuclear@1 205 xform[5] = 2.0 * nr / dy;
nuclear@1 206 xform[8] = a;
nuclear@1 207 xform[9] = b;
nuclear@1 208 xform[10] = c;
nuclear@1 209 xform[11] = -1.0f;
nuclear@1 210 xform[14] = d;
nuclear@1 211
nuclear@1 212 m3d_mult_matrix(xform);
nuclear@1 213 }
nuclear@1 214
nuclear@1 215 void m3d_perspective(float vfov, float aspect, float nr, float fr)
nuclear@1 216 {
nuclear@1 217 float vfov_rad = M_PI * vfov / 180.0;
nuclear@1 218 float x = nr * tan(vfov_rad / 2.0);
nuclear@1 219 m3d_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
nuclear@1 220 }
nuclear@1 221
nuclear@2 222 static void xform4(float *mat, float *vec)
nuclear@2 223 {
nuclear@6 224 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
nuclear@6 225 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
nuclear@6 226 float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
nuclear@6 227 float w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
nuclear@2 228
nuclear@2 229 vec[0] = x;
nuclear@2 230 vec[1] = y;
nuclear@2 231 vec[2] = z;
nuclear@2 232 vec[3] = w;
nuclear@2 233 }
nuclear@2 234
nuclear@3 235 static int proc_prim(int prim, struct min3d_vertex *res, struct min3d_vertex *v)
nuclear@2 236 {
nuclear@3 237 int i;
nuclear@3 238 int vcount = prim;
nuclear@3 239 int mvtop, ptop;
nuclear@3 240 float *mvmat, *pmat;
nuclear@6 241 int *vport = m3dctx->vport;
nuclear@3 242
nuclear@3 243 mvtop = m3dctx->mstack[M3D_MODELVIEW].top;
nuclear@3 244 mvmat = m3dctx->mstack[M3D_MODELVIEW].m[mvtop];
nuclear@3 245 ptop = m3dctx->mstack[M3D_PROJECTION].top;
nuclear@3 246 pmat = m3dctx->mstack[M3D_PROJECTION].m[ptop];
nuclear@3 247
nuclear@3 248 /* transform to view space */
nuclear@3 249 for(i=0; i<vcount; i++) {
nuclear@3 250 res[i] = v[i];
nuclear@3 251 xform4(mvmat, res[i].pos);
nuclear@3 252 /* TODO: normal */
nuclear@3 253 }
nuclear@3 254
nuclear@3 255 /* TODO: lighting */
nuclear@3 256
nuclear@3 257 /* project */
nuclear@3 258 for(i=0; i<vcount; i++) {
nuclear@3 259 xform4(pmat, res[i].pos);
nuclear@3 260 }
nuclear@3 261
nuclear@3 262 /* clip */
nuclear@3 263 switch(prim) {
nuclear@3 264 case M3D_POINTS:
nuclear@3 265 {
nuclear@3 266 float w = res[0].pos[3];
nuclear@3 267 if(res[0].pos[2] < -w || res[0].pos[2] >= w ||
nuclear@3 268 res[0].pos[0] / w < -1 || res[0].pos[0] / w >= 1 ||
nuclear@3 269 res[0].pos[1] / w < -1 || res[0].pos[1] / w >= 1) {
nuclear@3 270 vcount = 0;
nuclear@3 271 }
nuclear@3 272 }
nuclear@3 273 break;
nuclear@3 274
nuclear@3 275 default:
nuclear@3 276 break; /* TODO */
nuclear@3 277 }
nuclear@3 278
nuclear@6 279 /* perspective division & viewport */
nuclear@3 280 for(i=0; i<vcount; i++) {
nuclear@6 281 res[i].pos[0] /= res[i].pos[3];
nuclear@6 282 res[i].pos[1] /= res[i].pos[3];
nuclear@6 283 res[i].pos[2] /= res[i].pos[3];
nuclear@6 284
nuclear@6 285 res[i].pos[0] = (res[i].pos[0] * 0.5 + 0.5) * vport[2] + vport[0];
nuclear@9 286 res[i].pos[1] = (-res[i].pos[1] * 0.5 + 0.5) * vport[3] + vport[1];
nuclear@3 287 }
nuclear@3 288 return vcount;
nuclear@2 289 }
nuclear@2 290
nuclear@1 291 /* drawing */
nuclear@5 292 void m3d_vertex_array(const float *varr)
nuclear@5 293 {
nuclear@6 294 m3dctx->vert_array = (float*)varr;
nuclear@5 295 }
nuclear@5 296
nuclear@5 297 void m3d_normal_array(const float *narr)
nuclear@5 298 {
nuclear@6 299 m3dctx->norm_array = (float*)narr;
nuclear@5 300 }
nuclear@5 301
nuclear@5 302 void m3d_color_array(const float *carr)
nuclear@5 303 {
nuclear@6 304 m3dctx->col_array = (float*)carr;
nuclear@5 305 }
nuclear@5 306
nuclear@5 307 void m3d_texcoord_array(const float *tcarr)
nuclear@5 308 {
nuclear@6 309 m3dctx->tc_array = (float*)tcarr;
nuclear@5 310 }
nuclear@5 311
nuclear@5 312
nuclear@5 313 void m3d_draw(int prim, int vcount)
nuclear@1 314 {
nuclear@3 315 int i;
nuclear@3 316 struct min3d_vertex v[4];
nuclear@3 317 struct min3d_vertex resv[16];
nuclear@5 318 const float *varr = m3dctx->vert_array;
nuclear@5 319 const float *carr = m3dctx->col_array;
nuclear@5 320
nuclear@5 321 if(!varr) return;
nuclear@3 322
nuclear@3 323 for(i=0; i<vcount; i++) {
nuclear@9 324 int r, g, b;
nuclear@3 325 int idx = i % prim;
nuclear@3 326
nuclear@3 327 v[idx].pos[0] = *varr++;
nuclear@3 328 v[idx].pos[1] = *varr++;
nuclear@3 329 v[idx].pos[2] = *varr++;
nuclear@6 330 v[idx].pos[3] = 1.0;
nuclear@9 331 r = (carr ? *carr++ : m3dctx->im_color[0]) * 255.0;
nuclear@9 332 g = (carr ? *carr++ : m3dctx->im_color[1]) * 255.0;
nuclear@9 333 b = (carr ? *carr++ : m3dctx->im_color[2]) * 255.0;
nuclear@9 334 v[idx].color = (r << 16) | (g << 8) | b;
nuclear@3 335
nuclear@3 336 if(idx == prim - 1) {
nuclear@3 337 int resnum = proc_prim(prim, resv, v);
nuclear@3 338 switch(resnum) {
nuclear@3 339 case 1:
nuclear@9 340 m3d_draw_point(resv);
nuclear@3 341 break;
nuclear@3 342
nuclear@9 343 case 2:
nuclear@9 344 m3d_draw_line(resv);
nuclear@3 345 break;
nuclear@3 346
nuclear@3 347 default:
nuclear@9 348 m3d_draw_poly(resv, resnum);
nuclear@3 349 }
nuclear@3 350 }
nuclear@3 351 }
nuclear@1 352 }
nuclear@1 353
nuclear@9 354 void m3d_draw_indexed(int prim, const unsigned int *idxarr, int icount)
nuclear@1 355 {
nuclear@9 356 int i, vcount = prim;
nuclear@9 357 struct min3d_vertex v[4];
nuclear@9 358 struct min3d_vertex resv[16];
nuclear@9 359 const float *varr = m3dctx->vert_array;
nuclear@9 360 const float *carr = m3dctx->col_array;
nuclear@9 361
nuclear@9 362 if(!varr) return;
nuclear@9 363
nuclear@9 364 for(i=0; i<icount; i++) {
nuclear@9 365 int r, g, b;
nuclear@9 366 int vnum = i % vcount;
nuclear@9 367 int index = idxarr[i];
nuclear@9 368
nuclear@9 369 v[vnum].pos[0] = varr[index * 3];
nuclear@9 370 v[vnum].pos[1] = varr[index * 3 + 1];
nuclear@9 371 v[vnum].pos[2] = varr[index * 3 + 2];
nuclear@9 372 v[vnum].pos[3] = 1.0;
nuclear@9 373 r = (carr ? carr[index * 3] : m3dctx->im_color[0]) * 255.0;
nuclear@9 374 g = (carr ? carr[index * 3 + 1] : m3dctx->im_color[1]) * 255.0;
nuclear@9 375 b = (carr ? carr[index * 3 + 2] : m3dctx->im_color[2]) * 255.0;
nuclear@9 376 v[vnum].color = (r << 16) | (g << 8) | b;
nuclear@9 377
nuclear@9 378 if(vnum == vcount - 1) {
nuclear@9 379 int resnum = proc_prim(prim, resv, v);
nuclear@9 380 switch(resnum) {
nuclear@9 381 case 1:
nuclear@9 382 m3d_draw_point(resv);
nuclear@9 383 break;
nuclear@9 384
nuclear@9 385 case 2:
nuclear@9 386 m3d_draw_line(resv);
nuclear@9 387 break;
nuclear@9 388
nuclear@9 389 default:
nuclear@9 390 m3d_draw_poly(resv, resnum);
nuclear@9 391 }
nuclear@9 392 }
nuclear@9 393 }
nuclear@1 394 }
nuclear@1 395
nuclear@6 396 void m3d_begin(int prim)
nuclear@6 397 {
nuclear@6 398 m3dctx->im_prim = prim;
nuclear@6 399 m3dctx->im_idx = 0;
nuclear@6 400
nuclear@6 401 m3dctx->vert_array = m3dctx->im_varr;
nuclear@6 402 m3dctx->norm_array = 0;
nuclear@6 403 m3dctx->col_array = 0;
nuclear@6 404 m3dctx->tc_array = 0;
nuclear@6 405 }
nuclear@6 406
nuclear@6 407 void m3d_end(void)
nuclear@6 408 {
nuclear@6 409 }
nuclear@6 410
nuclear@6 411 void m3d_vertex(float x, float y, float z)
nuclear@6 412 {
nuclear@6 413 int nverts = m3dctx->im_prim;
nuclear@6 414 int idx = m3dctx->im_idx;
nuclear@6 415 float *v = m3dctx->vert_array + idx * 3;
nuclear@6 416
nuclear@6 417 v[0] = x;
nuclear@6 418 v[1] = y;
nuclear@6 419 v[2] = z;
nuclear@6 420
nuclear@6 421 if(m3dctx->norm_array) {
nuclear@6 422 float *ptr = m3dctx->im_narr + idx * 3;
nuclear@6 423 ptr[0] = m3dctx->im_normal[0];
nuclear@6 424 ptr[1] = m3dctx->im_normal[1];
nuclear@6 425 ptr[2] = m3dctx->im_normal[2];
nuclear@6 426 }
nuclear@6 427 if(m3dctx->col_array) {
nuclear@6 428 float *ptr = m3dctx->im_carr + idx * 3;
nuclear@6 429 ptr[0] = m3dctx->im_color[0];
nuclear@6 430 ptr[1] = m3dctx->im_color[1];
nuclear@6 431 ptr[2] = m3dctx->im_color[2];
nuclear@6 432 }
nuclear@6 433 if(m3dctx->tc_array) {
nuclear@6 434 float *ptr = m3dctx->im_texcoord + idx * 2;
nuclear@6 435 ptr[0] = m3dctx->im_texcoord[0];
nuclear@6 436 ptr[1] = m3dctx->im_texcoord[1];
nuclear@6 437 }
nuclear@6 438
nuclear@6 439 if(++idx == nverts) {
nuclear@6 440 m3d_draw(m3dctx->im_prim, nverts);
nuclear@6 441 idx = 0;
nuclear@6 442 }
nuclear@6 443
nuclear@6 444 m3dctx->im_idx = idx;
nuclear@6 445 }
nuclear@6 446
nuclear@6 447 void m3d_normal(float x, float y, float z)
nuclear@6 448 {
nuclear@6 449 m3dctx->im_normal[0] = x;
nuclear@6 450 m3dctx->im_normal[1] = y;
nuclear@6 451 m3dctx->im_normal[2] = z;
nuclear@6 452 }
nuclear@6 453
nuclear@6 454 void m3d_color(float x, float y, float z)
nuclear@6 455 {
nuclear@6 456 m3dctx->im_color[0] = x;
nuclear@6 457 m3dctx->im_color[1] = y;
nuclear@6 458 m3dctx->im_color[2] = z;
nuclear@6 459 }
nuclear@6 460
nuclear@6 461 void m3d_texcoord(float x, float y)
nuclear@6 462 {
nuclear@6 463 m3dctx->im_texcoord[0] = x;
nuclear@6 464 m3dctx->im_texcoord[1] = y;
nuclear@6 465 }