istereo

diff 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
line diff
     1.1 --- a/src/sanegl.c	Tue Sep 06 12:48:39 2011 +0300
     1.2 +++ b/src/sanegl.c	Wed Sep 07 02:48:35 2011 +0300
     1.3 @@ -1,5 +1,25 @@
     1.4 +/*
     1.5 +SaneGL - a small library to bring back sanity to OpenGL ES 2.x
     1.6 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
     1.7 +
     1.8 +This program is free software: you can redistribute it and/or modify
     1.9 +it under the terms of the GNU General Public License as published by
    1.10 +the Free Software Foundation, either version 3 of the License, or
    1.11 +(at your option) any later version.
    1.12 +
    1.13 +This program is distributed in the hope that it will be useful,
    1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +GNU General Public License for more details.
    1.17 +
    1.18 +You should have received a copy of the GNU General Public License
    1.19 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 +*/
    1.21 +
    1.22 +#include <stdio.h>
    1.23 +#include <string.h>
    1.24  #include <math.h>
    1.25 -#include <string.h>
    1.26 +#include <assert.h>
    1.27  #include "sanegl.h"
    1.28  
    1.29  #define MMODE_IDX(x)	((x) - GL_MODELVIEW)
    1.30 @@ -8,6 +28,8 @@
    1.31  
    1.32  #define MAX_VERTS	512
    1.33  
    1.34 +static void gl_draw_immediate(void);
    1.35 +
    1.36  typedef struct { float x, y; } vec2_t;
    1.37  typedef struct { float x, y, z; } vec3_t;
    1.38  typedef struct { float x, y, z, w; } vec4_t;
    1.39 @@ -26,7 +48,11 @@
    1.40  static vec4_t *vert_arr, *col_arr, *attr_arr;
    1.41  static vec3_t *norm_arr;
    1.42  static vec2_t *texc_arr;
    1.43 -static int vloc, nloc, cloc, tloc, aloc;
    1.44 +/*static unsigned int vbuf, cbuf, nbuf, tbuf, abuf;*/
    1.45 +static int vloc, nloc, cloc, tloc, aloc = -1;
    1.46 +
    1.47 +static int num_verts, vert_calls;
    1.48 +static int cur_prog;
    1.49  
    1.50  
    1.51  void gl_matrix_mode(int mm)
    1.52 @@ -205,16 +231,18 @@
    1.53  	ptop = stack_top[pidx];
    1.54  	ttop = stack_top[tidx];
    1.55  
    1.56 +	assert(prog);
    1.57 +
    1.58  	if((loc = glGetUniformLocation(prog, "matrix_modelview")) != -1) {
    1.59 -		glUniformMatrix4fv(loc, 16, 0, mat_stack[mvidx][mvtop]);
    1.60 +		glUniformMatrix4fv(loc, 1, 0, mat_stack[mvidx][mvtop]);
    1.61  	}
    1.62  
    1.63  	if((loc = glGetUniformLocation(prog, "matrix_projection")) != -1) {
    1.64 -		glUniformMatrix4fv(loc, 16, 0, mat_stack[pidx][ptop]);
    1.65 +		glUniformMatrix4fv(loc, 1, 0, mat_stack[pidx][ptop]);
    1.66  	}
    1.67  
    1.68  	if((loc = glGetUniformLocation(prog, "matrix_texture")) != -1) {
    1.69 -		glUniformMatrix4fv(loc, 16, 0, mat_stack[tidx][ttop]);
    1.70 +		glUniformMatrix4fv(loc, 1, 0, mat_stack[tidx][ttop]);
    1.71  	}
    1.72  
    1.73  	if((loc = glGetUniformLocation(prog, "matrix_normal")) != -1) {
    1.74 @@ -229,13 +257,232 @@
    1.75  		nmat[6] = mat_stack[mvidx][mvtop][8];
    1.76  		nmat[7] = mat_stack[mvidx][mvtop][9];
    1.77  		nmat[8] = mat_stack[mvidx][mvtop][10];
    1.78 -		glUniformMatrix3fv(loc, 9, 0, nmat);
    1.79 +		glUniformMatrix3fv(loc, 1, 0, nmat);
    1.80  	}
    1.81  
    1.82  	if((loc = glGetUniformLocation(prog, "matrix_modelview_projection")) != -1) {
    1.83  		if(!mvp_valid) {
    1.84  			/* TODO calc mvp */
    1.85  		}
    1.86 -		glUniformMatrix4fv(loc, 16, 0, mat_mvp);
    1.87 +		glUniformMatrix4fv(loc, 1, 0, mat_mvp);
    1.88  	}
    1.89  }
    1.90 +
    1.91 +
    1.92 +/* immediate mode rendering */
    1.93 +void gl_begin(int p)
    1.94 +{
    1.95 +	if(!vert_arr) {
    1.96 +		vert_arr = malloc(MAX_VERTS * sizeof *vert_arr);
    1.97 +		norm_arr = malloc(MAX_VERTS * sizeof *norm_arr);
    1.98 +		texc_arr = malloc(MAX_VERTS * sizeof *texc_arr);
    1.99 +		col_arr = malloc(MAX_VERTS * sizeof *col_arr);
   1.100 +		attr_arr = malloc(MAX_VERTS * sizeof *attr_arr);
   1.101 +		assert(vert_arr && norm_arr && texc_arr && col_arr && attr_arr);
   1.102 +	}
   1.103 +
   1.104 +	prim = p;
   1.105 +	num_verts = vert_calls = 0;
   1.106 +
   1.107 +	glGetIntegerv(GL_CURRENT_PROGRAM, &cur_prog);
   1.108 +	assert(cur_prog);
   1.109 +
   1.110 +	gl_apply_xform(cur_prog);
   1.111 +
   1.112 +	vloc = glGetAttribLocation(cur_prog, "attr_vertex");
   1.113 +	nloc = glGetAttribLocation(cur_prog, "attr_normal");
   1.114 +	cloc = glGetAttribLocation(cur_prog, "attr_color");
   1.115 +	tloc = glGetAttribLocation(cur_prog, "attr_texcoord");
   1.116 +}
   1.117 +
   1.118 +void gl_end(void)
   1.119 +{
   1.120 +	if(num_verts > 0) {
   1.121 +		gl_draw_immediate();
   1.122 +	}
   1.123 +	aloc = -1;
   1.124 +}
   1.125 +
   1.126 +static void gl_draw_immediate(void)
   1.127 +{
   1.128 +	int glprim;
   1.129 +
   1.130 +	if(vloc == -1) {
   1.131 +		fprintf(stderr, "gl_draw_immediate call with vloc == -1\n");
   1.132 +		return;
   1.133 +	}
   1.134 +
   1.135 +	glprim = prim == GL_QUADS ? GL_TRIANGLES : prim;
   1.136 +
   1.137 +	glVertexAttribPointer(vloc, 4, GL_FLOAT, 0, 0, vert_arr);
   1.138 +	glEnableVertexAttribArray(vloc);
   1.139 +
   1.140 +	if(nloc != -1) {
   1.141 +		glVertexAttribPointer(nloc, 3, GL_FLOAT, 0, 0, norm_arr);
   1.142 +		glEnableVertexAttribArray(nloc);
   1.143 +	}
   1.144 +
   1.145 +	if(cloc != -1) {
   1.146 +		glVertexAttribPointer(cloc, 4, GL_FLOAT, 0, 0, col_arr);
   1.147 +		glEnableVertexAttribArray(cloc);
   1.148 +	}
   1.149 +
   1.150 +	if(tloc != -1) {
   1.151 +		glVertexAttribPointer(tloc, 2, GL_FLOAT, 0, 0, texc_arr);
   1.152 +		glEnableVertexAttribArray(tloc);
   1.153 +	}
   1.154 +
   1.155 +	if(aloc != -1) {
   1.156 +		glVertexAttribPointer(aloc, 4, GL_FLOAT, 0, 0, attr_arr);
   1.157 +		glEnableVertexAttribArray(aloc);
   1.158 +	}
   1.159 +
   1.160 +	glDrawArrays(glprim, 0, num_verts);
   1.161 +
   1.162 +	glDisableVertexAttribArray(vloc);
   1.163 +	if(nloc != -1) {
   1.164 +		glDisableVertexAttribArray(nloc);
   1.165 +	}
   1.166 +	if(cloc != -1) {
   1.167 +		glDisableVertexAttribArray(cloc);
   1.168 +	}
   1.169 +	if(tloc != -1) {
   1.170 +		glDisableVertexAttribArray(tloc);
   1.171 +	}
   1.172 +	if(aloc != -1) {
   1.173 +		glDisableVertexAttribArray(aloc);
   1.174 +	}
   1.175 +}
   1.176 +
   1.177 +
   1.178 +void gl_vertex2f(float x, float y)
   1.179 +{
   1.180 +	gl_vertex4f(x, y, 0.0f, 1.0f);
   1.181 +}
   1.182 +
   1.183 +void gl_vertex3f(float x, float y, float z)
   1.184 +{
   1.185 +	gl_vertex4f(x, y, z, 1.0f);
   1.186 +}
   1.187 +
   1.188 +void gl_vertex4f(float x, float y, float z, float w)
   1.189 +{
   1.190 +	int i, buffer_full;
   1.191 +
   1.192 +	if(prim == GL_QUADS && vert_calls % 4 == 3) {
   1.193 +		for(i=0; i<2; i++) {
   1.194 +			if(aloc != -1) {
   1.195 +				attr_arr[num_verts] = attr_arr[num_verts - 3 + i];
   1.196 +			}
   1.197 +			if(cloc != -1) {
   1.198 +				col_arr[num_verts] = col_arr[num_verts - 3 + i];
   1.199 +			}
   1.200 +			if(tloc != -1) {
   1.201 +				texc_arr[num_verts] = texc_arr[num_verts - 3 + i];
   1.202 +			}
   1.203 +			if(nloc != -1) {
   1.204 +				norm_arr[num_verts] = norm_arr[num_verts - 3 + i];
   1.205 +			}
   1.206 +			vert_arr[num_verts] = vert_arr[num_verts - 3 + i];
   1.207 +			num_verts++;
   1.208 +		}
   1.209 +	}
   1.210 +
   1.211 +	vert_arr[num_verts].x = x;
   1.212 +	vert_arr[num_verts].y = y;
   1.213 +	vert_arr[num_verts].z = z;
   1.214 +	vert_arr[num_verts].w = w;
   1.215 +
   1.216 +	if(cloc != -1) {
   1.217 +		col_arr[num_verts] = cur_color;
   1.218 +	}
   1.219 +	if(nloc != -1) {
   1.220 +		norm_arr[num_verts] = cur_normal;
   1.221 +	}
   1.222 +	if(tloc != -1) {
   1.223 +		texc_arr[num_verts] = cur_texcoord;
   1.224 +	}
   1.225 +	if(aloc != -1) {
   1.226 +		attr_arr[num_verts] = cur_attrib;
   1.227 +	}
   1.228 +
   1.229 +	vert_calls++;
   1.230 +	num_verts++;
   1.231 +
   1.232 +	if(prim == GL_QUADS) {
   1.233 +		/* leave space for 6 more worst-case and don't allow flushes mid-quad */
   1.234 +		buffer_full = num_verts >= MAX_VERTS - 6 && vert_calls % 4 == 0;
   1.235 +	} else {
   1.236 +		buffer_full = num_verts >= MAX_VERTS - prim;
   1.237 +	}
   1.238 +
   1.239 +	if(buffer_full) {
   1.240 +		gl_draw_immediate();
   1.241 +		glBegin(prim);	/* reset everything */
   1.242 +	}
   1.243 +}
   1.244 +
   1.245 +
   1.246 +void gl_normal3f(float x, float y, float z)
   1.247 +{
   1.248 +	cur_normal.x = x;
   1.249 +	cur_normal.y = y;
   1.250 +	cur_normal.z = z;
   1.251 +}
   1.252 +
   1.253 +
   1.254 +void gl_color3f(float r, float g, float b)
   1.255 +{
   1.256 +	cur_color.x = r;
   1.257 +	cur_color.y = g;
   1.258 +	cur_color.z = b;
   1.259 +	cur_color.w = 1.0f;
   1.260 +}
   1.261 +
   1.262 +void gl_color4f(float r, float g, float b, float a)
   1.263 +{
   1.264 +	cur_color.x = r;
   1.265 +	cur_color.y = g;
   1.266 +	cur_color.z = b;
   1.267 +	cur_color.w = a;
   1.268 +}
   1.269 +
   1.270 +
   1.271 +void gl_texcoord1f(float s)
   1.272 +{
   1.273 +	cur_texcoord.x = s;
   1.274 +	cur_texcoord.y = 0.0f;
   1.275 +}
   1.276 +
   1.277 +void gl_texcoord2f(float s, float t)
   1.278 +{
   1.279 +	cur_texcoord.x = s;
   1.280 +	cur_texcoord.y = t;
   1.281 +}
   1.282 +
   1.283 +void gl_vertex_attrib2f(int loc, float x, float y)
   1.284 +{
   1.285 +	aloc = loc;
   1.286 +	cur_attrib.x = x;
   1.287 +	cur_attrib.y = y;
   1.288 +	cur_attrib.z = 0.0f;
   1.289 +	cur_attrib.w = 1.0f;
   1.290 +}
   1.291 +
   1.292 +void gl_vertex_attrib3f(int loc, float x, float y, float z)
   1.293 +{
   1.294 +	aloc = loc;
   1.295 +	cur_attrib.x = x;
   1.296 +	cur_attrib.y = y;
   1.297 +	cur_attrib.z = z;
   1.298 +	cur_attrib.w = 1.0f;
   1.299 +}
   1.300 +
   1.301 +void gl_vertex_attrib4f(int loc, float x, float y, float z, float w)
   1.302 +{
   1.303 +	aloc = loc;
   1.304 +	cur_attrib.x = x;
   1.305 +	cur_attrib.y = y;
   1.306 +	cur_attrib.z = z;
   1.307 +	cur_attrib.w = w;
   1.308 +}