istereo
view 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 source
1 #include <math.h>
2 #include <string.h>
3 #include "sanegl.h"
5 #define MMODE_IDX(x) ((x) - GL_MODELVIEW)
6 #define MAT_STACK_SIZE 32
7 #define MAT_IDENT {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
9 #define MAX_VERTS 512
11 typedef struct { float x, y; } vec2_t;
12 typedef struct { float x, y, z; } vec3_t;
13 typedef struct { float x, y, z, w; } vec4_t;
15 static int mm_idx = 0;
16 static float mat_stack[3][MAT_STACK_SIZE][16] = {{MAT_IDENT}, {MAT_IDENT}, {MAT_IDENT}};
17 static int stack_top[3];
18 static float mat_mvp[16];
19 static int mvp_valid;
20 static int prim = -1;
22 static vec3_t cur_normal;
23 static vec4_t cur_color, cur_attrib;
24 static vec2_t cur_texcoord;
26 static vec4_t *vert_arr, *col_arr, *attr_arr;
27 static vec3_t *norm_arr;
28 static vec2_t *texc_arr;
29 static int vloc, nloc, cloc, tloc, aloc;
32 void gl_matrix_mode(int mm)
33 {
34 mm_idx = MMODE_IDX(mm);
35 }
37 void gl_push_matrix(void)
38 {
39 int top = stack_top[mm_idx];
41 memcpy(mat_stack[mm_idx][top + 1], mat_stack[mm_idx][top], 16 * sizeof(float));
42 stack_top[mm_idx]++;
43 mvp_valid = 0;
44 }
46 void gl_pop_matrix(void)
47 {
48 stack_top[mm_idx]--;
49 mvp_valid = 0;
50 }
52 void gl_load_identity(void)
53 {
54 static const float idmat[] = MAT_IDENT;
55 int top = stack_top[mm_idx];
56 float *mat = mat_stack[mm_idx][top];
58 memcpy(mat, idmat, sizeof idmat);
59 mvp_valid = 0;
60 }
62 void gl_load_matrixf(const float *m)
63 {
64 int top = stack_top[mm_idx];
65 float *mat = mat_stack[mm_idx][top];
67 memcpy(mat, m, 16 * sizeof *mat);
68 mvp_valid = 0;
69 }
71 #define M(i, j) ((i << 2) + j)
73 void gl_mult_matrixf(const float *m2)
74 {
75 int i, j;
76 int top = stack_top[mm_idx];
77 float *m1 = mat_stack[mm_idx][top];
78 float res[16];
80 for(i=0; i<4; i++) {
81 for(j=0; j<4; j++) {
82 res[M(i,j)] = m1[M(i,0)] * m2[M(0,j)] +
83 m1[M(i,1)] * m2[M(1,j)] +
84 m1[M(i,2)] * m2[M(2,j)] +
85 m1[M(i,3)] * m2[M(3,j)];
86 }
87 }
89 memcpy(m1, res, sizeof res);
90 mvp_valid = 0;
91 }
93 void gl_translatef(float x, float y, float z)
94 {
95 float mat[] = MAT_IDENT;
97 mat[12] = x;
98 mat[13] = y;
99 mat[14] = z;
101 gl_mult_matrixf(mat);
102 }
104 void gl_rotatef(float angle, float x, float y, float z)
105 {
106 float mat[] = MAT_IDENT;
108 float angle_rad = M_PI * angle / 180.0;
109 float sina = sin(angle_rad);
110 float cosa = cos(angle_rad);
111 float one_minus_cosa = 1.0 - cosa;
112 float nxsq = x * x;
113 float nysq = y * y;
114 float nzsq = z * z;
116 mat[0] = nxsq + (1.0 - nxsq) * cosa;
117 mat[4] = x * y * one_minus_cosa - z * sina;
118 mat[8] = x * z * one_minus_cosa + y * sina;
119 mat[1] = x * y * one_minus_cosa + z * sina;
120 mat[5] = nysq + (1.0 - nysq) * cosa;
121 mat[9] = y * z * one_minus_cosa - x * sina;
122 mat[2] = x * z * one_minus_cosa - y * sina;
123 mat[6] = y * z * one_minus_cosa + x * sina;
124 mat[10] = nzsq + (1.0 - nzsq) * cosa;
126 gl_mult_matrixf(mat);
127 }
129 void gl_scalef(float x, float y, float z)
130 {
131 float mat[] = MAT_IDENT;
133 mat[0] = x;
134 mat[5] = y;
135 mat[10] = z;
137 gl_mult_matrixf(mat);
138 }
140 void gl_ortho(float left, float right, float bottom, float top, float near, float far)
141 {
142 float mat[] = MAT_IDENT;
144 float dx = right - left;
145 float dy = top - bottom;
146 float dz = far - near;
148 float tx = -(right + left) / dx;
149 float ty = -(top + bottom) / dy;
150 float tz = -(far + near) / dz;
152 float sx = 2.0 / dx;
153 float sy = 2.0 / dy;
154 float sz = -2.0 / dz;
156 mat[0] = sx;
157 mat[5] = sy;
158 mat[10] = sz;
159 mat[12] = tx;
160 mat[13] = ty;
161 mat[14] = tz;
163 gl_mult_matrixf(mat);
164 }
166 void gl_frustum(float left, float right, float bottom, float top, float near, float far)
167 {
168 float mat[] = MAT_IDENT;
170 float dx = right - left;
171 float dy = top - bottom;
172 float dz = far - near;
174 float a = (right + left) / dx;
175 float b = (top + bottom) / dy;
176 float c = -(far + near) / dz;
177 float d = -2.0 * far * near / dz;
179 mat[0] = 2.0 * near / dx;
180 mat[5] = 2.0 * near / dy;
181 mat[8] = a;
182 mat[9] = b;
183 mat[10] = c;
184 mat[11] = -1.0;
185 mat[14] = d;
187 gl_mult_matrixf(mat);
188 }
190 void glu_perspective(float vfov, float aspect, float near, float far)
191 {
192 float x = near * tan(vfov / 2.0);
193 gl_frustum(-aspect * x, aspect * x, -x, x, near, far);
194 }
196 void gl_apply_xform(unsigned int prog)
197 {
198 int loc, mvidx, pidx, tidx, mvtop, ptop, ttop;
200 mvidx = MMODE_IDX(GL_MODELVIEW);
201 pidx = MMODE_IDX(GL_PROJECTION);
202 tidx = MMODE_IDX(GL_TEXTURE);
204 mvtop = stack_top[mvidx];
205 ptop = stack_top[pidx];
206 ttop = stack_top[tidx];
208 if((loc = glGetUniformLocation(prog, "matrix_modelview")) != -1) {
209 glUniformMatrix4fv(loc, 16, 0, mat_stack[mvidx][mvtop]);
210 }
212 if((loc = glGetUniformLocation(prog, "matrix_projection")) != -1) {
213 glUniformMatrix4fv(loc, 16, 0, mat_stack[pidx][ptop]);
214 }
216 if((loc = glGetUniformLocation(prog, "matrix_texture")) != -1) {
217 glUniformMatrix4fv(loc, 16, 0, mat_stack[tidx][ttop]);
218 }
220 if((loc = glGetUniformLocation(prog, "matrix_normal")) != -1) {
221 float nmat[9];
223 nmat[0] = mat_stack[mvidx][mvtop][0];
224 nmat[1] = mat_stack[mvidx][mvtop][1];
225 nmat[2] = mat_stack[mvidx][mvtop][2];
226 nmat[3] = mat_stack[mvidx][mvtop][4];
227 nmat[4] = mat_stack[mvidx][mvtop][5];
228 nmat[5] = mat_stack[mvidx][mvtop][6];
229 nmat[6] = mat_stack[mvidx][mvtop][8];
230 nmat[7] = mat_stack[mvidx][mvtop][9];
231 nmat[8] = mat_stack[mvidx][mvtop][10];
232 glUniformMatrix3fv(loc, 9, 0, nmat);
233 }
235 if((loc = glGetUniformLocation(prog, "matrix_modelview_projection")) != -1) {
236 if(!mvp_valid) {
237 /* TODO calc mvp */
238 }
239 glUniformMatrix4fv(loc, 16, 0, mat_mvp);
240 }
241 }