#include "glText.h" GLText::GLText( bool setDebug=false ) { debug = setDebug; // default to non-italic characters setItalics( false ); // default scaling to none scale = 1.0f; // create the texture placeholder numtextures = 0; texture[0] = 0; } void GLText::killFont() { if (base) glDeleteLists(base, 256); if (texture) glDeleteTextures( 1, &texture[0] ); return; } void GLText::setRCoords( GLfloat x, GLfloat y ) { rasterX = x; rasterY = y; } void GLText::setRCoordX( GLfloat x ) { rasterX = x; } void GLText::setRCoordY( GLfloat y ) { rasterY = y; } void GLText::setScale ( GLfloat s ) { scale = s; } GLfloat GLText::getX() { return rasterX; } GLfloat GLText::getY() { return rasterY; } GLfloat GLText::getScale() { return scale; } void GLText::setItalics( bool toggle ) { italics = 0; if (toggle) italics = 1; } void GLText::updateText( std::string intext ) { if (intext.length()) { outtext = intext; } else { outtext.clear(); }; } std::string GLText::getText() { return outtext; } void GLText::drawText( std::string intext ) { if ( intext.length() ) { outtext = intext; }; // Enable textures glEnable(GL_TEXTURE_2D); // Don't draw text in wireframe glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // Bind our texture glBindTexture(GL_TEXTURE_2D, texture[0]); // Enable blending glEnable(GL_BLEND); glBlendFunc( GL_ONE, GL_ONE ); glTranslatef( rasterX, rasterY, 0.0f ); glScalef( scale, scale, 0 ); // set the x,y scaling glColor3f( 1.0f, 1.0f, 1.0f ); // there are two 'sets' of chars // set one (0) is from 0->127, non-italic // set two (1) is from 128->255, italic // we set the base to start at -32 to shift the ANSI character // values to correspond to the proper character. glListBase( base - 32 + ( 128 * italics )); glPushMatrix(); glCallLists( outtext.length() , GL_UNSIGNED_BYTE, outtext.c_str() ); glPopMatrix(); glDisable(GL_BLEND); glDisable(GL_TEXTURE_2D); } bool GLText::buildFont() { if (debug) std::cout << "\tLoading texture" << std::endl; numtextures = 1; glGenTextures(numtextures, &texture[0]); if (!( loadTexture( &font_img ) )) return false; glBindTexture(GL_TEXTURE_2D, texture[0]); // Our bmp file is 256x256, change this later to allow other bitmaps int bmpWidth = 256; base = glGenLists(256); float x,y; // create glyphs for the characters in the textmap for (int i = 0; i < bmpWidth; i++) { int b = 255 - i; x = float(i % 16)/16.0f; y = float(b / 16)/16.0f; glNewList(base + i,GL_COMPILE); glBegin(GL_QUADS); glTexCoord2f(x,1-y-0.0625f); // top left glVertex2i(0,16); glTexCoord2f(x,1-y); // bottom left glVertex2i(0,0); glTexCoord2f(x+0.0625f,1-y); // bottom right glVertex2i(16,0); glTexCoord2f(x+0.0625f,1-y-0.0625f); // top right glVertex2i(16,16); glEnd(); glTranslatef(10.0f,0.0f,0.0f); glEndList(); } if (debug) std::cout << "Font file loaded." << std::endl; return true; } bool GLText::loadTexture( const struct bitmap *texdata, int minFilter, int magFilter, bool clampToEdge) { // Texdata should be pointer to a texture struct of at least: // width (int) // height (int) // pixel_data (char array) killFont(); glBindTexture(GL_TEXTURE_2D,texture[0]); glPixelStorei(GL_UNPACK_ALIGNMENT,4); if (clampToEdge) { glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); } glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,minFilter); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,magFilter); // gluBuild2DMipmaps( GL_TEXTURE_2D, GL_RGB, texdata->width, texdata->height, // GL_RGB, GL_UNSIGNED_BYTE, texdata->pixel_data ); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texdata->width, texdata->height, 0, GL_RGB, GL_UNSIGNED_BYTE, texdata->pixel_data); if (!( glGetError() )) return true; return false; }