rayzor

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