rayzor

view src/min3d.c @ 5:5fcf72837b69

fixed the dosemu bit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 06 Apr 2014 02:43:24 +0300
parents 9035507275d6
children a68dbf80d547
line source
1 #include <stdlib.h>
2 #include <string.h>
3 #include <math.h>
4 #include "min3d.h"
5 #include "m3dimpl.h"
7 #ifndef M_PI
8 #define M_PI 3.141592653
9 #endif
11 struct min3d_context *m3dctx;
13 int m3d_init(void)
14 {
15 if(!(m3dctx = malloc(sizeof *m3dctx))) {
16 return -1;
17 }
18 memset(m3dctx, 0, sizeof *m3dctx);
20 m3d_matrix_mode(M3D_PROJECTION);
21 m3d_load_identity();
22 m3d_matrix_mode(M3D_MODELVIEW);
23 m3d_load_identity();
24 return 0;
25 }
27 void m3d_shutdown(void)
28 {
29 free(m3dctx);
30 }
32 void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf)
33 {
34 m3dctx->cbuf = cbuf;
35 m3dctx->zbuf = zbuf;
36 }
38 void m3d_clear(unsigned int bmask)
39 {
40 int num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz;
41 if(bmask & M3D_COLOR_BUFFER_BIT) {
42 memset(m3dctx->cbuf->pixels, 0, num_pixels * 3);
43 }
44 if(bmask & M3D_DEPTH_BUFFER_BIT) {
45 memset(m3dctx->zbuf, 0xff, num_pixels * sizeof *m3dctx->zbuf);
46 }
47 }
50 void m3d_enable(int bit)
51 {
52 m3dctx->state |= (1 << bit);
53 }
55 void m3d_disable(int bit)
56 {
57 m3dctx->state &= ~(1 << bit);
58 }
61 /* matrix stack */
62 void m3d_matrix_mode(int mode)
63 {
64 m3dctx->mmode = mode;
65 }
67 void m3d_load_identity(void)
68 {
69 static const float mid[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
70 m3d_load_matrix(mid);
71 }
73 void m3d_load_matrix(const float *m)
74 {
75 int top = m3dctx->mstack[m3dctx->mmode].top;
76 memcpy(m3dctx->mstack[m3dctx->mmode].m[top], m, 16 * sizeof *m);
77 }
79 #define M(i,j) (((i) << 2) + (j))
80 void m3d_mult_matrix(const float *m2)
81 {
82 int i, j, top = m3dctx->mstack[m3dctx->mmode].top;
83 float m1[16];
84 float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
86 memcpy(m1, dest, sizeof m1);
88 for(i=0; i<4; i++) {
89 for(j=0; j<4; j++) {
90 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
91 m1[M(1,j)] * m2[M(i,1)] +
92 m1[M(2,j)] * m2[M(i,2)] +
93 m1[M(3,j)] * m2[M(i,3)];
94 }
95 }
96 }
98 void m3d_translate(float x, float y, float z)
99 {
100 float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
101 m[12] = x;
102 m[13] = y;
103 m[14] = z;
104 m3d_mult_matrix(m);
105 }
107 void m3d_rotate(float deg, float x, float y, float z)
108 {
109 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
111 float angle = M_PI * deg / 180.0f;
112 float sina = sin(angle);
113 float cosa = cos(angle);
114 float one_minus_cosa = 1.0f - cosa;
115 float nxsq = x * x;
116 float nysq = y * y;
117 float nzsq = z * z;
119 xform[0] = nxsq + (1.0f - nxsq) * cosa;
120 xform[4] = x * y * one_minus_cosa - z * sina;
121 xform[8] = x * z * one_minus_cosa + y * sina;
122 xform[1] = x * y * one_minus_cosa + z * sina;
123 xform[5] = nysq + (1.0 - nysq) * cosa;
124 xform[9] = y * z * one_minus_cosa - x * sina;
125 xform[2] = x * z * one_minus_cosa - y * sina;
126 xform[6] = y * z * one_minus_cosa + x * sina;
127 xform[10] = nzsq + (1.0 - nzsq) * cosa;
129 m3d_mult_matrix(xform);
130 }
132 void m3d_scale(float x, float y, float z)
133 {
134 static float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
135 m[0] = x;
136 m[5] = y;
137 m[10] = z;
138 m3d_mult_matrix(m);
139 }
141 void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
142 {
143 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
145 float dx = right - left;
146 float dy = top - bottom;
147 float dz = fr - nr;
149 float a = (right + left) / dx;
150 float b = (top + bottom) / dy;
151 float c = -(fr + nr) / dz;
152 float d = -2.0 * fr * nr / dz;
154 xform[0] = 2.0 * nr / dx;
155 xform[5] = 2.0 * nr / dy;
156 xform[8] = a;
157 xform[9] = b;
158 xform[10] = c;
159 xform[11] = -1.0f;
160 xform[14] = d;
162 m3d_mult_matrix(xform);
163 }
165 void m3d_perspective(float vfov, float aspect, float nr, float fr)
166 {
167 float vfov_rad = M_PI * vfov / 180.0;
168 float x = nr * tan(vfov_rad / 2.0);
169 m3d_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
170 }
172 static void xform4(float *mat, float *vec)
173 {
174 float x = mat[0] * vec[0] + mat[1] * vec[1] + mat[2] * vec[2] + mat[3];
175 float y = mat[4] * vec[0] + mat[5] * vec[1] + mat[6] * vec[2] + mat[7];
176 float z = mat[8] * vec[0] + mat[9] * vec[1] + mat[10] * vec[2] + mat[11];
177 float w = mat[12] * vec[0] + mat[13] * vec[1] + mat[14] * vec[2] + mat[15];
179 vec[0] = x;
180 vec[1] = y;
181 vec[2] = z;
182 vec[3] = w;
183 }
185 static int proc_prim(int prim, struct min3d_vertex *res, struct min3d_vertex *v)
186 {
187 int i;
188 int vcount = prim;
189 int mvtop, ptop;
190 float *mvmat, *pmat;
192 mvtop = m3dctx->mstack[M3D_MODELVIEW].top;
193 mvmat = m3dctx->mstack[M3D_MODELVIEW].m[mvtop];
194 ptop = m3dctx->mstack[M3D_PROJECTION].top;
195 pmat = m3dctx->mstack[M3D_PROJECTION].m[ptop];
197 /* transform to view space */
198 for(i=0; i<vcount; i++) {
199 res[i] = v[i];
200 xform4(mvmat, res[i].pos);
201 /* TODO: normal */
202 }
204 /* TODO: lighting */
206 /* project */
207 for(i=0; i<vcount; i++) {
208 xform4(pmat, res[i].pos);
209 }
211 /* clip */
212 switch(prim) {
213 case M3D_POINTS:
214 {
215 float w = res[0].pos[3];
216 if(res[0].pos[2] < -w || res[0].pos[2] >= w ||
217 res[0].pos[0] / w < -1 || res[0].pos[0] / w >= 1 ||
218 res[0].pos[1] / w < -1 || res[0].pos[1] / w >= 1) {
219 vcount = 0;
220 }
221 }
222 break;
224 default:
225 break; /* TODO */
226 }
228 /* perspective division */
229 for(i=0; i<vcount; i++) {
230 res[i].pos[0] = res[i].pos[3];
231 res[i].pos[1] = res[i].pos[3];
232 res[i].pos[2] = res[i].pos[3];
233 }
234 return vcount;
235 }
237 /* drawing */
238 void m3d_vertex_array(const float *varr)
239 {
240 m3dctx->vert_array = varr;
241 }
243 void m3d_normal_array(const float *narr)
244 {
245 m3dctx->norm_array = narr;
246 }
248 void m3d_color_array(const float *carr)
249 {
250 m3dctx->col_array = carr;
251 }
253 void m3d_texcoord_array(const float *tcarr)
254 {
255 m3dctx->tc_array = tcarr;
256 }
259 void m3d_draw(int prim, int vcount)
260 {
261 int i;
262 struct min3d_vertex v[4];
263 struct min3d_vertex resv[16];
264 const float *varr = m3dctx->vert_array;
265 const float *carr = m3dctx->col_array;
267 if(!varr) return;
269 for(i=0; i<vcount; i++) {
270 int idx = i % prim;
272 v[idx].pos[0] = *varr++;
273 v[idx].pos[1] = *varr++;
274 v[idx].pos[2] = *varr++;
275 v[idx].color[0] = carr ? *carr++ : m3dctx->cur_color[0];
276 v[idx].color[1] = carr ? *carr++ : m3dctx->cur_color[1];
277 v[idx].color[2] = carr ? *carr++ : m3dctx->cur_color[2];
279 if(idx == prim - 1) {
280 int resnum = proc_prim(prim, resv, v);
281 switch(resnum) {
282 case 1:
283 draw_point(resv);
284 break;
286 case '2':
287 draw_line(resv);
288 break;
290 default:
291 draw_poly(resv, resnum);
292 }
293 }
294 }
295 }
297 void m3d_draw_indexed(int prim, const int *idxarr, int icount)
298 {
299 /* TODO */
300 }