Font rendering functional: test string

This commit is contained in:
Charles J. Cliffe 2014-12-08 19:38:38 -05:00
parent 6c7372ed90
commit 542326baab
3 changed files with 64 additions and 7 deletions

View File

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

View File

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

View File

@ -33,7 +33,16 @@ void SpectrumContext::Draw(std::vector<float> &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();
}