rayzor

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/min3d.c	Sat Apr 05 08:46:27 2014 +0300
     1.3 @@ -0,0 +1,178 @@
     1.4 +#include <stdlib.h>
     1.5 +#include "min3d.h"
     1.6 +#include "m3dimpl.h"
     1.7 +
     1.8 +#ifndef M_PI
     1.9 +#define M_PI	3.141592653
    1.10 +#endif
    1.11 +
    1.12 +int m3d_init(void)
    1.13 +{
    1.14 +	if(!(m3dctx = malloc(sizeof *m3dctx))) {
    1.15 +		return -1;
    1.16 +	}
    1.17 +	memset(m3dctx, 0, sizeof *m3dctx);
    1.18 +
    1.19 +	m3d_matrix_mode(M3D_PROJECTION);
    1.20 +	m3d_load_identity();
    1.21 +	m3d_matrix_mode(M3D_MODELVIEW);
    1.22 +	m3d_load_identity();
    1.23 +	return 0;
    1.24 +}
    1.25 +
    1.26 +void m3d_shutdown(void)
    1.27 +{
    1.28 +	free(m3dctx);
    1.29 +}
    1.30 +
    1.31 +void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf)
    1.32 +{
    1.33 +	m3dctx->cbuf = cbuf;
    1.34 +	m3dctx->zbuf = zbuf;
    1.35 +}
    1.36 +
    1.37 +void m3d_clear(unsigned int bmask)
    1.38 +{
    1.39 +	int num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz;
    1.40 +	if(bmask & M3D_COLOR_BUFFER_BIT) {
    1.41 +		memset(m3dctx->cbuf->pixels, 0, num_pixels * 3);
    1.42 +	}
    1.43 +	if(bmask & M3D_DEPTH_BUFFER_BIT) {
    1.44 +		memset(m3dctx->zbuf, 0xff, num_pixels * sizeof *m3dctx->zbuf);
    1.45 +	}
    1.46 +}
    1.47 +
    1.48 +
    1.49 +void m3d_enable(int bit)
    1.50 +{
    1.51 +	m3dctx->state |= (1 << bit);
    1.52 +}
    1.53 +
    1.54 +void m3d_disable(int bit)
    1.55 +{
    1.56 +	m3dctx->state &= ~(1 << bit);
    1.57 +}
    1.58 +
    1.59 +
    1.60 +/* matrix stack */
    1.61 +void m3d_matrix_mode(int mode)
    1.62 +{
    1.63 +	m3dctx->mmode = mode;
    1.64 +}
    1.65 +
    1.66 +void m3d_load_identity(void)
    1.67 +{
    1.68 +	static const float mid[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
    1.69 +	m3d_load_matrix(mid);
    1.70 +}
    1.71 +
    1.72 +void m3d_load_matrix(const float *m)
    1.73 +{
    1.74 +	int top = m3dctx->mstack[m3dctx->mmode].top;
    1.75 +	memcpy(m3dctx->mstack[m3dctx->mmode].m[top], m, 16 * sizeof *m);
    1.76 +}
    1.77 +
    1.78 +#define M(i,j)	(((i) << 2) + (j))
    1.79 +void m3d_mult_matrix(const float *m2)
    1.80 +{
    1.81 +	int i, j, top = m3dctx->mstack[m3dctx->mmode].top;
    1.82 +	float m1[16];
    1.83 +	float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
    1.84 +
    1.85 +	memcpy(m1, dest, sizeof m1);
    1.86 +
    1.87 +	for(i=0; i<4; i++) {
    1.88 +		for(j=0; j<4; j++) {
    1.89 +			dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
    1.90 +				m1[M(1,j)] * m2[M(i,1)] +
    1.91 +				m1[M(2,j)] * m2[M(i,2)] +
    1.92 +				m1[M(3,j)] * m2[M(i,3)];
    1.93 +		}
    1.94 +	}
    1.95 +}
    1.96 +
    1.97 +void m3d_translate(float x, float y, float z)
    1.98 +{
    1.99 +	float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
   1.100 +	m[12] = x;
   1.101 +	m[13] = y;
   1.102 +	m[14] = z;
   1.103 +	m3d_mult_matrix(m);
   1.104 +}
   1.105 +
   1.106 +void m3d_rotate(float deg, float x, float y, float z)
   1.107 +{
   1.108 +	float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
   1.109 +
   1.110 +	float angle = M_PI * deg / 180.0f;
   1.111 +	float sina = sin(angle);
   1.112 +	float cosa = cos(angle);
   1.113 +	float one_minus_cosa = 1.0f - cosa;
   1.114 +	float nxsq = x * x;
   1.115 +	float nysq = y * y;
   1.116 +	float nzsq = z * z;
   1.117 +
   1.118 +	xform[0] = nxsq + (1.0f - nxsq) * cosa;
   1.119 +	xform[4] = x * y * one_minus_cosa - z * sina;
   1.120 +	xform[8] = x * z * one_minus_cosa + y * sina;
   1.121 +	xform[1] = x * y * one_minus_cosa + z * sina;
   1.122 +	xform[5] = nysq + (1.0 - nysq) * cosa;
   1.123 +	xform[9] = y * z * one_minus_cosa - x * sina;
   1.124 +	xform[2] = x * z * one_minus_cosa - y * sina;
   1.125 +	xform[6] = y * z * one_minus_cosa + x * sina;
   1.126 +	xform[10] = nzsq + (1.0 - nzsq) * cosa;
   1.127 +
   1.128 +	m3d_mult_matrix(xform);
   1.129 +}
   1.130 +
   1.131 +void m3d_scale(float x, float y, float z)
   1.132 +{
   1.133 +	static float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
   1.134 +	m[0] = x;
   1.135 +	m[5] = y;
   1.136 +	m[10] = z;
   1.137 +	m3d_mult_matrix(m);
   1.138 +}
   1.139 +
   1.140 +void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
   1.141 +{
   1.142 +	float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
   1.143 +
   1.144 +	float dx = right - left;
   1.145 +	float dy = top - bottom;
   1.146 +	float dz = fr - nr;
   1.147 +
   1.148 +	float a = (right + left) / dx;
   1.149 +	float b = (top + bottom) / dy;
   1.150 +	float c = -(fr + nr) / dz;
   1.151 +	float d = -2.0 * fr * nr / dz;
   1.152 +
   1.153 +	xform[0] = 2.0 * nr / dx;
   1.154 +	xform[5] = 2.0 * nr / dy;
   1.155 +	xform[8] = a;
   1.156 +	xform[9] = b;
   1.157 +	xform[10] = c;
   1.158 +	xform[11] = -1.0f;
   1.159 +	xform[14] = d;
   1.160 +
   1.161 +	m3d_mult_matrix(xform);
   1.162 +}
   1.163 +
   1.164 +void m3d_perspective(float vfov, float aspect, float nr, float fr)
   1.165 +{
   1.166 +	float vfov_rad = M_PI * vfov / 180.0;
   1.167 +	float x = nr * tan(vfov_rad / 2.0);
   1.168 +	m3d_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
   1.169 +}
   1.170 +
   1.171 +/* drawing */
   1.172 +void m3d_draw(int prim, const float *varr, int vcount)
   1.173 +{
   1.174 +	/* TODO */
   1.175 +}
   1.176 +
   1.177 +void m3d_draw_indexed(int prim, const float *varr, const int *idxarr, int icount)
   1.178 +{
   1.179 +	/* TODO */
   1.180 +}
   1.181 +