istereo

diff src/sanegl.c @ 1:4d25539806d2

bollocks
author John Tsiombikas <nuclear@mutantstargoat.com>
date Tue, 06 Sep 2011 12:48:39 +0300
parents
children bb68fac22579
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/sanegl.c	Tue Sep 06 12:48:39 2011 +0300
     1.3 @@ -0,0 +1,241 @@
     1.4 +#include <math.h>
     1.5 +#include <string.h>
     1.6 +#include "sanegl.h"
     1.7 +
     1.8 +#define MMODE_IDX(x)	((x) - GL_MODELVIEW)
     1.9 +#define MAT_STACK_SIZE	32
    1.10 +#define MAT_IDENT	{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
    1.11 +
    1.12 +#define MAX_VERTS	512
    1.13 +
    1.14 +typedef struct { float x, y; } vec2_t;
    1.15 +typedef struct { float x, y, z; } vec3_t;
    1.16 +typedef struct { float x, y, z, w; } vec4_t;
    1.17 +
    1.18 +static int mm_idx = 0;
    1.19 +static float mat_stack[3][MAT_STACK_SIZE][16] = {{MAT_IDENT}, {MAT_IDENT}, {MAT_IDENT}};
    1.20 +static int stack_top[3];
    1.21 +static float mat_mvp[16];
    1.22 +static int mvp_valid;
    1.23 +static int prim = -1;
    1.24 +
    1.25 +static vec3_t cur_normal;
    1.26 +static vec4_t cur_color, cur_attrib;
    1.27 +static vec2_t cur_texcoord;
    1.28 +
    1.29 +static vec4_t *vert_arr, *col_arr, *attr_arr;
    1.30 +static vec3_t *norm_arr;
    1.31 +static vec2_t *texc_arr;
    1.32 +static int vloc, nloc, cloc, tloc, aloc;
    1.33 +
    1.34 +
    1.35 +void gl_matrix_mode(int mm)
    1.36 +{
    1.37 +	mm_idx = MMODE_IDX(mm);
    1.38 +}
    1.39 +
    1.40 +void gl_push_matrix(void)
    1.41 +{
    1.42 +	int top = stack_top[mm_idx];
    1.43 +
    1.44 +	memcpy(mat_stack[mm_idx][top + 1], mat_stack[mm_idx][top], 16 * sizeof(float));
    1.45 +	stack_top[mm_idx]++;
    1.46 +	mvp_valid = 0;
    1.47 +}
    1.48 +
    1.49 +void gl_pop_matrix(void)
    1.50 +{
    1.51 +	stack_top[mm_idx]--;
    1.52 +	mvp_valid = 0;
    1.53 +}
    1.54 +
    1.55 +void gl_load_identity(void)
    1.56 +{
    1.57 +	static const float idmat[] = MAT_IDENT;
    1.58 +	int top = stack_top[mm_idx];
    1.59 +	float *mat = mat_stack[mm_idx][top];
    1.60 +
    1.61 +	memcpy(mat, idmat, sizeof idmat);
    1.62 +	mvp_valid = 0;
    1.63 +}
    1.64 +
    1.65 +void gl_load_matrixf(const float *m)
    1.66 +{
    1.67 +	int top = stack_top[mm_idx];
    1.68 +	float *mat = mat_stack[mm_idx][top];
    1.69 +
    1.70 +	memcpy(mat, m, 16 * sizeof *mat);
    1.71 +	mvp_valid = 0;
    1.72 +}
    1.73 +
    1.74 +#define M(i, j)	((i << 2) + j)
    1.75 +
    1.76 +void gl_mult_matrixf(const float *m2)
    1.77 +{
    1.78 +	int i, j;
    1.79 +	int top = stack_top[mm_idx];
    1.80 +	float *m1 = mat_stack[mm_idx][top];
    1.81 +	float res[16];
    1.82 +
    1.83 +	for(i=0; i<4; i++) {
    1.84 +		for(j=0; j<4; j++) {
    1.85 +			res[M(i,j)] = m1[M(i,0)] * m2[M(0,j)] +
    1.86 +						m1[M(i,1)] * m2[M(1,j)] +
    1.87 +						m1[M(i,2)] * m2[M(2,j)] +
    1.88 +						m1[M(i,3)] * m2[M(3,j)];
    1.89 +		}
    1.90 +	}
    1.91 +
    1.92 +	memcpy(m1, res, sizeof res);
    1.93 +	mvp_valid = 0;
    1.94 +}
    1.95 +
    1.96 +void gl_translatef(float x, float y, float z)
    1.97 +{
    1.98 +	float mat[] = MAT_IDENT;
    1.99 +
   1.100 +	mat[12] = x;
   1.101 +	mat[13] = y;
   1.102 +	mat[14] = z;
   1.103 +
   1.104 +	gl_mult_matrixf(mat);
   1.105 +}
   1.106 +
   1.107 +void gl_rotatef(float angle, float x, float y, float z)
   1.108 +{
   1.109 +	float mat[] = MAT_IDENT;
   1.110 +
   1.111 +	float angle_rad = M_PI * angle / 180.0;
   1.112 +	float sina = sin(angle_rad);
   1.113 +	float cosa = cos(angle_rad);
   1.114 +	float one_minus_cosa = 1.0 - cosa;
   1.115 +	float nxsq = x * x;
   1.116 +	float nysq = y * y;
   1.117 +	float nzsq = z * z;
   1.118 +
   1.119 +	mat[0] = nxsq + (1.0 - nxsq) * cosa;
   1.120 +	mat[4] = x * y * one_minus_cosa - z * sina;
   1.121 +	mat[8] = x * z * one_minus_cosa + y * sina;
   1.122 +	mat[1] = x * y * one_minus_cosa + z * sina;
   1.123 +	mat[5] = nysq + (1.0 - nysq) * cosa;
   1.124 +	mat[9] = y * z * one_minus_cosa - x * sina;
   1.125 +	mat[2] = x * z * one_minus_cosa - y * sina;
   1.126 +	mat[6] = y * z * one_minus_cosa + x * sina;
   1.127 +	mat[10] = nzsq + (1.0 - nzsq) * cosa;
   1.128 +
   1.129 +	gl_mult_matrixf(mat);
   1.130 +}
   1.131 +
   1.132 +void gl_scalef(float x, float y, float z)
   1.133 +{
   1.134 +	float mat[] = MAT_IDENT;
   1.135 +
   1.136 +	mat[0] = x;
   1.137 +	mat[5] = y;
   1.138 +	mat[10] = z;
   1.139 +
   1.140 +	gl_mult_matrixf(mat);
   1.141 +}
   1.142 +
   1.143 +void gl_ortho(float left, float right, float bottom, float top, float near, float far)
   1.144 +{
   1.145 +	float mat[] = MAT_IDENT;
   1.146 +
   1.147 +	float dx = right - left;
   1.148 +	float dy = top - bottom;
   1.149 +	float dz = far - near;
   1.150 +
   1.151 +	float tx = -(right + left) / dx;
   1.152 +	float ty = -(top + bottom) / dy;
   1.153 +	float tz = -(far + near) / dz;
   1.154 +
   1.155 +	float sx = 2.0 / dx;
   1.156 +	float sy = 2.0 / dy;
   1.157 +	float sz = -2.0 / dz;
   1.158 +
   1.159 +	mat[0] = sx;
   1.160 +	mat[5] = sy;
   1.161 +	mat[10] = sz;
   1.162 +	mat[12] = tx;
   1.163 +	mat[13] = ty;
   1.164 +	mat[14] = tz;
   1.165 +
   1.166 +	gl_mult_matrixf(mat);
   1.167 +}
   1.168 +
   1.169 +void gl_frustum(float left, float right, float bottom, float top, float near, float far)
   1.170 +{
   1.171 +	float mat[] = MAT_IDENT;
   1.172 +
   1.173 +	float dx = right - left;
   1.174 +	float dy = top - bottom;
   1.175 +	float dz = far - near;
   1.176 +
   1.177 +	float a = (right + left) / dx;
   1.178 +	float b = (top + bottom) / dy;
   1.179 +	float c = -(far + near) / dz;
   1.180 +	float d = -2.0 * far * near / dz;
   1.181 +
   1.182 +	mat[0] = 2.0 * near / dx;
   1.183 +	mat[5] = 2.0 * near / dy;
   1.184 +	mat[8] = a;
   1.185 +	mat[9] = b;
   1.186 +	mat[10] = c;
   1.187 +	mat[11] = -1.0;
   1.188 +	mat[14] = d;
   1.189 +
   1.190 +	gl_mult_matrixf(mat);
   1.191 +}
   1.192 +
   1.193 +void glu_perspective(float vfov, float aspect, float near, float far)
   1.194 +{
   1.195 +	float x = near * tan(vfov / 2.0);
   1.196 +	gl_frustum(-aspect * x, aspect * x, -x, x, near, far);
   1.197 +}
   1.198 +
   1.199 +void gl_apply_xform(unsigned int prog)
   1.200 +{
   1.201 +	int loc, mvidx, pidx, tidx, mvtop, ptop, ttop;
   1.202 +
   1.203 +	mvidx = MMODE_IDX(GL_MODELVIEW);
   1.204 +	pidx = MMODE_IDX(GL_PROJECTION);
   1.205 +	tidx = MMODE_IDX(GL_TEXTURE);
   1.206 +
   1.207 +	mvtop = stack_top[mvidx];
   1.208 +	ptop = stack_top[pidx];
   1.209 +	ttop = stack_top[tidx];
   1.210 +
   1.211 +	if((loc = glGetUniformLocation(prog, "matrix_modelview")) != -1) {
   1.212 +		glUniformMatrix4fv(loc, 16, 0, mat_stack[mvidx][mvtop]);
   1.213 +	}
   1.214 +
   1.215 +	if((loc = glGetUniformLocation(prog, "matrix_projection")) != -1) {
   1.216 +		glUniformMatrix4fv(loc, 16, 0, mat_stack[pidx][ptop]);
   1.217 +	}
   1.218 +
   1.219 +	if((loc = glGetUniformLocation(prog, "matrix_texture")) != -1) {
   1.220 +		glUniformMatrix4fv(loc, 16, 0, mat_stack[tidx][ttop]);
   1.221 +	}
   1.222 +
   1.223 +	if((loc = glGetUniformLocation(prog, "matrix_normal")) != -1) {
   1.224 +		float nmat[9];
   1.225 +
   1.226 +		nmat[0] = mat_stack[mvidx][mvtop][0];
   1.227 +		nmat[1] = mat_stack[mvidx][mvtop][1];
   1.228 +		nmat[2] = mat_stack[mvidx][mvtop][2];
   1.229 +		nmat[3] = mat_stack[mvidx][mvtop][4];
   1.230 +		nmat[4] = mat_stack[mvidx][mvtop][5];
   1.231 +		nmat[5] = mat_stack[mvidx][mvtop][6];
   1.232 +		nmat[6] = mat_stack[mvidx][mvtop][8];
   1.233 +		nmat[7] = mat_stack[mvidx][mvtop][9];
   1.234 +		nmat[8] = mat_stack[mvidx][mvtop][10];
   1.235 +		glUniformMatrix3fv(loc, 9, 0, nmat);
   1.236 +	}
   1.237 +
   1.238 +	if((loc = glGetUniformLocation(prog, "matrix_modelview_projection")) != -1) {
   1.239 +		if(!mvp_valid) {
   1.240 +			/* TODO calc mvp */
   1.241 +		}
   1.242 +		glUniformMatrix4fv(loc, 16, 0, mat_mvp);
   1.243 +	}
   1.244 +}