3dphotoshoot
diff libs/drawtext/drawgl.c @ 10:c71c477521ca
converting to GLES2 and C++
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 31 May 2015 00:40:26 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/drawtext/drawgl.c Sun May 31 00:40:26 2015 +0300 1.3 @@ -0,0 +1,302 @@ 1.4 +/* 1.5 +libdrawtext - a simple library for fast text rendering in OpenGL 1.6 +Copyright (C) 2011-2012 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 Lesser 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 Lesser General Public License for more details. 1.17 + 1.18 +You should have received a copy of the GNU Lesser General Public License 1.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 1.20 +*/ 1.21 +#ifndef NO_OPENGL 1.22 +#include <stdarg.h> 1.23 +#include <math.h> 1.24 +#include <ctype.h> 1.25 + 1.26 +#include <stdlib.h> 1.27 + 1.28 +#ifndef _MSC_VER 1.29 +#include <alloca.h> 1.30 +#else 1.31 +#include <malloc.h> 1.32 +#endif 1.33 + 1.34 +#ifdef TARGET_IPHONE 1.35 +#include <OpenGLES/ES2/gl.h> 1.36 +#define GL_ES 1.37 +#elif defined(ANDROID) 1.38 +#include <GLES2/gl2.h> 1.39 +#define GL_ES 1.40 +#else 1.41 +#include <GL/glew.h> 1.42 +#endif 1.43 + 1.44 +#include "drawtext.h" 1.45 +#include "drawtext_impl.h" 1.46 + 1.47 +struct vertex { 1.48 + float x, y; 1.49 + float s, t; 1.50 +}; 1.51 + 1.52 +struct quad { 1.53 + struct vertex v[6]; 1.54 +}; 1.55 + 1.56 +static void cleanup(void); 1.57 +static void add_glyph(struct glyph *g, float x, float y); 1.58 + 1.59 +#define QBUF_SZ 512 1.60 +static struct quad *qbuf; 1.61 +static int num_quads; 1.62 +static int vattr = -1; 1.63 +static int tattr = -1; 1.64 +static unsigned int font_tex; 1.65 +static int buf_mode = DTX_NBF; 1.66 + 1.67 + 1.68 +int dtx_gl_init(void) 1.69 +{ 1.70 + if(qbuf) { 1.71 + return 0; /* already initialized */ 1.72 + } 1.73 + 1.74 +#ifdef __glew_h__ 1.75 + glewInit(); 1.76 +#endif 1.77 + 1.78 + if(!(qbuf = malloc(QBUF_SZ * sizeof *qbuf))) { 1.79 + return -1; 1.80 + } 1.81 + num_quads = 0; 1.82 + 1.83 + atexit(cleanup); 1.84 + return 0; 1.85 +} 1.86 + 1.87 +static void cleanup(void) 1.88 +{ 1.89 + free(qbuf); 1.90 +} 1.91 + 1.92 + 1.93 +void dtx_vertex_attribs(int vert_attr, int tex_attr) 1.94 +{ 1.95 + vattr = vert_attr; 1.96 + tattr = tex_attr; 1.97 +} 1.98 + 1.99 +static void set_glyphmap_texture(struct dtx_glyphmap *gmap) 1.100 +{ 1.101 + if(!gmap->tex) { 1.102 + glGenTextures(1, &gmap->tex); 1.103 + glBindTexture(GL_TEXTURE_2D, gmap->tex); 1.104 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 1.105 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.106 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 1.107 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 1.108 + 1.109 +#ifdef GL_ES 1.110 + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gmap->xsz, gmap->ysz, 0, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels); 1.111 + glGenerateMipmap(GL_TEXTURE_2D); 1.112 +#else 1.113 + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, gmap->xsz, gmap->ysz, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels); 1.114 +#endif 1.115 + } 1.116 + 1.117 + if(font_tex != gmap->tex) { 1.118 + dtx_flush(); 1.119 + } 1.120 + font_tex = gmap->tex; 1.121 +} 1.122 + 1.123 +void dtx_glyph(int code) 1.124 +{ 1.125 + struct dtx_glyphmap *gmap; 1.126 + 1.127 + if(!dtx_font || !(gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code))) { 1.128 + return; 1.129 + } 1.130 + set_glyphmap_texture(gmap); 1.131 + 1.132 + add_glyph(gmap->glyphs + code - gmap->cstart, 0, 0); 1.133 + dtx_flush(); 1.134 +} 1.135 + 1.136 +static const char *put_char(const char *str, float *pos_x, float *pos_y, int *should_flush) 1.137 +{ 1.138 + struct dtx_glyphmap *gmap; 1.139 + float px, py; 1.140 + int code = dtx_utf8_char_code(str); 1.141 + str = dtx_utf8_next_char((char*)str); 1.142 + 1.143 + if(buf_mode == DTX_LBF && code == '\n') { 1.144 + *should_flush = 1; 1.145 + } 1.146 + 1.147 + px = *pos_x; 1.148 + py = *pos_y; 1.149 + 1.150 + if((gmap = dtx_proc_char(code, pos_x, pos_y))) { 1.151 + int idx = code - gmap->cstart; 1.152 + 1.153 + set_glyphmap_texture(gmap); 1.154 + add_glyph(gmap->glyphs + idx, px, py); 1.155 + } 1.156 + return str; 1.157 +} 1.158 + 1.159 +void dtx_string(const char *str) 1.160 +{ 1.161 + int should_flush = buf_mode == DTX_NBF; 1.162 + float pos_x = 0.0f; 1.163 + float pos_y = 0.0f; 1.164 + 1.165 + if(!dtx_font) { 1.166 + return; 1.167 + } 1.168 + 1.169 + while(*str) { 1.170 + str = put_char(str, &pos_x, &pos_y, &should_flush); 1.171 + } 1.172 + 1.173 + if(should_flush) { 1.174 + dtx_flush(); 1.175 + } 1.176 +} 1.177 + 1.178 +void dtx_printf(const char *fmt, ...) 1.179 +{ 1.180 + va_list ap; 1.181 + int buf_size; 1.182 + char *buf, tmp; 1.183 + 1.184 + if(!dtx_font) { 1.185 + return; 1.186 + } 1.187 + 1.188 + va_start(ap, fmt); 1.189 + buf_size = vsnprintf(&tmp, 0, fmt, ap); 1.190 + va_end(ap); 1.191 + 1.192 + if(buf_size == -1) { 1.193 + buf_size = 512; 1.194 + } 1.195 + 1.196 + buf = alloca(buf_size + 1); 1.197 + va_start(ap, fmt); 1.198 + vsnprintf(buf, buf_size + 1, fmt, ap); 1.199 + va_end(ap); 1.200 + 1.201 + dtx_string(buf); 1.202 +} 1.203 + 1.204 +static void qvertex(struct vertex *v, float x, float y, float s, float t) 1.205 +{ 1.206 + v->x = x; 1.207 + v->y = y; 1.208 + v->s = s; 1.209 + v->t = t; 1.210 +} 1.211 + 1.212 +static void add_glyph(struct glyph *g, float x, float y) 1.213 +{ 1.214 + struct quad *qptr = qbuf + num_quads; 1.215 + 1.216 + x -= g->orig_x; 1.217 + y -= g->orig_y; 1.218 + 1.219 + qvertex(qptr->v + 0, x, y, g->nx, g->ny + g->nheight); 1.220 + qvertex(qptr->v + 1, x + g->width, y, g->nx + g->nwidth, g->ny + g->nheight); 1.221 + qvertex(qptr->v + 2, x + g->width, y + g->height, g->nx + g->nwidth, g->ny); 1.222 + 1.223 + qvertex(qptr->v + 3, x, y, g->nx, g->ny + g->nheight); 1.224 + qvertex(qptr->v + 4, x + g->width, y + g->height, g->nx + g->nwidth, g->ny); 1.225 + qvertex(qptr->v + 5, x, y + g->height, g->nx, g->ny); 1.226 + 1.227 + if(++num_quads >= QBUF_SZ) { 1.228 + dtx_flush(); 1.229 + } 1.230 +} 1.231 + 1.232 +void dtx_flush(void) 1.233 +{ 1.234 + int vbo; 1.235 + 1.236 + if(!num_quads) { 1.237 + return; 1.238 + } 1.239 + 1.240 + glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &vbo); 1.241 + glBindBuffer(GL_ARRAY_BUFFER, 0); 1.242 + 1.243 +#ifndef GL_ES 1.244 + glPushAttrib(GL_ENABLE_BIT); 1.245 + glEnable(GL_TEXTURE_2D); 1.246 +#endif 1.247 + glBindTexture(GL_TEXTURE_2D, font_tex); 1.248 + 1.249 + if(vattr != -1) { 1.250 + glEnableVertexAttribArray(vattr); 1.251 + glVertexAttribPointer(vattr, 2, GL_FLOAT, 0, sizeof(struct vertex), qbuf); 1.252 +#ifndef GL_ES 1.253 + } else { 1.254 + glEnableClientState(GL_VERTEX_ARRAY); 1.255 + glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), qbuf); 1.256 +#endif 1.257 + } 1.258 + if(tattr != -1) { 1.259 + glEnableVertexAttribArray(tattr); 1.260 + glVertexAttribPointer(tattr, 2, GL_FLOAT, 0, sizeof(struct vertex), &qbuf->v[0].s); 1.261 +#ifndef GL_ES 1.262 + } else { 1.263 + glEnableClientState(GL_TEXTURE_COORD_ARRAY); 1.264 + glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), &qbuf->v[0].s); 1.265 +#endif 1.266 + } 1.267 + 1.268 + glEnable(GL_BLEND); 1.269 + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 1.270 + 1.271 + glDepthMask(0); 1.272 + 1.273 + glDrawArrays(GL_TRIANGLES, 0, num_quads * 6); 1.274 + 1.275 + glDepthMask(1); 1.276 + 1.277 + if(vattr != -1) { 1.278 + glDisableVertexAttribArray(vattr); 1.279 +#ifndef GL_ES 1.280 + } else { 1.281 + glDisableClientState(GL_VERTEX_ARRAY); 1.282 +#endif 1.283 + } 1.284 + if(tattr != -1) { 1.285 + glDisableVertexAttribArray(tattr); 1.286 +#ifndef GL_ES 1.287 + } else { 1.288 + glDisableClientState(GL_TEXTURE_COORD_ARRAY); 1.289 +#endif 1.290 + } 1.291 + 1.292 +#ifndef GL_ES 1.293 + glPopAttrib(); 1.294 +#else 1.295 + glDisable(GL_BLEND); 1.296 +#endif 1.297 + 1.298 + if(vbo) { 1.299 + glBindBuffer(GL_ARRAY_BUFFER, vbo); 1.300 + } 1.301 + 1.302 + num_quads = 0; 1.303 +} 1.304 + 1.305 +#endif /* !def NO_OPENGL */