rev |
line source |
nuclear@1
|
1 #include <stdlib.h>
|
nuclear@5
|
2 #include <string.h>
|
nuclear@5
|
3 #include <math.h>
|
nuclear@1
|
4 #include "min3d.h"
|
nuclear@1
|
5 #include "m3dimpl.h"
|
nuclear@6
|
6 #include "logger.h"
|
nuclear@1
|
7
|
nuclear@1
|
8 #ifndef M_PI
|
nuclear@1
|
9 #define M_PI 3.141592653
|
nuclear@1
|
10 #endif
|
nuclear@1
|
11
|
nuclear@3
|
12 struct min3d_context *m3dctx;
|
nuclear@3
|
13
|
nuclear@1
|
14 int m3d_init(void)
|
nuclear@1
|
15 {
|
nuclear@1
|
16 if(!(m3dctx = malloc(sizeof *m3dctx))) {
|
nuclear@1
|
17 return -1;
|
nuclear@1
|
18 }
|
nuclear@1
|
19 memset(m3dctx, 0, sizeof *m3dctx);
|
nuclear@1
|
20
|
nuclear@1
|
21 m3d_matrix_mode(M3D_PROJECTION);
|
nuclear@1
|
22 m3d_load_identity();
|
nuclear@1
|
23 m3d_matrix_mode(M3D_MODELVIEW);
|
nuclear@1
|
24 m3d_load_identity();
|
nuclear@6
|
25
|
nuclear@6
|
26 m3d_color(1, 1, 1);
|
nuclear@1
|
27 return 0;
|
nuclear@1
|
28 }
|
nuclear@1
|
29
|
nuclear@1
|
30 void m3d_shutdown(void)
|
nuclear@1
|
31 {
|
nuclear@1
|
32 free(m3dctx);
|
nuclear@1
|
33 }
|
nuclear@1
|
34
|
nuclear@1
|
35 void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf)
|
nuclear@1
|
36 {
|
nuclear@1
|
37 m3dctx->cbuf = cbuf;
|
nuclear@1
|
38 m3dctx->zbuf = zbuf;
|
nuclear@6
|
39
|
nuclear@9
|
40 m3d_viewport(0, 0, cbuf->xsz, cbuf->ysz);
|
nuclear@6
|
41 }
|
nuclear@6
|
42
|
nuclear@6
|
43 void m3d_clear_color(float r, float g, float b)
|
nuclear@6
|
44 {
|
nuclear@6
|
45 m3dctx->clear_color[0] = (int)((r > 1.0 ? 1.0 : r) * 255.0);
|
nuclear@6
|
46 m3dctx->clear_color[1] = (int)((g > 1.0 ? 1.0 : g) * 255.0);
|
nuclear@6
|
47 m3dctx->clear_color[2] = (int)((b > 1.0 ? 1.0 : b) * 255.0);
|
nuclear@1
|
48 }
|
nuclear@1
|
49
|
nuclear@1
|
50 void m3d_clear(unsigned int bmask)
|
nuclear@1
|
51 {
|
nuclear@9
|
52 int num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz;
|
nuclear@1
|
53 if(bmask & M3D_COLOR_BUFFER_BIT) {
|
nuclear@9
|
54 memset(m3dctx->cbuf->pixels, 0, num_pixels * 4);
|
nuclear@9
|
55 /*
|
nuclear@9
|
56 int i;
|
nuclear@6
|
57 unsigned char *ptr = m3dctx->cbuf->pixels;
|
nuclear@6
|
58 unsigned char r = m3dctx->clear_color[0];
|
nuclear@6
|
59 unsigned char g = m3dctx->clear_color[1];
|
nuclear@6
|
60 unsigned char b = m3dctx->clear_color[2];
|
nuclear@6
|
61 for(i=0; i<num_pixels; i++) {
|
nuclear@6
|
62 *ptr++ = r;
|
nuclear@6
|
63 *ptr++ = g;
|
nuclear@6
|
64 *ptr++ = b;
|
nuclear@9
|
65 }*/
|
nuclear@1
|
66 }
|
nuclear@1
|
67 if(bmask & M3D_DEPTH_BUFFER_BIT) {
|
nuclear@1
|
68 memset(m3dctx->zbuf, 0xff, num_pixels * sizeof *m3dctx->zbuf);
|
nuclear@1
|
69 }
|
nuclear@1
|
70 }
|
nuclear@1
|
71
|
nuclear@1
|
72
|
nuclear@1
|
73 void m3d_enable(int bit)
|
nuclear@1
|
74 {
|
nuclear@1
|
75 m3dctx->state |= (1 << bit);
|
nuclear@1
|
76 }
|
nuclear@1
|
77
|
nuclear@1
|
78 void m3d_disable(int bit)
|
nuclear@1
|
79 {
|
nuclear@1
|
80 m3dctx->state &= ~(1 << bit);
|
nuclear@1
|
81 }
|
nuclear@1
|
82
|
nuclear@9
|
83 void m3d_viewport(int x, int y, int xsz, int ysz)
|
nuclear@9
|
84 {
|
nuclear@9
|
85 m3dctx->vport[0] = x;
|
nuclear@9
|
86 m3dctx->vport[1] = y;
|
nuclear@9
|
87 m3dctx->vport[2] = xsz;
|
nuclear@9
|
88 m3dctx->vport[3] = ysz;
|
nuclear@9
|
89 }
|
nuclear@9
|
90
|
nuclear@1
|
91
|
nuclear@1
|
92 /* matrix stack */
|
nuclear@1
|
93 void m3d_matrix_mode(int mode)
|
nuclear@1
|
94 {
|
nuclear@1
|
95 m3dctx->mmode = mode;
|
nuclear@1
|
96 }
|
nuclear@1
|
97
|
nuclear@6
|
98 void m3d_push_matrix(void)
|
nuclear@6
|
99 {
|
nuclear@6
|
100 int mm = m3dctx->mmode;
|
nuclear@6
|
101 int top = m3dctx->mstack[mm].top;
|
nuclear@6
|
102 if(top < MSTACK_SIZE) {
|
nuclear@6
|
103 float *cur = m3dctx->mstack[mm].m[top++];
|
nuclear@6
|
104 memcpy(m3dctx->mstack[mm].m[top], cur, 16 * sizeof *cur);
|
nuclear@6
|
105 m3dctx->mstack[mm].top = top;
|
nuclear@6
|
106 }
|
nuclear@6
|
107 }
|
nuclear@6
|
108
|
nuclear@6
|
109 void m3d_pop_matrix(void)
|
nuclear@6
|
110 {
|
nuclear@6
|
111 int mm = m3dctx->mmode;
|
nuclear@6
|
112 if(m3dctx->mstack[mm].top > 0) {
|
nuclear@6
|
113 --m3dctx->mstack[mm].top;
|
nuclear@6
|
114 }
|
nuclear@6
|
115 }
|
nuclear@6
|
116
|
nuclear@1
|
117 void m3d_load_identity(void)
|
nuclear@1
|
118 {
|
nuclear@1
|
119 static const float mid[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@1
|
120 m3d_load_matrix(mid);
|
nuclear@1
|
121 }
|
nuclear@1
|
122
|
nuclear@1
|
123 void m3d_load_matrix(const float *m)
|
nuclear@1
|
124 {
|
nuclear@1
|
125 int top = m3dctx->mstack[m3dctx->mmode].top;
|
nuclear@1
|
126 memcpy(m3dctx->mstack[m3dctx->mmode].m[top], m, 16 * sizeof *m);
|
nuclear@1
|
127 }
|
nuclear@1
|
128
|
nuclear@17
|
129 void m3d_load_transpose_matrix(const float *m)
|
nuclear@17
|
130 {
|
nuclear@18
|
131 int i, j;
|
nuclear@17
|
132 int top = m3dctx->mstack[m3dctx->mmode].top;
|
nuclear@17
|
133 float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
|
nuclear@17
|
134
|
nuclear@17
|
135 for(i=0; i<4; i++) {
|
nuclear@17
|
136 for(j=0; j<4; j++) {
|
nuclear@17
|
137 *dest++ = m[j * 4 + i];
|
nuclear@17
|
138 }
|
nuclear@17
|
139 }
|
nuclear@17
|
140 }
|
nuclear@17
|
141
|
nuclear@1
|
142 #define M(i,j) (((i) << 2) + (j))
|
nuclear@1
|
143 void m3d_mult_matrix(const float *m2)
|
nuclear@1
|
144 {
|
nuclear@1
|
145 int i, j, top = m3dctx->mstack[m3dctx->mmode].top;
|
nuclear@1
|
146 float m1[16];
|
nuclear@1
|
147 float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
|
nuclear@1
|
148
|
nuclear@1
|
149 memcpy(m1, dest, sizeof m1);
|
nuclear@1
|
150
|
nuclear@1
|
151 for(i=0; i<4; i++) {
|
nuclear@1
|
152 for(j=0; j<4; j++) {
|
nuclear@17
|
153 *dest++ = m1[M(0,j)] * m2[M(i,0)] +
|
nuclear@1
|
154 m1[M(1,j)] * m2[M(i,1)] +
|
nuclear@1
|
155 m1[M(2,j)] * m2[M(i,2)] +
|
nuclear@1
|
156 m1[M(3,j)] * m2[M(i,3)];
|
nuclear@1
|
157 }
|
nuclear@1
|
158 }
|
nuclear@1
|
159 }
|
nuclear@1
|
160
|
nuclear@17
|
161 void m3d_mult_transpose_matrix(const float *m2)
|
nuclear@17
|
162 {
|
nuclear@17
|
163 int i, j, top = m3dctx->mstack[m3dctx->mmode].top;
|
nuclear@17
|
164 float m1[16];
|
nuclear@17
|
165 float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
|
nuclear@17
|
166
|
nuclear@17
|
167 memcpy(m1, dest, sizeof m1);
|
nuclear@17
|
168
|
nuclear@17
|
169 for(i=0; i<4; i++) {
|
nuclear@17
|
170 for(j=0; j<4; j++) {
|
nuclear@17
|
171 *dest++ = m1[M(0,j)] * m2[M(0,i)] +
|
nuclear@17
|
172 m1[M(1,j)] * m2[M(1,i)] +
|
nuclear@17
|
173 m1[M(2,j)] * m2[M(2,i)] +
|
nuclear@17
|
174 m1[M(3,j)] * m2[M(3,i)];
|
nuclear@17
|
175 }
|
nuclear@17
|
176 }
|
nuclear@17
|
177 }
|
nuclear@17
|
178
|
nuclear@1
|
179 void m3d_translate(float x, float y, float z)
|
nuclear@1
|
180 {
|
nuclear@1
|
181 float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@1
|
182 m[12] = x;
|
nuclear@1
|
183 m[13] = y;
|
nuclear@1
|
184 m[14] = z;
|
nuclear@1
|
185 m3d_mult_matrix(m);
|
nuclear@1
|
186 }
|
nuclear@1
|
187
|
nuclear@1
|
188 void m3d_rotate(float deg, float x, float y, float z)
|
nuclear@1
|
189 {
|
nuclear@1
|
190 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@1
|
191
|
nuclear@1
|
192 float angle = M_PI * deg / 180.0f;
|
nuclear@1
|
193 float sina = sin(angle);
|
nuclear@1
|
194 float cosa = cos(angle);
|
nuclear@1
|
195 float one_minus_cosa = 1.0f - cosa;
|
nuclear@1
|
196 float nxsq = x * x;
|
nuclear@1
|
197 float nysq = y * y;
|
nuclear@1
|
198 float nzsq = z * z;
|
nuclear@1
|
199
|
nuclear@1
|
200 xform[0] = nxsq + (1.0f - nxsq) * cosa;
|
nuclear@1
|
201 xform[4] = x * y * one_minus_cosa - z * sina;
|
nuclear@1
|
202 xform[8] = x * z * one_minus_cosa + y * sina;
|
nuclear@1
|
203 xform[1] = x * y * one_minus_cosa + z * sina;
|
nuclear@1
|
204 xform[5] = nysq + (1.0 - nysq) * cosa;
|
nuclear@1
|
205 xform[9] = y * z * one_minus_cosa - x * sina;
|
nuclear@1
|
206 xform[2] = x * z * one_minus_cosa - y * sina;
|
nuclear@1
|
207 xform[6] = y * z * one_minus_cosa + x * sina;
|
nuclear@1
|
208 xform[10] = nzsq + (1.0 - nzsq) * cosa;
|
nuclear@1
|
209
|
nuclear@1
|
210 m3d_mult_matrix(xform);
|
nuclear@1
|
211 }
|
nuclear@1
|
212
|
nuclear@1
|
213 void m3d_scale(float x, float y, float z)
|
nuclear@1
|
214 {
|
nuclear@1
|
215 static float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@1
|
216 m[0] = x;
|
nuclear@1
|
217 m[5] = y;
|
nuclear@1
|
218 m[10] = z;
|
nuclear@1
|
219 m3d_mult_matrix(m);
|
nuclear@1
|
220 }
|
nuclear@1
|
221
|
nuclear@1
|
222 void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
|
nuclear@1
|
223 {
|
nuclear@1
|
224 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@1
|
225
|
nuclear@1
|
226 float dx = right - left;
|
nuclear@1
|
227 float dy = top - bottom;
|
nuclear@1
|
228 float dz = fr - nr;
|
nuclear@1
|
229
|
nuclear@1
|
230 float a = (right + left) / dx;
|
nuclear@1
|
231 float b = (top + bottom) / dy;
|
nuclear@1
|
232 float c = -(fr + nr) / dz;
|
nuclear@1
|
233 float d = -2.0 * fr * nr / dz;
|
nuclear@1
|
234
|
nuclear@1
|
235 xform[0] = 2.0 * nr / dx;
|
nuclear@1
|
236 xform[5] = 2.0 * nr / dy;
|
nuclear@1
|
237 xform[8] = a;
|
nuclear@1
|
238 xform[9] = b;
|
nuclear@1
|
239 xform[10] = c;
|
nuclear@1
|
240 xform[11] = -1.0f;
|
nuclear@1
|
241 xform[14] = d;
|
nuclear@1
|
242
|
nuclear@1
|
243 m3d_mult_matrix(xform);
|
nuclear@1
|
244 }
|
nuclear@1
|
245
|
nuclear@1
|
246 void m3d_perspective(float vfov, float aspect, float nr, float fr)
|
nuclear@1
|
247 {
|
nuclear@1
|
248 float vfov_rad = M_PI * vfov / 180.0;
|
nuclear@1
|
249 float x = nr * tan(vfov_rad / 2.0);
|
nuclear@1
|
250 m3d_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
|
nuclear@1
|
251 }
|
nuclear@1
|
252
|
nuclear@2
|
253 static void xform4(float *mat, float *vec)
|
nuclear@2
|
254 {
|
nuclear@6
|
255 float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
|
nuclear@6
|
256 float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
|
nuclear@6
|
257 float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
|
nuclear@6
|
258 float w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
|
nuclear@2
|
259
|
nuclear@2
|
260 vec[0] = x;
|
nuclear@2
|
261 vec[1] = y;
|
nuclear@2
|
262 vec[2] = z;
|
nuclear@2
|
263 vec[3] = w;
|
nuclear@2
|
264 }
|
nuclear@2
|
265
|
nuclear@3
|
266 static int proc_prim(int prim, struct min3d_vertex *res, struct min3d_vertex *v)
|
nuclear@2
|
267 {
|
nuclear@3
|
268 int i;
|
nuclear@3
|
269 int vcount = prim;
|
nuclear@3
|
270 int mvtop, ptop;
|
nuclear@3
|
271 float *mvmat, *pmat;
|
nuclear@6
|
272 int *vport = m3dctx->vport;
|
nuclear@3
|
273
|
nuclear@3
|
274 mvtop = m3dctx->mstack[M3D_MODELVIEW].top;
|
nuclear@3
|
275 mvmat = m3dctx->mstack[M3D_MODELVIEW].m[mvtop];
|
nuclear@3
|
276 ptop = m3dctx->mstack[M3D_PROJECTION].top;
|
nuclear@3
|
277 pmat = m3dctx->mstack[M3D_PROJECTION].m[ptop];
|
nuclear@3
|
278
|
nuclear@3
|
279 /* transform to view space */
|
nuclear@3
|
280 for(i=0; i<vcount; i++) {
|
nuclear@3
|
281 res[i] = v[i];
|
nuclear@3
|
282 xform4(mvmat, res[i].pos);
|
nuclear@3
|
283 /* TODO: normal */
|
nuclear@3
|
284 }
|
nuclear@3
|
285
|
nuclear@3
|
286 /* TODO: lighting */
|
nuclear@3
|
287
|
nuclear@3
|
288 /* project */
|
nuclear@3
|
289 for(i=0; i<vcount; i++) {
|
nuclear@3
|
290 xform4(pmat, res[i].pos);
|
nuclear@3
|
291 }
|
nuclear@3
|
292
|
nuclear@3
|
293 /* clip */
|
nuclear@3
|
294 switch(prim) {
|
nuclear@3
|
295 case M3D_POINTS:
|
nuclear@3
|
296 {
|
nuclear@3
|
297 float w = res[0].pos[3];
|
nuclear@3
|
298 if(res[0].pos[2] < -w || res[0].pos[2] >= w ||
|
nuclear@3
|
299 res[0].pos[0] / w < -1 || res[0].pos[0] / w >= 1 ||
|
nuclear@3
|
300 res[0].pos[1] / w < -1 || res[0].pos[1] / w >= 1) {
|
nuclear@3
|
301 vcount = 0;
|
nuclear@3
|
302 }
|
nuclear@3
|
303 }
|
nuclear@3
|
304 break;
|
nuclear@3
|
305
|
nuclear@3
|
306 default:
|
nuclear@3
|
307 break; /* TODO */
|
nuclear@3
|
308 }
|
nuclear@3
|
309
|
nuclear@6
|
310 /* perspective division & viewport */
|
nuclear@3
|
311 for(i=0; i<vcount; i++) {
|
nuclear@6
|
312 res[i].pos[0] /= res[i].pos[3];
|
nuclear@6
|
313 res[i].pos[1] /= res[i].pos[3];
|
nuclear@6
|
314 res[i].pos[2] /= res[i].pos[3];
|
nuclear@6
|
315
|
nuclear@6
|
316 res[i].pos[0] = (res[i].pos[0] * 0.5 + 0.5) * vport[2] + vport[0];
|
nuclear@9
|
317 res[i].pos[1] = (-res[i].pos[1] * 0.5 + 0.5) * vport[3] + vport[1];
|
nuclear@3
|
318 }
|
nuclear@3
|
319 return vcount;
|
nuclear@2
|
320 }
|
nuclear@2
|
321
|
nuclear@1
|
322 /* drawing */
|
nuclear@5
|
323 void m3d_vertex_array(const float *varr)
|
nuclear@5
|
324 {
|
nuclear@6
|
325 m3dctx->vert_array = (float*)varr;
|
nuclear@5
|
326 }
|
nuclear@5
|
327
|
nuclear@5
|
328 void m3d_normal_array(const float *narr)
|
nuclear@5
|
329 {
|
nuclear@6
|
330 m3dctx->norm_array = (float*)narr;
|
nuclear@5
|
331 }
|
nuclear@5
|
332
|
nuclear@5
|
333 void m3d_color_array(const float *carr)
|
nuclear@5
|
334 {
|
nuclear@6
|
335 m3dctx->col_array = (float*)carr;
|
nuclear@5
|
336 }
|
nuclear@5
|
337
|
nuclear@5
|
338 void m3d_texcoord_array(const float *tcarr)
|
nuclear@5
|
339 {
|
nuclear@6
|
340 m3dctx->tc_array = (float*)tcarr;
|
nuclear@5
|
341 }
|
nuclear@5
|
342
|
nuclear@5
|
343
|
nuclear@5
|
344 void m3d_draw(int prim, int vcount)
|
nuclear@1
|
345 {
|
nuclear@3
|
346 int i;
|
nuclear@3
|
347 struct min3d_vertex v[4];
|
nuclear@3
|
348 struct min3d_vertex resv[16];
|
nuclear@5
|
349 const float *varr = m3dctx->vert_array;
|
nuclear@5
|
350 const float *carr = m3dctx->col_array;
|
nuclear@5
|
351
|
nuclear@5
|
352 if(!varr) return;
|
nuclear@3
|
353
|
nuclear@3
|
354 for(i=0; i<vcount; i++) {
|
nuclear@9
|
355 int r, g, b;
|
nuclear@3
|
356 int idx = i % prim;
|
nuclear@3
|
357
|
nuclear@3
|
358 v[idx].pos[0] = *varr++;
|
nuclear@3
|
359 v[idx].pos[1] = *varr++;
|
nuclear@3
|
360 v[idx].pos[2] = *varr++;
|
nuclear@6
|
361 v[idx].pos[3] = 1.0;
|
nuclear@9
|
362 r = (carr ? *carr++ : m3dctx->im_color[0]) * 255.0;
|
nuclear@9
|
363 g = (carr ? *carr++ : m3dctx->im_color[1]) * 255.0;
|
nuclear@9
|
364 b = (carr ? *carr++ : m3dctx->im_color[2]) * 255.0;
|
nuclear@9
|
365 v[idx].color = (r << 16) | (g << 8) | b;
|
nuclear@3
|
366
|
nuclear@3
|
367 if(idx == prim - 1) {
|
nuclear@3
|
368 int resnum = proc_prim(prim, resv, v);
|
nuclear@3
|
369 switch(resnum) {
|
nuclear@3
|
370 case 1:
|
nuclear@9
|
371 m3d_draw_point(resv);
|
nuclear@3
|
372 break;
|
nuclear@3
|
373
|
nuclear@9
|
374 case 2:
|
nuclear@9
|
375 m3d_draw_line(resv);
|
nuclear@3
|
376 break;
|
nuclear@3
|
377
|
nuclear@3
|
378 default:
|
nuclear@9
|
379 m3d_draw_poly(resv, resnum);
|
nuclear@3
|
380 }
|
nuclear@3
|
381 }
|
nuclear@3
|
382 }
|
nuclear@1
|
383 }
|
nuclear@1
|
384
|
nuclear@9
|
385 void m3d_draw_indexed(int prim, const unsigned int *idxarr, int icount)
|
nuclear@1
|
386 {
|
nuclear@9
|
387 int i, vcount = prim;
|
nuclear@9
|
388 struct min3d_vertex v[4];
|
nuclear@9
|
389 struct min3d_vertex resv[16];
|
nuclear@9
|
390 const float *varr = m3dctx->vert_array;
|
nuclear@9
|
391 const float *carr = m3dctx->col_array;
|
nuclear@9
|
392
|
nuclear@9
|
393 if(!varr) return;
|
nuclear@9
|
394
|
nuclear@9
|
395 for(i=0; i<icount; i++) {
|
nuclear@9
|
396 int r, g, b;
|
nuclear@9
|
397 int vnum = i % vcount;
|
nuclear@9
|
398 int index = idxarr[i];
|
nuclear@9
|
399
|
nuclear@9
|
400 v[vnum].pos[0] = varr[index * 3];
|
nuclear@9
|
401 v[vnum].pos[1] = varr[index * 3 + 1];
|
nuclear@9
|
402 v[vnum].pos[2] = varr[index * 3 + 2];
|
nuclear@9
|
403 v[vnum].pos[3] = 1.0;
|
nuclear@9
|
404 r = (carr ? carr[index * 3] : m3dctx->im_color[0]) * 255.0;
|
nuclear@9
|
405 g = (carr ? carr[index * 3 + 1] : m3dctx->im_color[1]) * 255.0;
|
nuclear@9
|
406 b = (carr ? carr[index * 3 + 2] : m3dctx->im_color[2]) * 255.0;
|
nuclear@9
|
407 v[vnum].color = (r << 16) | (g << 8) | b;
|
nuclear@9
|
408
|
nuclear@9
|
409 if(vnum == vcount - 1) {
|
nuclear@9
|
410 int resnum = proc_prim(prim, resv, v);
|
nuclear@9
|
411 switch(resnum) {
|
nuclear@9
|
412 case 1:
|
nuclear@9
|
413 m3d_draw_point(resv);
|
nuclear@9
|
414 break;
|
nuclear@9
|
415
|
nuclear@9
|
416 case 2:
|
nuclear@9
|
417 m3d_draw_line(resv);
|
nuclear@9
|
418 break;
|
nuclear@9
|
419
|
nuclear@9
|
420 default:
|
nuclear@9
|
421 m3d_draw_poly(resv, resnum);
|
nuclear@9
|
422 }
|
nuclear@9
|
423 }
|
nuclear@9
|
424 }
|
nuclear@1
|
425 }
|
nuclear@1
|
426
|
nuclear@6
|
427 void m3d_begin(int prim)
|
nuclear@6
|
428 {
|
nuclear@6
|
429 m3dctx->im_prim = prim;
|
nuclear@6
|
430 m3dctx->im_idx = 0;
|
nuclear@6
|
431
|
nuclear@6
|
432 m3dctx->vert_array = m3dctx->im_varr;
|
nuclear@6
|
433 m3dctx->norm_array = 0;
|
nuclear@6
|
434 m3dctx->col_array = 0;
|
nuclear@6
|
435 m3dctx->tc_array = 0;
|
nuclear@6
|
436 }
|
nuclear@6
|
437
|
nuclear@6
|
438 void m3d_end(void)
|
nuclear@6
|
439 {
|
nuclear@6
|
440 }
|
nuclear@6
|
441
|
nuclear@6
|
442 void m3d_vertex(float x, float y, float z)
|
nuclear@6
|
443 {
|
nuclear@6
|
444 int nverts = m3dctx->im_prim;
|
nuclear@6
|
445 int idx = m3dctx->im_idx;
|
nuclear@6
|
446 float *v = m3dctx->vert_array + idx * 3;
|
nuclear@6
|
447
|
nuclear@6
|
448 v[0] = x;
|
nuclear@6
|
449 v[1] = y;
|
nuclear@6
|
450 v[2] = z;
|
nuclear@6
|
451
|
nuclear@6
|
452 if(m3dctx->norm_array) {
|
nuclear@6
|
453 float *ptr = m3dctx->im_narr + idx * 3;
|
nuclear@6
|
454 ptr[0] = m3dctx->im_normal[0];
|
nuclear@6
|
455 ptr[1] = m3dctx->im_normal[1];
|
nuclear@6
|
456 ptr[2] = m3dctx->im_normal[2];
|
nuclear@6
|
457 }
|
nuclear@6
|
458 if(m3dctx->col_array) {
|
nuclear@6
|
459 float *ptr = m3dctx->im_carr + idx * 3;
|
nuclear@6
|
460 ptr[0] = m3dctx->im_color[0];
|
nuclear@6
|
461 ptr[1] = m3dctx->im_color[1];
|
nuclear@6
|
462 ptr[2] = m3dctx->im_color[2];
|
nuclear@6
|
463 }
|
nuclear@6
|
464 if(m3dctx->tc_array) {
|
nuclear@6
|
465 float *ptr = m3dctx->im_texcoord + idx * 2;
|
nuclear@6
|
466 ptr[0] = m3dctx->im_texcoord[0];
|
nuclear@6
|
467 ptr[1] = m3dctx->im_texcoord[1];
|
nuclear@6
|
468 }
|
nuclear@6
|
469
|
nuclear@6
|
470 if(++idx == nverts) {
|
nuclear@6
|
471 m3d_draw(m3dctx->im_prim, nverts);
|
nuclear@6
|
472 idx = 0;
|
nuclear@6
|
473 }
|
nuclear@6
|
474
|
nuclear@6
|
475 m3dctx->im_idx = idx;
|
nuclear@6
|
476 }
|
nuclear@6
|
477
|
nuclear@6
|
478 void m3d_normal(float x, float y, float z)
|
nuclear@6
|
479 {
|
nuclear@6
|
480 m3dctx->im_normal[0] = x;
|
nuclear@6
|
481 m3dctx->im_normal[1] = y;
|
nuclear@6
|
482 m3dctx->im_normal[2] = z;
|
nuclear@6
|
483 }
|
nuclear@6
|
484
|
nuclear@6
|
485 void m3d_color(float x, float y, float z)
|
nuclear@6
|
486 {
|
nuclear@6
|
487 m3dctx->im_color[0] = x;
|
nuclear@6
|
488 m3dctx->im_color[1] = y;
|
nuclear@6
|
489 m3dctx->im_color[2] = z;
|
nuclear@6
|
490 }
|
nuclear@6
|
491
|
nuclear@6
|
492 void m3d_texcoord(float x, float y)
|
nuclear@6
|
493 {
|
nuclear@6
|
494 m3dctx->im_texcoord[0] = x;
|
nuclear@6
|
495 m3dctx->im_texcoord[1] = y;
|
nuclear@6
|
496 }
|