diff options
author | Mike Erwin <significant.bit@gmail.com> | 2016-10-11 21:36:16 +0300 |
---|---|---|
committer | Mike Erwin <significant.bit@gmail.com> | 2016-10-11 21:36:16 +0300 |
commit | 53d82c3e8d608bc05e40e26950a98d15b16c562c (patch) | |
tree | 0690b828537db41ce10453875f6d3e6eee6aadd2 /source/blender/blenfont/intern | |
parent | 2fe7e70e928a80276bb4fbcfa9f32419d6c4baa7 (diff) |
BLF/OpenGL: draw text with new immediate mode
Part of T49043
Diffstat (limited to 'source/blender/blenfont/intern')
-rw-r--r-- | source/blender/blenfont/intern/blf.c | 23 | ||||
-rw-r--r-- | source/blender/blenfont/intern/blf_glyph.c | 70 | ||||
-rw-r--r-- | source/blender/blenfont/intern/blf_internal.h | 5 |
3 files changed, 52 insertions, 46 deletions
diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c index d7fcb2c727f..9bd43f7a69e 100644 --- a/source/blender/blenfont/intern/blf.c +++ b/source/blender/blenfont/intern/blf.c @@ -57,6 +57,7 @@ #ifndef BLF_STANDALONE #include "GPU_shader.h" +#include "GPU_immediate.h" #endif #include "blf_internal_types.h" @@ -500,10 +501,6 @@ static void blf_draw_gl__start(FontBLF *font, GLint *mode) glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); -#ifndef BLF_STANDALONE - GPU_shader_bind(GPU_shader_get_builtin_shader(GPU_SHADER_TEXT)); -#endif - /* Save the current matrix mode. */ glGetIntegerv(GL_MATRIX_MODE, mode); @@ -522,8 +519,20 @@ static void blf_draw_gl__start(FontBLF *font, GLint *mode) if (font->flags & BLF_ROTATION) /* radians -> degrees */ glRotatef(font->angle * (float)(180.0 / M_PI), 0.0f, 0.0f, 1.0f); - if (font->shadow || font->blur) - glGetFloatv(GL_CURRENT_COLOR, font->orig_col); + glGetFloatv(GL_CURRENT_COLOR, font->orig_col); /* TODO(merwin): new BLF_color function? */ + +#ifndef BLF_STANDALONE + VertexFormat *format = immVertexFormat(); + unsigned pos = add_attrib(format, "pos", GL_FLOAT, 2, KEEP_FLOAT); + unsigned texCoord = add_attrib(format, "texCoord", GL_FLOAT, 2, KEEP_FLOAT); + unsigned color = add_attrib(format, "color", GL_FLOAT, 4, KEEP_FLOAT); + + BLI_assert(pos == BLF_POS_ID); + BLI_assert(texCoord == BLF_COORD_ID); + BLI_assert(color == BLF_COLOR_ID); + + immBindBuiltinProgram(GPU_SHADER_TEXT); +#endif /* always bind the texture for the first glyph */ font->tex_bind_state = -1; @@ -537,7 +546,7 @@ static void blf_draw_gl__end(GLint mode) glMatrixMode(mode); #ifndef BLF_STANDALONE - GPU_shader_unbind(); + immUnbindProgram(); #endif glDisable(GL_BLEND); diff --git a/source/blender/blenfont/intern/blf_glyph.c b/source/blender/blenfont/intern/blf_glyph.c index aa7d539538b..2354769fee0 100644 --- a/source/blender/blenfont/intern/blf_glyph.c +++ b/source/blender/blenfont/intern/blf_glyph.c @@ -56,7 +56,7 @@ #include "BLF_api.h" #ifndef BLF_STANDALONE -#include "GPU_basic_shader.h" +#include "GPU_immediate.h" #endif #include "blf_internal_types.h" @@ -182,17 +182,6 @@ static void blf_glyph_cache_texture(FontBLF *font, GlyphCacheBLF *gc) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - -#ifndef BLF_STANDALONE - /* needed since basic shader doesn't support alpha-only textures, - * while we could add support this is only used in a few places - * (an alternative could be to have a simple shader for BLF). */ - if (GLEW_ARB_texture_swizzle && GPU_basic_shader_use_glsl_get()) { - GLint swizzle_mask[] = {GL_ONE, GL_ONE, GL_ONE, GL_ALPHA}; - glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzle_mask); - } -#endif - glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA8, gc->p2_width, gc->p2_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, NULL); } @@ -328,19 +317,17 @@ void blf_glyph_free(GlyphBLF *g) static void blf_texture_draw(float uv[2][2], float dx, float y1, float dx1, float y2) { - glBegin(GL_QUADS); - glTexCoord2f(uv[0][0], uv[0][1]); - glVertex2f(dx, y1); - - glTexCoord2f(uv[0][0], uv[1][1]); - glVertex2f(dx, y2); - - glTexCoord2f(uv[1][0], uv[1][1]); - glVertex2f(dx1, y2); - - glTexCoord2f(uv[1][0], uv[0][1]); - glVertex2f(dx1, y1); - glEnd(); + immAttrib2f(BLF_COORD_ID, uv[0][0], uv[0][1]); + immVertex2f(BLF_POS_ID, dx, y1); + + immAttrib2f(BLF_COORD_ID, uv[0][0], uv[1][1]); + immVertex2f(BLF_POS_ID, dx, y2); + + immAttrib2f(BLF_COORD_ID, uv[1][0], uv[1][1]); + immVertex2f(BLF_POS_ID, dx1, y2); + + immAttrib2f(BLF_COORD_ID, uv[1][0], uv[0][1]); + immVertex2f(BLF_POS_ID, dx1, y1); } static void blf_texture5_draw(const float shadow_col[4], float uv[2][2], float x1, float y1, float x2, float y2) @@ -350,7 +337,7 @@ static void blf_texture5_draw(const float shadow_col[4], float uv[2][2], float x 2 / 60.0f, 5 / 60.0f, 8 / 60.0f, 5 / 60.0f, 2 / 60.0f, 1 / 60.0f, 3 / 60.0f, 5 / 60.0f, 3 / 60.0f, 1 / 60.0f, 1 / 60.0f, 1 / 60.0f, 2 / 60.0f, 1 / 60.0f, 1 / 60.0f}; - + const float *fp = soft; float color[4]; float dx, dy; @@ -358,16 +345,14 @@ static void blf_texture5_draw(const float shadow_col[4], float uv[2][2], float x color[0] = shadow_col[0]; color[1] = shadow_col[1]; color[2] = shadow_col[2]; - + for (dx = -2; dx < 3; dx++) { for (dy = -2; dy < 3; dy++, fp++) { color[3] = *(fp) * shadow_col[3]; - glColor4fv(color); + immAttrib4fv(BLF_COLOR_ID, color); blf_texture_draw(uv, x1 + dx, y1 + dy, x2 + dx, y2 + dy); } } - - glColor4fv(color); } static void blf_texture3_draw(const float shadow_col[4], float uv[2][2], float x1, float y1, float x2, float y2) @@ -387,12 +372,10 @@ static void blf_texture3_draw(const float shadow_col[4], float uv[2][2], float x for (dx = -1; dx < 2; dx++) { for (dy = -1; dy < 2; dy++, fp++) { color[3] = *(fp) * shadow_col[3]; - glColor4fv(color); + immAttrib4fv(BLF_COLOR_ID, color); blf_texture_draw(uv, x1 + dx, y1 + dy, x2 + dx, y2 + dy); } } - - glColor4fv(color); } static void blf_glyph_calc_rect(rctf *rect, GlyphBLF *g, float x, float y) @@ -486,6 +469,18 @@ void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y) glBindTexture(GL_TEXTURE_2D, (font->tex_bind_state = g->tex)); } +#if 0 /* determine exact count of quads */ + unsigned quad_ct = 1 + (unsigned)font->blur; + if (font->flags & BLF_SHADOW) + quad_ct += (unsigned)font->shadow; + + immBegin(GL_QUADS, quad_ct * 4); +#else + immBeginAtMost(GL_QUADS, 40); /* (5 shadow + 5 blur) * 4 verts per quad */ +#endif + + /* TODO: blur & shadow in shader, single quad per glyph */ + if (font->flags & BLF_SHADOW) { rctf rect_ofs; blf_glyph_calc_rect(&rect_ofs, g, @@ -500,12 +495,9 @@ void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y) blf_texture5_draw(font->shadow_col, g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax); break; default: - glColor4fv(font->shadow_col); + immAttrib4fv(BLF_COLOR_ID, font->shadow_col); blf_texture_draw(g->uv, rect_ofs.xmin, rect_ofs.ymin, rect_ofs.xmax, rect_ofs.ymax); - break; } - - glColor4fv(font->orig_col); } switch (font->blur) { @@ -516,9 +508,9 @@ void blf_glyph_render(FontBLF *font, GlyphBLF *g, float x, float y) blf_texture5_draw(font->orig_col, g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax); break; default: + immAttrib4fv(BLF_COLOR_ID, font->orig_col); blf_texture_draw(g->uv, rect.xmin, rect.ymin, rect.xmax, rect.ymax); - break; } - return; + immEnd(); } diff --git a/source/blender/blenfont/intern/blf_internal.h b/source/blender/blenfont/intern/blf_internal.h index d9d758ce548..ba17e050399 100644 --- a/source/blender/blenfont/intern/blf_internal.h +++ b/source/blender/blenfont/intern/blf_internal.h @@ -37,6 +37,11 @@ struct GlyphBLF; struct GlyphCacheBLF; struct rctf; +/* vertex attribute IDs (fixed IDs so we don't have to pass them around) */ +#define BLF_POS_ID 0 +#define BLF_COORD_ID 1 +#define BLF_COLOR_ID 2 + unsigned int blf_next_p2(unsigned int x); unsigned int blf_hash(unsigned int val); |