rayzor
view src/min3d.c @ 2:c273c6f799a4
added fixcase utility to fix the ftp-ed files' case on linux
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 05 Apr 2014 18:31:00 +0300 |
parents | a826bf0fb169 |
children | 9035507275d6 |
line source
1 #include <stdlib.h>
2 #include "min3d.h"
3 #include "m3dimpl.h"
5 #ifndef M_PI
6 #define M_PI 3.141592653
7 #endif
9 int m3d_init(void)
10 {
11 if(!(m3dctx = malloc(sizeof *m3dctx))) {
12 return -1;
13 }
14 memset(m3dctx, 0, sizeof *m3dctx);
16 m3d_matrix_mode(M3D_PROJECTION);
17 m3d_load_identity();
18 m3d_matrix_mode(M3D_MODELVIEW);
19 m3d_load_identity();
20 return 0;
21 }
23 void m3d_shutdown(void)
24 {
25 free(m3dctx);
26 }
28 void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf)
29 {
30 m3dctx->cbuf = cbuf;
31 m3dctx->zbuf = zbuf;
32 }
34 void m3d_clear(unsigned int bmask)
35 {
36 int num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz;
37 if(bmask & M3D_COLOR_BUFFER_BIT) {
38 memset(m3dctx->cbuf->pixels, 0, num_pixels * 3);
39 }
40 if(bmask & M3D_DEPTH_BUFFER_BIT) {
41 memset(m3dctx->zbuf, 0xff, num_pixels * sizeof *m3dctx->zbuf);
42 }
43 }
46 void m3d_enable(int bit)
47 {
48 m3dctx->state |= (1 << bit);
49 }
51 void m3d_disable(int bit)
52 {
53 m3dctx->state &= ~(1 << bit);
54 }
57 /* matrix stack */
58 void m3d_matrix_mode(int mode)
59 {
60 m3dctx->mmode = mode;
61 }
63 void m3d_load_identity(void)
64 {
65 static const float mid[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
66 m3d_load_matrix(mid);
67 }
69 void m3d_load_matrix(const float *m)
70 {
71 int top = m3dctx->mstack[m3dctx->mmode].top;
72 memcpy(m3dctx->mstack[m3dctx->mmode].m[top], m, 16 * sizeof *m);
73 }
75 #define M(i,j) (((i) << 2) + (j))
76 void m3d_mult_matrix(const float *m2)
77 {
78 int i, j, top = m3dctx->mstack[m3dctx->mmode].top;
79 float m1[16];
80 float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
82 memcpy(m1, dest, sizeof m1);
84 for(i=0; i<4; i++) {
85 for(j=0; j<4; j++) {
86 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
87 m1[M(1,j)] * m2[M(i,1)] +
88 m1[M(2,j)] * m2[M(i,2)] +
89 m1[M(3,j)] * m2[M(i,3)];
90 }
91 }
92 }
94 void m3d_translate(float x, float y, float z)
95 {
96 float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
97 m[12] = x;
98 m[13] = y;
99 m[14] = z;
100 m3d_mult_matrix(m);
101 }
103 void m3d_rotate(float deg, float x, float y, float z)
104 {
105 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
107 float angle = M_PI * deg / 180.0f;
108 float sina = sin(angle);
109 float cosa = cos(angle);
110 float one_minus_cosa = 1.0f - cosa;
111 float nxsq = x * x;
112 float nysq = y * y;
113 float nzsq = z * z;
115 xform[0] = nxsq + (1.0f - nxsq) * cosa;
116 xform[4] = x * y * one_minus_cosa - z * sina;
117 xform[8] = x * z * one_minus_cosa + y * sina;
118 xform[1] = x * y * one_minus_cosa + z * sina;
119 xform[5] = nysq + (1.0 - nysq) * cosa;
120 xform[9] = y * z * one_minus_cosa - x * sina;
121 xform[2] = x * z * one_minus_cosa - y * sina;
122 xform[6] = y * z * one_minus_cosa + x * sina;
123 xform[10] = nzsq + (1.0 - nzsq) * cosa;
125 m3d_mult_matrix(xform);
126 }
128 void m3d_scale(float x, float y, float z)
129 {
130 static float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
131 m[0] = x;
132 m[5] = y;
133 m[10] = z;
134 m3d_mult_matrix(m);
135 }
137 void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
138 {
139 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
141 float dx = right - left;
142 float dy = top - bottom;
143 float dz = fr - nr;
145 float a = (right + left) / dx;
146 float b = (top + bottom) / dy;
147 float c = -(fr + nr) / dz;
148 float d = -2.0 * fr * nr / dz;
150 xform[0] = 2.0 * nr / dx;
151 xform[5] = 2.0 * nr / dy;
152 xform[8] = a;
153 xform[9] = b;
154 xform[10] = c;
155 xform[11] = -1.0f;
156 xform[14] = d;
158 m3d_mult_matrix(xform);
159 }
161 void m3d_perspective(float vfov, float aspect, float nr, float fr)
162 {
163 float vfov_rad = M_PI * vfov / 180.0;
164 float x = nr * tan(vfov_rad / 2.0);
165 m3d_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
166 }
168 static void xform4(float *mat, float *vec)
169 {
170 float x = mat[0] * vec[0] + mat[1] * vec[1] + mat[2] * vec[2] + mat[3];
171 float y = mat[4] * vec[0] + mat[5] * vec[1] + mat[6] * vec[2] + mat[7];
172 float z = mat[8] * vec[0] + mat[9] * vec[1] + mat[10] * vec[2] + mat[11];
173 float w = mat[12] * vec[0] + mat[13] * vec[1] + mat[14] * vec[2] + mat[15];
175 vec[0] = x;
176 vec[1] = y;
177 vec[2] = z;
178 vec[3] = w;
179 }
181 static void proc_vertex(struct min3d_vertex *v)
182 {
183 struct min3d_vertex *tv;
184 }
186 /* drawing */
187 void m3d_draw(int prim, const float *varr, int vcount)
188 {
189 /* TODO */
190 }
192 void m3d_draw_indexed(int prim, const float *varr, const int *idxarr, int icount)
193 {
194 /* TODO */
195 }