istereo
changeset 2:bb68fac22579
sanegl and shit
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Wed, 07 Sep 2011 02:48:35 +0300 |
parents | 4d25539806d2 |
children | 2c5620f0670c |
files | sdr/test.p.glsl sdr/test.v.glsl src/glutmain.c src/istereo.c src/istereo.h src/sanegl.c src/sdr.c |
diffstat | 7 files changed, 326 insertions(+), 36 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/sdr/test.p.glsl Wed Sep 07 02:48:35 2011 +0300 1.3 @@ -0,0 +1,6 @@ 1.4 +varying vec4 var_color; 1.5 + 1.6 +void main() 1.7 +{ 1.8 + gl_FragColor = var_color; 1.9 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/sdr/test.v.glsl Wed Sep 07 02:48:35 2011 +0300 2.3 @@ -0,0 +1,12 @@ 2.4 +uniform mat4 matrix_modelview, matrix_projection; 2.5 + 2.6 +attribute vec4 attr_vertex, attr_color; 2.7 + 2.8 +varying vec4 var_color; 2.9 + 2.10 +void main() 2.11 +{ 2.12 + mat4 mvp = matrix_projection * matrix_modelview; 2.13 + gl_Position = mvp * attr_vertex; 2.14 + var_color = attr_color; 2.15 +}
3.1 --- a/src/glutmain.c Tue Sep 06 12:48:39 2011 +0300 3.2 +++ b/src/glutmain.c Wed Sep 07 02:48:35 2011 +0300 3.3 @@ -3,9 +3,10 @@ 3.4 #include <GL/glew.h> 3.5 #include <GL/glut.h> 3.6 #include "sanegl.h" 3.7 +#include "istereo.h" 3.8 +#include "sdr.h" 3.9 3.10 void disp(void); 3.11 -void reshape(int x, int y); 3.12 void keyb(unsigned char key, int x, int y); 3.13 3.14 int main(int argc, char **argv) 3.15 @@ -20,41 +21,23 @@ 3.16 glutReshapeFunc(reshape); 3.17 glutKeyboardFunc(keyb); 3.18 3.19 + glewInit(); 3.20 + 3.21 + if(init() == -1) { 3.22 + return 1; 3.23 + } 3.24 + 3.25 glutMainLoop(); 3.26 return 0; 3.27 } 3.28 3.29 void disp(void) 3.30 { 3.31 - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 3.32 - 3.33 - glMatrixMode(GL_MODELVIEW); 3.34 - glLoadIdentity(); 3.35 - glTranslatef(0, 0, -8); 3.36 - 3.37 - glBegin(GL_QUADS); 3.38 - glColor3f(1, 0, 0); 3.39 - glVertex3f(-1, -1, 0); 3.40 - glColor3f(0, 1, 0); 3.41 - glVertex3f(1, -1, 0); 3.42 - glColor3f(0, 0, 1); 3.43 - glVertex3f(1, 1, 0); 3.44 - glColor3f(1, 1, 0); 3.45 - glVertex3f(-1, 1, 0); 3.46 - glEnd(); 3.47 + redraw(); 3.48 3.49 glutSwapBuffers(); 3.50 } 3.51 3.52 -void reshape(int x, int y) 3.53 -{ 3.54 - glViewport(0, 0, x, y); 3.55 - 3.56 - glMatrixMode(GL_PROJECTION); 3.57 - glLoadIdentity(); 3.58 - gluPerspective(45.0, (float)x / (float)y, 1.0, 1000.0); 3.59 -} 3.60 - 3.61 void keyb(unsigned char key, int x, int y) 3.62 { 3.63 switch(key) {
4.1 --- a/src/istereo.c Tue Sep 06 12:48:39 2011 +0300 4.2 +++ b/src/istereo.c Wed Sep 07 02:48:35 2011 +0300 4.3 @@ -1,17 +1,58 @@ 4.4 +#include <stdio.h> 4.5 +#include <assert.h> 4.6 #include "opengl.h" 4.7 #include "istereo.h" 4.8 +#include "sanegl.h" 4.9 +#include "sdr.h" 4.10 + 4.11 +unsigned int prog; 4.12 4.13 int init(void) 4.14 { 4.15 + if(!(prog = create_program_load("sdr/test.v.glsl", "sdr/test.p.glsl"))) { 4.16 + fprintf(stderr, "failed to load shader program\n"); 4.17 + return -1; 4.18 + } 4.19 + 4.20 return 0; 4.21 } 4.22 4.23 void cleanup(void) 4.24 { 4.25 + free_program(prog); 4.26 } 4.27 4.28 void redraw(void) 4.29 { 4.30 - glClearColor(0, 1, 0, 1); 4.31 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 4.32 + 4.33 + bind_program(prog); 4.34 + 4.35 + gl_matrix_mode(GL_MODELVIEW); 4.36 + gl_load_identity(); 4.37 + gl_translatef(0, 0, -8); 4.38 + 4.39 + gl_apply_xform(prog); 4.40 + 4.41 + gl_begin(GL_QUADS); 4.42 + gl_color3f(1, 0, 0); 4.43 + gl_vertex3f(-1, -1, 0); 4.44 + gl_color3f(0, 1, 0); 4.45 + gl_vertex3f(1, -1, 0); 4.46 + gl_color3f(0, 0, 1); 4.47 + gl_vertex3f(1, 1, 0); 4.48 + gl_color3f(1, 1, 0); 4.49 + gl_vertex3f(-1, 1, 0); 4.50 + gl_end(); 4.51 + 4.52 + assert(glGetError() == GL_NO_ERROR); 4.53 } 4.54 + 4.55 +void reshape(int x, int y) 4.56 +{ 4.57 + glViewport(0, 0, x, y); 4.58 + 4.59 + gl_matrix_mode(GL_PROJECTION); 4.60 + gl_load_identity(); 4.61 + glu_perspective(45.0, (float)x / (float)y, 1.0, 1000.0); 4.62 +}
5.1 --- a/src/istereo.h Tue Sep 06 12:48:39 2011 +0300 5.2 +++ b/src/istereo.h Wed Sep 07 02:48:35 2011 +0300 5.3 @@ -4,5 +4,6 @@ 5.4 int init(void); 5.5 void cleanup(void); 5.6 void redraw(void); 5.7 +void reshape(int x, int y); 5.8 5.9 #endif /* ISTEREO_H_ */
6.1 --- a/src/sanegl.c Tue Sep 06 12:48:39 2011 +0300 6.2 +++ b/src/sanegl.c Wed Sep 07 02:48:35 2011 +0300 6.3 @@ -1,5 +1,25 @@ 6.4 +/* 6.5 +SaneGL - a small library to bring back sanity to OpenGL ES 2.x 6.6 +Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org> 6.7 + 6.8 +This program is free software: you can redistribute it and/or modify 6.9 +it under the terms of the GNU General Public License as published by 6.10 +the Free Software Foundation, either version 3 of the License, or 6.11 +(at your option) any later version. 6.12 + 6.13 +This program is distributed in the hope that it will be useful, 6.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 6.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 6.16 +GNU General Public License for more details. 6.17 + 6.18 +You should have received a copy of the GNU General Public License 6.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 6.20 +*/ 6.21 + 6.22 +#include <stdio.h> 6.23 +#include <string.h> 6.24 #include <math.h> 6.25 -#include <string.h> 6.26 +#include <assert.h> 6.27 #include "sanegl.h" 6.28 6.29 #define MMODE_IDX(x) ((x) - GL_MODELVIEW) 6.30 @@ -8,6 +28,8 @@ 6.31 6.32 #define MAX_VERTS 512 6.33 6.34 +static void gl_draw_immediate(void); 6.35 + 6.36 typedef struct { float x, y; } vec2_t; 6.37 typedef struct { float x, y, z; } vec3_t; 6.38 typedef struct { float x, y, z, w; } vec4_t; 6.39 @@ -26,7 +48,11 @@ 6.40 static vec4_t *vert_arr, *col_arr, *attr_arr; 6.41 static vec3_t *norm_arr; 6.42 static vec2_t *texc_arr; 6.43 -static int vloc, nloc, cloc, tloc, aloc; 6.44 +/*static unsigned int vbuf, cbuf, nbuf, tbuf, abuf;*/ 6.45 +static int vloc, nloc, cloc, tloc, aloc = -1; 6.46 + 6.47 +static int num_verts, vert_calls; 6.48 +static int cur_prog; 6.49 6.50 6.51 void gl_matrix_mode(int mm) 6.52 @@ -205,16 +231,18 @@ 6.53 ptop = stack_top[pidx]; 6.54 ttop = stack_top[tidx]; 6.55 6.56 + assert(prog); 6.57 + 6.58 if((loc = glGetUniformLocation(prog, "matrix_modelview")) != -1) { 6.59 - glUniformMatrix4fv(loc, 16, 0, mat_stack[mvidx][mvtop]); 6.60 + glUniformMatrix4fv(loc, 1, 0, mat_stack[mvidx][mvtop]); 6.61 } 6.62 6.63 if((loc = glGetUniformLocation(prog, "matrix_projection")) != -1) { 6.64 - glUniformMatrix4fv(loc, 16, 0, mat_stack[pidx][ptop]); 6.65 + glUniformMatrix4fv(loc, 1, 0, mat_stack[pidx][ptop]); 6.66 } 6.67 6.68 if((loc = glGetUniformLocation(prog, "matrix_texture")) != -1) { 6.69 - glUniformMatrix4fv(loc, 16, 0, mat_stack[tidx][ttop]); 6.70 + glUniformMatrix4fv(loc, 1, 0, mat_stack[tidx][ttop]); 6.71 } 6.72 6.73 if((loc = glGetUniformLocation(prog, "matrix_normal")) != -1) { 6.74 @@ -229,13 +257,232 @@ 6.75 nmat[6] = mat_stack[mvidx][mvtop][8]; 6.76 nmat[7] = mat_stack[mvidx][mvtop][9]; 6.77 nmat[8] = mat_stack[mvidx][mvtop][10]; 6.78 - glUniformMatrix3fv(loc, 9, 0, nmat); 6.79 + glUniformMatrix3fv(loc, 1, 0, nmat); 6.80 } 6.81 6.82 if((loc = glGetUniformLocation(prog, "matrix_modelview_projection")) != -1) { 6.83 if(!mvp_valid) { 6.84 /* TODO calc mvp */ 6.85 } 6.86 - glUniformMatrix4fv(loc, 16, 0, mat_mvp); 6.87 + glUniformMatrix4fv(loc, 1, 0, mat_mvp); 6.88 } 6.89 } 6.90 + 6.91 + 6.92 +/* immediate mode rendering */ 6.93 +void gl_begin(int p) 6.94 +{ 6.95 + if(!vert_arr) { 6.96 + vert_arr = malloc(MAX_VERTS * sizeof *vert_arr); 6.97 + norm_arr = malloc(MAX_VERTS * sizeof *norm_arr); 6.98 + texc_arr = malloc(MAX_VERTS * sizeof *texc_arr); 6.99 + col_arr = malloc(MAX_VERTS * sizeof *col_arr); 6.100 + attr_arr = malloc(MAX_VERTS * sizeof *attr_arr); 6.101 + assert(vert_arr && norm_arr && texc_arr && col_arr && attr_arr); 6.102 + } 6.103 + 6.104 + prim = p; 6.105 + num_verts = vert_calls = 0; 6.106 + 6.107 + glGetIntegerv(GL_CURRENT_PROGRAM, &cur_prog); 6.108 + assert(cur_prog); 6.109 + 6.110 + gl_apply_xform(cur_prog); 6.111 + 6.112 + vloc = glGetAttribLocation(cur_prog, "attr_vertex"); 6.113 + nloc = glGetAttribLocation(cur_prog, "attr_normal"); 6.114 + cloc = glGetAttribLocation(cur_prog, "attr_color"); 6.115 + tloc = glGetAttribLocation(cur_prog, "attr_texcoord"); 6.116 +} 6.117 + 6.118 +void gl_end(void) 6.119 +{ 6.120 + if(num_verts > 0) { 6.121 + gl_draw_immediate(); 6.122 + } 6.123 + aloc = -1; 6.124 +} 6.125 + 6.126 +static void gl_draw_immediate(void) 6.127 +{ 6.128 + int glprim; 6.129 + 6.130 + if(vloc == -1) { 6.131 + fprintf(stderr, "gl_draw_immediate call with vloc == -1\n"); 6.132 + return; 6.133 + } 6.134 + 6.135 + glprim = prim == GL_QUADS ? GL_TRIANGLES : prim; 6.136 + 6.137 + glVertexAttribPointer(vloc, 4, GL_FLOAT, 0, 0, vert_arr); 6.138 + glEnableVertexAttribArray(vloc); 6.139 + 6.140 + if(nloc != -1) { 6.141 + glVertexAttribPointer(nloc, 3, GL_FLOAT, 0, 0, norm_arr); 6.142 + glEnableVertexAttribArray(nloc); 6.143 + } 6.144 + 6.145 + if(cloc != -1) { 6.146 + glVertexAttribPointer(cloc, 4, GL_FLOAT, 0, 0, col_arr); 6.147 + glEnableVertexAttribArray(cloc); 6.148 + } 6.149 + 6.150 + if(tloc != -1) { 6.151 + glVertexAttribPointer(tloc, 2, GL_FLOAT, 0, 0, texc_arr); 6.152 + glEnableVertexAttribArray(tloc); 6.153 + } 6.154 + 6.155 + if(aloc != -1) { 6.156 + glVertexAttribPointer(aloc, 4, GL_FLOAT, 0, 0, attr_arr); 6.157 + glEnableVertexAttribArray(aloc); 6.158 + } 6.159 + 6.160 + glDrawArrays(glprim, 0, num_verts); 6.161 + 6.162 + glDisableVertexAttribArray(vloc); 6.163 + if(nloc != -1) { 6.164 + glDisableVertexAttribArray(nloc); 6.165 + } 6.166 + if(cloc != -1) { 6.167 + glDisableVertexAttribArray(cloc); 6.168 + } 6.169 + if(tloc != -1) { 6.170 + glDisableVertexAttribArray(tloc); 6.171 + } 6.172 + if(aloc != -1) { 6.173 + glDisableVertexAttribArray(aloc); 6.174 + } 6.175 +} 6.176 + 6.177 + 6.178 +void gl_vertex2f(float x, float y) 6.179 +{ 6.180 + gl_vertex4f(x, y, 0.0f, 1.0f); 6.181 +} 6.182 + 6.183 +void gl_vertex3f(float x, float y, float z) 6.184 +{ 6.185 + gl_vertex4f(x, y, z, 1.0f); 6.186 +} 6.187 + 6.188 +void gl_vertex4f(float x, float y, float z, float w) 6.189 +{ 6.190 + int i, buffer_full; 6.191 + 6.192 + if(prim == GL_QUADS && vert_calls % 4 == 3) { 6.193 + for(i=0; i<2; i++) { 6.194 + if(aloc != -1) { 6.195 + attr_arr[num_verts] = attr_arr[num_verts - 3 + i]; 6.196 + } 6.197 + if(cloc != -1) { 6.198 + col_arr[num_verts] = col_arr[num_verts - 3 + i]; 6.199 + } 6.200 + if(tloc != -1) { 6.201 + texc_arr[num_verts] = texc_arr[num_verts - 3 + i]; 6.202 + } 6.203 + if(nloc != -1) { 6.204 + norm_arr[num_verts] = norm_arr[num_verts - 3 + i]; 6.205 + } 6.206 + vert_arr[num_verts] = vert_arr[num_verts - 3 + i]; 6.207 + num_verts++; 6.208 + } 6.209 + } 6.210 + 6.211 + vert_arr[num_verts].x = x; 6.212 + vert_arr[num_verts].y = y; 6.213 + vert_arr[num_verts].z = z; 6.214 + vert_arr[num_verts].w = w; 6.215 + 6.216 + if(cloc != -1) { 6.217 + col_arr[num_verts] = cur_color; 6.218 + } 6.219 + if(nloc != -1) { 6.220 + norm_arr[num_verts] = cur_normal; 6.221 + } 6.222 + if(tloc != -1) { 6.223 + texc_arr[num_verts] = cur_texcoord; 6.224 + } 6.225 + if(aloc != -1) { 6.226 + attr_arr[num_verts] = cur_attrib; 6.227 + } 6.228 + 6.229 + vert_calls++; 6.230 + num_verts++; 6.231 + 6.232 + if(prim == GL_QUADS) { 6.233 + /* leave space for 6 more worst-case and don't allow flushes mid-quad */ 6.234 + buffer_full = num_verts >= MAX_VERTS - 6 && vert_calls % 4 == 0; 6.235 + } else { 6.236 + buffer_full = num_verts >= MAX_VERTS - prim; 6.237 + } 6.238 + 6.239 + if(buffer_full) { 6.240 + gl_draw_immediate(); 6.241 + glBegin(prim); /* reset everything */ 6.242 + } 6.243 +} 6.244 + 6.245 + 6.246 +void gl_normal3f(float x, float y, float z) 6.247 +{ 6.248 + cur_normal.x = x; 6.249 + cur_normal.y = y; 6.250 + cur_normal.z = z; 6.251 +} 6.252 + 6.253 + 6.254 +void gl_color3f(float r, float g, float b) 6.255 +{ 6.256 + cur_color.x = r; 6.257 + cur_color.y = g; 6.258 + cur_color.z = b; 6.259 + cur_color.w = 1.0f; 6.260 +} 6.261 + 6.262 +void gl_color4f(float r, float g, float b, float a) 6.263 +{ 6.264 + cur_color.x = r; 6.265 + cur_color.y = g; 6.266 + cur_color.z = b; 6.267 + cur_color.w = a; 6.268 +} 6.269 + 6.270 + 6.271 +void gl_texcoord1f(float s) 6.272 +{ 6.273 + cur_texcoord.x = s; 6.274 + cur_texcoord.y = 0.0f; 6.275 +} 6.276 + 6.277 +void gl_texcoord2f(float s, float t) 6.278 +{ 6.279 + cur_texcoord.x = s; 6.280 + cur_texcoord.y = t; 6.281 +} 6.282 + 6.283 +void gl_vertex_attrib2f(int loc, float x, float y) 6.284 +{ 6.285 + aloc = loc; 6.286 + cur_attrib.x = x; 6.287 + cur_attrib.y = y; 6.288 + cur_attrib.z = 0.0f; 6.289 + cur_attrib.w = 1.0f; 6.290 +} 6.291 + 6.292 +void gl_vertex_attrib3f(int loc, float x, float y, float z) 6.293 +{ 6.294 + aloc = loc; 6.295 + cur_attrib.x = x; 6.296 + cur_attrib.y = y; 6.297 + cur_attrib.z = z; 6.298 + cur_attrib.w = 1.0f; 6.299 +} 6.300 + 6.301 +void gl_vertex_attrib4f(int loc, float x, float y, float z, float w) 6.302 +{ 6.303 + aloc = loc; 6.304 + cur_attrib.x = x; 6.305 + cur_attrib.y = y; 6.306 + cur_attrib.z = z; 6.307 + cur_attrib.w = w; 6.308 +}
7.1 --- a/src/sdr.c Tue Sep 06 12:48:39 2011 +0300 7.2 +++ b/src/sdr.c Wed Sep 07 02:48:35 2011 +0300 7.3 @@ -287,7 +287,7 @@ 7.4 int set_uniform_matrix4(unsigned int prog, const char *name, float *mat) 7.5 { 7.6 BEGIN_UNIFORM_CODE { 7.7 - glUniformMatrix4fv(loc, 16, GL_FALSE, mat); 7.8 + glUniformMatrix4fv(loc, 1, GL_FALSE, mat); 7.9 } 7.10 END_UNIFORM_CODE; 7.11 } 7.12 @@ -295,7 +295,7 @@ 7.13 int set_uniform_matrix4_transposed(unsigned int prog, const char *name, float *mat) 7.14 { 7.15 BEGIN_UNIFORM_CODE { 7.16 - glUniformMatrix4fv(loc, 16, GL_TRUE, mat); 7.17 + glUniformMatrix4fv(loc, 1, GL_TRUE, mat); 7.18 } 7.19 END_UNIFORM_CODE; 7.20 }