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