wrote most of the 3d pipeline stuff
[dosdemo] / src / 3dgfx.c
1 #include <stdio.h>
2 #include <math.h>
3 #include <string.h>
4 #include "3dgfx.h"
5 #include "polyfill.h"
6
7 #define STACK_SIZE      8
8 typedef float g3d_matrix[16];
9
10 struct g3d_state {
11         unsigned int opt;
12
13         g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
14         int mtop[G3D_NUM_MATRICES];
15
16         int width, height;
17         void *pixels;
18 };
19
20 static void xform4_vec3(const float *mat, float *vec);
21 static void xform3_vec3(const float *mat, float *vec);
22 static void proc_vertex(struct pvertex *res, const struct g3d_vertex *vert, int space);
23
24 static struct g3d_state st;
25 static const float idmat[] = {
26         1, 0, 0, 0,
27         0, 1, 0, 0,
28         0, 0, 1, 0,
29         0, 0, 0, 1
30 };
31
32 void g3d_init(void)
33 {
34         int i;
35
36         memset(&st, 0, sizeof st);
37
38         for(i=0; i<G3D_NUM_MATRICES; i++) {
39                 g3d_set_matrix(i, 0);
40         }
41 }
42
43 void g3d_framebuffer(int width, int height, void *pixels)
44 {
45         st.width = width;
46         st.height = height;
47         st.pixels = pixels;
48 }
49
50 void g3d_enable(unsigned int opt)
51 {
52         st.opt |= opt;
53 }
54
55 void g3d_disable(unsigned int opt)
56 {
57         st.opt &= ~opt;
58 }
59
60 void g3d_setopt(unsigned int opt, unsigned int mask)
61 {
62         st.opt = (st.opt & ~mask) | (opt & mask);
63 }
64
65 unsigned int g3d_getopt(unsigned int mask)
66 {
67         return st.opt & mask;
68 }
69
70 void g3d_set_matrix(int which, const float *m)
71 {
72         int top = st.mtop[which];
73
74         if(!m) m = idmat;
75         memcpy(st.mat[which][top], m, 16 * sizeof(float));
76 }
77
78 #define M(i,j)  (((i) << 2) + (j))
79 void g3d_mult_matrix(int which, const float *m2)
80 {
81         int i, j, top = st.mtop[which];
82         float m1[16];
83         float *dest = st.mat[which][top];
84
85         memcpy(m1, dest, sizeof m1);
86
87         for(i=0; i<4; i++) {
88                 for(j=0; j<4; j++) {
89                         *dest++ = m1[M(0,j)] * m2[M(i,0)] +
90                                 m1[M(1,j)] * m2[M(i,1)] +
91                                 m1[M(2,j)] * m2[M(i,2)] +
92                                 m1[M(3,j)] * m2[M(i,3)];
93                 }
94         }
95 }
96
97 void g3d_push_matrix(int which)
98 {
99         int top = st.mtop[which];
100         if(top >= G3D_NUM_MATRICES) {
101                 fprintf(stderr, "g3d_push_matrix overflow\n");
102                 return;
103         }
104         memcpy(st.mat[which][top + 1], st.mat[which][top], 16 * sizeof(float));
105         st.mtop[which] = top + 1;;
106 }
107
108 void g3d_pop_matrix(int which)
109 {
110         if(st.mtop[which] <= 0) {
111                 fprintf(stderr, "g3d_pop_matrix underflow\n");
112                 return;
113         }
114         --st.mtop[which];
115 }
116
117 void g3d_translate(int which, float x, float y, float z)
118 {
119         float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
120         m[12] = x;
121         m[13] = y;
122         m[14] = z;
123         g3d_mult_matrix(which, m);
124 }
125
126 void g3d_rotate(int which, float deg, float x, float y, float z)
127 {
128         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
129
130         float angle = M_PI * deg / 180.0f;
131         float sina = sin(angle);
132         float cosa = cos(angle);
133         float one_minus_cosa = 1.0f - cosa;
134         float nxsq = x * x;
135         float nysq = y * y;
136         float nzsq = z * z;
137
138         m[0] = nxsq + (1.0f - nxsq) * cosa;
139         m[4] = x * y * one_minus_cosa - z * sina;
140         m[8] = x * z * one_minus_cosa + y * sina;
141         m[1] = x * y * one_minus_cosa + z * sina;
142         m[5] = nysq + (1.0 - nysq) * cosa;
143         m[9] = y * z * one_minus_cosa - x * sina;
144         m[2] = x * z * one_minus_cosa - y * sina;
145         m[6] = y * z * one_minus_cosa + x * sina;
146         m[10] = nzsq + (1.0 - nzsq) * cosa;
147
148         g3d_mult_matrix(which, m);
149 }
150
151 void g3d_scale(int which, float x, float y, float z)
152 {
153         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
154         m[0] = x;
155         m[5] = y;
156         m[10] = z;
157         g3d_mult_matrix(which, m);
158 }
159
160 void g3d_ortho(int which, float left, float right, float bottom, float top, float znear, float zfar)
161 {
162         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
163
164         float dx = right - left;
165         float dy = top - bottom;
166         float dz = zfar - znear;
167
168         m[0] = 2.0 / dx;
169         m[5] = 2.0 / dy;
170         m[10] = -2.0 / dz;
171         m[12] = -(right + left) / dx;
172         m[13] = -(top + bottom) / dy;
173         m[14] = -(zfar + znear) / dz;
174
175         g3d_mult_matrix(which, m);
176 }
177
178 void g3d_frustum(int which, float left, float right, float bottom, float top, float nr, float fr)
179 {
180         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
181
182         float dx = right - left;
183         float dy = top - bottom;
184         float dz = fr - nr;
185
186         float a = (right + left) / dx;
187         float b = (top + bottom) / dy;
188         float c = -(fr + nr) / dz;
189         float d = -2.0 * fr * nr / dz;
190
191         m[0] = 2.0 * nr / dx;
192         m[5] = 2.0 * nr / dy;
193         m[8] = a;
194         m[9] = b;
195         m[10] = c;
196         m[11] = -1.0f;
197         m[14] = d;
198
199         g3d_mult_matrix(which, m);
200 }
201
202 void g3d_perspective(int which, float vfov_deg, float aspect, float znear, float zfar)
203 {
204         float m[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
205
206         float vfov = M_PI * vfov_deg / 180.0f;
207         float s = 1.0f / tan(vfov * 0.5f);
208         float range = znear - zfar;
209
210         m[0] = s / aspect;
211         m[5] = s;
212         m[10] = (znear + zfar) / range;
213         m[11] = -1.0f;
214         m[14] = 2.0f * znear * zfar / range;
215
216         g3d_mult_matrix(which, m);
217 }
218
219 void g3d_draw(int prim, int space, const struct g3d_vertex *varr, int varr_size)
220 {
221         int i;
222         int vnum = prim; /* primitive vertex counts correspond to the enum values */
223
224         while(varr_size >= vnum) {
225                 struct pvertex pv[4];
226
227                 for(i=0; i<vnum; i++) {
228                         proc_vertex(pv + i, varr++, space);
229                 }
230
231                 polyfill_wire(pv, vnum);
232
233                 varr_size -= vnum;
234         }
235 }
236
237 static void xform4_vec3(const float *mat, float *vec)
238 {
239         float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12];
240         float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13];
241         float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14];
242         float w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15];
243
244         vec[0] = x;
245         vec[1] = y;
246         vec[2] = z;
247         vec[3] = w;
248 }
249
250 static void xform3_vec3(const float *mat, float *vec)
251 {
252         float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2];
253         float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2];
254         float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2];
255
256         vec[0] = x;
257         vec[1] = y;
258         vec[2] = z;
259 }
260
261 #define VEC3(v, x, y, z) do { v[0] = x; v[1] = y; v[2] = z; } while(0)
262 #define VEC4(v, x, y, z, w) do { v[0] = x; v[1] = y; v[2] = z; v[3] = w; } while(0)
263
264 static void proc_vertex(struct pvertex *res, const struct g3d_vertex *vert, int space)
265 {
266         float pos[4];
267         float norm[3];
268         float color[3];
269         int mvtop = st.mtop[G3D_MODELVIEW];
270         int ptop = st.mtop[G3D_PROJECTION];
271         g3d_matrix norm_mat;
272
273         VEC4(pos, vert->x, vert->y, vert->z, 1.0f);
274         VEC3(norm, vert->nx, vert->ny, vert->nz);
275
276         switch(space) {
277         case G3D_LOCAL_SPACE:
278                 memcpy(norm_mat, st.mat[G3D_MODELVIEW][mvtop], 16 * sizeof(float));
279                 norm_mat[12] = norm_mat[13] = norm_mat[14] = 0.0f;
280
281                 xform4_vec3(pos, st.mat[G3D_MODELVIEW][mvtop]);
282                 xform3_vec3(norm, norm_mat);
283
284         case G3D_VIEW_SPACE:
285                 if(st.opt & G3D_LIGHTING) {
286                         /* TODO lighting */
287                         color[0] = vert->r;
288                         color[1] = vert->g;
289                         color[2] = vert->b;
290                 }
291                 xform4_vec3(pos, st.mat[G3D_PROJECTION][ptop]);
292
293         case G3D_CLIP_SPACE:
294                 /* TODO clipping */
295                 if(pos[3] != 0.0f) {
296                         pos[0] /= pos[3];
297                         pos[1] /= pos[3];
298                         pos[2] /= pos[3];
299                 }
300
301         case G3D_SCREEN_SPACE:
302                 pos[0] = (pos[0] * 0.5 + 0.5) * (float)st.width;
303                 pos[1] = (0.5 - pos[0] * 0.5) * (float)st.height;
304
305         case G3D_PIXEL_SPACE:
306                 break;
307         }
308
309         /* convert pos to 24.8 fixed point */
310         res->x = (int32_t)(pos[0] * 256.0f);
311         res->y = (int32_t)(pos[1] * 256.0f);
312
313         /* convert tex coords to 16.16 fixed point */
314         res->u = (int32_t)(vert->u * 65536.0f);
315         res->v = (int32_t)(vert->v * 65536.0f);
316
317         /* pass color through as is */
318         res->r = color[0];
319         res->g = color[1];
320         res->b = color[2];
321 }