font machine still broke

This commit is contained in:
nullifiedcat 2017-11-16 12:38:00 +03:00
parent 9f372c4623
commit 19ede3b694
9 changed files with 62 additions and 50 deletions

View File

@ -1,5 +1,5 @@
CC=$(shell sh -c "which gcc-7 || which gcc") CC=$(shell sh -c "which gcc-7 || which gcc")
CFLAGS=-O3 -Wall -fPIC -fmessage-length=0 -D_GNU_SOURCE=1 -DFREETYPE_GL_USE_VAO=1 -g3 -ggdb -Iinclude -isystemftgl -isystem/usr/local/include/freetype2 -isystem/usr/include/freetype2 CFLAGS=-O3 -Wall -fPIC -fmessage-length=0 -D_GNU_SOURCE=1 -g3 -ggdb -Iinclude -isystemftgl -isystem/usr/local/include/freetype2 -isystem/usr/include/freetype2
LDFLAGS=-shared -Wl,--no-undefined LDFLAGS=-shared -Wl,--no-undefined
LDLIBS=-lm -lX11 -lXext -lrt -lpthread -lXfixes -lGL -lfreetype -lGLEW -lpng LDLIBS=-lm -lX11 -lXext -lrt -lpthread -lXfixes -lGL -lfreetype -lGLEW -lpng
SRC_DIR=src SRC_DIR=src

Binary file not shown.

Binary file not shown.

View File

@ -135,6 +135,19 @@ vertex_attribute_enable( vertex_attribute_t *attr )
} }
} }
glEnableVertexAttribArray( attr->index ); glEnableVertexAttribArray( attr->index );
switch (attr->type)
{
case GL_UNSIGNED_SHORT:
case GL_UNSIGNED_INT:
case GL_UNSIGNED_BYTE:
case GL_SHORT:
case GL_INT:
case GL_BYTE:
glVertexAttribIPointer( attr->index, attr->size, attr->type,
attr->stride, attr->pointer );
break;
default:
glVertexAttribPointer( attr->index, attr->size, attr->type, glVertexAttribPointer( attr->index, attr->size, attr->type,
attr->normalized, attr->stride, attr->pointer ); attr->normalized, attr->stride, attr->pointer );
}
} }

View File

@ -12,6 +12,6 @@ struct vertex_main
vec2 position; vec2 position;
vec2 tex_coords; vec2 tex_coords;
vec4 color; vec4 color;
unsigned mode; int mode;
}; };

View File

@ -329,7 +329,7 @@ xoverlay_draw_rect_textured(float x, float y, float w, float h, xoverlay_rgba_t
if (tex == NULL) if (tex == NULL)
return; return;
ds_bind_texture(tex->texture_id); textureapi_bind(texture);
x += 0.5f; x += 0.5f;
y += 0.5f; y += 0.5f;
@ -358,7 +358,7 @@ xoverlay_draw_rect_textured(float x, float y, float w, float h, xoverlay_rgba_t
vertices[1].tex_coords.x = s0; vertices[1].tex_coords.x = s0;
vertices[1].tex_coords.y = t0; vertices[1].tex_coords.y = t0;
vertices[1].color = *(vec4*)&color; vertices[1].color = *(vec4*)&color;
vertices[0].mode = DRAW_MODE_TEXTURED; vertices[1].mode = DRAW_MODE_TEXTURED;
vertices[2].position.x = x + w; vertices[2].position.x = x + w;
vertices[2].position.y = y + h; vertices[2].position.y = y + h;
@ -386,6 +386,17 @@ draw_string_internal(float x, float y, const char *string, texture_font_t *fnt,
texture_font_load_glyphs(fnt, string); texture_font_load_glyphs(fnt, string);
if (fnt->atlas->dirty)
{
log_write("Atlas updated\n");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, fnt->atlas->width, fnt->atlas->height, 0, GL_RED, GL_UNSIGNED_BYTE, fnt->atlas->data);
fnt->atlas->dirty = 0;
}
int len = strlen(string); int len = strlen(string);
if (len == 0) if (len == 0)
return; return;
@ -422,12 +433,12 @@ draw_string_internal(float x, float y, const char *string, texture_font_t *fnt,
indices[i * 6 + 3] = idx + 2; indices[i * 6 + 3] = idx + 2;
indices[i * 6 + 4] = idx + 3; indices[i * 6 + 4] = idx + 3;
indices[i * 6 + 5] = idx; indices[i * 6 + 5] = idx;
idx += 6; idx += 4;
vertices[i * 4 + 0] = (struct vertex_main){ (vec2){ x0, y0 }, (vec2){ s0, t0 }, color, DRAW_MODE_FREETYPE }; vertices[i * 4 + 0] = (struct vertex_main){ (vec2){ x0, y0 }, (vec2){ s0, t0 }, color, DRAW_MODE_TEXTURED };
vertices[i * 4 + 1] = (struct vertex_main){ (vec2){ x0, y1 }, (vec2){ s0, t1 }, color, DRAW_MODE_FREETYPE }; vertices[i * 4 + 1] = (struct vertex_main){ (vec2){ x0, y1 }, (vec2){ s0, t1 }, color, DRAW_MODE_TEXTURED };
vertices[i * 4 + 2] = (struct vertex_main){ (vec2){ x1, y1 }, (vec2){ s1, t1 }, color, DRAW_MODE_FREETYPE }; vertices[i * 4 + 2] = (struct vertex_main){ (vec2){ x1, y1 }, (vec2){ s1, t1 }, color, DRAW_MODE_TEXTURED };
vertices[i * 4 + 3] = (struct vertex_main){ (vec2){ x1, y0 }, (vec2){ s1, t0 }, color, DRAW_MODE_FREETYPE }; vertices[i * 4 + 3] = (struct vertex_main){ (vec2){ x1, y0 }, (vec2){ s1, t0 }, color, DRAW_MODE_TEXTURED };
pen_x += glyph->advance_x; pen_x += glyph->advance_x;
if (glyph->height > size_y) if (glyph->height > size_y)

View File

@ -36,22 +36,6 @@ ds_destroy()
{ {
} }
void
ds_mark_dirty()
{
ds.dirty = 1;
}
void
ds_render_if_needed()
{
if (ds.dirty)
{
program_draw();
}
ds.dirty = 0;
}
void void
ds_pre_render() ds_pre_render()
{ {
@ -88,6 +72,8 @@ ds_pre_render()
void void
ds_post_render() ds_post_render()
{ {
program_draw();
program_reset();
glPopClientAttrib(); glPopClientAttrib();
glPopAttrib(); glPopAttrib();
glFlush(); glFlush();
@ -100,7 +86,8 @@ ds_bind_texture(GLuint texture)
{ {
if (ds.texture != texture) if (ds.texture != texture)
{ {
ds_render_if_needed(); program_draw();
program_reset();
ds.texture = texture; ds.texture = texture;
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
} }

View File

@ -38,6 +38,7 @@ xoverlay_font_load(const char *path, float size)
result.atlas = texture_atlas_new(1024, 1024, 1); result.atlas = texture_atlas_new(1024, 1024, 1);
if (result.atlas != NULL) if (result.atlas != NULL)
{ {
glGenTextures(1, &result.atlas->id);
result.font = texture_font_new_from_file(result.atlas, size, path); result.font = texture_font_new_from_file(result.atlas, size, path);
if (result.font == NULL) if (result.font == NULL)
{ {

View File

@ -11,7 +11,7 @@ GLuint compile_shader(const char *source, GLenum type)
glGetShaderiv(shader, GL_COMPILE_STATUS, &status); glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) if (status == GL_FALSE)
{ {
char error_log[256]; char error_log[512];
glGetShaderInfoLog(shader, sizeof(error_log), NULL, error_log); glGetShaderInfoLog(shader, sizeof(error_log), NULL, error_log);
log_write("GL Shader compilation error:\n%s\n", error_log); log_write("GL Shader compilation error:\n%s\n", error_log);
exit(1); exit(1);
@ -31,16 +31,16 @@ setup_matrices(GLuint shader, int textures)
} }
const char *shader_ultimate_vert = const char *shader_ultimate_vert =
"#version 150\n" "#version 330\n"
"\n" "\n"
"uniform mat4 model;\n" "uniform mat4 model;\n"
"uniform mat4 view;\n" "uniform mat4 view;\n"
"uniform mat4 projection;\n" "uniform mat4 projection;\n"
"attribute vec2 vertex;\n" "in vec2 vertex;\n"
"attribute vec2 tex_coord;\n" "in vec2 tex_coord;\n"
"attribute vec4 color;\n" "in vec4 color;\n"
"attribute uint draw_mode;\n" "in int drawmode;\n"
"flat out uint frag_DrawMode;\n" "flat out int frag_DrawMode;\n"
"out vec4 frag_Color;\n" "out vec4 frag_Color;\n"
"out vec2 frag_TexCoord;\n" "out vec2 frag_TexCoord;\n"
"void main()\n" "void main()\n"
@ -48,35 +48,35 @@ const char *shader_ultimate_vert =
" frag_TexCoord = tex_coord;\n" " frag_TexCoord = tex_coord;\n"
" frag_Color = color;\n" " frag_Color = color;\n"
" gl_Position = projection*(view*(model*vec4(vertex,0.0,1.0)));\n" " gl_Position = projection*(view*(model*vec4(vertex,0.0,1.0)));\n"
" frag_DrawMode = draw_mode;\n" " frag_DrawMode = drawmode;\n"
"}\n"; "}";
const char *shader_ultimate_frag = const char *shader_ultimate_frag =
"#version 150\n" "#version 330\n"
"\n" "\n"
"uniform sampler2D texture;\n" "uniform sampler2D texture;\n"
"out vec4 out_Color;\n"
"in vec4 frag_Color;\n" "in vec4 frag_Color;\n"
"in vec2 frag_TexCoord;\n" "in vec2 frag_TexCoord;\n"
"flat in uint frag_DrawMode;\n" "flat in int frag_DrawMode;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
" if (frag_DrawMode == 1u)\n" " if (frag_DrawMode == 1)\n"
" out_Color = frag_Color;\n" " gl_FragColor = frag_Color;\n"
" else\n" " else\n"
" {\n" " {\n"
" vec4 tex = texture2D(texture, frag_TexCoord);\n" " vec4 tex = texture2D(texture, frag_TexCoord);\n"
" if (frag_DrawMode == 2u)\n" " if (frag_DrawMode == 2)\n"
" out_Color = frag_Color * tex;\n" " gl_FragColor = frag_Color * tex;\n"
" else if (frag_DrawMode == 3u)\n" " else if (frag_DrawMode == 3)\n"
" out_Color = vec4(in_Color.rgb, frag_Color.a * texture2D(texture, frag_TexCoord).r);\n" " gl_FragColor = vec4(frag_Color.rgb, frag_Color.a * texture2D(texture, frag_TexCoord).r);\n"
" else\n" " else\n"
" out_Color = vec4(1.0, 0.0, 0.0, 1.0);\n" " gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);\n"
"}\n"; " }\n"
"}";
void void
program_init() program_init()
{ {
program.buffer = vertex_buffer_new("vertex:2f,tex_coord:2f,color:4f,draw_mode:1I"); program.buffer = vertex_buffer_new("vertex:2f,tex_coord:2f,color:4f,drawmode:1i");
program.shader = glCreateProgram(); program.shader = glCreateProgram();
GLint status; GLint status;
GLuint sh_frag = compile_shader(shader_ultimate_frag, GL_FRAGMENT_SHADER); GLuint sh_frag = compile_shader(shader_ultimate_frag, GL_FRAGMENT_SHADER);
@ -92,7 +92,7 @@ program_init()
if (status == GL_FALSE) if (status == GL_FALSE)
{ {
char error_log[512]; char error_log[512];
glGetShaderInfoLog(program.shader, sizeof(error_log), NULL, error_log); glGetProgramInfoLog(program.shader, sizeof(error_log), NULL, error_log);
log_write("GL Shader linking error:\n%s\n", error_log); log_write("GL Shader linking error:\n%s\n", error_log);
exit(1); exit(1);
} }