istereo

view src/sanegl.c @ 2:bb68fac22579

sanegl and shit
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 07 Sep 2011 02:48:35 +0300
parents 4d25539806d2
children 2c5620f0670c
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 <string.h>
21 #include <math.h>
22 #include <assert.h>
23 #include "sanegl.h"
25 #define MMODE_IDX(x) ((x) - GL_MODELVIEW)
26 #define MAT_STACK_SIZE 32
27 #define MAT_IDENT {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
29 #define MAX_VERTS 512
31 static void gl_draw_immediate(void);
33 typedef struct { float x, y; } vec2_t;
34 typedef struct { float x, y, z; } vec3_t;
35 typedef struct { float x, y, z, w; } vec4_t;
37 static int mm_idx = 0;
38 static float mat_stack[3][MAT_STACK_SIZE][16] = {{MAT_IDENT}, {MAT_IDENT}, {MAT_IDENT}};
39 static int stack_top[3];
40 static float mat_mvp[16];
41 static int mvp_valid;
42 static int prim = -1;
44 static vec3_t cur_normal;
45 static vec4_t cur_color, cur_attrib;
46 static vec2_t cur_texcoord;
48 static vec4_t *vert_arr, *col_arr, *attr_arr;
49 static vec3_t *norm_arr;
50 static vec2_t *texc_arr;
51 /*static unsigned int vbuf, cbuf, nbuf, tbuf, abuf;*/
52 static int vloc, nloc, cloc, tloc, aloc = -1;
54 static int num_verts, vert_calls;
55 static int cur_prog;
58 void gl_matrix_mode(int mm)
59 {
60 mm_idx = MMODE_IDX(mm);
61 }
63 void gl_push_matrix(void)
64 {
65 int top = stack_top[mm_idx];
67 memcpy(mat_stack[mm_idx][top + 1], mat_stack[mm_idx][top], 16 * sizeof(float));
68 stack_top[mm_idx]++;
69 mvp_valid = 0;
70 }
72 void gl_pop_matrix(void)
73 {
74 stack_top[mm_idx]--;
75 mvp_valid = 0;
76 }
78 void gl_load_identity(void)
79 {
80 static const float idmat[] = MAT_IDENT;
81 int top = stack_top[mm_idx];
82 float *mat = mat_stack[mm_idx][top];
84 memcpy(mat, idmat, sizeof idmat);
85 mvp_valid = 0;
86 }
88 void gl_load_matrixf(const float *m)
89 {
90 int top = stack_top[mm_idx];
91 float *mat = mat_stack[mm_idx][top];
93 memcpy(mat, m, 16 * sizeof *mat);
94 mvp_valid = 0;
95 }
97 #define M(i, j) ((i << 2) + j)
99 void gl_mult_matrixf(const float *m2)
100 {
101 int i, j;
102 int top = stack_top[mm_idx];
103 float *m1 = mat_stack[mm_idx][top];
104 float res[16];
106 for(i=0; i<4; i++) {
107 for(j=0; j<4; j++) {
108 res[M(i,j)] = m1[M(i,0)] * m2[M(0,j)] +
109 m1[M(i,1)] * m2[M(1,j)] +
110 m1[M(i,2)] * m2[M(2,j)] +
111 m1[M(i,3)] * m2[M(3,j)];
112 }
113 }
115 memcpy(m1, res, sizeof res);
116 mvp_valid = 0;
117 }
119 void gl_translatef(float x, float y, float z)
120 {
121 float mat[] = MAT_IDENT;
123 mat[12] = x;
124 mat[13] = y;
125 mat[14] = z;
127 gl_mult_matrixf(mat);
128 }
130 void gl_rotatef(float angle, float x, float y, float z)
131 {
132 float mat[] = MAT_IDENT;
134 float angle_rad = M_PI * angle / 180.0;
135 float sina = sin(angle_rad);
136 float cosa = cos(angle_rad);
137 float one_minus_cosa = 1.0 - cosa;
138 float nxsq = x * x;
139 float nysq = y * y;
140 float nzsq = z * z;
142 mat[0] = nxsq + (1.0 - nxsq) * cosa;
143 mat[4] = x * y * one_minus_cosa - z * sina;
144 mat[8] = x * z * one_minus_cosa + y * sina;
145 mat[1] = x * y * one_minus_cosa + z * sina;
146 mat[5] = nysq + (1.0 - nysq) * cosa;
147 mat[9] = y * z * one_minus_cosa - x * sina;
148 mat[2] = x * z * one_minus_cosa - y * sina;
149 mat[6] = y * z * one_minus_cosa + x * sina;
150 mat[10] = nzsq + (1.0 - nzsq) * cosa;
152 gl_mult_matrixf(mat);
153 }
155 void gl_scalef(float x, float y, float z)
156 {
157 float mat[] = MAT_IDENT;
159 mat[0] = x;
160 mat[5] = y;
161 mat[10] = z;
163 gl_mult_matrixf(mat);
164 }
166 void gl_ortho(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 tx = -(right + left) / dx;
175 float ty = -(top + bottom) / dy;
176 float tz = -(far + near) / dz;
178 float sx = 2.0 / dx;
179 float sy = 2.0 / dy;
180 float sz = -2.0 / dz;
182 mat[0] = sx;
183 mat[5] = sy;
184 mat[10] = sz;
185 mat[12] = tx;
186 mat[13] = ty;
187 mat[14] = tz;
189 gl_mult_matrixf(mat);
190 }
192 void gl_frustum(float left, float right, float bottom, float top, float near, float far)
193 {
194 float mat[] = MAT_IDENT;
196 float dx = right - left;
197 float dy = top - bottom;
198 float dz = far - near;
200 float a = (right + left) / dx;
201 float b = (top + bottom) / dy;
202 float c = -(far + near) / dz;
203 float d = -2.0 * far * near / dz;
205 mat[0] = 2.0 * near / dx;
206 mat[5] = 2.0 * near / dy;
207 mat[8] = a;
208 mat[9] = b;
209 mat[10] = c;
210 mat[11] = -1.0;
211 mat[14] = d;
213 gl_mult_matrixf(mat);
214 }
216 void glu_perspective(float vfov, float aspect, float near, float far)
217 {
218 float x = near * tan(vfov / 2.0);
219 gl_frustum(-aspect * x, aspect * x, -x, x, near, far);
220 }
222 void gl_apply_xform(unsigned int prog)
223 {
224 int loc, mvidx, pidx, tidx, mvtop, ptop, ttop;
226 mvidx = MMODE_IDX(GL_MODELVIEW);
227 pidx = MMODE_IDX(GL_PROJECTION);
228 tidx = MMODE_IDX(GL_TEXTURE);
230 mvtop = stack_top[mvidx];
231 ptop = stack_top[pidx];
232 ttop = stack_top[tidx];
234 assert(prog);
236 if((loc = glGetUniformLocation(prog, "matrix_modelview")) != -1) {
237 glUniformMatrix4fv(loc, 1, 0, mat_stack[mvidx][mvtop]);
238 }
240 if((loc = glGetUniformLocation(prog, "matrix_projection")) != -1) {
241 glUniformMatrix4fv(loc, 1, 0, mat_stack[pidx][ptop]);
242 }
244 if((loc = glGetUniformLocation(prog, "matrix_texture")) != -1) {
245 glUniformMatrix4fv(loc, 1, 0, mat_stack[tidx][ttop]);
246 }
248 if((loc = glGetUniformLocation(prog, "matrix_normal")) != -1) {
249 float nmat[9];
251 nmat[0] = mat_stack[mvidx][mvtop][0];
252 nmat[1] = mat_stack[mvidx][mvtop][1];
253 nmat[2] = mat_stack[mvidx][mvtop][2];
254 nmat[3] = mat_stack[mvidx][mvtop][4];
255 nmat[4] = mat_stack[mvidx][mvtop][5];
256 nmat[5] = mat_stack[mvidx][mvtop][6];
257 nmat[6] = mat_stack[mvidx][mvtop][8];
258 nmat[7] = mat_stack[mvidx][mvtop][9];
259 nmat[8] = mat_stack[mvidx][mvtop][10];
260 glUniformMatrix3fv(loc, 1, 0, nmat);
261 }
263 if((loc = glGetUniformLocation(prog, "matrix_modelview_projection")) != -1) {
264 if(!mvp_valid) {
265 /* TODO calc mvp */
266 }
267 glUniformMatrix4fv(loc, 1, 0, mat_mvp);
268 }
269 }
272 /* immediate mode rendering */
273 void gl_begin(int p)
274 {
275 if(!vert_arr) {
276 vert_arr = malloc(MAX_VERTS * sizeof *vert_arr);
277 norm_arr = malloc(MAX_VERTS * sizeof *norm_arr);
278 texc_arr = malloc(MAX_VERTS * sizeof *texc_arr);
279 col_arr = malloc(MAX_VERTS * sizeof *col_arr);
280 attr_arr = malloc(MAX_VERTS * sizeof *attr_arr);
281 assert(vert_arr && norm_arr && texc_arr && col_arr && attr_arr);
282 }
284 prim = p;
285 num_verts = vert_calls = 0;
287 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_prog);
288 assert(cur_prog);
290 gl_apply_xform(cur_prog);
292 vloc = glGetAttribLocation(cur_prog, "attr_vertex");
293 nloc = glGetAttribLocation(cur_prog, "attr_normal");
294 cloc = glGetAttribLocation(cur_prog, "attr_color");
295 tloc = glGetAttribLocation(cur_prog, "attr_texcoord");
296 }
298 void gl_end(void)
299 {
300 if(num_verts > 0) {
301 gl_draw_immediate();
302 }
303 aloc = -1;
304 }
306 static void gl_draw_immediate(void)
307 {
308 int glprim;
310 if(vloc == -1) {
311 fprintf(stderr, "gl_draw_immediate call with vloc == -1\n");
312 return;
313 }
315 glprim = prim == GL_QUADS ? GL_TRIANGLES : prim;
317 glVertexAttribPointer(vloc, 4, GL_FLOAT, 0, 0, vert_arr);
318 glEnableVertexAttribArray(vloc);
320 if(nloc != -1) {
321 glVertexAttribPointer(nloc, 3, GL_FLOAT, 0, 0, norm_arr);
322 glEnableVertexAttribArray(nloc);
323 }
325 if(cloc != -1) {
326 glVertexAttribPointer(cloc, 4, GL_FLOAT, 0, 0, col_arr);
327 glEnableVertexAttribArray(cloc);
328 }
330 if(tloc != -1) {
331 glVertexAttribPointer(tloc, 2, GL_FLOAT, 0, 0, texc_arr);
332 glEnableVertexAttribArray(tloc);
333 }
335 if(aloc != -1) {
336 glVertexAttribPointer(aloc, 4, GL_FLOAT, 0, 0, attr_arr);
337 glEnableVertexAttribArray(aloc);
338 }
340 glDrawArrays(glprim, 0, num_verts);
342 glDisableVertexAttribArray(vloc);
343 if(nloc != -1) {
344 glDisableVertexAttribArray(nloc);
345 }
346 if(cloc != -1) {
347 glDisableVertexAttribArray(cloc);
348 }
349 if(tloc != -1) {
350 glDisableVertexAttribArray(tloc);
351 }
352 if(aloc != -1) {
353 glDisableVertexAttribArray(aloc);
354 }
355 }
358 void gl_vertex2f(float x, float y)
359 {
360 gl_vertex4f(x, y, 0.0f, 1.0f);
361 }
363 void gl_vertex3f(float x, float y, float z)
364 {
365 gl_vertex4f(x, y, z, 1.0f);
366 }
368 void gl_vertex4f(float x, float y, float z, float w)
369 {
370 int i, buffer_full;
372 if(prim == GL_QUADS && vert_calls % 4 == 3) {
373 for(i=0; i<2; i++) {
374 if(aloc != -1) {
375 attr_arr[num_verts] = attr_arr[num_verts - 3 + i];
376 }
377 if(cloc != -1) {
378 col_arr[num_verts] = col_arr[num_verts - 3 + i];
379 }
380 if(tloc != -1) {
381 texc_arr[num_verts] = texc_arr[num_verts - 3 + i];
382 }
383 if(nloc != -1) {
384 norm_arr[num_verts] = norm_arr[num_verts - 3 + i];
385 }
386 vert_arr[num_verts] = vert_arr[num_verts - 3 + i];
387 num_verts++;
388 }
389 }
391 vert_arr[num_verts].x = x;
392 vert_arr[num_verts].y = y;
393 vert_arr[num_verts].z = z;
394 vert_arr[num_verts].w = w;
396 if(cloc != -1) {
397 col_arr[num_verts] = cur_color;
398 }
399 if(nloc != -1) {
400 norm_arr[num_verts] = cur_normal;
401 }
402 if(tloc != -1) {
403 texc_arr[num_verts] = cur_texcoord;
404 }
405 if(aloc != -1) {
406 attr_arr[num_verts] = cur_attrib;
407 }
409 vert_calls++;
410 num_verts++;
412 if(prim == GL_QUADS) {
413 /* leave space for 6 more worst-case and don't allow flushes mid-quad */
414 buffer_full = num_verts >= MAX_VERTS - 6 && vert_calls % 4 == 0;
415 } else {
416 buffer_full = num_verts >= MAX_VERTS - prim;
417 }
419 if(buffer_full) {
420 gl_draw_immediate();
421 glBegin(prim); /* reset everything */
422 }
423 }
426 void gl_normal3f(float x, float y, float z)
427 {
428 cur_normal.x = x;
429 cur_normal.y = y;
430 cur_normal.z = z;
431 }
434 void gl_color3f(float r, float g, float b)
435 {
436 cur_color.x = r;
437 cur_color.y = g;
438 cur_color.z = b;
439 cur_color.w = 1.0f;
440 }
442 void gl_color4f(float r, float g, float b, float a)
443 {
444 cur_color.x = r;
445 cur_color.y = g;
446 cur_color.z = b;
447 cur_color.w = a;
448 }
451 void gl_texcoord1f(float s)
452 {
453 cur_texcoord.x = s;
454 cur_texcoord.y = 0.0f;
455 }
457 void gl_texcoord2f(float s, float t)
458 {
459 cur_texcoord.x = s;
460 cur_texcoord.y = t;
461 }
463 void gl_vertex_attrib2f(int loc, float x, float y)
464 {
465 aloc = loc;
466 cur_attrib.x = x;
467 cur_attrib.y = y;
468 cur_attrib.z = 0.0f;
469 cur_attrib.w = 1.0f;
470 }
472 void gl_vertex_attrib3f(int loc, float x, float y, float z)
473 {
474 aloc = loc;
475 cur_attrib.x = x;
476 cur_attrib.y = y;
477 cur_attrib.z = z;
478 cur_attrib.w = 1.0f;
479 }
481 void gl_vertex_attrib4f(int loc, float x, float y, float z, float w)
482 {
483 aloc = loc;
484 cur_attrib.x = x;
485 cur_attrib.y = y;
486 cur_attrib.z = z;
487 cur_attrib.w = w;
488 }