rayzor

annotate src/min3d.c @ 3:9035507275d6

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Apr 2014 20:53:44 +0300
parents c273c6f799a4
children 5fcf72837b69
rev   line source
nuclear@1 1 #include <stdlib.h>
nuclear@1 2 #include "min3d.h"
nuclear@1 3 #include "m3dimpl.h"
nuclear@1 4
nuclear@1 5 #ifndef M_PI
nuclear@1 6 #define M_PI 3.141592653
nuclear@1 7 #endif
nuclear@1 8
nuclear@3 9 struct min3d_context *m3dctx;
nuclear@3 10
nuclear@1 11 int m3d_init(void)
nuclear@1 12 {
nuclear@1 13 if(!(m3dctx = malloc(sizeof *m3dctx))) {
nuclear@1 14 return -1;
nuclear@1 15 }
nuclear@1 16 memset(m3dctx, 0, sizeof *m3dctx);
nuclear@1 17
nuclear@1 18 m3d_matrix_mode(M3D_PROJECTION);
nuclear@1 19 m3d_load_identity();
nuclear@1 20 m3d_matrix_mode(M3D_MODELVIEW);
nuclear@1 21 m3d_load_identity();
nuclear@1 22 return 0;
nuclear@1 23 }
nuclear@1 24
nuclear@1 25 void m3d_shutdown(void)
nuclear@1 26 {
nuclear@1 27 free(m3dctx);
nuclear@1 28 }
nuclear@1 29
nuclear@1 30 void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf)
nuclear@1 31 {
nuclear@1 32 m3dctx->cbuf = cbuf;
nuclear@1 33 m3dctx->zbuf = zbuf;
nuclear@1 34 }
nuclear@1 35
nuclear@1 36 void m3d_clear(unsigned int bmask)
nuclear@1 37 {
nuclear@1 38 int num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz;
nuclear@1 39 if(bmask & M3D_COLOR_BUFFER_BIT) {
nuclear@1 40 memset(m3dctx->cbuf->pixels, 0, num_pixels * 3);
nuclear@1 41 }
nuclear@1 42 if(bmask & M3D_DEPTH_BUFFER_BIT) {
nuclear@1 43 memset(m3dctx->zbuf, 0xff, num_pixels * sizeof *m3dctx->zbuf);
nuclear@1 44 }
nuclear@1 45 }
nuclear@1 46
nuclear@1 47
nuclear@1 48 void m3d_enable(int bit)
nuclear@1 49 {
nuclear@1 50 m3dctx->state |= (1 << bit);
nuclear@1 51 }
nuclear@1 52
nuclear@1 53 void m3d_disable(int bit)
nuclear@1 54 {
nuclear@1 55 m3dctx->state &= ~(1 << bit);
nuclear@1 56 }
nuclear@1 57
nuclear@1 58
nuclear@1 59 /* matrix stack */
nuclear@1 60 void m3d_matrix_mode(int mode)
nuclear@1 61 {
nuclear@1 62 m3dctx->mmode = mode;
nuclear@1 63 }
nuclear@1 64
nuclear@1 65 void m3d_load_identity(void)
nuclear@1 66 {
nuclear@1 67 static const float mid[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 68 m3d_load_matrix(mid);
nuclear@1 69 }
nuclear@1 70
nuclear@1 71 void m3d_load_matrix(const float *m)
nuclear@1 72 {
nuclear@1 73 int top = m3dctx->mstack[m3dctx->mmode].top;
nuclear@1 74 memcpy(m3dctx->mstack[m3dctx->mmode].m[top], m, 16 * sizeof *m);
nuclear@1 75 }
nuclear@1 76
nuclear@1 77 #define M(i,j) (((i) << 2) + (j))
nuclear@1 78 void m3d_mult_matrix(const float *m2)
nuclear@1 79 {
nuclear@1 80 int i, j, top = m3dctx->mstack[m3dctx->mmode].top;
nuclear@1 81 float m1[16];
nuclear@1 82 float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
nuclear@1 83
nuclear@1 84 memcpy(m1, dest, sizeof m1);
nuclear@1 85
nuclear@1 86 for(i=0; i<4; i++) {
nuclear@1 87 for(j=0; j<4; j++) {
nuclear@1 88 dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
nuclear@1 89 m1[M(1,j)] * m2[M(i,1)] +
nuclear@1 90 m1[M(2,j)] * m2[M(i,2)] +
nuclear@1 91 m1[M(3,j)] * m2[M(i,3)];
nuclear@1 92 }
nuclear@1 93 }
nuclear@1 94 }
nuclear@1 95
nuclear@1 96 void m3d_translate(float x, float y, float z)
nuclear@1 97 {
nuclear@1 98 float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 99 m[12] = x;
nuclear@1 100 m[13] = y;
nuclear@1 101 m[14] = z;
nuclear@1 102 m3d_mult_matrix(m);
nuclear@1 103 }
nuclear@1 104
nuclear@1 105 void m3d_rotate(float deg, float x, float y, float z)
nuclear@1 106 {
nuclear@1 107 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 108
nuclear@1 109 float angle = M_PI * deg / 180.0f;
nuclear@1 110 float sina = sin(angle);
nuclear@1 111 float cosa = cos(angle);
nuclear@1 112 float one_minus_cosa = 1.0f - cosa;
nuclear@1 113 float nxsq = x * x;
nuclear@1 114 float nysq = y * y;
nuclear@1 115 float nzsq = z * z;
nuclear@1 116
nuclear@1 117 xform[0] = nxsq + (1.0f - nxsq) * cosa;
nuclear@1 118 xform[4] = x * y * one_minus_cosa - z * sina;
nuclear@1 119 xform[8] = x * z * one_minus_cosa + y * sina;
nuclear@1 120 xform[1] = x * y * one_minus_cosa + z * sina;
nuclear@1 121 xform[5] = nysq + (1.0 - nysq) * cosa;
nuclear@1 122 xform[9] = y * z * one_minus_cosa - x * sina;
nuclear@1 123 xform[2] = x * z * one_minus_cosa - y * sina;
nuclear@1 124 xform[6] = y * z * one_minus_cosa + x * sina;
nuclear@1 125 xform[10] = nzsq + (1.0 - nzsq) * cosa;
nuclear@1 126
nuclear@1 127 m3d_mult_matrix(xform);
nuclear@1 128 }
nuclear@1 129
nuclear@1 130 void m3d_scale(float x, float y, float z)
nuclear@1 131 {
nuclear@1 132 static float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 133 m[0] = x;
nuclear@1 134 m[5] = y;
nuclear@1 135 m[10] = z;
nuclear@1 136 m3d_mult_matrix(m);
nuclear@1 137 }
nuclear@1 138
nuclear@1 139 void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
nuclear@1 140 {
nuclear@1 141 float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
nuclear@1 142
nuclear@1 143 float dx = right - left;
nuclear@1 144 float dy = top - bottom;
nuclear@1 145 float dz = fr - nr;
nuclear@1 146
nuclear@1 147 float a = (right + left) / dx;
nuclear@1 148 float b = (top + bottom) / dy;
nuclear@1 149 float c = -(fr + nr) / dz;
nuclear@1 150 float d = -2.0 * fr * nr / dz;
nuclear@1 151
nuclear@1 152 xform[0] = 2.0 * nr / dx;
nuclear@1 153 xform[5] = 2.0 * nr / dy;
nuclear@1 154 xform[8] = a;
nuclear@1 155 xform[9] = b;
nuclear@1 156 xform[10] = c;
nuclear@1 157 xform[11] = -1.0f;
nuclear@1 158 xform[14] = d;
nuclear@1 159
nuclear@1 160 m3d_mult_matrix(xform);
nuclear@1 161 }
nuclear@1 162
nuclear@1 163 void m3d_perspective(float vfov, float aspect, float nr, float fr)
nuclear@1 164 {
nuclear@1 165 float vfov_rad = M_PI * vfov / 180.0;
nuclear@1 166 float x = nr * tan(vfov_rad / 2.0);
nuclear@1 167 m3d_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
nuclear@1 168 }
nuclear@1 169
nuclear@2 170 static void xform4(float *mat, float *vec)
nuclear@2 171 {
nuclear@2 172 float x = mat[0] * vec[0] + mat[1] * vec[1] + mat[2] * vec[2] + mat[3];
nuclear@2 173 float y = mat[4] * vec[0] + mat[5] * vec[1] + mat[6] * vec[2] + mat[7];
nuclear@2 174 float z = mat[8] * vec[0] + mat[9] * vec[1] + mat[10] * vec[2] + mat[11];
nuclear@2 175 float w = mat[12] * vec[0] + mat[13] * vec[1] + mat[14] * vec[2] + mat[15];
nuclear@2 176
nuclear@2 177 vec[0] = x;
nuclear@2 178 vec[1] = y;
nuclear@2 179 vec[2] = z;
nuclear@2 180 vec[3] = w;
nuclear@2 181 }
nuclear@2 182
nuclear@3 183 static int proc_prim(int prim, struct min3d_vertex *res, struct min3d_vertex *v)
nuclear@2 184 {
nuclear@3 185 int i;
nuclear@3 186 int vcount = prim;
nuclear@3 187 int mvtop, ptop;
nuclear@3 188 float *mvmat, *pmat;
nuclear@3 189
nuclear@3 190 mvtop = m3dctx->mstack[M3D_MODELVIEW].top;
nuclear@3 191 mvmat = m3dctx->mstack[M3D_MODELVIEW].m[mvtop];
nuclear@3 192 ptop = m3dctx->mstack[M3D_PROJECTION].top;
nuclear@3 193 pmat = m3dctx->mstack[M3D_PROJECTION].m[ptop];
nuclear@3 194
nuclear@3 195 /* transform to view space */
nuclear@3 196 for(i=0; i<vcount; i++) {
nuclear@3 197 res[i] = v[i];
nuclear@3 198 xform4(mvmat, res[i].pos);
nuclear@3 199 /* TODO: normal */
nuclear@3 200 }
nuclear@3 201
nuclear@3 202 /* TODO: lighting */
nuclear@3 203
nuclear@3 204 /* project */
nuclear@3 205 for(i=0; i<vcount; i++) {
nuclear@3 206 xform4(pmat, res[i].pos);
nuclear@3 207 }
nuclear@3 208
nuclear@3 209 /* clip */
nuclear@3 210 switch(prim) {
nuclear@3 211 case M3D_POINTS:
nuclear@3 212 {
nuclear@3 213 float w = res[0].pos[3];
nuclear@3 214 if(res[0].pos[2] < -w || res[0].pos[2] >= w ||
nuclear@3 215 res[0].pos[0] / w < -1 || res[0].pos[0] / w >= 1 ||
nuclear@3 216 res[0].pos[1] / w < -1 || res[0].pos[1] / w >= 1) {
nuclear@3 217 vcount = 0;
nuclear@3 218 }
nuclear@3 219 }
nuclear@3 220 break;
nuclear@3 221
nuclear@3 222 default:
nuclear@3 223 break; /* TODO */
nuclear@3 224 }
nuclear@3 225
nuclear@3 226 /* perspective division */
nuclear@3 227 for(i=0; i<vcount; i++) {
nuclear@3 228 res[i].pos[0] = res[i].pos[3];
nuclear@3 229 res[i].pos[1] = res[i].pos[3];
nuclear@3 230 res[i].pos[2] = res[i].pos[3];
nuclear@3 231 }
nuclear@3 232 return vcount;
nuclear@2 233 }
nuclear@2 234
nuclear@1 235 /* drawing */
nuclear@1 236 void m3d_draw(int prim, const float *varr, int vcount)
nuclear@1 237 {
nuclear@3 238 int i;
nuclear@3 239 struct min3d_vertex v[4];
nuclear@3 240 struct min3d_vertex resv[16];
nuclear@3 241
nuclear@3 242 for(i=0; i<vcount; i++) {
nuclear@3 243 int idx = i % prim;
nuclear@3 244
nuclear@3 245 v[idx].pos[0] = *varr++;
nuclear@3 246 v[idx].pos[1] = *varr++;
nuclear@3 247 v[idx].pos[2] = *varr++;
nuclear@3 248
nuclear@3 249 if(idx == prim - 1) {
nuclear@3 250 int resnum = proc_prim(prim, resv, v);
nuclear@3 251 switch(resnum) {
nuclear@3 252 case 1:
nuclear@3 253 draw_point(resv);
nuclear@3 254 break;
nuclear@3 255
nuclear@3 256 case '2':
nuclear@3 257 draw_line(resv);
nuclear@3 258 break;
nuclear@3 259
nuclear@3 260 default:
nuclear@3 261 draw_poly(resv, resnum);
nuclear@3 262 }
nuclear@3 263 }
nuclear@3 264 }
nuclear@1 265 }
nuclear@1 266
nuclear@1 267 void m3d_draw_indexed(int prim, const float *varr, const int *idxarr, int icount)
nuclear@1 268 {
nuclear@1 269 /* TODO */
nuclear@1 270 }
nuclear@1 271