rev |
line source |
nuclear@9
|
1 #include "config.h"
|
nuclear@14
|
2 #include <stdio.h>
|
nuclear@7
|
3 #include <string.h>
|
nuclear@12
|
4 #include <math.h>
|
nuclear@7
|
5 #include "x3d.h"
|
nuclear@7
|
6 #include "fixed.h"
|
nuclear@7
|
7 #include "sincos.h"
|
nuclear@8
|
8 #include "logger.h"
|
nuclear@8
|
9 #include "polyfill.h"
|
nuclear@8
|
10 #include "gbasys.h"
|
nuclear@7
|
11
|
nuclear@14
|
12 int dbg_fill_dump;
|
nuclear@14
|
13
|
nuclear@7
|
14 #define MAT_STACK_SIZE 4
|
nuclear@7
|
15
|
nuclear@7
|
16 struct matrix {
|
nuclear@8
|
17 int32_t m[12];
|
nuclear@7
|
18 };
|
nuclear@7
|
19
|
nuclear@15
|
20 static void proc_vertex(const int32_t *vin, const int32_t *cin, const int32_t *tin,
|
nuclear@15
|
21 pvec3 *vout, pvec3 *cout, pvec2 *tout);
|
nuclear@14
|
22 static int dump_frame(struct pixel_buffer *frame);
|
nuclear@8
|
23
|
nuclear@8
|
24
|
nuclear@7
|
25 static int32_t proj_fov = M_PI_X16;
|
nuclear@7
|
26 static int32_t proj_aspect = 65536;
|
nuclear@13
|
27 static int32_t inv_proj_aspect = 65536;
|
nuclear@7
|
28 static int32_t proj_near = ftox16(0.5);
|
nuclear@7
|
29 static int32_t proj_far = 500 << 16;
|
nuclear@13
|
30 static int32_t inv_tan_half_xfov, inv_tan_half_yfov;
|
nuclear@7
|
31
|
nuclear@8
|
32 #define ID_INIT {65536, 0, 0, 0, 0, 65536, 0, 0, 0, 0, 65536, 0}
|
nuclear@8
|
33
|
nuclear@8
|
34 static struct matrix identity = { ID_INIT };
|
nuclear@7
|
35
|
nuclear@7
|
36 static short mtop;
|
nuclear@8
|
37 static struct matrix mstack[MAT_STACK_SIZE] = { {ID_INIT}, {ID_INIT} };
|
nuclear@8
|
38
|
nuclear@8
|
39 static const int32_t *vertex_array;
|
nuclear@8
|
40 static unsigned short vertex_count;
|
nuclear@8
|
41 static const int32_t *color_array;
|
nuclear@8
|
42 static unsigned short color_count;
|
nuclear@15
|
43 static const int32_t *texcoord_array;
|
nuclear@15
|
44 static unsigned short texcoord_count;
|
nuclear@8
|
45
|
nuclear@8
|
46 static int32_t im_color[3];
|
nuclear@15
|
47 static int32_t im_texcoord[2];
|
nuclear@9
|
48 static uint8_t im_color_index;
|
nuclear@7
|
49
|
nuclear@12
|
50 void x3d_projection(int fov, int32_t aspect, int32_t nearz, int32_t farz)
|
nuclear@7
|
51 {
|
nuclear@12
|
52 proj_fov = (M_PI_X16 * fov) / 180;
|
nuclear@7
|
53 proj_aspect = aspect;
|
nuclear@13
|
54 inv_proj_aspect = x16div(65536, proj_aspect);
|
nuclear@7
|
55 proj_near = nearz;
|
nuclear@7
|
56 proj_far = farz;
|
nuclear@12
|
57
|
nuclear@13
|
58 inv_tan_half_yfov = (int32_t)(65536.0 / tan(0.5 * proj_fov / 65536.0));
|
nuclear@13
|
59 inv_tan_half_xfov = x16mul(inv_tan_half_yfov, aspect);
|
nuclear@7
|
60 }
|
nuclear@7
|
61
|
nuclear@7
|
62 int x3d_push_matrix(void)
|
nuclear@7
|
63 {
|
nuclear@7
|
64 short newtop = mtop + 1;
|
nuclear@7
|
65 if(newtop >= MAT_STACK_SIZE) {
|
nuclear@7
|
66 return -1;
|
nuclear@7
|
67 }
|
nuclear@7
|
68 memcpy(mstack + newtop, mstack + mtop, sizeof *mstack);
|
nuclear@7
|
69 mtop = newtop;
|
nuclear@7
|
70 return 0;
|
nuclear@7
|
71 }
|
nuclear@7
|
72
|
nuclear@7
|
73 int x3d_pop_matrix(void)
|
nuclear@7
|
74 {
|
nuclear@7
|
75 if(mtop <= 0) {
|
nuclear@7
|
76 return -1;
|
nuclear@7
|
77 }
|
nuclear@7
|
78 --mtop;
|
nuclear@7
|
79 return 0;
|
nuclear@7
|
80 }
|
nuclear@7
|
81
|
nuclear@7
|
82 void x3d_load_matrix(int32_t *m)
|
nuclear@7
|
83 {
|
nuclear@8
|
84 memcpy(mstack[mtop].m, m, sizeof *mstack);
|
nuclear@7
|
85 }
|
nuclear@7
|
86
|
nuclear@7
|
87
|
nuclear@7
|
88 #define M(i,j) (((i) << 2) + (j))
|
nuclear@7
|
89 void x3d_mult_matrix(int32_t *m)
|
nuclear@7
|
90 {
|
nuclear@7
|
91 int i, j;
|
nuclear@7
|
92 struct matrix tmp;
|
nuclear@7
|
93
|
nuclear@8
|
94 memcpy(tmp.m, mstack[mtop].m, sizeof tmp);
|
nuclear@7
|
95
|
nuclear@7
|
96 for(i=0; i<3; i++) {
|
nuclear@7
|
97 for(j=0; j<4; j++) {
|
nuclear@8
|
98 mstack[mtop].m[M(i, j)] =
|
nuclear@14
|
99 x16mul(m[M(0, j)], tmp.m[M(i, 0)]) +
|
nuclear@14
|
100 x16mul(m[M(1, j)], tmp.m[M(i, 1)]) +
|
nuclear@14
|
101 x16mul(m[M(2, j)], tmp.m[M(i, 2)]);
|
nuclear@7
|
102 }
|
nuclear@14
|
103 mstack[mtop].m[M(i, 3)] += tmp.m[M(i, 3)];
|
nuclear@7
|
104 }
|
nuclear@7
|
105 }
|
nuclear@7
|
106
|
nuclear@7
|
107 void x3d_load_identity(void)
|
nuclear@7
|
108 {
|
nuclear@8
|
109 memcpy(mstack[mtop].m, identity.m, sizeof identity);
|
nuclear@7
|
110 }
|
nuclear@7
|
111
|
nuclear@8
|
112 void x3d_translate(int32_t x, int32_t y, int32_t z)
|
nuclear@8
|
113 {
|
nuclear@8
|
114 int32_t m[] = ID_INIT;
|
nuclear@8
|
115 m[3] = x;
|
nuclear@8
|
116 m[7] = y;
|
nuclear@8
|
117 m[11] = z;
|
nuclear@8
|
118
|
nuclear@8
|
119 x3d_mult_matrix(m);
|
nuclear@8
|
120 }
|
nuclear@8
|
121
|
nuclear@8
|
122 void x3d_rotate(int32_t deg, int32_t x, int32_t y, int32_t z)
|
nuclear@8
|
123 {
|
nuclear@8
|
124 int32_t xform[] = ID_INIT;
|
nuclear@8
|
125
|
nuclear@8
|
126 int32_t angle = x16mul(M_PI_X16, deg) / 180;
|
nuclear@8
|
127 int32_t sina = sin_x16(angle);
|
nuclear@8
|
128 int32_t cosa = cos_x16(angle);
|
nuclear@8
|
129 int32_t one_minus_cosa = 65536 - cosa;
|
nuclear@8
|
130 int32_t nxsq = x16sq(x);
|
nuclear@8
|
131 int32_t nysq = x16sq(y);
|
nuclear@8
|
132 int32_t nzsq = x16sq(z);
|
nuclear@8
|
133
|
nuclear@8
|
134 xform[0] = nxsq + x16mul(65536 - nxsq, cosa);
|
nuclear@8
|
135 xform[4] = x16mul(x16mul(x, y), one_minus_cosa) - x16mul(z, sina);
|
nuclear@8
|
136 xform[8] = x16mul(x16mul(x, z), one_minus_cosa) + x16mul(y, sina);
|
nuclear@8
|
137 xform[1] = x16mul(x16mul(x, y), one_minus_cosa) + x16mul(z, sina);
|
nuclear@8
|
138 xform[5] = nysq + x16mul(65536 - nysq, cosa);
|
nuclear@8
|
139 xform[9] = x16mul(x16mul(y, z), one_minus_cosa) - x16mul(x, sina);
|
nuclear@8
|
140 xform[2] = x16mul(x16mul(x, z), one_minus_cosa) - x16mul(y, sina);
|
nuclear@8
|
141 xform[6] = x16mul(x16mul(y, z), one_minus_cosa) + x16mul(x, sina);
|
nuclear@8
|
142 xform[10] = nzsq + x16mul(65536 - nzsq, cosa);
|
nuclear@8
|
143
|
nuclear@8
|
144 x3d_mult_matrix(xform);
|
nuclear@8
|
145 }
|
nuclear@8
|
146
|
nuclear@8
|
147 void x3d_scale(int32_t x, int32_t y, int32_t z)
|
nuclear@8
|
148 {
|
nuclear@8
|
149 int32_t m[] = ID_INIT;
|
nuclear@8
|
150
|
nuclear@8
|
151 m[0] = x;
|
nuclear@8
|
152 m[5] = y;
|
nuclear@8
|
153 m[10] = z;
|
nuclear@8
|
154
|
nuclear@8
|
155 x3d_mult_matrix(m);
|
nuclear@8
|
156 }
|
nuclear@8
|
157
|
nuclear@8
|
158 void x3d_vertex_array(int count, const int32_t *ptr)
|
nuclear@8
|
159 {
|
nuclear@8
|
160 vertex_array = ptr;
|
nuclear@8
|
161 vertex_count = count;
|
nuclear@8
|
162 }
|
nuclear@8
|
163
|
nuclear@8
|
164 void x3d_color_array(int count, const int32_t *ptr)
|
nuclear@8
|
165 {
|
nuclear@8
|
166 color_array = ptr;
|
nuclear@8
|
167 color_count = count;
|
nuclear@8
|
168 }
|
nuclear@8
|
169
|
nuclear@15
|
170 void x3d_texcoord_array(int count, const int32_t *ptr)
|
nuclear@15
|
171 {
|
nuclear@15
|
172 texcoord_array = ptr;
|
nuclear@15
|
173 texcoord_count = count;
|
nuclear@15
|
174 }
|
nuclear@15
|
175
|
nuclear@12
|
176 int x3d_draw(int prim, int vnum)
|
nuclear@8
|
177 {
|
nuclear@8
|
178 int i, j, pverts = prim;
|
nuclear@8
|
179 const int32_t *vptr = vertex_array;
|
nuclear@8
|
180 const int32_t *cptr = color_array;
|
nuclear@15
|
181 const int32_t *tptr = texcoord_array;
|
nuclear@9
|
182 #ifndef PALMODE
|
nuclear@8
|
183 short cr, cg, cb;
|
nuclear@9
|
184 #endif
|
nuclear@9
|
185 uint16_t color;
|
nuclear@8
|
186
|
nuclear@8
|
187 if(!vertex_array) return -1;
|
nuclear@8
|
188
|
nuclear@8
|
189 if(vnum > vertex_count) {
|
nuclear@8
|
190 logmsg(LOG_DBG, "%s called with vnum=%d, but current vertex array has %d vertices\n",
|
nuclear@8
|
191 __FUNCTION__, vnum, vertex_count);
|
nuclear@8
|
192 vnum = vertex_count;
|
nuclear@8
|
193 }
|
nuclear@8
|
194 if(color_array && vnum > color_count) {
|
nuclear@8
|
195 logmsg(LOG_DBG, "%s called with vnum=%d, but current color array has %d elements\n",
|
nuclear@8
|
196 __FUNCTION__, vnum, color_count);
|
nuclear@8
|
197 vnum = color_count;
|
nuclear@8
|
198 }
|
nuclear@15
|
199 if(texcoord_array && vnum > texcoord_count) {
|
nuclear@15
|
200 logmsg(LOG_DBG, "%s called with vnum=%d, but current texcoord array has %d elements\n",
|
nuclear@15
|
201 __FUNCTION__, vnum, texcoord_count);
|
nuclear@15
|
202 vnum = texcoord_count;
|
nuclear@15
|
203 }
|
nuclear@8
|
204
|
nuclear@8
|
205 for(i=0; i<vnum; i+=pverts) {
|
nuclear@8
|
206 /* process vertices */
|
nuclear@8
|
207 pvec3 vpos[4];
|
nuclear@8
|
208 pvec3 col[4];
|
nuclear@15
|
209 pvec2 tex[4];
|
nuclear@8
|
210
|
nuclear@8
|
211 for(j=0; j<pverts; j++) {
|
nuclear@15
|
212 proc_vertex(vptr, cptr, tptr, vpos + j, col + j, tex + j);
|
nuclear@12
|
213
|
nuclear@12
|
214 if(vpos[j].z <= proj_near) {
|
nuclear@12
|
215 goto skip_prim;
|
nuclear@12
|
216 }
|
nuclear@12
|
217
|
nuclear@8
|
218 vptr += 3;
|
nuclear@8
|
219 if(cptr) cptr += 3;
|
nuclear@15
|
220 if(tptr) tptr += 2;
|
nuclear@8
|
221 }
|
nuclear@8
|
222
|
nuclear@9
|
223 #ifdef PALMODE
|
nuclear@9
|
224 color = im_color_index;
|
nuclear@9
|
225 #else
|
nuclear@8
|
226 cr = col[0].x >> 8;
|
nuclear@8
|
227 cg = col[0].y >> 8;
|
nuclear@8
|
228 cb = col[0].z >> 8;
|
nuclear@8
|
229
|
nuclear@8
|
230 if(cr > 255) cr = 255;
|
nuclear@8
|
231 if(cg > 255) cg = 255;
|
nuclear@8
|
232 if(cb > 255) cb = 255;
|
nuclear@8
|
233
|
nuclear@9
|
234 color = RGB(cr, cg, cb);
|
nuclear@9
|
235 #endif
|
nuclear@9
|
236
|
nuclear@12
|
237 /* project & viewport */
|
nuclear@12
|
238 for(j=0; j<pverts; j++) {
|
nuclear@12
|
239 int32_t x, y;
|
nuclear@12
|
240
|
nuclear@13
|
241 x = x16mul(vpos[j].x, inv_tan_half_xfov);
|
nuclear@12
|
242 x = x16div(x, vpos[j].z);
|
nuclear@13
|
243 vpos[j].x = (x16mul(x, inv_proj_aspect) + 65536) * (WIDTH / 2);
|
nuclear@12
|
244
|
nuclear@13
|
245 y = x16mul(vpos[j].y, inv_tan_half_yfov);
|
nuclear@12
|
246 y = x16div(y, vpos[j].z);
|
nuclear@12
|
247 vpos[j].y = (65536 - y) * (HEIGHT / 2);
|
nuclear@12
|
248 }
|
nuclear@12
|
249
|
nuclear@8
|
250 switch(pverts) {
|
nuclear@8
|
251 case X3D_POINTS:
|
nuclear@9
|
252 draw_point(vpos, color);
|
nuclear@8
|
253 break;
|
nuclear@8
|
254
|
nuclear@8
|
255 case X3D_LINES:
|
nuclear@8
|
256 break;
|
nuclear@8
|
257
|
nuclear@8
|
258 case X3D_TRIANGLES:
|
nuclear@8
|
259 case X3D_QUADS:
|
nuclear@15
|
260 draw_poly(pverts, vpos, tex, color);
|
nuclear@14
|
261 if(dbg_fill_dump) {
|
nuclear@14
|
262 dump_frame(back_buffer);
|
nuclear@14
|
263 }
|
nuclear@8
|
264 break;
|
nuclear@8
|
265 }
|
nuclear@12
|
266 skip_prim: ;
|
nuclear@8
|
267 }
|
nuclear@14
|
268
|
nuclear@14
|
269 dbg_fill_dump = 0;
|
nuclear@8
|
270 return 0;
|
nuclear@8
|
271 }
|
nuclear@8
|
272
|
nuclear@15
|
273 static void proc_vertex(const int32_t *vin, const int32_t *cin, const int32_t *tin,
|
nuclear@15
|
274 pvec3 *vout, pvec3 *cout, pvec2 *tout)
|
nuclear@8
|
275 {
|
nuclear@8
|
276 int i;
|
nuclear@8
|
277 int32_t tvert[3];
|
nuclear@8
|
278 int32_t *mvmat = mstack[mtop].m;
|
nuclear@8
|
279
|
nuclear@8
|
280 /* transform vertex with current matrix */
|
nuclear@8
|
281 for(i=0; i<3; i++) {
|
nuclear@8
|
282 tvert[i] = x16mul(mvmat[0], vin[0]) +
|
nuclear@8
|
283 x16mul(mvmat[1], vin[1]) +
|
nuclear@8
|
284 x16mul(mvmat[2], vin[2]) +
|
nuclear@8
|
285 mvmat[3];
|
nuclear@8
|
286 mvmat += 4;
|
nuclear@8
|
287 }
|
nuclear@8
|
288
|
nuclear@8
|
289 vout->x = tvert[0];
|
nuclear@8
|
290 vout->y = tvert[1];
|
nuclear@8
|
291 vout->z = tvert[2];
|
nuclear@8
|
292 /*logmsg(LOG_DBG, "%s: (%g %g %g) -> (%g %g %g)\n", __FUNCTION__,
|
nuclear@8
|
293 x16tof(vin[0]), x16tof(vin[1]), x16tof(vin[2]),
|
nuclear@8
|
294 x16tof(vout->x), x16tof(vout->y), x16tof(vout->z));*/
|
nuclear@8
|
295
|
nuclear@8
|
296 if(color_array) {
|
nuclear@8
|
297 cout->x = cin[0];
|
nuclear@8
|
298 cout->y = cin[1];
|
nuclear@8
|
299 cout->z = cin[2];
|
nuclear@8
|
300 } else {
|
nuclear@8
|
301 cout->x = im_color[0];
|
nuclear@8
|
302 cout->y = im_color[1];
|
nuclear@8
|
303 cout->z = im_color[2];
|
nuclear@8
|
304 }
|
nuclear@15
|
305
|
nuclear@15
|
306 if(texcoord_array) {
|
nuclear@15
|
307 tout->x = tin[0];
|
nuclear@15
|
308 tout->y = tin[1];
|
nuclear@15
|
309 } else {
|
nuclear@15
|
310 tout->x = im_texcoord[0];
|
nuclear@15
|
311 tout->y = im_texcoord[1];
|
nuclear@15
|
312 }
|
nuclear@8
|
313 }
|
nuclear@8
|
314
|
nuclear@9
|
315 void x3d_color_index(int cidx)
|
nuclear@9
|
316 {
|
nuclear@9
|
317 im_color_index = cidx;
|
nuclear@9
|
318 }
|
nuclear@9
|
319
|
nuclear@8
|
320 void x3d_color(int32_t r, int32_t g, int32_t b)
|
nuclear@8
|
321 {
|
nuclear@8
|
322 im_color[0] = r;
|
nuclear@8
|
323 im_color[1] = g;
|
nuclear@8
|
324 im_color[2] = b;
|
nuclear@8
|
325 }
|
nuclear@14
|
326
|
nuclear@14
|
327 static int dump_frame(struct pixel_buffer *frame)
|
nuclear@14
|
328 {
|
nuclear@14
|
329 static int frameno;
|
nuclear@14
|
330 char buf[128];
|
nuclear@14
|
331 FILE *fp;
|
nuclear@14
|
332 int i, npix;
|
nuclear@14
|
333 uint16_t *ptr = frame->pixels;
|
nuclear@14
|
334
|
nuclear@14
|
335 sprintf(buf, "dump%03d.ppm", ++frameno);
|
nuclear@14
|
336
|
nuclear@14
|
337 if(!(fp = fopen(buf, "wb"))) {
|
nuclear@14
|
338 fprintf(stderr, "failed to dump file: %s\n", buf);
|
nuclear@14
|
339 return -1;
|
nuclear@14
|
340 }
|
nuclear@14
|
341
|
nuclear@14
|
342 fprintf(fp, "P6\n%d %d\n255\n", frame->x, frame->y);
|
nuclear@14
|
343
|
nuclear@14
|
344 npix = frame->x * frame->y;
|
nuclear@14
|
345 for(i=0; i<npix; i++) {
|
nuclear@14
|
346 uint16_t pixel = *ptr++;
|
nuclear@14
|
347 fputc(GET_R(pixel), fp);
|
nuclear@14
|
348 fputc(GET_G(pixel), fp);
|
nuclear@14
|
349 fputc(GET_B(pixel), fp);
|
nuclear@14
|
350 }
|
nuclear@14
|
351 fclose(fp);
|
nuclear@14
|
352 return 0;
|
nuclear@14
|
353 }
|