istereo
view src/sanegl.c @ 20:21d58290e634
forgot the cam files
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Wed, 07 Sep 2011 10:15:35 +0300 |
parents | bb68fac22579 |
children | 75a63f9ab7cc |
line source
1 /*
2 SaneGL - a small library to bring back sanity to OpenGL ES 2.x
3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <math.h>
23 #include <assert.h>
24 #include "sanegl.h"
26 #define MMODE_IDX(x) ((x) - GL_MODELVIEW)
27 #define MAT_STACK_SIZE 32
28 #define MAT_IDENT {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
30 #define MAX_VERTS 512
32 static void gl_draw_immediate(void);
34 typedef struct { float x, y; } vec2_t;
35 typedef struct { float x, y, z; } vec3_t;
36 typedef struct { float x, y, z, w; } vec4_t;
38 static int mm_idx = 0;
39 static float mat_stack[3][MAT_STACK_SIZE][16] = {{MAT_IDENT}, {MAT_IDENT}, {MAT_IDENT}};
40 static int stack_top[3];
41 static float mat_mvp[16];
42 static int mvp_valid;
43 static int prim = -1;
45 static vec3_t cur_normal;
46 static vec4_t cur_color, cur_attrib;
47 static vec2_t cur_texcoord;
49 static vec4_t *vert_arr, *col_arr, *attr_arr;
50 static vec3_t *norm_arr;
51 static vec2_t *texc_arr;
52 /*static unsigned int vbuf, cbuf, nbuf, tbuf, abuf;*/
53 static int vloc, nloc, cloc, tloc, aloc = -1;
55 static int num_verts, vert_calls;
56 static int cur_prog;
59 void gl_matrix_mode(int mm)
60 {
61 mm_idx = MMODE_IDX(mm);
62 }
64 void gl_push_matrix(void)
65 {
66 int top = stack_top[mm_idx];
68 memcpy(mat_stack[mm_idx][top + 1], mat_stack[mm_idx][top], 16 * sizeof(float));
69 stack_top[mm_idx]++;
70 mvp_valid = 0;
71 }
73 void gl_pop_matrix(void)
74 {
75 stack_top[mm_idx]--;
76 mvp_valid = 0;
77 }
79 void gl_load_identity(void)
80 {
81 static const float idmat[] = MAT_IDENT;
82 int top = stack_top[mm_idx];
83 float *mat = mat_stack[mm_idx][top];
85 memcpy(mat, idmat, sizeof idmat);
86 mvp_valid = 0;
87 }
89 void gl_load_matrixf(const float *m)
90 {
91 int top = stack_top[mm_idx];
92 float *mat = mat_stack[mm_idx][top];
94 memcpy(mat, m, 16 * sizeof *mat);
95 mvp_valid = 0;
96 }
98 #define M(i, j) ((i << 2) + j)
100 void gl_mult_matrixf(const float *m2)
101 {
102 int i, j;
103 int top = stack_top[mm_idx];
104 float *m1 = mat_stack[mm_idx][top];
105 float res[16];
107 for(i=0; i<4; i++) {
108 for(j=0; j<4; j++) {
109 res[M(i,j)] = m1[M(i,0)] * m2[M(0,j)] +
110 m1[M(i,1)] * m2[M(1,j)] +
111 m1[M(i,2)] * m2[M(2,j)] +
112 m1[M(i,3)] * m2[M(3,j)];
113 }
114 }
116 memcpy(m1, res, sizeof res);
117 mvp_valid = 0;
118 }
120 void gl_translatef(float x, float y, float z)
121 {
122 float mat[] = MAT_IDENT;
124 mat[12] = x;
125 mat[13] = y;
126 mat[14] = z;
128 gl_mult_matrixf(mat);
129 }
131 void gl_rotatef(float angle, float x, float y, float z)
132 {
133 float mat[] = MAT_IDENT;
135 float angle_rad = M_PI * angle / 180.0;
136 float sina = sin(angle_rad);
137 float cosa = cos(angle_rad);
138 float one_minus_cosa = 1.0 - cosa;
139 float nxsq = x * x;
140 float nysq = y * y;
141 float nzsq = z * z;
143 mat[0] = nxsq + (1.0 - nxsq) * cosa;
144 mat[4] = x * y * one_minus_cosa - z * sina;
145 mat[8] = x * z * one_minus_cosa + y * sina;
146 mat[1] = x * y * one_minus_cosa + z * sina;
147 mat[5] = nysq + (1.0 - nysq) * cosa;
148 mat[9] = y * z * one_minus_cosa - x * sina;
149 mat[2] = x * z * one_minus_cosa - y * sina;
150 mat[6] = y * z * one_minus_cosa + x * sina;
151 mat[10] = nzsq + (1.0 - nzsq) * cosa;
153 gl_mult_matrixf(mat);
154 }
156 void gl_scalef(float x, float y, float z)
157 {
158 float mat[] = MAT_IDENT;
160 mat[0] = x;
161 mat[5] = y;
162 mat[10] = z;
164 gl_mult_matrixf(mat);
165 }
167 void gl_ortho(float left, float right, float bottom, float top, float near, float far)
168 {
169 float mat[] = MAT_IDENT;
171 float dx = right - left;
172 float dy = top - bottom;
173 float dz = far - near;
175 float tx = -(right + left) / dx;
176 float ty = -(top + bottom) / dy;
177 float tz = -(far + near) / dz;
179 float sx = 2.0 / dx;
180 float sy = 2.0 / dy;
181 float sz = -2.0 / dz;
183 mat[0] = sx;
184 mat[5] = sy;
185 mat[10] = sz;
186 mat[12] = tx;
187 mat[13] = ty;
188 mat[14] = tz;
190 gl_mult_matrixf(mat);
191 }
193 void gl_frustum(float left, float right, float bottom, float top, float near, float far)
194 {
195 float mat[] = MAT_IDENT;
197 float dx = right - left;
198 float dy = top - bottom;
199 float dz = far - near;
201 float a = (right + left) / dx;
202 float b = (top + bottom) / dy;
203 float c = -(far + near) / dz;
204 float d = -2.0 * far * near / dz;
206 mat[0] = 2.0 * near / dx;
207 mat[5] = 2.0 * near / dy;
208 mat[8] = a;
209 mat[9] = b;
210 mat[10] = c;
211 mat[11] = -1.0;
212 mat[14] = d;
214 gl_mult_matrixf(mat);
215 }
217 void glu_perspective(float vfov, float aspect, float near, float far)
218 {
219 float x = near * tan(vfov / 2.0);
220 gl_frustum(-aspect * x, aspect * x, -x, x, near, far);
221 }
223 void gl_apply_xform(unsigned int prog)
224 {
225 int loc, mvidx, pidx, tidx, mvtop, ptop, ttop;
227 mvidx = MMODE_IDX(GL_MODELVIEW);
228 pidx = MMODE_IDX(GL_PROJECTION);
229 tidx = MMODE_IDX(GL_TEXTURE);
231 mvtop = stack_top[mvidx];
232 ptop = stack_top[pidx];
233 ttop = stack_top[tidx];
235 assert(prog);
237 if((loc = glGetUniformLocation(prog, "matrix_modelview")) != -1) {
238 glUniformMatrix4fv(loc, 1, 0, mat_stack[mvidx][mvtop]);
239 }
241 if((loc = glGetUniformLocation(prog, "matrix_projection")) != -1) {
242 glUniformMatrix4fv(loc, 1, 0, mat_stack[pidx][ptop]);
243 }
245 if((loc = glGetUniformLocation(prog, "matrix_texture")) != -1) {
246 glUniformMatrix4fv(loc, 1, 0, mat_stack[tidx][ttop]);
247 }
249 if((loc = glGetUniformLocation(prog, "matrix_normal")) != -1) {
250 float nmat[9];
252 nmat[0] = mat_stack[mvidx][mvtop][0];
253 nmat[1] = mat_stack[mvidx][mvtop][1];
254 nmat[2] = mat_stack[mvidx][mvtop][2];
255 nmat[3] = mat_stack[mvidx][mvtop][4];
256 nmat[4] = mat_stack[mvidx][mvtop][5];
257 nmat[5] = mat_stack[mvidx][mvtop][6];
258 nmat[6] = mat_stack[mvidx][mvtop][8];
259 nmat[7] = mat_stack[mvidx][mvtop][9];
260 nmat[8] = mat_stack[mvidx][mvtop][10];
261 glUniformMatrix3fv(loc, 1, 0, nmat);
262 }
264 if((loc = glGetUniformLocation(prog, "matrix_modelview_projection")) != -1) {
265 if(!mvp_valid) {
266 /* TODO calc mvp */
267 }
268 glUniformMatrix4fv(loc, 1, 0, mat_mvp);
269 }
270 }
273 /* immediate mode rendering */
274 void gl_begin(int p)
275 {
276 if(!vert_arr) {
277 vert_arr = malloc(MAX_VERTS * sizeof *vert_arr);
278 norm_arr = malloc(MAX_VERTS * sizeof *norm_arr);
279 texc_arr = malloc(MAX_VERTS * sizeof *texc_arr);
280 col_arr = malloc(MAX_VERTS * sizeof *col_arr);
281 attr_arr = malloc(MAX_VERTS * sizeof *attr_arr);
282 assert(vert_arr && norm_arr && texc_arr && col_arr && attr_arr);
283 }
285 prim = p;
286 num_verts = vert_calls = 0;
288 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_prog);
289 assert(cur_prog);
291 gl_apply_xform(cur_prog);
293 vloc = glGetAttribLocation(cur_prog, "attr_vertex");
294 nloc = glGetAttribLocation(cur_prog, "attr_normal");
295 cloc = glGetAttribLocation(cur_prog, "attr_color");
296 tloc = glGetAttribLocation(cur_prog, "attr_texcoord");
297 }
299 void gl_end(void)
300 {
301 if(num_verts > 0) {
302 gl_draw_immediate();
303 }
304 aloc = -1;
305 }
307 static void gl_draw_immediate(void)
308 {
309 int glprim;
311 if(vloc == -1) {
312 fprintf(stderr, "gl_draw_immediate call with vloc == -1\n");
313 return;
314 }
316 glprim = prim == GL_QUADS ? GL_TRIANGLES : prim;
318 glVertexAttribPointer(vloc, 4, GL_FLOAT, 0, 0, vert_arr);
319 glEnableVertexAttribArray(vloc);
321 if(nloc != -1) {
322 glVertexAttribPointer(nloc, 3, GL_FLOAT, 0, 0, norm_arr);
323 glEnableVertexAttribArray(nloc);
324 }
326 if(cloc != -1) {
327 glVertexAttribPointer(cloc, 4, GL_FLOAT, 0, 0, col_arr);
328 glEnableVertexAttribArray(cloc);
329 }
331 if(tloc != -1) {
332 glVertexAttribPointer(tloc, 2, GL_FLOAT, 0, 0, texc_arr);
333 glEnableVertexAttribArray(tloc);
334 }
336 if(aloc != -1) {
337 glVertexAttribPointer(aloc, 4, GL_FLOAT, 0, 0, attr_arr);
338 glEnableVertexAttribArray(aloc);
339 }
341 glDrawArrays(glprim, 0, num_verts);
343 glDisableVertexAttribArray(vloc);
344 if(nloc != -1) {
345 glDisableVertexAttribArray(nloc);
346 }
347 if(cloc != -1) {
348 glDisableVertexAttribArray(cloc);
349 }
350 if(tloc != -1) {
351 glDisableVertexAttribArray(tloc);
352 }
353 if(aloc != -1) {
354 glDisableVertexAttribArray(aloc);
355 }
356 }
359 void gl_vertex2f(float x, float y)
360 {
361 gl_vertex4f(x, y, 0.0f, 1.0f);
362 }
364 void gl_vertex3f(float x, float y, float z)
365 {
366 gl_vertex4f(x, y, z, 1.0f);
367 }
369 void gl_vertex4f(float x, float y, float z, float w)
370 {
371 int i, buffer_full;
373 if(prim == GL_QUADS && vert_calls % 4 == 3) {
374 for(i=0; i<2; i++) {
375 if(aloc != -1) {
376 attr_arr[num_verts] = attr_arr[num_verts - 3 + i];
377 }
378 if(cloc != -1) {
379 col_arr[num_verts] = col_arr[num_verts - 3 + i];
380 }
381 if(tloc != -1) {
382 texc_arr[num_verts] = texc_arr[num_verts - 3 + i];
383 }
384 if(nloc != -1) {
385 norm_arr[num_verts] = norm_arr[num_verts - 3 + i];
386 }
387 vert_arr[num_verts] = vert_arr[num_verts - 3 + i];
388 num_verts++;
389 }
390 }
392 vert_arr[num_verts].x = x;
393 vert_arr[num_verts].y = y;
394 vert_arr[num_verts].z = z;
395 vert_arr[num_verts].w = w;
397 if(cloc != -1) {
398 col_arr[num_verts] = cur_color;
399 }
400 if(nloc != -1) {
401 norm_arr[num_verts] = cur_normal;
402 }
403 if(tloc != -1) {
404 texc_arr[num_verts] = cur_texcoord;
405 }
406 if(aloc != -1) {
407 attr_arr[num_verts] = cur_attrib;
408 }
410 vert_calls++;
411 num_verts++;
413 if(prim == GL_QUADS) {
414 /* leave space for 6 more worst-case and don't allow flushes mid-quad */
415 buffer_full = num_verts >= MAX_VERTS - 6 && vert_calls % 4 == 0;
416 } else {
417 buffer_full = num_verts >= MAX_VERTS - prim;
418 }
420 if(buffer_full) {
421 gl_draw_immediate();
422 gl_begin(prim); /* reset everything */
423 }
424 }
427 void gl_normal3f(float x, float y, float z)
428 {
429 cur_normal.x = x;
430 cur_normal.y = y;
431 cur_normal.z = z;
432 }
435 void gl_color3f(float r, float g, float b)
436 {
437 cur_color.x = r;
438 cur_color.y = g;
439 cur_color.z = b;
440 cur_color.w = 1.0f;
441 }
443 void gl_color4f(float r, float g, float b, float a)
444 {
445 cur_color.x = r;
446 cur_color.y = g;
447 cur_color.z = b;
448 cur_color.w = a;
449 }
452 void gl_texcoord1f(float s)
453 {
454 cur_texcoord.x = s;
455 cur_texcoord.y = 0.0f;
456 }
458 void gl_texcoord2f(float s, float t)
459 {
460 cur_texcoord.x = s;
461 cur_texcoord.y = t;
462 }
464 void gl_vertex_attrib2f(int loc, float x, float y)
465 {
466 aloc = loc;
467 cur_attrib.x = x;
468 cur_attrib.y = y;
469 cur_attrib.z = 0.0f;
470 cur_attrib.w = 1.0f;
471 }
473 void gl_vertex_attrib3f(int loc, float x, float y, float z)
474 {
475 aloc = loc;
476 cur_attrib.x = x;
477 cur_attrib.y = y;
478 cur_attrib.z = z;
479 cur_attrib.w = 1.0f;
480 }
482 void gl_vertex_attrib4f(int loc, float x, float y, float z, float w)
483 {
484 aloc = loc;
485 cur_attrib.x = x;
486 cur_attrib.y = y;
487 cur_attrib.z = z;
488 cur_attrib.w = w;
489 }