labyrinth
diff src/gles/sanegl.c @ 3:45b91185b298
android port
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 01 May 2015 04:36:50 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/gles/sanegl.c Fri May 01 04:36:50 2015 +0300 1.3 @@ -0,0 +1,185 @@ 1.4 +/* 1.5 +SaneGL - a small library to bring back sanity to OpenGL ES 2.x 1.6 +Copyright (C) 2011-2013 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 <stdlib.h> 1.24 +#include <string.h> 1.25 +#include <math.h> 1.26 +#include <assert.h> 1.27 +#include "sanegl.h" 1.28 + 1.29 +#define MAX_VERTS 512 1.30 + 1.31 +static void gl_draw_immediate(void); 1.32 + 1.33 +typedef struct { float x, y; } vec2_t; 1.34 +typedef struct { float x, y, z; } vec3_t; 1.35 +typedef struct { float x, y, z, w; } vec4_t; 1.36 + 1.37 +static int prim = -1; 1.38 + 1.39 +static vec3_t cur_normal; 1.40 +static vec4_t cur_color; 1.41 +static vec2_t cur_texcoord; 1.42 + 1.43 +static vec4_t *vert_arr, *col_arr; 1.44 +static vec3_t *norm_arr; 1.45 +static vec2_t *texc_arr; 1.46 + 1.47 +static int num_verts, vert_calls; 1.48 + 1.49 +/* immediate mode rendering */ 1.50 +void gl_begin(int p) 1.51 +{ 1.52 + if(!vert_arr) { 1.53 + vert_arr = malloc(MAX_VERTS * sizeof *vert_arr); 1.54 + norm_arr = malloc(MAX_VERTS * sizeof *norm_arr); 1.55 + texc_arr = malloc(MAX_VERTS * sizeof *texc_arr); 1.56 + col_arr = malloc(MAX_VERTS * sizeof *col_arr); 1.57 + assert(vert_arr && norm_arr && texc_arr && col_arr); 1.58 + } 1.59 + 1.60 + prim = p; 1.61 + num_verts = vert_calls = 0; 1.62 +} 1.63 + 1.64 +void gl_end(void) 1.65 +{ 1.66 + if(num_verts > 0) { 1.67 + gl_draw_immediate(); 1.68 + } 1.69 +} 1.70 + 1.71 +static void gl_draw_immediate(void) 1.72 +{ 1.73 + int glprim; 1.74 + 1.75 + glprim = prim == GL_QUADS ? GL_TRIANGLES : prim; 1.76 + 1.77 + glEnableClientState(GL_VERTEX_ARRAY); 1.78 + glEnableClientState(GL_NORMAL_ARRAY); 1.79 + glEnableClientState(GL_COLOR_ARRAY); 1.80 + glEnableClientState(GL_TEXTURE_COORD_ARRAY); 1.81 + 1.82 + glVertexPointer(4, GL_FLOAT, 0, vert_arr); 1.83 + glNormalPointer(GL_FLOAT, 0, norm_arr); 1.84 + glColorPointer(4, GL_FLOAT, 0, col_arr); 1.85 + glTexCoordPointer(2, GL_FLOAT, 0, texc_arr); 1.86 + 1.87 + glDrawArrays(glprim, 0, num_verts); 1.88 + 1.89 + glDisableClientState(GL_VERTEX_ARRAY); 1.90 + glDisableClientState(GL_NORMAL_ARRAY); 1.91 + glDisableClientState(GL_COLOR_ARRAY); 1.92 + glDisableClientState(GL_TEXTURE_COORD_ARRAY); 1.93 +} 1.94 + 1.95 +void gl_vertex2f(float x, float y) 1.96 +{ 1.97 + gl_vertex4f(x, y, 0.0f, 1.0f); 1.98 +} 1.99 + 1.100 +void gl_vertex3f(float x, float y, float z) 1.101 +{ 1.102 + gl_vertex4f(x, y, z, 1.0f); 1.103 +} 1.104 + 1.105 +void gl_vertex4f(float x, float y, float z, float w) 1.106 +{ 1.107 + int i, buffer_full; 1.108 + 1.109 + if(prim == GL_QUADS && vert_calls % 4 == 3) { 1.110 + for(i=0; i<2; i++) { 1.111 + col_arr[num_verts] = col_arr[num_verts - 3 + i]; 1.112 + texc_arr[num_verts] = texc_arr[num_verts - 3 + i]; 1.113 + norm_arr[num_verts] = norm_arr[num_verts - 3 + i]; 1.114 + vert_arr[num_verts] = vert_arr[num_verts - 3 + i]; 1.115 + num_verts++; 1.116 + } 1.117 + } 1.118 + 1.119 + vert_arr[num_verts].x = x; 1.120 + vert_arr[num_verts].y = y; 1.121 + vert_arr[num_verts].z = z; 1.122 + vert_arr[num_verts].w = w; 1.123 + 1.124 + col_arr[num_verts] = cur_color; 1.125 + norm_arr[num_verts] = cur_normal; 1.126 + texc_arr[num_verts] = cur_texcoord; 1.127 + 1.128 + vert_calls++; 1.129 + num_verts++; 1.130 + 1.131 + if(prim == GL_QUADS) { 1.132 + /* leave space for 6 more worst-case and don't allow flushes mid-quad */ 1.133 + buffer_full = num_verts >= MAX_VERTS - 6 && vert_calls % 4 == 0; 1.134 + } else { 1.135 + buffer_full = num_verts >= MAX_VERTS - prim; 1.136 + } 1.137 + 1.138 + if(buffer_full) { 1.139 + gl_draw_immediate(); 1.140 + gl_begin(prim); /* reset everything */ 1.141 + } 1.142 +} 1.143 + 1.144 + 1.145 +void gl_normal3f(float x, float y, float z) 1.146 +{ 1.147 + cur_normal.x = x; 1.148 + cur_normal.y = y; 1.149 + cur_normal.z = z; 1.150 +} 1.151 + 1.152 + 1.153 +void gl_color3f(float r, float g, float b) 1.154 +{ 1.155 + cur_color.x = r; 1.156 + cur_color.y = g; 1.157 + cur_color.z = b; 1.158 + cur_color.w = 1.0f; 1.159 +} 1.160 + 1.161 +void gl_color4f(float r, float g, float b, float a) 1.162 +{ 1.163 + cur_color.x = r; 1.164 + cur_color.y = g; 1.165 + cur_color.z = b; 1.166 + cur_color.w = a; 1.167 +} 1.168 + 1.169 + 1.170 +void gl_texcoord1f(float s) 1.171 +{ 1.172 + cur_texcoord.x = s; 1.173 + cur_texcoord.y = 0.0f; 1.174 +} 1.175 + 1.176 +void gl_texcoord2f(float s, float t) 1.177 +{ 1.178 + cur_texcoord.x = s; 1.179 + cur_texcoord.y = t; 1.180 +} 1.181 + 1.182 +void glu_perspective(float fov, float aspect, float nearz, float farz) 1.183 +{ 1.184 + float fovrad = M_PI * fov / 180.0; 1.185 + float halfsz = tan(fovrad) * nearz; 1.186 + 1.187 + glFrustumf(-halfsz * aspect, halfsz * aspect, -halfsz, halfsz, nearz, farz); 1.188 +}