rayzor

view src/min3d.c @ 11:fe94d9e986ae

optimized 16bpp swap_buffers
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 10 Apr 2014 08:42:33 +0300
parents a68dbf80d547
children 79609d482762
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 #define M(i,j) (((i) << 2) + (j))
130 void m3d_mult_matrix(const float *m2)
131 {
132 int i, j, top = m3dctx->mstack[m3dctx->mmode].top;
133 float m1[16];
134 float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
136 memcpy(m1, dest, sizeof m1);
138 for(i=0; i<4; i++) {
139 for(j=0; j<4; j++) {
140 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
141 m1[M(1,j)] * m2[M(i,1)] +
142 m1[M(2,j)] * m2[M(i,2)] +
143 m1[M(3,j)] * m2[M(i,3)];
144 }
145 }
146 }
148 void m3d_translate(float x, float y, float z)
149 {
150 float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
151 m[12] = x;
152 m[13] = y;
153 m[14] = z;
154 m3d_mult_matrix(m);
155 }
157 void m3d_rotate(float deg, float x, float y, float z)
158 {
159 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
161 float angle = M_PI * deg / 180.0f;
162 float sina = sin(angle);
163 float cosa = cos(angle);
164 float one_minus_cosa = 1.0f - cosa;
165 float nxsq = x * x;
166 float nysq = y * y;
167 float nzsq = z * z;
169 xform[0] = nxsq + (1.0f - nxsq) * cosa;
170 xform[4] = x * y * one_minus_cosa - z * sina;
171 xform[8] = x * z * one_minus_cosa + y * sina;
172 xform[1] = x * y * one_minus_cosa + z * sina;
173 xform[5] = nysq + (1.0 - nysq) * cosa;
174 xform[9] = y * z * one_minus_cosa - x * sina;
175 xform[2] = x * z * one_minus_cosa - y * sina;
176 xform[6] = y * z * one_minus_cosa + x * sina;
177 xform[10] = nzsq + (1.0 - nzsq) * cosa;
179 m3d_mult_matrix(xform);
180 }
182 void m3d_scale(float x, float y, float z)
183 {
184 static float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
185 m[0] = x;
186 m[5] = y;
187 m[10] = z;
188 m3d_mult_matrix(m);
189 }
191 void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
192 {
193 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
195 float dx = right - left;
196 float dy = top - bottom;
197 float dz = fr - nr;
199 float a = (right + left) / dx;
200 float b = (top + bottom) / dy;
201 float c = -(fr + nr) / dz;
202 float d = -2.0 * fr * nr / dz;
204 xform[0] = 2.0 * nr / dx;
205 xform[5] = 2.0 * nr / dy;
206 xform[8] = a;
207 xform[9] = b;
208 xform[10] = c;
209 xform[11] = -1.0f;
210 xform[14] = d;
212 m3d_mult_matrix(xform);
213 }
215 void m3d_perspective(float vfov, float aspect, float nr, float fr)
216 {
217 float vfov_rad = M_PI * vfov / 180.0;
218 float x = nr * tan(vfov_rad / 2.0);
219 m3d_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
220 }
222 static void xform4(float *mat, float *vec)
223 {
224 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
225 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
226 float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
227 float w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
229 vec[0] = x;
230 vec[1] = y;
231 vec[2] = z;
232 vec[3] = w;
233 }
235 static int proc_prim(int prim, struct min3d_vertex *res, struct min3d_vertex *v)
236 {
237 int i;
238 int vcount = prim;
239 int mvtop, ptop;
240 float *mvmat, *pmat;
241 int *vport = m3dctx->vport;
243 mvtop = m3dctx->mstack[M3D_MODELVIEW].top;
244 mvmat = m3dctx->mstack[M3D_MODELVIEW].m[mvtop];
245 ptop = m3dctx->mstack[M3D_PROJECTION].top;
246 pmat = m3dctx->mstack[M3D_PROJECTION].m[ptop];
248 /* transform to view space */
249 for(i=0; i<vcount; i++) {
250 res[i] = v[i];
251 xform4(mvmat, res[i].pos);
252 /* TODO: normal */
253 }
255 /* TODO: lighting */
257 /* project */
258 for(i=0; i<vcount; i++) {
259 xform4(pmat, res[i].pos);
260 }
262 /* clip */
263 switch(prim) {
264 case M3D_POINTS:
265 {
266 float w = res[0].pos[3];
267 if(res[0].pos[2] < -w || res[0].pos[2] >= w ||
268 res[0].pos[0] / w < -1 || res[0].pos[0] / w >= 1 ||
269 res[0].pos[1] / w < -1 || res[0].pos[1] / w >= 1) {
270 vcount = 0;
271 }
272 }
273 break;
275 default:
276 break; /* TODO */
277 }
279 /* perspective division & viewport */
280 for(i=0; i<vcount; i++) {
281 res[i].pos[0] /= res[i].pos[3];
282 res[i].pos[1] /= res[i].pos[3];
283 res[i].pos[2] /= res[i].pos[3];
285 res[i].pos[0] = (res[i].pos[0] * 0.5 + 0.5) * vport[2] + vport[0];
286 res[i].pos[1] = (-res[i].pos[1] * 0.5 + 0.5) * vport[3] + vport[1];
287 }
288 return vcount;
289 }
291 /* drawing */
292 void m3d_vertex_array(const float *varr)
293 {
294 m3dctx->vert_array = (float*)varr;
295 }
297 void m3d_normal_array(const float *narr)
298 {
299 m3dctx->norm_array = (float*)narr;
300 }
302 void m3d_color_array(const float *carr)
303 {
304 m3dctx->col_array = (float*)carr;
305 }
307 void m3d_texcoord_array(const float *tcarr)
308 {
309 m3dctx->tc_array = (float*)tcarr;
310 }
313 void m3d_draw(int prim, int vcount)
314 {
315 int i;
316 struct min3d_vertex v[4];
317 struct min3d_vertex resv[16];
318 const float *varr = m3dctx->vert_array;
319 const float *carr = m3dctx->col_array;
321 if(!varr) return;
323 for(i=0; i<vcount; i++) {
324 int r, g, b;
325 int idx = i % prim;
327 v[idx].pos[0] = *varr++;
328 v[idx].pos[1] = *varr++;
329 v[idx].pos[2] = *varr++;
330 v[idx].pos[3] = 1.0;
331 r = (carr ? *carr++ : m3dctx->im_color[0]) * 255.0;
332 g = (carr ? *carr++ : m3dctx->im_color[1]) * 255.0;
333 b = (carr ? *carr++ : m3dctx->im_color[2]) * 255.0;
334 v[idx].color = (r << 16) | (g << 8) | b;
336 if(idx == prim - 1) {
337 int resnum = proc_prim(prim, resv, v);
338 switch(resnum) {
339 case 1:
340 m3d_draw_point(resv);
341 break;
343 case 2:
344 m3d_draw_line(resv);
345 break;
347 default:
348 m3d_draw_poly(resv, resnum);
349 }
350 }
351 }
352 }
354 void m3d_draw_indexed(int prim, const unsigned int *idxarr, int icount)
355 {
356 int i, vcount = prim;
357 struct min3d_vertex v[4];
358 struct min3d_vertex resv[16];
359 const float *varr = m3dctx->vert_array;
360 const float *carr = m3dctx->col_array;
362 if(!varr) return;
364 for(i=0; i<icount; i++) {
365 int r, g, b;
366 int vnum = i % vcount;
367 int index = idxarr[i];
369 v[vnum].pos[0] = varr[index * 3];
370 v[vnum].pos[1] = varr[index * 3 + 1];
371 v[vnum].pos[2] = varr[index * 3 + 2];
372 v[vnum].pos[3] = 1.0;
373 r = (carr ? carr[index * 3] : m3dctx->im_color[0]) * 255.0;
374 g = (carr ? carr[index * 3 + 1] : m3dctx->im_color[1]) * 255.0;
375 b = (carr ? carr[index * 3 + 2] : m3dctx->im_color[2]) * 255.0;
376 v[vnum].color = (r << 16) | (g << 8) | b;
378 if(vnum == vcount - 1) {
379 int resnum = proc_prim(prim, resv, v);
380 switch(resnum) {
381 case 1:
382 m3d_draw_point(resv);
383 break;
385 case 2:
386 m3d_draw_line(resv);
387 break;
389 default:
390 m3d_draw_poly(resv, resnum);
391 }
392 }
393 }
394 }
396 void m3d_begin(int prim)
397 {
398 m3dctx->im_prim = prim;
399 m3dctx->im_idx = 0;
401 m3dctx->vert_array = m3dctx->im_varr;
402 m3dctx->norm_array = 0;
403 m3dctx->col_array = 0;
404 m3dctx->tc_array = 0;
405 }
407 void m3d_end(void)
408 {
409 }
411 void m3d_vertex(float x, float y, float z)
412 {
413 int nverts = m3dctx->im_prim;
414 int idx = m3dctx->im_idx;
415 float *v = m3dctx->vert_array + idx * 3;
417 v[0] = x;
418 v[1] = y;
419 v[2] = z;
421 if(m3dctx->norm_array) {
422 float *ptr = m3dctx->im_narr + idx * 3;
423 ptr[0] = m3dctx->im_normal[0];
424 ptr[1] = m3dctx->im_normal[1];
425 ptr[2] = m3dctx->im_normal[2];
426 }
427 if(m3dctx->col_array) {
428 float *ptr = m3dctx->im_carr + idx * 3;
429 ptr[0] = m3dctx->im_color[0];
430 ptr[1] = m3dctx->im_color[1];
431 ptr[2] = m3dctx->im_color[2];
432 }
433 if(m3dctx->tc_array) {
434 float *ptr = m3dctx->im_texcoord + idx * 2;
435 ptr[0] = m3dctx->im_texcoord[0];
436 ptr[1] = m3dctx->im_texcoord[1];
437 }
439 if(++idx == nverts) {
440 m3d_draw(m3dctx->im_prim, nverts);
441 idx = 0;
442 }
444 m3dctx->im_idx = idx;
445 }
447 void m3d_normal(float x, float y, float z)
448 {
449 m3dctx->im_normal[0] = x;
450 m3dctx->im_normal[1] = y;
451 m3dctx->im_normal[2] = z;
452 }
454 void m3d_color(float x, float y, float z)
455 {
456 m3dctx->im_color[0] = x;
457 m3dctx->im_color[1] = y;
458 m3dctx->im_color[2] = z;
459 }
461 void m3d_texcoord(float x, float y)
462 {
463 m3dctx->im_texcoord[0] = x;
464 m3dctx->im_texcoord[1] = y;
465 }