From 542326baab4a02480b60c7859dbf4f4ea2a93993 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Mon, 8 Dec 2014 19:38:38 -0500 Subject: [PATCH] Font rendering functional: test string --- src/util/GLFont.cpp | 56 +++++++++++++++++++++++++++++++--- src/util/GLFont.h | 4 ++- src/visual/SpectrumContext.cpp | 11 ++++++- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/util/GLFont.cpp b/src/util/GLFont.cpp index 0a3cb15..2124ff3 100644 --- a/src/util/GLFont.cpp +++ b/src/util/GLFont.cpp @@ -277,7 +277,7 @@ void GLFont::loadFont(std::string fontFile) { glGenTextures(1, &texId); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texId); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, 4, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, &image[0]); glDisable(GL_TEXTURE_2D); @@ -292,7 +292,7 @@ void GLFont::loadFont(std::string fontFile) { int charId = (*char_i).first; GLFontChar *fchar = (*char_i).second; - float faspect = fchar->getAspect()/8.0; + float faspect = fchar->getAspect(); float uv_xpos = (float) fchar->getX() / (float) imageWidth; float uv_ypos = ((float) fchar->getY() / (float) imageHeight); @@ -340,16 +340,50 @@ void GLFont::loadFont(std::string fontFile) { input.close(); } -void GLFont::drawString(std::string str, float xpos, float ypos, float size) { +float GLFont::getStringWidth(std::string str, float size, float viewAspect) { + + float scalex = size/viewAspect; + + float width = 0; + + for (int i = 0, iMax = str.length(); i < iMax; i++) { + int charId = str.at(i); + + if (characters.find(charId) == characters.end()) { + continue; + } + + GLFontChar *fchar = characters[charId]; + + float ofsx = (float)fchar->getXOffset()/(float)imageWidth; + float advx = (float)fchar->getXAdvance()/(float)imageWidth; + + if (charId == 32) { + advx = characters['_']->getAspect(); + } + + width += fchar->getAspect()+advx-ofsx; + } + + width *= scalex; + + return width; +} + + +void GLFont::drawString(std::string str, float xpos, float ypos, float size, float viewAspect) { glPushMatrix(); glTranslatef(xpos, ypos, 0.0f); - glScalef(size, size, 1.0f); glPushMatrix(); + glScalef(size/viewAspect, size, 1.0f); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texId); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); + glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(2, GL_FLOAT, 0, &gl_vertices[0]); @@ -364,13 +398,25 @@ void GLFont::drawString(std::string str, float xpos, float ypos, float size) { GLFontChar *fchar = characters[charId]; + + float ofsx = (float)fchar->getXOffset()/(float)imageWidth; + float advx = (float)fchar->getXAdvance()/(float)imageWidth; + + + if (charId == 32) { + advx = characters['_']->getAspect(); + } + + glTranslatef(-ofsx,0.0,0.0); glDrawArrays(GL_QUADS, fchar->getIndex()/2, 4); - glTranslatef(fchar->getAspect()/8.0,0.0,0.0); + glTranslatef(fchar->getAspect()+advx,0.0,0.0); } glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glPopMatrix(); glPopMatrix(); + + glDisable(GL_BLEND); } diff --git a/src/util/GLFont.h b/src/util/GLFont.h index 7a648a3..e0a8f6f 100644 --- a/src/util/GLFont.h +++ b/src/util/GLFont.h @@ -55,7 +55,9 @@ public: GLFont(); ~GLFont(); void loadFont(std::string fontFile); - void drawString(std::string str, float xpos, float ypos, float height); + + float getStringWidth(std::string str, float size, float viewAspect); + void drawString(std::string str, float xpos, float ypos, float height, float viewAspect); private: std::string nextParam(std::istringstream &str); diff --git a/src/visual/SpectrumContext.cpp b/src/visual/SpectrumContext.cpp index d3c1990..6196e35 100644 --- a/src/visual/SpectrumContext.cpp +++ b/src/visual/SpectrumContext.cpp @@ -33,7 +33,16 @@ void SpectrumContext::Draw(std::vector &points) { glPopMatrix(); } - getFont()->drawString("Testing",0.0,0.0,0.7); + GLint vp[4]; + + glGetIntegerv( GL_VIEWPORT, vp); + + std::string msgStr("Welcome to CubicSDR -- This is a test string. 01234567890!@#$%^&*()_[]"); + float charHeight = 62.0; + float viewAspect = (float)vp[2]/(float)vp[3]; + float msgWidth = getFont()->getStringWidth(msgStr,charHeight/(float)vp[3],viewAspect); + + getFont()->drawString(msgStr,0.0-(msgWidth/2.0),0.0,charHeight/(float)vp[3],viewAspect); CheckGLError(); }