istereo

view src/sanegl.c @ 21:75a63f9ab7cc

ha!
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 07 Sep 2011 10:49:11 +0300
parents 2c5620f0670c
children fd39c0198935
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 vfov_rad = M_PI * vfov / 180.0;
220 float x = near * tan(vfov_rad / 2.0);
221 gl_frustum(-aspect * x, aspect * x, -x, x, near, far);
222 }
224 void gl_apply_xform(unsigned int prog)
225 {
226 int loc, mvidx, pidx, tidx, mvtop, ptop, ttop;
228 mvidx = MMODE_IDX(GL_MODELVIEW);
229 pidx = MMODE_IDX(GL_PROJECTION);
230 tidx = MMODE_IDX(GL_TEXTURE);
232 mvtop = stack_top[mvidx];
233 ptop = stack_top[pidx];
234 ttop = stack_top[tidx];
236 assert(prog);
238 if((loc = glGetUniformLocation(prog, "matrix_modelview")) != -1) {
239 glUniformMatrix4fv(loc, 1, 0, mat_stack[mvidx][mvtop]);
240 }
242 if((loc = glGetUniformLocation(prog, "matrix_projection")) != -1) {
243 glUniformMatrix4fv(loc, 1, 0, mat_stack[pidx][ptop]);
244 }
246 if((loc = glGetUniformLocation(prog, "matrix_texture")) != -1) {
247 glUniformMatrix4fv(loc, 1, 0, mat_stack[tidx][ttop]);
248 }
250 if((loc = glGetUniformLocation(prog, "matrix_normal")) != -1) {
251 float nmat[9];
253 nmat[0] = mat_stack[mvidx][mvtop][0];
254 nmat[1] = mat_stack[mvidx][mvtop][1];
255 nmat[2] = mat_stack[mvidx][mvtop][2];
256 nmat[3] = mat_stack[mvidx][mvtop][4];
257 nmat[4] = mat_stack[mvidx][mvtop][5];
258 nmat[5] = mat_stack[mvidx][mvtop][6];
259 nmat[6] = mat_stack[mvidx][mvtop][8];
260 nmat[7] = mat_stack[mvidx][mvtop][9];
261 nmat[8] = mat_stack[mvidx][mvtop][10];
262 glUniformMatrix3fv(loc, 1, 0, nmat);
263 }
265 if((loc = glGetUniformLocation(prog, "matrix_modelview_projection")) != -1) {
266 if(!mvp_valid) {
267 /* TODO calc mvp */
268 }
269 glUniformMatrix4fv(loc, 1, 0, mat_mvp);
270 }
271 }
274 /* immediate mode rendering */
275 void gl_begin(int p)
276 {
277 if(!vert_arr) {
278 vert_arr = malloc(MAX_VERTS * sizeof *vert_arr);
279 norm_arr = malloc(MAX_VERTS * sizeof *norm_arr);
280 texc_arr = malloc(MAX_VERTS * sizeof *texc_arr);
281 col_arr = malloc(MAX_VERTS * sizeof *col_arr);
282 attr_arr = malloc(MAX_VERTS * sizeof *attr_arr);
283 assert(vert_arr && norm_arr && texc_arr && col_arr && attr_arr);
284 }
286 prim = p;
287 num_verts = vert_calls = 0;
289 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_prog);
290 assert(cur_prog);
292 gl_apply_xform(cur_prog);
294 vloc = glGetAttribLocation(cur_prog, "attr_vertex");
295 nloc = glGetAttribLocation(cur_prog, "attr_normal");
296 cloc = glGetAttribLocation(cur_prog, "attr_color");
297 tloc = glGetAttribLocation(cur_prog, "attr_texcoord");
298 }
300 void gl_end(void)
301 {
302 if(num_verts > 0) {
303 gl_draw_immediate();
304 }
305 aloc = -1;
306 }
308 static void gl_draw_immediate(void)
309 {
310 int glprim;
312 if(vloc == -1) {
313 fprintf(stderr, "gl_draw_immediate call with vloc == -1\n");
314 return;
315 }
317 glprim = prim == GL_QUADS ? GL_TRIANGLES : prim;
319 glVertexAttribPointer(vloc, 4, GL_FLOAT, 0, 0, vert_arr);
320 glEnableVertexAttribArray(vloc);
322 if(nloc != -1) {
323 glVertexAttribPointer(nloc, 3, GL_FLOAT, 0, 0, norm_arr);
324 glEnableVertexAttribArray(nloc);
325 }
327 if(cloc != -1) {
328 glVertexAttribPointer(cloc, 4, GL_FLOAT, 0, 0, col_arr);
329 glEnableVertexAttribArray(cloc);
330 }
332 if(tloc != -1) {
333 glVertexAttribPointer(tloc, 2, GL_FLOAT, 0, 0, texc_arr);
334 glEnableVertexAttribArray(tloc);
335 }
337 if(aloc != -1) {
338 glVertexAttribPointer(aloc, 4, GL_FLOAT, 0, 0, attr_arr);
339 glEnableVertexAttribArray(aloc);
340 }
342 glDrawArrays(glprim, 0, num_verts);
344 glDisableVertexAttribArray(vloc);
345 if(nloc != -1) {
346 glDisableVertexAttribArray(nloc);
347 }
348 if(cloc != -1) {
349 glDisableVertexAttribArray(cloc);
350 }
351 if(tloc != -1) {
352 glDisableVertexAttribArray(tloc);
353 }
354 if(aloc != -1) {
355 glDisableVertexAttribArray(aloc);
356 }
357 }
360 void gl_vertex2f(float x, float y)
361 {
362 gl_vertex4f(x, y, 0.0f, 1.0f);
363 }
365 void gl_vertex3f(float x, float y, float z)
366 {
367 gl_vertex4f(x, y, z, 1.0f);
368 }
370 void gl_vertex4f(float x, float y, float z, float w)
371 {
372 int i, buffer_full;
374 if(prim == GL_QUADS && vert_calls % 4 == 3) {
375 for(i=0; i<2; i++) {
376 if(aloc != -1) {
377 attr_arr[num_verts] = attr_arr[num_verts - 3 + i];
378 }
379 if(cloc != -1) {
380 col_arr[num_verts] = col_arr[num_verts - 3 + i];
381 }
382 if(tloc != -1) {
383 texc_arr[num_verts] = texc_arr[num_verts - 3 + i];
384 }
385 if(nloc != -1) {
386 norm_arr[num_verts] = norm_arr[num_verts - 3 + i];
387 }
388 vert_arr[num_verts] = vert_arr[num_verts - 3 + i];
389 num_verts++;
390 }
391 }
393 vert_arr[num_verts].x = x;
394 vert_arr[num_verts].y = y;
395 vert_arr[num_verts].z = z;
396 vert_arr[num_verts].w = w;
398 if(cloc != -1) {
399 col_arr[num_verts] = cur_color;
400 }
401 if(nloc != -1) {
402 norm_arr[num_verts] = cur_normal;
403 }
404 if(tloc != -1) {
405 texc_arr[num_verts] = cur_texcoord;
406 }
407 if(aloc != -1) {
408 attr_arr[num_verts] = cur_attrib;
409 }
411 vert_calls++;
412 num_verts++;
414 if(prim == GL_QUADS) {
415 /* leave space for 6 more worst-case and don't allow flushes mid-quad */
416 buffer_full = num_verts >= MAX_VERTS - 6 && vert_calls % 4 == 0;
417 } else {
418 buffer_full = num_verts >= MAX_VERTS - prim;
419 }
421 if(buffer_full) {
422 gl_draw_immediate();
423 gl_begin(prim); /* reset everything */
424 }
425 }
428 void gl_normal3f(float x, float y, float z)
429 {
430 cur_normal.x = x;
431 cur_normal.y = y;
432 cur_normal.z = z;
433 }
436 void gl_color3f(float r, float g, float b)
437 {
438 cur_color.x = r;
439 cur_color.y = g;
440 cur_color.z = b;
441 cur_color.w = 1.0f;
442 }
444 void gl_color4f(float r, float g, float b, float a)
445 {
446 cur_color.x = r;
447 cur_color.y = g;
448 cur_color.z = b;
449 cur_color.w = a;
450 }
453 void gl_texcoord1f(float s)
454 {
455 cur_texcoord.x = s;
456 cur_texcoord.y = 0.0f;
457 }
459 void gl_texcoord2f(float s, float t)
460 {
461 cur_texcoord.x = s;
462 cur_texcoord.y = t;
463 }
465 void gl_vertex_attrib2f(int loc, float x, float y)
466 {
467 aloc = loc;
468 cur_attrib.x = x;
469 cur_attrib.y = y;
470 cur_attrib.z = 0.0f;
471 cur_attrib.w = 1.0f;
472 }
474 void gl_vertex_attrib3f(int loc, float x, float y, float z)
475 {
476 aloc = loc;
477 cur_attrib.x = x;
478 cur_attrib.y = y;
479 cur_attrib.z = z;
480 cur_attrib.w = 1.0f;
481 }
483 void gl_vertex_attrib4f(int loc, float x, float y, float z, float w)
484 {
485 aloc = loc;
486 cur_attrib.x = x;
487 cur_attrib.y = y;
488 cur_attrib.z = z;
489 cur_attrib.w = w;
490 }