istereo2

view src/sanegl.c @ 14:018f997dc646

button done
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 29 Sep 2015 01:11:54 +0300
parents 81d35769f546
children
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>
25 #include "opengl.h"
27 #ifdef GLDEF
28 #undef GLDEF
29 #endif
30 #include "sanegl.h"
32 #define MMODE_IDX(x) ((x) - GL_MODELVIEW)
33 #define MAT_STACK_SIZE 32
34 #define MAT_IDENT {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
36 #define MAX_VERTS 512
38 static void gl_draw_immediate(void);
40 typedef struct { float x, y; } vec2_t;
41 typedef struct { float x, y, z; } vec3_t;
42 typedef struct { float x, y, z, w; } vec4_t;
44 static int mm_idx = 0;
45 static float mat_stack[3][MAT_STACK_SIZE][16] = {{MAT_IDENT}, {MAT_IDENT}, {MAT_IDENT}};
46 static int stack_top[3];
47 static float mat_mvp[16];
48 static int mvp_valid;
49 static int prim = -1;
51 static vec3_t cur_normal;
52 static vec4_t cur_color, cur_attrib;
53 static vec2_t cur_texcoord;
55 static vec4_t *vert_arr, *col_arr, *attr_arr;
56 static vec3_t *norm_arr;
57 static vec2_t *texc_arr;
58 /*static unsigned int vbuf, cbuf, nbuf, tbuf, abuf;*/
59 static int vloc, nloc, cloc, tloc, aloc = -1;
61 static int num_verts, vert_calls;
62 static int cur_prog;
65 void gl_matrix_mode(int mm)
66 {
67 mm_idx = MMODE_IDX(mm);
68 }
70 void gl_push_matrix(void)
71 {
72 int top = stack_top[mm_idx];
74 memcpy(mat_stack[mm_idx][top + 1], mat_stack[mm_idx][top], 16 * sizeof(float));
75 stack_top[mm_idx]++;
76 mvp_valid = 0;
77 }
79 void gl_pop_matrix(void)
80 {
81 stack_top[mm_idx]--;
82 mvp_valid = 0;
83 }
85 void gl_load_identity(void)
86 {
87 static const float idmat[] = MAT_IDENT;
88 int top = stack_top[mm_idx];
89 float *mat = mat_stack[mm_idx][top];
91 memcpy(mat, idmat, sizeof idmat);
92 mvp_valid = 0;
93 }
95 void gl_load_matrixf(const float *m)
96 {
97 int top = stack_top[mm_idx];
98 float *mat = mat_stack[mm_idx][top];
100 memcpy(mat, m, 16 * sizeof *mat);
101 mvp_valid = 0;
102 }
104 #define M(i, j) ((i << 2) + j)
106 void gl_mult_matrixf(const float *m2)
107 {
108 int i, j;
109 int top = stack_top[mm_idx];
110 float *m1 = mat_stack[mm_idx][top];
111 float res[16];
113 for(i=0; i<4; i++) {
114 for(j=0; j<4; j++) {
115 res[M(i,j)] = m1[M(i,0)] * m2[M(0,j)] +
116 m1[M(i,1)] * m2[M(1,j)] +
117 m1[M(i,2)] * m2[M(2,j)] +
118 m1[M(i,3)] * m2[M(3,j)];
119 }
120 }
122 memcpy(m1, res, sizeof res);
123 mvp_valid = 0;
124 }
126 void gl_translatef(float x, float y, float z)
127 {
128 float mat[] = MAT_IDENT;
130 mat[12] = x;
131 mat[13] = y;
132 mat[14] = z;
134 gl_mult_matrixf(mat);
135 }
137 void gl_rotatef(float angle, float x, float y, float z)
138 {
139 float mat[] = MAT_IDENT;
141 float angle_rad = M_PI * angle / 180.0;
142 float sina = sin(angle_rad);
143 float cosa = cos(angle_rad);
144 float one_minus_cosa = 1.0 - cosa;
145 float nxsq = x * x;
146 float nysq = y * y;
147 float nzsq = z * z;
149 mat[0] = nxsq + (1.0 - nxsq) * cosa;
150 mat[4] = x * y * one_minus_cosa - z * sina;
151 mat[8] = x * z * one_minus_cosa + y * sina;
152 mat[1] = x * y * one_minus_cosa + z * sina;
153 mat[5] = nysq + (1.0 - nysq) * cosa;
154 mat[9] = y * z * one_minus_cosa - x * sina;
155 mat[2] = x * z * one_minus_cosa - y * sina;
156 mat[6] = y * z * one_minus_cosa + x * sina;
157 mat[10] = nzsq + (1.0 - nzsq) * cosa;
159 gl_mult_matrixf(mat);
160 }
162 void gl_scalef(float x, float y, float z)
163 {
164 float mat[] = MAT_IDENT;
166 mat[0] = x;
167 mat[5] = y;
168 mat[10] = z;
170 gl_mult_matrixf(mat);
171 }
173 void gl_ortho(float left, float right, float bottom, float top, float near, float far)
174 {
175 float mat[] = MAT_IDENT;
177 float dx = right - left;
178 float dy = top - bottom;
179 float dz = far - near;
181 float tx = -(right + left) / dx;
182 float ty = -(top + bottom) / dy;
183 float tz = -(far + near) / dz;
185 float sx = 2.0 / dx;
186 float sy = 2.0 / dy;
187 float sz = -2.0 / dz;
189 mat[0] = sx;
190 mat[5] = sy;
191 mat[10] = sz;
192 mat[12] = tx;
193 mat[13] = ty;
194 mat[14] = tz;
196 gl_mult_matrixf(mat);
197 }
199 void gl_frustum(float left, float right, float bottom, float top, float near, float far)
200 {
201 float mat[] = MAT_IDENT;
203 float dx = right - left;
204 float dy = top - bottom;
205 float dz = far - near;
207 float a = (right + left) / dx;
208 float b = (top + bottom) / dy;
209 float c = -(far + near) / dz;
210 float d = -2.0 * far * near / dz;
212 mat[0] = 2.0 * near / dx;
213 mat[5] = 2.0 * near / dy;
214 mat[8] = a;
215 mat[9] = b;
216 mat[10] = c;
217 mat[11] = -1.0;
218 mat[14] = d;
220 gl_mult_matrixf(mat);
221 }
223 void glu_perspective(float vfov, float aspect, float near, float far)
224 {
225 float vfov_rad = M_PI * vfov / 180.0;
226 float x = near * tan(vfov_rad / 2.0);
227 gl_frustum(-aspect * x, aspect * x, -x, x, near, far);
228 }
230 void gl_apply_xform(unsigned int prog)
231 {
232 int loc, mvidx, pidx, tidx, mvtop, ptop, ttop;
234 mvidx = MMODE_IDX(GL_MODELVIEW);
235 pidx = MMODE_IDX(GL_PROJECTION);
236 tidx = MMODE_IDX(GL_TEXTURE);
238 mvtop = stack_top[mvidx];
239 ptop = stack_top[pidx];
240 ttop = stack_top[tidx];
242 assert(prog);
244 if((loc = glGetUniformLocation(prog, "matrix_modelview")) != -1) {
245 glUniformMatrix4fv(loc, 1, 0, mat_stack[mvidx][mvtop]);
246 }
248 if((loc = glGetUniformLocation(prog, "matrix_projection")) != -1) {
249 glUniformMatrix4fv(loc, 1, 0, mat_stack[pidx][ptop]);
250 }
252 if((loc = glGetUniformLocation(prog, "matrix_texture")) != -1) {
253 glUniformMatrix4fv(loc, 1, 0, mat_stack[tidx][ttop]);
254 }
256 if((loc = glGetUniformLocation(prog, "matrix_normal")) != -1) {
257 float nmat[9];
259 nmat[0] = mat_stack[mvidx][mvtop][0];
260 nmat[1] = mat_stack[mvidx][mvtop][1];
261 nmat[2] = mat_stack[mvidx][mvtop][2];
262 nmat[3] = mat_stack[mvidx][mvtop][4];
263 nmat[4] = mat_stack[mvidx][mvtop][5];
264 nmat[5] = mat_stack[mvidx][mvtop][6];
265 nmat[6] = mat_stack[mvidx][mvtop][8];
266 nmat[7] = mat_stack[mvidx][mvtop][9];
267 nmat[8] = mat_stack[mvidx][mvtop][10];
268 glUniformMatrix3fv(loc, 1, 0, nmat);
269 }
271 if((loc = glGetUniformLocation(prog, "matrix_modelview_projection")) != -1) {
272 if(!mvp_valid) {
273 /* TODO calc mvp */
274 }
275 glUniformMatrix4fv(loc, 1, 0, mat_mvp);
276 }
277 }
280 /* immediate mode rendering */
281 void gl_begin(int p)
282 {
283 if(!vert_arr) {
284 vert_arr = malloc(MAX_VERTS * sizeof *vert_arr);
285 norm_arr = malloc(MAX_VERTS * sizeof *norm_arr);
286 texc_arr = malloc(MAX_VERTS * sizeof *texc_arr);
287 col_arr = malloc(MAX_VERTS * sizeof *col_arr);
288 attr_arr = malloc(MAX_VERTS * sizeof *attr_arr);
289 assert(vert_arr && norm_arr && texc_arr && col_arr && attr_arr);
290 }
292 prim = p;
293 num_verts = vert_calls = 0;
295 glGetIntegerv(GL_CURRENT_PROGRAM, &cur_prog);
296 assert(cur_prog);
298 gl_apply_xform(cur_prog);
300 vloc = glGetAttribLocation(cur_prog, "attr_vertex");
301 nloc = glGetAttribLocation(cur_prog, "attr_normal");
302 cloc = glGetAttribLocation(cur_prog, "attr_color");
303 tloc = glGetAttribLocation(cur_prog, "attr_texcoord");
304 }
306 void gl_end(void)
307 {
308 if(num_verts > 0) {
309 gl_draw_immediate();
310 }
311 aloc = -1;
312 }
314 static void gl_draw_immediate(void)
315 {
316 int glprim;
318 if(vloc == -1) {
319 fprintf(stderr, "gl_draw_immediate call with vloc == -1\n");
320 return;
321 }
323 glprim = prim == GL_QUADS ? GL_TRIANGLES : prim;
325 glVertexAttribPointer(vloc, 4, GL_FLOAT, 0, 0, vert_arr);
326 glEnableVertexAttribArray(vloc);
328 if(nloc != -1) {
329 glVertexAttribPointer(nloc, 3, GL_FLOAT, 0, 0, norm_arr);
330 glEnableVertexAttribArray(nloc);
331 }
333 if(cloc != -1) {
334 glVertexAttribPointer(cloc, 4, GL_FLOAT, 1, 0, col_arr);
335 glEnableVertexAttribArray(cloc);
336 }
338 if(tloc != -1) {
339 glVertexAttribPointer(tloc, 2, GL_FLOAT, 0, 0, texc_arr);
340 glEnableVertexAttribArray(tloc);
341 }
343 if(aloc != -1) {
344 glVertexAttribPointer(aloc, 4, GL_FLOAT, 0, 0, attr_arr);
345 glEnableVertexAttribArray(aloc);
346 }
348 glDrawArrays(glprim, 0, num_verts);
350 glDisableVertexAttribArray(vloc);
351 if(nloc != -1) {
352 glDisableVertexAttribArray(nloc);
353 }
354 if(cloc != -1) {
355 glDisableVertexAttribArray(cloc);
356 }
357 if(tloc != -1) {
358 glDisableVertexAttribArray(tloc);
359 }
360 if(aloc != -1) {
361 glDisableVertexAttribArray(aloc);
362 }
363 }
366 void gl_vertex2f(float x, float y)
367 {
368 gl_vertex4f(x, y, 0.0f, 1.0f);
369 }
371 void gl_vertex3f(float x, float y, float z)
372 {
373 gl_vertex4f(x, y, z, 1.0f);
374 }
376 void gl_vertex4f(float x, float y, float z, float w)
377 {
378 int i, buffer_full;
380 if(prim == GL_QUADS && vert_calls % 4 == 3) {
381 for(i=0; i<2; i++) {
382 if(aloc != -1) {
383 attr_arr[num_verts] = attr_arr[num_verts - 3 + i];
384 }
385 if(cloc != -1) {
386 col_arr[num_verts] = col_arr[num_verts - 3 + i];
387 }
388 if(tloc != -1) {
389 texc_arr[num_verts] = texc_arr[num_verts - 3 + i];
390 }
391 if(nloc != -1) {
392 norm_arr[num_verts] = norm_arr[num_verts - 3 + i];
393 }
394 vert_arr[num_verts] = vert_arr[num_verts - 3 + i];
395 num_verts++;
396 }
397 }
399 vert_arr[num_verts].x = x;
400 vert_arr[num_verts].y = y;
401 vert_arr[num_verts].z = z;
402 vert_arr[num_verts].w = w;
404 if(cloc != -1) {
405 col_arr[num_verts] = cur_color;
406 }
407 if(nloc != -1) {
408 norm_arr[num_verts] = cur_normal;
409 }
410 if(tloc != -1) {
411 texc_arr[num_verts] = cur_texcoord;
412 }
413 if(aloc != -1) {
414 attr_arr[num_verts] = cur_attrib;
415 }
417 vert_calls++;
418 num_verts++;
420 if(prim == GL_QUADS) {
421 /* leave space for 6 more worst-case and don't allow flushes mid-quad */
422 buffer_full = num_verts >= MAX_VERTS - 6 && vert_calls % 4 == 0;
423 } else {
424 buffer_full = num_verts >= MAX_VERTS - prim;
425 }
427 if(buffer_full) {
428 gl_draw_immediate();
429 gl_begin(prim); /* reset everything */
430 }
431 }
434 void gl_normal3f(float x, float y, float z)
435 {
436 cur_normal.x = x;
437 cur_normal.y = y;
438 cur_normal.z = z;
439 }
442 void gl_color3f(float r, float g, float b)
443 {
444 cur_color.x = r;
445 cur_color.y = g;
446 cur_color.z = b;
447 cur_color.w = 1.0f;
448 }
450 void gl_color4f(float r, float g, float b, float a)
451 {
452 cur_color.x = r;
453 cur_color.y = g;
454 cur_color.z = b;
455 cur_color.w = a;
456 }
459 void gl_texcoord1f(float s)
460 {
461 cur_texcoord.x = s;
462 cur_texcoord.y = 0.0f;
463 }
465 void gl_texcoord2f(float s, float t)
466 {
467 cur_texcoord.x = s;
468 cur_texcoord.y = t;
469 }
471 void gl_vertex_attrib2f(int loc, float x, float y)
472 {
473 aloc = loc;
474 cur_attrib.x = x;
475 cur_attrib.y = y;
476 cur_attrib.z = 0.0f;
477 cur_attrib.w = 1.0f;
478 }
480 void gl_vertex_attrib3f(int loc, float x, float y, float z)
481 {
482 aloc = loc;
483 cur_attrib.x = x;
484 cur_attrib.y = y;
485 cur_attrib.z = z;
486 cur_attrib.w = 1.0f;
487 }
489 void gl_vertex_attrib4f(int loc, float x, float y, float z, float w)
490 {
491 aloc = loc;
492 cur_attrib.x = x;
493 cur_attrib.y = y;
494 cur_attrib.z = z;
495 cur_attrib.w = w;
496 }