# HG changeset patch # User John Tsiombikas # Date 1315352915 -10800 # Node ID bb68fac225795576df27416b5fbe2e1c8af444d4 # Parent 4d25539806d2bc2ed28e4424a563c93a442a5f22 sanegl and shit diff -r 4d25539806d2 -r bb68fac22579 sdr/test.p.glsl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sdr/test.p.glsl Wed Sep 07 02:48:35 2011 +0300 @@ -0,0 +1,6 @@ +varying vec4 var_color; + +void main() +{ + gl_FragColor = var_color; +} diff -r 4d25539806d2 -r bb68fac22579 sdr/test.v.glsl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sdr/test.v.glsl Wed Sep 07 02:48:35 2011 +0300 @@ -0,0 +1,12 @@ +uniform mat4 matrix_modelview, matrix_projection; + +attribute vec4 attr_vertex, attr_color; + +varying vec4 var_color; + +void main() +{ + mat4 mvp = matrix_projection * matrix_modelview; + gl_Position = mvp * attr_vertex; + var_color = attr_color; +} diff -r 4d25539806d2 -r bb68fac22579 src/glutmain.c --- a/src/glutmain.c Tue Sep 06 12:48:39 2011 +0300 +++ b/src/glutmain.c Wed Sep 07 02:48:35 2011 +0300 @@ -3,9 +3,10 @@ #include #include #include "sanegl.h" +#include "istereo.h" +#include "sdr.h" void disp(void); -void reshape(int x, int y); void keyb(unsigned char key, int x, int y); int main(int argc, char **argv) @@ -20,41 +21,23 @@ glutReshapeFunc(reshape); glutKeyboardFunc(keyb); + glewInit(); + + if(init() == -1) { + return 1; + } + glutMainLoop(); return 0; } void disp(void) { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glTranslatef(0, 0, -8); - - glBegin(GL_QUADS); - glColor3f(1, 0, 0); - glVertex3f(-1, -1, 0); - glColor3f(0, 1, 0); - glVertex3f(1, -1, 0); - glColor3f(0, 0, 1); - glVertex3f(1, 1, 0); - glColor3f(1, 1, 0); - glVertex3f(-1, 1, 0); - glEnd(); + redraw(); glutSwapBuffers(); } -void reshape(int x, int y) -{ - glViewport(0, 0, x, y); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0); -} - void keyb(unsigned char key, int x, int y) { switch(key) { diff -r 4d25539806d2 -r bb68fac22579 src/istereo.c --- a/src/istereo.c Tue Sep 06 12:48:39 2011 +0300 +++ b/src/istereo.c Wed Sep 07 02:48:35 2011 +0300 @@ -1,17 +1,58 @@ +#include +#include #include "opengl.h" #include "istereo.h" +#include "sanegl.h" +#include "sdr.h" + +unsigned int prog; int init(void) { + if(!(prog = create_program_load("sdr/test.v.glsl", "sdr/test.p.glsl"))) { + fprintf(stderr, "failed to load shader program\n"); + return -1; + } + return 0; } void cleanup(void) { + free_program(prog); } void redraw(void) { - glClearColor(0, 1, 0, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + bind_program(prog); + + gl_matrix_mode(GL_MODELVIEW); + gl_load_identity(); + gl_translatef(0, 0, -8); + + gl_apply_xform(prog); + + gl_begin(GL_QUADS); + gl_color3f(1, 0, 0); + gl_vertex3f(-1, -1, 0); + gl_color3f(0, 1, 0); + gl_vertex3f(1, -1, 0); + gl_color3f(0, 0, 1); + gl_vertex3f(1, 1, 0); + gl_color3f(1, 1, 0); + gl_vertex3f(-1, 1, 0); + gl_end(); + + assert(glGetError() == GL_NO_ERROR); } + +void reshape(int x, int y) +{ + glViewport(0, 0, x, y); + + gl_matrix_mode(GL_PROJECTION); + gl_load_identity(); + glu_perspective(45.0, (float)x / (float)y, 1.0, 1000.0); +} diff -r 4d25539806d2 -r bb68fac22579 src/istereo.h --- a/src/istereo.h Tue Sep 06 12:48:39 2011 +0300 +++ b/src/istereo.h Wed Sep 07 02:48:35 2011 +0300 @@ -4,5 +4,6 @@ int init(void); void cleanup(void); void redraw(void); +void reshape(int x, int y); #endif /* ISTEREO_H_ */ diff -r 4d25539806d2 -r bb68fac22579 src/sanegl.c --- a/src/sanegl.c Tue Sep 06 12:48:39 2011 +0300 +++ b/src/sanegl.c Wed Sep 07 02:48:35 2011 +0300 @@ -1,5 +1,25 @@ +/* +SaneGL - a small library to bring back sanity to OpenGL ES 2.x +Copyright (C) 2011 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include +#include #include -#include +#include #include "sanegl.h" #define MMODE_IDX(x) ((x) - GL_MODELVIEW) @@ -8,6 +28,8 @@ #define MAX_VERTS 512 +static void gl_draw_immediate(void); + typedef struct { float x, y; } vec2_t; typedef struct { float x, y, z; } vec3_t; typedef struct { float x, y, z, w; } vec4_t; @@ -26,7 +48,11 @@ static vec4_t *vert_arr, *col_arr, *attr_arr; static vec3_t *norm_arr; static vec2_t *texc_arr; -static int vloc, nloc, cloc, tloc, aloc; +/*static unsigned int vbuf, cbuf, nbuf, tbuf, abuf;*/ +static int vloc, nloc, cloc, tloc, aloc = -1; + +static int num_verts, vert_calls; +static int cur_prog; void gl_matrix_mode(int mm) @@ -205,16 +231,18 @@ ptop = stack_top[pidx]; ttop = stack_top[tidx]; + assert(prog); + if((loc = glGetUniformLocation(prog, "matrix_modelview")) != -1) { - glUniformMatrix4fv(loc, 16, 0, mat_stack[mvidx][mvtop]); + glUniformMatrix4fv(loc, 1, 0, mat_stack[mvidx][mvtop]); } if((loc = glGetUniformLocation(prog, "matrix_projection")) != -1) { - glUniformMatrix4fv(loc, 16, 0, mat_stack[pidx][ptop]); + glUniformMatrix4fv(loc, 1, 0, mat_stack[pidx][ptop]); } if((loc = glGetUniformLocation(prog, "matrix_texture")) != -1) { - glUniformMatrix4fv(loc, 16, 0, mat_stack[tidx][ttop]); + glUniformMatrix4fv(loc, 1, 0, mat_stack[tidx][ttop]); } if((loc = glGetUniformLocation(prog, "matrix_normal")) != -1) { @@ -229,13 +257,232 @@ nmat[6] = mat_stack[mvidx][mvtop][8]; nmat[7] = mat_stack[mvidx][mvtop][9]; nmat[8] = mat_stack[mvidx][mvtop][10]; - glUniformMatrix3fv(loc, 9, 0, nmat); + glUniformMatrix3fv(loc, 1, 0, nmat); } if((loc = glGetUniformLocation(prog, "matrix_modelview_projection")) != -1) { if(!mvp_valid) { /* TODO calc mvp */ } - glUniformMatrix4fv(loc, 16, 0, mat_mvp); + glUniformMatrix4fv(loc, 1, 0, mat_mvp); } } + + +/* immediate mode rendering */ +void gl_begin(int p) +{ + if(!vert_arr) { + vert_arr = malloc(MAX_VERTS * sizeof *vert_arr); + norm_arr = malloc(MAX_VERTS * sizeof *norm_arr); + texc_arr = malloc(MAX_VERTS * sizeof *texc_arr); + col_arr = malloc(MAX_VERTS * sizeof *col_arr); + attr_arr = malloc(MAX_VERTS * sizeof *attr_arr); + assert(vert_arr && norm_arr && texc_arr && col_arr && attr_arr); + } + + prim = p; + num_verts = vert_calls = 0; + + glGetIntegerv(GL_CURRENT_PROGRAM, &cur_prog); + assert(cur_prog); + + gl_apply_xform(cur_prog); + + vloc = glGetAttribLocation(cur_prog, "attr_vertex"); + nloc = glGetAttribLocation(cur_prog, "attr_normal"); + cloc = glGetAttribLocation(cur_prog, "attr_color"); + tloc = glGetAttribLocation(cur_prog, "attr_texcoord"); +} + +void gl_end(void) +{ + if(num_verts > 0) { + gl_draw_immediate(); + } + aloc = -1; +} + +static void gl_draw_immediate(void) +{ + int glprim; + + if(vloc == -1) { + fprintf(stderr, "gl_draw_immediate call with vloc == -1\n"); + return; + } + + glprim = prim == GL_QUADS ? GL_TRIANGLES : prim; + + glVertexAttribPointer(vloc, 4, GL_FLOAT, 0, 0, vert_arr); + glEnableVertexAttribArray(vloc); + + if(nloc != -1) { + glVertexAttribPointer(nloc, 3, GL_FLOAT, 0, 0, norm_arr); + glEnableVertexAttribArray(nloc); + } + + if(cloc != -1) { + glVertexAttribPointer(cloc, 4, GL_FLOAT, 0, 0, col_arr); + glEnableVertexAttribArray(cloc); + } + + if(tloc != -1) { + glVertexAttribPointer(tloc, 2, GL_FLOAT, 0, 0, texc_arr); + glEnableVertexAttribArray(tloc); + } + + if(aloc != -1) { + glVertexAttribPointer(aloc, 4, GL_FLOAT, 0, 0, attr_arr); + glEnableVertexAttribArray(aloc); + } + + glDrawArrays(glprim, 0, num_verts); + + glDisableVertexAttribArray(vloc); + if(nloc != -1) { + glDisableVertexAttribArray(nloc); + } + if(cloc != -1) { + glDisableVertexAttribArray(cloc); + } + if(tloc != -1) { + glDisableVertexAttribArray(tloc); + } + if(aloc != -1) { + glDisableVertexAttribArray(aloc); + } +} + + +void gl_vertex2f(float x, float y) +{ + gl_vertex4f(x, y, 0.0f, 1.0f); +} + +void gl_vertex3f(float x, float y, float z) +{ + gl_vertex4f(x, y, z, 1.0f); +} + +void gl_vertex4f(float x, float y, float z, float w) +{ + int i, buffer_full; + + if(prim == GL_QUADS && vert_calls % 4 == 3) { + for(i=0; i<2; i++) { + if(aloc != -1) { + attr_arr[num_verts] = attr_arr[num_verts - 3 + i]; + } + if(cloc != -1) { + col_arr[num_verts] = col_arr[num_verts - 3 + i]; + } + if(tloc != -1) { + texc_arr[num_verts] = texc_arr[num_verts - 3 + i]; + } + if(nloc != -1) { + norm_arr[num_verts] = norm_arr[num_verts - 3 + i]; + } + vert_arr[num_verts] = vert_arr[num_verts - 3 + i]; + num_verts++; + } + } + + vert_arr[num_verts].x = x; + vert_arr[num_verts].y = y; + vert_arr[num_verts].z = z; + vert_arr[num_verts].w = w; + + if(cloc != -1) { + col_arr[num_verts] = cur_color; + } + if(nloc != -1) { + norm_arr[num_verts] = cur_normal; + } + if(tloc != -1) { + texc_arr[num_verts] = cur_texcoord; + } + if(aloc != -1) { + attr_arr[num_verts] = cur_attrib; + } + + vert_calls++; + num_verts++; + + if(prim == GL_QUADS) { + /* leave space for 6 more worst-case and don't allow flushes mid-quad */ + buffer_full = num_verts >= MAX_VERTS - 6 && vert_calls % 4 == 0; + } else { + buffer_full = num_verts >= MAX_VERTS - prim; + } + + if(buffer_full) { + gl_draw_immediate(); + glBegin(prim); /* reset everything */ + } +} + + +void gl_normal3f(float x, float y, float z) +{ + cur_normal.x = x; + cur_normal.y = y; + cur_normal.z = z; +} + + +void gl_color3f(float r, float g, float b) +{ + cur_color.x = r; + cur_color.y = g; + cur_color.z = b; + cur_color.w = 1.0f; +} + +void gl_color4f(float r, float g, float b, float a) +{ + cur_color.x = r; + cur_color.y = g; + cur_color.z = b; + cur_color.w = a; +} + + +void gl_texcoord1f(float s) +{ + cur_texcoord.x = s; + cur_texcoord.y = 0.0f; +} + +void gl_texcoord2f(float s, float t) +{ + cur_texcoord.x = s; + cur_texcoord.y = t; +} + +void gl_vertex_attrib2f(int loc, float x, float y) +{ + aloc = loc; + cur_attrib.x = x; + cur_attrib.y = y; + cur_attrib.z = 0.0f; + cur_attrib.w = 1.0f; +} + +void gl_vertex_attrib3f(int loc, float x, float y, float z) +{ + aloc = loc; + cur_attrib.x = x; + cur_attrib.y = y; + cur_attrib.z = z; + cur_attrib.w = 1.0f; +} + +void gl_vertex_attrib4f(int loc, float x, float y, float z, float w) +{ + aloc = loc; + cur_attrib.x = x; + cur_attrib.y = y; + cur_attrib.z = z; + cur_attrib.w = w; +} diff -r 4d25539806d2 -r bb68fac22579 src/sdr.c --- a/src/sdr.c Tue Sep 06 12:48:39 2011 +0300 +++ b/src/sdr.c Wed Sep 07 02:48:35 2011 +0300 @@ -287,7 +287,7 @@ int set_uniform_matrix4(unsigned int prog, const char *name, float *mat) { BEGIN_UNIFORM_CODE { - glUniformMatrix4fv(loc, 16, GL_FALSE, mat); + glUniformMatrix4fv(loc, 1, GL_FALSE, mat); } END_UNIFORM_CODE; } @@ -295,7 +295,7 @@ int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat) { BEGIN_UNIFORM_CODE { - glUniformMatrix4fv(loc, 16, GL_TRUE, mat); + glUniformMatrix4fv(loc, 1, GL_TRUE, mat); } END_UNIFORM_CODE; }