rayzor

view src/min3d.c @ 6:a68dbf80d547

finally showing something ... :)
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 07 Apr 2014 06:04:11 +0300
parents 5fcf72837b69
children 70e332156d02
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 m3dctx->vport[0] = m3dctx->vport[1] = 0;
41 m3dctx->vport[2] = cbuf->xsz;
42 m3dctx->vport[3] = cbuf->ysz;
43 }
45 void m3d_clear_color(float r, float g, float b)
46 {
47 m3dctx->clear_color[0] = (int)((r > 1.0 ? 1.0 : r) * 255.0);
48 m3dctx->clear_color[1] = (int)((g > 1.0 ? 1.0 : g) * 255.0);
49 m3dctx->clear_color[2] = (int)((b > 1.0 ? 1.0 : b) * 255.0);
50 }
52 void m3d_clear(unsigned int bmask)
53 {
54 int i, num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz;
55 if(bmask & M3D_COLOR_BUFFER_BIT) {
56 /*memset(m3dctx->cbuf->pixels, 0, num_pixels * 3);*/
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 }
84 /* matrix stack */
85 void m3d_matrix_mode(int mode)
86 {
87 m3dctx->mmode = mode;
88 }
90 void m3d_push_matrix(void)
91 {
92 int mm = m3dctx->mmode;
93 int top = m3dctx->mstack[mm].top;
94 if(top < MSTACK_SIZE) {
95 float *cur = m3dctx->mstack[mm].m[top++];
96 memcpy(m3dctx->mstack[mm].m[top], cur, 16 * sizeof *cur);
97 m3dctx->mstack[mm].top = top;
98 }
99 }
101 void m3d_pop_matrix(void)
102 {
103 int mm = m3dctx->mmode;
104 if(m3dctx->mstack[mm].top > 0) {
105 --m3dctx->mstack[mm].top;
106 }
107 }
109 void m3d_load_identity(void)
110 {
111 static const float mid[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
112 m3d_load_matrix(mid);
113 }
115 void m3d_load_matrix(const float *m)
116 {
117 int top = m3dctx->mstack[m3dctx->mmode].top;
118 memcpy(m3dctx->mstack[m3dctx->mmode].m[top], m, 16 * sizeof *m);
119 }
121 #define M(i,j) (((i) << 2) + (j))
122 void m3d_mult_matrix(const float *m2)
123 {
124 int i, j, top = m3dctx->mstack[m3dctx->mmode].top;
125 float m1[16];
126 float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
128 memcpy(m1, dest, sizeof m1);
130 for(i=0; i<4; i++) {
131 for(j=0; j<4; j++) {
132 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
133 m1[M(1,j)] * m2[M(i,1)] +
134 m1[M(2,j)] * m2[M(i,2)] +
135 m1[M(3,j)] * m2[M(i,3)];
136 }
137 }
138 }
140 void m3d_translate(float x, float y, float z)
141 {
142 float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
143 m[12] = x;
144 m[13] = y;
145 m[14] = z;
146 m3d_mult_matrix(m);
147 }
149 void m3d_rotate(float deg, float x, float y, float z)
150 {
151 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
153 float angle = M_PI * deg / 180.0f;
154 float sina = sin(angle);
155 float cosa = cos(angle);
156 float one_minus_cosa = 1.0f - cosa;
157 float nxsq = x * x;
158 float nysq = y * y;
159 float nzsq = z * z;
161 xform[0] = nxsq + (1.0f - nxsq) * cosa;
162 xform[4] = x * y * one_minus_cosa - z * sina;
163 xform[8] = x * z * one_minus_cosa + y * sina;
164 xform[1] = x * y * one_minus_cosa + z * sina;
165 xform[5] = nysq + (1.0 - nysq) * cosa;
166 xform[9] = y * z * one_minus_cosa - x * sina;
167 xform[2] = x * z * one_minus_cosa - y * sina;
168 xform[6] = y * z * one_minus_cosa + x * sina;
169 xform[10] = nzsq + (1.0 - nzsq) * cosa;
171 m3d_mult_matrix(xform);
172 }
174 void m3d_scale(float x, float y, float z)
175 {
176 static float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
177 m[0] = x;
178 m[5] = y;
179 m[10] = z;
180 m3d_mult_matrix(m);
181 }
183 void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
184 {
185 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
187 float dx = right - left;
188 float dy = top - bottom;
189 float dz = fr - nr;
191 float a = (right + left) / dx;
192 float b = (top + bottom) / dy;
193 float c = -(fr + nr) / dz;
194 float d = -2.0 * fr * nr / dz;
196 xform[0] = 2.0 * nr / dx;
197 xform[5] = 2.0 * nr / dy;
198 xform[8] = a;
199 xform[9] = b;
200 xform[10] = c;
201 xform[11] = -1.0f;
202 xform[14] = d;
204 m3d_mult_matrix(xform);
205 }
207 void m3d_perspective(float vfov, float aspect, float nr, float fr)
208 {
209 float vfov_rad = M_PI * vfov / 180.0;
210 float x = nr * tan(vfov_rad / 2.0);
211 m3d_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
212 }
214 static void xform4(float *mat, float *vec)
215 {
216 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
217 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
218 float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
219 float w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
221 vec[0] = x;
222 vec[1] = y;
223 vec[2] = z;
224 vec[3] = w;
225 }
227 static int proc_prim(int prim, struct min3d_vertex *res, struct min3d_vertex *v)
228 {
229 int i;
230 int vcount = prim;
231 int mvtop, ptop;
232 float *mvmat, *pmat;
233 int *vport = m3dctx->vport;
235 mvtop = m3dctx->mstack[M3D_MODELVIEW].top;
236 mvmat = m3dctx->mstack[M3D_MODELVIEW].m[mvtop];
237 ptop = m3dctx->mstack[M3D_PROJECTION].top;
238 pmat = m3dctx->mstack[M3D_PROJECTION].m[ptop];
240 /* transform to view space */
241 for(i=0; i<vcount; i++) {
242 res[i] = v[i];
243 xform4(mvmat, res[i].pos);
244 /* TODO: normal */
245 }
247 /* TODO: lighting */
249 /* project */
250 for(i=0; i<vcount; i++) {
251 xform4(pmat, res[i].pos);
252 }
254 /* clip */
255 switch(prim) {
256 case M3D_POINTS:
257 {
258 float w = res[0].pos[3];
259 if(res[0].pos[2] < -w || res[0].pos[2] >= w ||
260 res[0].pos[0] / w < -1 || res[0].pos[0] / w >= 1 ||
261 res[0].pos[1] / w < -1 || res[0].pos[1] / w >= 1) {
262 vcount = 0;
263 }
264 }
265 break;
267 default:
268 break; /* TODO */
269 }
271 /* perspective division & viewport */
272 for(i=0; i<vcount; i++) {
273 res[i].pos[0] /= res[i].pos[3];
274 res[i].pos[1] /= res[i].pos[3];
275 res[i].pos[2] /= res[i].pos[3];
277 res[i].pos[0] = (res[i].pos[0] * 0.5 + 0.5) * vport[2] + vport[0];
278 res[i].pos[1] = (res[i].pos[1] * 0.5 + 0.5) * vport[3] + vport[1];
279 }
280 return vcount;
281 }
283 /* drawing */
284 void m3d_vertex_array(const float *varr)
285 {
286 m3dctx->vert_array = (float*)varr;
287 }
289 void m3d_normal_array(const float *narr)
290 {
291 m3dctx->norm_array = (float*)narr;
292 }
294 void m3d_color_array(const float *carr)
295 {
296 m3dctx->col_array = (float*)carr;
297 }
299 void m3d_texcoord_array(const float *tcarr)
300 {
301 m3dctx->tc_array = (float*)tcarr;
302 }
305 void m3d_draw(int prim, int vcount)
306 {
307 int i;
308 struct min3d_vertex v[4];
309 struct min3d_vertex resv[16];
310 const float *varr = m3dctx->vert_array;
311 const float *carr = m3dctx->col_array;
313 if(!varr) return;
315 for(i=0; i<vcount; i++) {
316 int idx = i % prim;
318 v[idx].pos[0] = *varr++;
319 v[idx].pos[1] = *varr++;
320 v[idx].pos[2] = *varr++;
321 v[idx].pos[3] = 1.0;
322 v[idx].color[0] = carr ? *carr++ : m3dctx->im_color[0];
323 v[idx].color[1] = carr ? *carr++ : m3dctx->im_color[1];
324 v[idx].color[2] = carr ? *carr++ : m3dctx->im_color[2];
326 if(idx == prim - 1) {
327 int resnum = proc_prim(prim, resv, v);
328 switch(resnum) {
329 case 1:
330 draw_point(resv);
331 break;
333 case '2':
334 draw_line(resv);
335 break;
337 default:
338 draw_poly(resv, resnum);
339 }
340 }
341 }
342 }
344 void m3d_draw_indexed(int prim, const int *idxarr, int icount)
345 {
346 /* TODO */
347 }
349 void m3d_begin(int prim)
350 {
351 m3dctx->im_prim = prim;
352 m3dctx->im_idx = 0;
354 m3dctx->vert_array = m3dctx->im_varr;
355 m3dctx->norm_array = 0;
356 m3dctx->col_array = 0;
357 m3dctx->tc_array = 0;
358 }
360 void m3d_end(void)
361 {
362 }
364 void m3d_vertex(float x, float y, float z)
365 {
366 int nverts = m3dctx->im_prim;
367 int idx = m3dctx->im_idx;
368 float *v = m3dctx->vert_array + idx * 3;
370 v[0] = x;
371 v[1] = y;
372 v[2] = z;
374 if(m3dctx->norm_array) {
375 float *ptr = m3dctx->im_narr + idx * 3;
376 ptr[0] = m3dctx->im_normal[0];
377 ptr[1] = m3dctx->im_normal[1];
378 ptr[2] = m3dctx->im_normal[2];
379 }
380 if(m3dctx->col_array) {
381 float *ptr = m3dctx->im_carr + idx * 3;
382 ptr[0] = m3dctx->im_color[0];
383 ptr[1] = m3dctx->im_color[1];
384 ptr[2] = m3dctx->im_color[2];
385 }
386 if(m3dctx->tc_array) {
387 float *ptr = m3dctx->im_texcoord + idx * 2;
388 ptr[0] = m3dctx->im_texcoord[0];
389 ptr[1] = m3dctx->im_texcoord[1];
390 }
392 if(++idx == nverts) {
393 m3d_draw(m3dctx->im_prim, nverts);
394 idx = 0;
395 }
397 m3dctx->im_idx = idx;
398 }
400 void m3d_normal(float x, float y, float z)
401 {
402 m3dctx->im_normal[0] = x;
403 m3dctx->im_normal[1] = y;
404 m3dctx->im_normal[2] = z;
405 }
407 void m3d_color(float x, float y, float z)
408 {
409 m3dctx->im_color[0] = x;
410 m3dctx->im_color[1] = y;
411 m3dctx->im_color[2] = z;
412 }
414 void m3d_texcoord(float x, float y)
415 {
416 m3dctx->im_texcoord[0] = x;
417 m3dctx->im_texcoord[1] = y;
418 }