Text rendering works again.

This commit is contained in:
John Alanbrook 2022-07-01 16:14:43 +00:00
parent e96d7fb701
commit 3190c7edcd
4 changed files with 37 additions and 38 deletions

View file

@ -78,6 +78,7 @@ struct sFont *MakeFont(const char *fontfile, int height)
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;
@ -85,7 +86,7 @@ struct sFont *MakeFont(const char *fontfile, int height)
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);
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);
@ -96,7 +97,8 @@ struct sFont *MakeFont(const char *fontfile, int height)
newfont->Characters[c].Size[0] = w;
newfont->Characters[c].Size[1] = h;
newfont->Characters[c].Bearing[0] = x0;
newfont->Characters[c].Bearing[1] = y0;
newfont->Characters[c].Bearing[1] = y0*-1;
printf("Y0 is %d for %c.\n", y0, c);
}
return newfont;
@ -104,12 +106,12 @@ struct sFont *MakeFont(const char *fontfile, int height)
void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct mShader *shader, float color[3])
{
float xpos = cursor[0] + c.Bearing[0] * scale;
float ypos = cursor[1] - (c.Size[1] - c.Bearing[1]) * scale;
float w = c.Size[0] * scale;
float h = c.Size[1] * scale;
float xpos = cursor[0] + c.Bearing[0] * scale;
float ypos = cursor[1] + (c.Bearing[1] * scale) - h;
float verts[4 * 4] = {
xpos, ypos, 0.f, 0.f,
xpos+w, ypos, 1.f, 0.f,
@ -136,32 +138,32 @@ void sdrawCharacter(struct Character c, mfloat_t cursor[2], float scale, struct
/////////// Shadow calculation
/*
float shadowOffset = 6.f;
float sxpos = cursor[0] + c.Bearing[0] * scale + (scale * shadowOffset);
float sypos = cursor[1] - (c.Size[1] - c.Bearing[1]) * scale - (scale * shadowOffset);
float sverts[4 * 4] = {
sxpos, sypos + h, 0.f, 0.f,
sxpos, sypos, 0.f, 1.f,
sxpos + w, sypos + h, 1.f, 0.f,
sxpos + w, sypos, 1.f, 1.f
sxpos, sypos, 0.f, 0.f,
sxpos+w, sypos, 1.f, 0.f,
sxpos, sypos + h, 0.f, 1.f,
sxpos + w, sypos+h, 1.f, 1.f
};
*/
glBindTexture(GL_TEXTURE_2D, c.TextureID);
//// Shadow pass
/*
float black[3] = { 0, 0, 0 };
shader_setvec3(shader, "textColor", black);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(sverts), sverts);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
*/
//// Character pass
//shader_setvec3(shader, "textColor", color);
shader_setvec3(shader, "textColor", color);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(verts), verts);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

View file

@ -115,10 +115,12 @@ void openglInit()
//debugdraw_init();
//glEnable(GL_STENCIL_TEST);
glClearColor(editorClearColor[0], editorClearColor[1], editorClearColor[2], editorClearColor[3]);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -158,38 +160,26 @@ void openglRender(struct mSDLWindow *window, struct mCamera *mcamera)
glColorMask(true, true, true, true);
//glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
//glCullFace(GL_BACK);
// Clear color and depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
////// TEXT && GUI
// glDepthFunc(GL_ALWAYS);
shader_use(textShader);
shader_setmat4(textShader, "projection", projection);
mfloat_t fontpos[2] = { 25.f, 25.f };
mfloat_t fontcolor[3] = { 0.5f, 0.8f, 0.2f };
renderText("Sample text", fontpos, 1.f, fontcolor, -1.f);
///// Sprites
shader_use(spriteShader);
//shader_setmat4(spriteShader, "projection", window->projection);
for (int i = 0; i < numSprites; i++) {
sprite_draw(sprites[i]);
}
glDepthFunc(GL_LESS);
shader_use(spriteShader);
sprite_draw_all();
glDepthFunc(GL_ALWAYS);
shader_use(textShader);
shader_setmat4(textShader, "projection", projection);
}

View file

@ -48,6 +48,13 @@ void sprite_init(struct mSprite *sprite, struct mGameObject *go)
sprite->go = go;
}
void sprite_draw_all()
{
shader_use(spriteShader);
for (int i = 0; i < numSprites; i++)
sprite_draw(sprites[i]);
}
void sprite_loadtex(struct mSprite *sprite, const char *path)
{
sprite->tex = texture_loadfromfile(path);
@ -60,8 +67,7 @@ void sprite_loadanim(struct mSprite *sprite, const char *path,
sprite->anim = anim;
sprite->anim.timer = timer_make(sprite->anim.ms, &incrementAnimFrame, sprite);
/*
sprite->tex = texture_loadanimfromfile(sprite->tex, path, sprite->anim.frames,
sprite->anim.dimensions);
sprite->tex = texture_loadanimfromfile(sprite->tex, path, sprite->anim.frames, sprite->anim.dimensions);
*/
}

View file

@ -41,6 +41,7 @@ void sprite_initialize();
void sprite_draw(struct mSprite *sprite);
void spriteanim_draw(struct mSprite *sprite);
void video_draw(struct datastream *ds, mfloat_t pos[2], mfloat_t size[2], float rotate, mfloat_t color[3]);
void sprite_draw_all();
unsigned int incrementAnimFrame(unsigned int interval, struct mSprite *sprite);
struct mSprite *gui_makesprite();