rayzor

view src/min3d.c @ 0:2a5340a6eee4

rayzor first commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Apr 2014 08:46:27 +0300
parents
children a826bf0fb169
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 /* drawing */
169 void m3d_draw(int prim, const float *varr, int vcount)
170 {
171 /* TODO */
172 }
174 void m3d_draw_indexed(int prim, const float *varr, const int *idxarr, int icount)
175 {
176 /* TODO */
177 }