BLF/OpenGL: draw text with new immediate mode

Part of T49043
This commit is contained in:
2016-10-11 14:36:16 -04:00
parent 2fe7e70e92
commit 53d82c3e8d
5 changed files with 73 additions and 62 deletions

View File

@@ -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);

View File

@@ -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);
immAttrib2f(BLF_COORD_ID, uv[0][0], uv[0][1]);
immVertex2f(BLF_POS_ID, dx, y1);
glTexCoord2f(uv[0][0], uv[1][1]);
glVertex2f(dx, y2);
immAttrib2f(BLF_COORD_ID, uv[0][0], uv[1][1]);
immVertex2f(BLF_POS_ID, dx, y2);
glTexCoord2f(uv[1][0], uv[1][1]);
glVertex2f(dx1, y2);
immAttrib2f(BLF_COORD_ID, uv[1][0], uv[1][1]);
immVertex2f(BLF_POS_ID, dx1, y2);
glTexCoord2f(uv[1][0], uv[0][1]);
glVertex2f(dx1, y1);
glEnd();
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)
@@ -362,12 +349,10 @@ static void blf_texture5_draw(const float shadow_col[4], float uv[2][2], float x
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();
}

View File

@@ -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);

View File

@@ -1,12 +1,13 @@
#if __VERSION__ == 120
flat varying vec4 color;
noperspective varying vec2 texcoord;
flat varying vec4 color_flat;
noperspective varying vec2 texCoord_interp;
#define fragColor gl_FragColor
#else
flat in vec4 color;
noperspective in vec2 texcoord;
flat in vec4 color_flat;
noperspective in vec2 texCoord_interp;
out vec4 fragColor;
#define texture2D texture
#endif
uniform sampler2D glyph;
@@ -14,8 +15,8 @@ uniform sampler2D glyph;
void main()
{
// input color replaces texture color
fragColor.rgb = color.rgb;
fragColor.rgb = color_flat.rgb;
// modulate input alpha & texture alpha
fragColor.a = color.a * texture2D(glyph, texcoord).a;
fragColor.a = color_flat.a * texture2D(glyph, texCoord_interp).a;
}

View File

@@ -1,20 +1,24 @@
// TODO(merwin):
// - uniform color, not per vertex
// - generic attrib inputs (2D pos, tex coord)
uniform mat4 ModelViewProjectionMatrix;
#if __VERSION__ == 120
flat varying vec4 color;
noperspective varying vec2 texcoord;
attribute vec2 pos;
attribute vec2 texCoord;
attribute vec4 color;
flat varying vec4 color_flat;
noperspective varying vec2 texCoord_interp;
#else
flat out vec4 color;
noperspective out vec2 texcoord;
in vec2 pos;
in vec2 texCoord;
in vec4 color;
flat out vec4 color_flat;
noperspective out vec2 texCoord_interp;
#endif
void main()
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_Position = ModelViewProjectionMatrix * vec4(pos, 0.0, 1.0);
color = gl_Color;
texcoord = gl_MultiTexCoord0.st;
color_flat = color;
texCoord_interp = texCoord;
}