CSCI441 OpenGL Library 6.0.1.1
CS@Mines CSCI441 Computer Graphics Course Library
Loading...
Searching...
No Matches
Functions
CSCI441::FontUtils Namespace Reference

Utility class to render text overlays on screen

Usage:
(1) During setup, call loadFont() to load a ttf font file
(2) Immediately prior to rendering, call bindFont() to load font context
(3) Subsequently call renderText() for every string to be rendered to screen
(4) During cleanup, call releaseFont() to deallocate font memory

To make placement of text within window easier, such that the (x, y) coordinate corresponds to the pixel location, call setWindowSize() after loading the font (and anytime the window size changes).. More...

Functions

bool loadFont (const char *filename)
 registers a TrueTypeFont file with a shader program to render text to screen
 
void releaseFont ()
 deallocates CPU and GPU memory related to text rendering
 
void bindFont ()
 sets all necessary font contexts
 
void setColor (glm::vec3 color)
 specify the color to render the text
 
void setColor (glm::vec4 color)
 specify the color to render the text
 
void setWindowSize (GLint width, GLint height)
 store the size of the window
 
void setWindowSize (GLfloat width, GLfloat height)
 store the size of the window
 
void setFontSize (GLfloat scaleX, GLfloat scaleY)
 set the amount to scale font when drawing
 
void renderText (const char *str, GLfloat x, GLfloat y)
 draws the given text starting at the top left (x, y) coordinate specified
 

Detailed Description

Utility class to render text overlays on screen

Usage:
(1) During setup, call loadFont() to load a ttf font file
(2) Immediately prior to rendering, call bindFont() to load font context
(3) Subsequently call renderText() for every string to be rendered to screen
(4) During cleanup, call releaseFont() to deallocate font memory

To make placement of text within window easier, such that the (x, y) coordinate corresponds to the pixel location, call setWindowSize() after loading the font (and anytime the window size changes)..

Function Documentation

◆ bindFont()

void CSCI441::FontUtils::bindFont ( )
inline

sets all necessary font contexts

Note
binds VertexArray and GL_ARRAY_BUFFER
binds GL_TEXTURE_2D to GL_TEXTURE0
uses shader program
Precondition
MUST call CSCI441::FontUtility::loadFont(const char*) prior to this call

◆ loadFont()

bool CSCI441::FontUtils::loadFont ( const char *  filename)
inline

registers a TrueTypeFont file with a shader program to render text to screen

Parameters
filenamettf font file to load
Returns
true if font file was successfully loaded AND internal font shader program successfully compiled

◆ renderText()

void CSCI441::FontUtils::renderText ( const char *  str,
GLfloat  x,
GLfloat  y 
)
inline

draws the given text starting at the top left (x, y) coordinate specified

Parameters
strtext to render to screen
xx coordinate in screen space to place text within window (corresponds to left edge of text)
yy coordinate in screen space to place text within window (corresponds to top edge of text)
Precondition
MUST call CSCI441::FontUtility::bindFont() prior to this call
Note
see CSCI441::FontUtils::setWindowSize() for help in specifying the (x, y) coordinate
see CSCI441::FontUtility::setColor() to change the displayed color

◆ setColor() [1/2]

void CSCI441::FontUtils::setColor ( glm::vec3  color)
inline

specify the color to render the text

Parameters
colortext color
Note
sets alpha channel to 1.0

◆ setColor() [2/2]

void CSCI441::FontUtils::setColor ( glm::vec4  color)
inline

specify the color to render the text

Parameters
colortext color

◆ setFontSize()

void CSCI441::FontUtils::setFontSize ( GLfloat  scaleX,
GLfloat  scaleY 
)
inline

set the amount to scale font when drawing

Parameters
scaleXamount to scale font horizontally
scaleYamount to scale font vertically
Note
values typically refer to inverse screen size, e.g. the size of each pixel

◆ setWindowSize() [1/2]

void CSCI441::FontUtils::setWindowSize ( GLfloat  width,
GLfloat  height 
)
inline

store the size of the window

Parameters
widthwindow width
heightwindow height
Note
this allows CSCI441::FontUtils::renderText() to specify the (x, y) coordinate in screen space. The (x, y) coordinate then corresponds to the top left pixel to begin drawing at.

◆ setWindowSize() [2/2]

void CSCI441::FontUtils::setWindowSize ( GLint  width,
GLint  height 
)
inline

store the size of the window

Parameters
widthwindow width
heightwindow height
Note
this allows CSCI441::FontUtils::renderText() to specify the (x, y) coordinate in screen space. The (x, y) coordinate then corresponds to the top left pixel to begin drawing at.