rayzor

view src/min3d.c @ 3:9035507275d6

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Apr 2014 20:53:44 +0300
parents c273c6f799a4
children 5fcf72837b69
line source
1 #include <stdlib.h>
2 #include "min3d.h"
3 #include "m3dimpl.h"
5 #ifndef M_PI
6 #define M_PI 3.141592653
7 #endif
9 struct min3d_context *m3dctx;
11 int m3d_init(void)
12 {
13 if(!(m3dctx = malloc(sizeof *m3dctx))) {
14 return -1;
15 }
16 memset(m3dctx, 0, sizeof *m3dctx);
18 m3d_matrix_mode(M3D_PROJECTION);
19 m3d_load_identity();
20 m3d_matrix_mode(M3D_MODELVIEW);
21 m3d_load_identity();
22 return 0;
23 }
25 void m3d_shutdown(void)
26 {
27 free(m3dctx);
28 }
30 void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf)
31 {
32 m3dctx->cbuf = cbuf;
33 m3dctx->zbuf = zbuf;
34 }
36 void m3d_clear(unsigned int bmask)
37 {
38 int num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz;
39 if(bmask & M3D_COLOR_BUFFER_BIT) {
40 memset(m3dctx->cbuf->pixels, 0, num_pixels * 3);
41 }
42 if(bmask & M3D_DEPTH_BUFFER_BIT) {
43 memset(m3dctx->zbuf, 0xff, num_pixels * sizeof *m3dctx->zbuf);
44 }
45 }
48 void m3d_enable(int bit)
49 {
50 m3dctx->state |= (1 << bit);
51 }
53 void m3d_disable(int bit)
54 {
55 m3dctx->state &= ~(1 << bit);
56 }
59 /* matrix stack */
60 void m3d_matrix_mode(int mode)
61 {
62 m3dctx->mmode = mode;
63 }
65 void m3d_load_identity(void)
66 {
67 static const float mid[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
68 m3d_load_matrix(mid);
69 }
71 void m3d_load_matrix(const float *m)
72 {
73 int top = m3dctx->mstack[m3dctx->mmode].top;
74 memcpy(m3dctx->mstack[m3dctx->mmode].m[top], m, 16 * sizeof *m);
75 }
77 #define M(i,j) (((i) << 2) + (j))
78 void m3d_mult_matrix(const float *m2)
79 {
80 int i, j, top = m3dctx->mstack[m3dctx->mmode].top;
81 float m1[16];
82 float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
84 memcpy(m1, dest, sizeof m1);
86 for(i=0; i<4; i++) {
87 for(j=0; j<4; j++) {
88 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
89 m1[M(1,j)] * m2[M(i,1)] +
90 m1[M(2,j)] * m2[M(i,2)] +
91 m1[M(3,j)] * m2[M(i,3)];
92 }
93 }
94 }
96 void m3d_translate(float x, float y, float z)
97 {
98 float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
99 m[12] = x;
100 m[13] = y;
101 m[14] = z;
102 m3d_mult_matrix(m);
103 }
105 void m3d_rotate(float deg, float x, float y, float z)
106 {
107 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
109 float angle = M_PI * deg / 180.0f;
110 float sina = sin(angle);
111 float cosa = cos(angle);
112 float one_minus_cosa = 1.0f - cosa;
113 float nxsq = x * x;
114 float nysq = y * y;
115 float nzsq = z * z;
117 xform[0] = nxsq + (1.0f - nxsq) * cosa;
118 xform[4] = x * y * one_minus_cosa - z * sina;
119 xform[8] = x * z * one_minus_cosa + y * sina;
120 xform[1] = x * y * one_minus_cosa + z * sina;
121 xform[5] = nysq + (1.0 - nysq) * cosa;
122 xform[9] = y * z * one_minus_cosa - x * sina;
123 xform[2] = x * z * one_minus_cosa - y * sina;
124 xform[6] = y * z * one_minus_cosa + x * sina;
125 xform[10] = nzsq + (1.0 - nzsq) * cosa;
127 m3d_mult_matrix(xform);
128 }
130 void m3d_scale(float x, float y, float z)
131 {
132 static float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
133 m[0] = x;
134 m[5] = y;
135 m[10] = z;
136 m3d_mult_matrix(m);
137 }
139 void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
140 {
141 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
143 float dx = right - left;
144 float dy = top - bottom;
145 float dz = fr - nr;
147 float a = (right + left) / dx;
148 float b = (top + bottom) / dy;
149 float c = -(fr + nr) / dz;
150 float d = -2.0 * fr * nr / dz;
152 xform[0] = 2.0 * nr / dx;
153 xform[5] = 2.0 * nr / dy;
154 xform[8] = a;
155 xform[9] = b;
156 xform[10] = c;
157 xform[11] = -1.0f;
158 xform[14] = d;
160 m3d_mult_matrix(xform);
161 }
163 void m3d_perspective(float vfov, float aspect, float nr, float fr)
164 {
165 float vfov_rad = M_PI * vfov / 180.0;
166 float x = nr * tan(vfov_rad / 2.0);
167 m3d_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
168 }
170 static void xform4(float *mat, float *vec)
171 {
172 float x = mat[0] * vec[0] + mat[1] * vec[1] + mat[2] * vec[2] + mat[3];
173 float y = mat[4] * vec[0] + mat[5] * vec[1] + mat[6] * vec[2] + mat[7];
174 float z = mat[8] * vec[0] + mat[9] * vec[1] + mat[10] * vec[2] + mat[11];
175 float w = mat[12] * vec[0] + mat[13] * vec[1] + mat[14] * vec[2] + mat[15];
177 vec[0] = x;
178 vec[1] = y;
179 vec[2] = z;
180 vec[3] = w;
181 }
183 static int proc_prim(int prim, struct min3d_vertex *res, struct min3d_vertex *v)
184 {
185 int i;
186 int vcount = prim;
187 int mvtop, ptop;
188 float *mvmat, *pmat;
190 mvtop = m3dctx->mstack[M3D_MODELVIEW].top;
191 mvmat = m3dctx->mstack[M3D_MODELVIEW].m[mvtop];
192 ptop = m3dctx->mstack[M3D_PROJECTION].top;
193 pmat = m3dctx->mstack[M3D_PROJECTION].m[ptop];
195 /* transform to view space */
196 for(i=0; i<vcount; i++) {
197 res[i] = v[i];
198 xform4(mvmat, res[i].pos);
199 /* TODO: normal */
200 }
202 /* TODO: lighting */
204 /* project */
205 for(i=0; i<vcount; i++) {
206 xform4(pmat, res[i].pos);
207 }
209 /* clip */
210 switch(prim) {
211 case M3D_POINTS:
212 {
213 float w = res[0].pos[3];
214 if(res[0].pos[2] < -w || res[0].pos[2] >= w ||
215 res[0].pos[0] / w < -1 || res[0].pos[0] / w >= 1 ||
216 res[0].pos[1] / w < -1 || res[0].pos[1] / w >= 1) {
217 vcount = 0;
218 }
219 }
220 break;
222 default:
223 break; /* TODO */
224 }
226 /* perspective division */
227 for(i=0; i<vcount; i++) {
228 res[i].pos[0] = res[i].pos[3];
229 res[i].pos[1] = res[i].pos[3];
230 res[i].pos[2] = res[i].pos[3];
231 }
232 return vcount;
233 }
235 /* drawing */
236 void m3d_draw(int prim, const float *varr, int vcount)
237 {
238 int i;
239 struct min3d_vertex v[4];
240 struct min3d_vertex resv[16];
242 for(i=0; i<vcount; i++) {
243 int idx = i % prim;
245 v[idx].pos[0] = *varr++;
246 v[idx].pos[1] = *varr++;
247 v[idx].pos[2] = *varr++;
249 if(idx == prim - 1) {
250 int resnum = proc_prim(prim, resv, v);
251 switch(resnum) {
252 case 1:
253 draw_point(resv);
254 break;
256 case '2':
257 draw_line(resv);
258 break;
260 default:
261 draw_poly(resv, resnum);
262 }
263 }
264 }
265 }
267 void m3d_draw_indexed(int prim, const float *varr, const int *idxarr, int icount)
268 {
269 /* TODO */
270 }