rayzor

diff src/min3d.c @ 1:a826bf0fb169

fixed line endings
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Apr 2014 09:05:26 +0300
parents 2a5340a6eee4
children c273c6f799a4
line diff
     1.1 --- a/src/min3d.c	Sat Apr 05 08:46:27 2014 +0300
     1.2 +++ b/src/min3d.c	Sat Apr 05 09:05:26 2014 +0300
     1.3 @@ -1,178 +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 -
   1.182 +#include <stdlib.h>
   1.183 +#include "min3d.h"
   1.184 +#include "m3dimpl.h"
   1.185 +
   1.186 +#ifndef M_PI
   1.187 +#define M_PI	3.141592653
   1.188 +#endif
   1.189 +
   1.190 +int m3d_init(void)
   1.191 +{
   1.192 +	if(!(m3dctx = malloc(sizeof *m3dctx))) {
   1.193 +		return -1;
   1.194 +	}
   1.195 +	memset(m3dctx, 0, sizeof *m3dctx);
   1.196 +
   1.197 +	m3d_matrix_mode(M3D_PROJECTION);
   1.198 +	m3d_load_identity();
   1.199 +	m3d_matrix_mode(M3D_MODELVIEW);
   1.200 +	m3d_load_identity();
   1.201 +	return 0;
   1.202 +}
   1.203 +
   1.204 +void m3d_shutdown(void)
   1.205 +{
   1.206 +	free(m3dctx);
   1.207 +}
   1.208 +
   1.209 +void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf)
   1.210 +{
   1.211 +	m3dctx->cbuf = cbuf;
   1.212 +	m3dctx->zbuf = zbuf;
   1.213 +}
   1.214 +
   1.215 +void m3d_clear(unsigned int bmask)
   1.216 +{
   1.217 +	int num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz;
   1.218 +	if(bmask & M3D_COLOR_BUFFER_BIT) {
   1.219 +		memset(m3dctx->cbuf->pixels, 0, num_pixels * 3);
   1.220 +	}
   1.221 +	if(bmask & M3D_DEPTH_BUFFER_BIT) {
   1.222 +		memset(m3dctx->zbuf, 0xff, num_pixels * sizeof *m3dctx->zbuf);
   1.223 +	}
   1.224 +}
   1.225 +
   1.226 +
   1.227 +void m3d_enable(int bit)
   1.228 +{
   1.229 +	m3dctx->state |= (1 << bit);
   1.230 +}
   1.231 +
   1.232 +void m3d_disable(int bit)
   1.233 +{
   1.234 +	m3dctx->state &= ~(1 << bit);
   1.235 +}
   1.236 +
   1.237 +
   1.238 +/* matrix stack */
   1.239 +void m3d_matrix_mode(int mode)
   1.240 +{
   1.241 +	m3dctx->mmode = mode;
   1.242 +}
   1.243 +
   1.244 +void m3d_load_identity(void)
   1.245 +{
   1.246 +	static const float mid[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
   1.247 +	m3d_load_matrix(mid);
   1.248 +}
   1.249 +
   1.250 +void m3d_load_matrix(const float *m)
   1.251 +{
   1.252 +	int top = m3dctx->mstack[m3dctx->mmode].top;
   1.253 +	memcpy(m3dctx->mstack[m3dctx->mmode].m[top], m, 16 * sizeof *m);
   1.254 +}
   1.255 +
   1.256 +#define M(i,j)	(((i) << 2) + (j))
   1.257 +void m3d_mult_matrix(const float *m2)
   1.258 +{
   1.259 +	int i, j, top = m3dctx->mstack[m3dctx->mmode].top;
   1.260 +	float m1[16];
   1.261 +	float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
   1.262 +
   1.263 +	memcpy(m1, dest, sizeof m1);
   1.264 +
   1.265 +	for(i=0; i<4; i++) {
   1.266 +		for(j=0; j<4; j++) {
   1.267 +			dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
   1.268 +				m1[M(1,j)] * m2[M(i,1)] +
   1.269 +				m1[M(2,j)] * m2[M(i,2)] +
   1.270 +				m1[M(3,j)] * m2[M(i,3)];
   1.271 +		}
   1.272 +	}
   1.273 +}
   1.274 +
   1.275 +void m3d_translate(float x, float y, float z)
   1.276 +{
   1.277 +	float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
   1.278 +	m[12] = x;
   1.279 +	m[13] = y;
   1.280 +	m[14] = z;
   1.281 +	m3d_mult_matrix(m);
   1.282 +}
   1.283 +
   1.284 +void m3d_rotate(float deg, float x, float y, float z)
   1.285 +{
   1.286 +	float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
   1.287 +
   1.288 +	float angle = M_PI * deg / 180.0f;
   1.289 +	float sina = sin(angle);
   1.290 +	float cosa = cos(angle);
   1.291 +	float one_minus_cosa = 1.0f - cosa;
   1.292 +	float nxsq = x * x;
   1.293 +	float nysq = y * y;
   1.294 +	float nzsq = z * z;
   1.295 +
   1.296 +	xform[0] = nxsq + (1.0f - nxsq) * cosa;
   1.297 +	xform[4] = x * y * one_minus_cosa - z * sina;
   1.298 +	xform[8] = x * z * one_minus_cosa + y * sina;
   1.299 +	xform[1] = x * y * one_minus_cosa + z * sina;
   1.300 +	xform[5] = nysq + (1.0 - nysq) * cosa;
   1.301 +	xform[9] = y * z * one_minus_cosa - x * sina;
   1.302 +	xform[2] = x * z * one_minus_cosa - y * sina;
   1.303 +	xform[6] = y * z * one_minus_cosa + x * sina;
   1.304 +	xform[10] = nzsq + (1.0 - nzsq) * cosa;
   1.305 +
   1.306 +	m3d_mult_matrix(xform);
   1.307 +}
   1.308 +
   1.309 +void m3d_scale(float x, float y, float z)
   1.310 +{
   1.311 +	static float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
   1.312 +	m[0] = x;
   1.313 +	m[5] = y;
   1.314 +	m[10] = z;
   1.315 +	m3d_mult_matrix(m);
   1.316 +}
   1.317 +
   1.318 +void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
   1.319 +{
   1.320 +	float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
   1.321 +
   1.322 +	float dx = right - left;
   1.323 +	float dy = top - bottom;
   1.324 +	float dz = fr - nr;
   1.325 +
   1.326 +	float a = (right + left) / dx;
   1.327 +	float b = (top + bottom) / dy;
   1.328 +	float c = -(fr + nr) / dz;
   1.329 +	float d = -2.0 * fr * nr / dz;
   1.330 +
   1.331 +	xform[0] = 2.0 * nr / dx;
   1.332 +	xform[5] = 2.0 * nr / dy;
   1.333 +	xform[8] = a;
   1.334 +	xform[9] = b;
   1.335 +	xform[10] = c;
   1.336 +	xform[11] = -1.0f;
   1.337 +	xform[14] = d;
   1.338 +
   1.339 +	m3d_mult_matrix(xform);
   1.340 +}
   1.341 +
   1.342 +void m3d_perspective(float vfov, float aspect, float nr, float fr)
   1.343 +{
   1.344 +	float vfov_rad = M_PI * vfov / 180.0;
   1.345 +	float x = nr * tan(vfov_rad / 2.0);
   1.346 +	m3d_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
   1.347 +}
   1.348 +
   1.349 +/* drawing */
   1.350 +void m3d_draw(int prim, const float *varr, int vcount)
   1.351 +{
   1.352 +	/* TODO */
   1.353 +}
   1.354 +
   1.355 +void m3d_draw_indexed(int prim, const float *varr, const int *idxarr, int icount)
   1.356 +{
   1.357 +	/* TODO */
   1.358 +}
   1.359 +