istereo

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