rev |
line source |
nuclear@0
|
1 #include <stdlib.h>
|
nuclear@0
|
2 #include "min3d.h"
|
nuclear@0
|
3 #include "m3dimpl.h"
|
nuclear@0
|
4
|
nuclear@0
|
5 #ifndef M_PI
|
nuclear@0
|
6 #define M_PI 3.141592653
|
nuclear@0
|
7 #endif
|
nuclear@0
|
8
|
nuclear@0
|
9 int m3d_init(void)
|
nuclear@0
|
10 {
|
nuclear@0
|
11 if(!(m3dctx = malloc(sizeof *m3dctx))) {
|
nuclear@0
|
12 return -1;
|
nuclear@0
|
13 }
|
nuclear@0
|
14 memset(m3dctx, 0, sizeof *m3dctx);
|
nuclear@0
|
15
|
nuclear@0
|
16 m3d_matrix_mode(M3D_PROJECTION);
|
nuclear@0
|
17 m3d_load_identity();
|
nuclear@0
|
18 m3d_matrix_mode(M3D_MODELVIEW);
|
nuclear@0
|
19 m3d_load_identity();
|
nuclear@0
|
20 return 0;
|
nuclear@0
|
21 }
|
nuclear@0
|
22
|
nuclear@0
|
23 void m3d_shutdown(void)
|
nuclear@0
|
24 {
|
nuclear@0
|
25 free(m3dctx);
|
nuclear@0
|
26 }
|
nuclear@0
|
27
|
nuclear@0
|
28 void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf)
|
nuclear@0
|
29 {
|
nuclear@0
|
30 m3dctx->cbuf = cbuf;
|
nuclear@0
|
31 m3dctx->zbuf = zbuf;
|
nuclear@0
|
32 }
|
nuclear@0
|
33
|
nuclear@0
|
34 void m3d_clear(unsigned int bmask)
|
nuclear@0
|
35 {
|
nuclear@0
|
36 int num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz;
|
nuclear@0
|
37 if(bmask & M3D_COLOR_BUFFER_BIT) {
|
nuclear@0
|
38 memset(m3dctx->cbuf->pixels, 0, num_pixels * 3);
|
nuclear@0
|
39 }
|
nuclear@0
|
40 if(bmask & M3D_DEPTH_BUFFER_BIT) {
|
nuclear@0
|
41 memset(m3dctx->zbuf, 0xff, num_pixels * sizeof *m3dctx->zbuf);
|
nuclear@0
|
42 }
|
nuclear@0
|
43 }
|
nuclear@0
|
44
|
nuclear@0
|
45
|
nuclear@0
|
46 void m3d_enable(int bit)
|
nuclear@0
|
47 {
|
nuclear@0
|
48 m3dctx->state |= (1 << bit);
|
nuclear@0
|
49 }
|
nuclear@0
|
50
|
nuclear@0
|
51 void m3d_disable(int bit)
|
nuclear@0
|
52 {
|
nuclear@0
|
53 m3dctx->state &= ~(1 << bit);
|
nuclear@0
|
54 }
|
nuclear@0
|
55
|
nuclear@0
|
56
|
nuclear@0
|
57 /* matrix stack */
|
nuclear@0
|
58 void m3d_matrix_mode(int mode)
|
nuclear@0
|
59 {
|
nuclear@0
|
60 m3dctx->mmode = mode;
|
nuclear@0
|
61 }
|
nuclear@0
|
62
|
nuclear@0
|
63 void m3d_load_identity(void)
|
nuclear@0
|
64 {
|
nuclear@0
|
65 static const float mid[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
66 m3d_load_matrix(mid);
|
nuclear@0
|
67 }
|
nuclear@0
|
68
|
nuclear@0
|
69 void m3d_load_matrix(const float *m)
|
nuclear@0
|
70 {
|
nuclear@0
|
71 int top = m3dctx->mstack[m3dctx->mmode].top;
|
nuclear@0
|
72 memcpy(m3dctx->mstack[m3dctx->mmode].m[top], m, 16 * sizeof *m);
|
nuclear@0
|
73 }
|
nuclear@0
|
74
|
nuclear@0
|
75 #define M(i,j) (((i) << 2) + (j))
|
nuclear@0
|
76 void m3d_mult_matrix(const float *m2)
|
nuclear@0
|
77 {
|
nuclear@0
|
78 int i, j, top = m3dctx->mstack[m3dctx->mmode].top;
|
nuclear@0
|
79 float m1[16];
|
nuclear@0
|
80 float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
|
nuclear@0
|
81
|
nuclear@0
|
82 memcpy(m1, dest, sizeof m1);
|
nuclear@0
|
83
|
nuclear@0
|
84 for(i=0; i<4; i++) {
|
nuclear@0
|
85 for(j=0; j<4; j++) {
|
nuclear@0
|
86 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
|
nuclear@0
|
87 m1[M(1,j)] * m2[M(i,1)] +
|
nuclear@0
|
88 m1[M(2,j)] * m2[M(i,2)] +
|
nuclear@0
|
89 m1[M(3,j)] * m2[M(i,3)];
|
nuclear@0
|
90 }
|
nuclear@0
|
91 }
|
nuclear@0
|
92 }
|
nuclear@0
|
93
|
nuclear@0
|
94 void m3d_translate(float x, float y, float z)
|
nuclear@0
|
95 {
|
nuclear@0
|
96 float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
97 m[12] = x;
|
nuclear@0
|
98 m[13] = y;
|
nuclear@0
|
99 m[14] = z;
|
nuclear@0
|
100 m3d_mult_matrix(m);
|
nuclear@0
|
101 }
|
nuclear@0
|
102
|
nuclear@0
|
103 void m3d_rotate(float deg, float x, float y, float z)
|
nuclear@0
|
104 {
|
nuclear@0
|
105 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
106
|
nuclear@0
|
107 float angle = M_PI * deg / 180.0f;
|
nuclear@0
|
108 float sina = sin(angle);
|
nuclear@0
|
109 float cosa = cos(angle);
|
nuclear@0
|
110 float one_minus_cosa = 1.0f - cosa;
|
nuclear@0
|
111 float nxsq = x * x;
|
nuclear@0
|
112 float nysq = y * y;
|
nuclear@0
|
113 float nzsq = z * z;
|
nuclear@0
|
114
|
nuclear@0
|
115 xform[0] = nxsq + (1.0f - nxsq) * cosa;
|
nuclear@0
|
116 xform[4] = x * y * one_minus_cosa - z * sina;
|
nuclear@0
|
117 xform[8] = x * z * one_minus_cosa + y * sina;
|
nuclear@0
|
118 xform[1] = x * y * one_minus_cosa + z * sina;
|
nuclear@0
|
119 xform[5] = nysq + (1.0 - nysq) * cosa;
|
nuclear@0
|
120 xform[9] = y * z * one_minus_cosa - x * sina;
|
nuclear@0
|
121 xform[2] = x * z * one_minus_cosa - y * sina;
|
nuclear@0
|
122 xform[6] = y * z * one_minus_cosa + x * sina;
|
nuclear@0
|
123 xform[10] = nzsq + (1.0 - nzsq) * cosa;
|
nuclear@0
|
124
|
nuclear@0
|
125 m3d_mult_matrix(xform);
|
nuclear@0
|
126 }
|
nuclear@0
|
127
|
nuclear@0
|
128 void m3d_scale(float x, float y, float z)
|
nuclear@0
|
129 {
|
nuclear@0
|
130 static float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
131 m[0] = x;
|
nuclear@0
|
132 m[5] = y;
|
nuclear@0
|
133 m[10] = z;
|
nuclear@0
|
134 m3d_mult_matrix(m);
|
nuclear@0
|
135 }
|
nuclear@0
|
136
|
nuclear@0
|
137 void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
|
nuclear@0
|
138 {
|
nuclear@0
|
139 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
|
nuclear@0
|
140
|
nuclear@0
|
141 float dx = right - left;
|
nuclear@0
|
142 float dy = top - bottom;
|
nuclear@0
|
143 float dz = fr - nr;
|
nuclear@0
|
144
|
nuclear@0
|
145 float a = (right + left) / dx;
|
nuclear@0
|
146 float b = (top + bottom) / dy;
|
nuclear@0
|
147 float c = -(fr + nr) / dz;
|
nuclear@0
|
148 float d = -2.0 * fr * nr / dz;
|
nuclear@0
|
149
|
nuclear@0
|
150 xform[0] = 2.0 * nr / dx;
|
nuclear@0
|
151 xform[5] = 2.0 * nr / dy;
|
nuclear@0
|
152 xform[8] = a;
|
nuclear@0
|
153 xform[9] = b;
|
nuclear@0
|
154 xform[10] = c;
|
nuclear@0
|
155 xform[11] = -1.0f;
|
nuclear@0
|
156 xform[14] = d;
|
nuclear@0
|
157
|
nuclear@0
|
158 m3d_mult_matrix(xform);
|
nuclear@0
|
159 }
|
nuclear@0
|
160
|
nuclear@0
|
161 void m3d_perspective(float vfov, float aspect, float nr, float fr)
|
nuclear@0
|
162 {
|
nuclear@0
|
163 float vfov_rad = M_PI * vfov / 180.0;
|
nuclear@0
|
164 float x = nr * tan(vfov_rad / 2.0);
|
nuclear@0
|
165 m3d_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
|
nuclear@0
|
166 }
|
nuclear@0
|
167
|
nuclear@0
|
168 /* drawing */
|
nuclear@0
|
169 void m3d_draw(int prim, const float *varr, int vcount)
|
nuclear@0
|
170 {
|
nuclear@0
|
171 /* TODO */
|
nuclear@0
|
172 }
|
nuclear@0
|
173
|
nuclear@0
|
174 void m3d_draw_indexed(int prim, const float *varr, const int *idxarr, int icount)
|
nuclear@0
|
175 {
|
nuclear@0
|
176 /* TODO */
|
nuclear@0
|
177 }
|
nuclear@0
|
178
|