Font rendering

This commit is contained in:
John Alanbrook 2022-06-30 18:03:17 +00:00
parent 2bc9bb65af
commit edf16b4267
2 changed files with 81 additions and 71 deletions

View file

@ -22,64 +22,8 @@ static struct mShader *shader;
void font_init(struct mShader *textshader) { void font_init(struct mShader *textshader) {
shader = textshader; shader = textshader;
}
void font_frame(struct mSDLWindow *w) {
shader_use(shader); shader_use(shader);
}
struct sFont *MakeFont(const char *fontfile, int height)
{
struct sFont *newfont = calloc(1, sizeof(struct sFont));
newfont->height = height;
char fontpath[256];
snprintf(fontpath, 256, "fonts/%s", fontfile);
fread(ttf_buffer, 1, 1<<25, fopen(fontpath, "rb"));
stbtt_fontinfo fontinfo = { 0 };
if (!stbtt_InitFont(&fontinfo, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0))) {
printf("failed\n");
}
float scale = stbtt_ScaleForPixelHeight(&fontinfo, 64);
int ascent, descent, linegap;
stbtt_GetFontVMetrics(&fontinfo, &ascent, &descent, &linegap);
ascent = roundf(ascent*scale);
descent=roundf(descent*scale);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for (unsigned char c = 32; c < 128; c++) {
unsigned char *bitmap;
int advance, lsb, w, h;
stbtt_GetCodepointHMetrics(&fontinfo, c, &advance, &lsb);
bitmap = stbtt_GetCodepointBitmap(&fontinfo, 0,
stbtt_ScaleForPixelHeight(&fontinfo, newfont->height), c, &w, &h, 0, 0);
GLuint ftexture;
glGenTextures(1, &ftexture);
glBindTexture(GL_TEXTURE_2D, ftexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, bitmap);
//glGenerateMipmap(GL_TEXTURE_2D);
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_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
newfont->Characters[c].TextureID = ftexture;
newfont->Characters[c].Advance = advance;
newfont->Characters[c].Size[0] = w;
newfont->Characters[c].Size[1] = h;
newfont->Characters[c].Bearing[0] = lsb;
newfont->Characters[c].Bearing[1] = 0;
}
// configure VAO/VBO for texture quads // configure VAO/VBO for texture quads
glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &VAO);
@ -93,7 +37,67 @@ struct sFont *MakeFont(const char *fontfile, int height)
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0); glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
}
void font_frame(struct mSDLWindow *w) {
shader_use(shader);
}
// Height in pixels
struct sFont *MakeFont(const char *fontfile, int height)
{
shader_use(shader);
struct sFont *newfont = calloc(1, sizeof(struct sFont));
newfont->height = height;
char fontpath[256];
snprintf(fontpath, 256, "fonts/%s", fontfile);
fread(ttf_buffer, 1, 1<<25, fopen(fontpath, "rb"));
stbtt_fontinfo fontinfo;
if (!stbtt_InitFont(&fontinfo, ttf_buffer, stbtt_GetFontOffsetForIndex(ttf_buffer,0))) {
printf("failed\n");
}
float scale = stbtt_ScaleForPixelHeight(&fontinfo, height);
int ascent, descent, linegap;
stbtt_GetFontVMetrics(&fontinfo, &ascent, &descent, &linegap);
ascent = roundf(ascent*scale);
descent = roundf(descent*scale);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for (unsigned char c = 32; c < 128; c++) {
unsigned char *bitmap;
int advance, lsb, w, h, x0, y0;
stbtt_GetCodepointHMetrics(&fontinfo, c, &advance, &lsb);
bitmap = stbtt_GetCodepointBitmap(&fontinfo, scale, scale, c, &w, &h, &x0, &y0);
GLuint ftexture;
glGenTextures(1, &ftexture);
glBindTexture(GL_TEXTURE_2D, ftexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, bitmap);
//glGenerateMipmap(GL_TEXTURE_2D);
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_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
newfont->Characters[c].TextureID = ftexture;
newfont->Characters[c].Advance = advance * scale;
newfont->Characters[c].Size[0] = w;
newfont->Characters[c].Size[1] = h;
newfont->Characters[c].Bearing[0] = x0;
newfont->Characters[c].Bearing[1] = y0;
}
return newfont; return newfont;
} }
@ -157,7 +161,7 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct
*/ */
//// Character pass //// Character pass
shader_setvec3(shader, "textColor", color); //shader_setvec3(shader, "textColor", color);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(verts), verts); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(verts), verts);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

View file

@ -105,11 +105,14 @@ void openglInit()
animSpriteShader = MakeShader("animspritevert.glsl", "animspritefrag.glsl"); animSpriteShader = MakeShader("animspritevert.glsl", "animspritefrag.glsl");
textShader = MakeShader("textvert.glsl", "textfrag.glsl"); textShader = MakeShader("textvert.glsl", "textfrag.glsl");
shader_use(textShader);
shader_setint(textShader, "text", 0);
font_init(textShader); font_init(textShader);
sprite_initialize(); sprite_initialize();
debugdraw_init(); //debugdraw_init();
@ -118,12 +121,14 @@ void openglInit()
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLineWidth(1.3f);
/* //glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_3D); //glLineWidth(1.3f);
glEnable(GL_MULTISAMPLE);
glLineWidth(2); //glEnable(GL_TEXTURE_3D);
*/ //glEnable(GL_MULTISAMPLE);
//glLineWidth(2);
glGenBuffers(1, &projUBO); glGenBuffers(1, &projUBO);
glBindBuffer(GL_UNIFORM_BUFFER, projUBO); glBindBuffer(GL_UNIFORM_BUFFER, projUBO);
@ -150,40 +155,41 @@ void openglRender(struct mSDLWindow *window, struct mCamera *mcamera)
glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, projection); glBufferSubData(GL_UNIFORM_BUFFER, 0, 64, projection);
glColorMask(true, true, true, true);
glEnable(GL_CULL_FACE); //glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK); //glCullFace(GL_BACK);
// Clear color and depth // Clear color and depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
////// TEXT && GUI ////// TEXT && GUI
glDepthFunc(GL_ALWAYS); // glDepthFunc(GL_ALWAYS);
shader_use(textShader); shader_use(textShader);
//shader_setmat4(textShader, "projection", window->projection); shader_setmat4(textShader, "projection", projection);
mfloat_t fontpos[2] = { 25.f, 25.f }; mfloat_t fontpos[2] = { 25.f, 25.f };
mfloat_t fontcolor[3] = { 0.5f, 0.8f, 0.2f }; mfloat_t fontcolor[3] = { 0.5f, 0.8f, 0.2f };
renderText("Sample text", fontpos, 4.f, fontcolor, -1.f); renderText("Sample text", fontpos, 1.f, fontcolor, -1.f);
///// Sprites ///// Sprites
shader_use(spriteShader); shader_use(spriteShader);
shader_setmat4(spriteShader, "projection", window->projection); //shader_setmat4(spriteShader, "projection", window->projection);
for (int i = 0; i < numSprites; i++) { for (int i = 0; i < numSprites; i++) {
sprite_draw(sprites[i]); sprite_draw(sprites[i]);
} }
//glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
} }