Formatted classes

This commit is contained in:
Julian Nießner
2018-05-26 11:14:12 +02:00
parent ae5fa3f2aa
commit 9ffdf66bd0
27 changed files with 574 additions and 461 deletions

View File

@@ -6,20 +6,25 @@
#include <iostream>
BitmapFont::BitmapFont(std::string fontLocation) {
BitmapFont::BitmapFont(std::string fontLocation)
{
FT_Library ft;
if(FT_Init_FreeType(&ft)) {
if (FT_Init_FreeType(&ft))
{
std::cout << "ERROR::FreeType: Could not init FreeType Library" << std::endl;
}
FT_Face face;
if(FT_New_Face(ft, fontLocation.c_str(), 0, &face)){
if (FT_New_Face(ft, fontLocation.c_str(), 0, &face))
{
std::cout << "ERROR::FreeType: Failed to load font" << std::endl;
}
FT_Set_Pixel_Sizes(face, 0, 48);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); //Disable byte-alignment restriction
for(GLubyte c = 0; c < 128; c++) {
for (GLubyte c = 0; c < 128; c++)
{
//Map bitmaps to chars
if(FT_Load_Char(face, c, FT_LOAD_RENDER)) {
if (FT_Load_Char(face, c, FT_LOAD_RENDER))
{
std::cout << "ERROR::FreeType: Could not load Glyph: " << c << std::endl;
continue;
}
@@ -27,27 +32,25 @@ BitmapFont::BitmapFont(std::string fontLocation) {
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
GL_TEXTURE_2D,
0,
GL_RED,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer);
// Set texture options
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Character character = {
texture,
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
face->glyph->advance.x
};
face->glyph->advance.x};
characters.insert(std::pair<GLchar, Character>(c, character));
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 4); //Reset byte-alignment restriction
@@ -57,7 +60,7 @@ BitmapFont::BitmapFont(std::string fontLocation) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
projection = glm::ortho(0.0f,800.0f,0.0f,600.0f);
projection = glm::ortho(0.0f, 800.0f, 0.0f, 600.0f);
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
@@ -70,17 +73,18 @@ BitmapFont::BitmapFont(std::string fontLocation) {
glBindVertexArray(0);
}
void BitmapFont::drawText(Shader &s, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color) {
void BitmapFont::drawText(Shader &s, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color)
{
s.Use();
s.SetVector3f("textColor", color, true);
s.SetMatrix4("projection",projection,true);
s.SetMatrix4("projection", projection, true);
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(VAO);
// Iterate through all characters
std::string::const_iterator c;
for (c = text.begin(); c != text.end(); c++)
for (c = text.begin(); c != text.end(); c++)
{
Character ch = characters[*c];
@@ -91,14 +95,13 @@ void BitmapFont::drawText(Shader &s, std::string text, GLfloat x, GLfloat y, GLf
GLfloat h = ch.size.y * scale;
// Update VBO for each character
GLfloat vertices[6][4] = {
{ xpos, ypos + h, 0.0, 0.0 },
{ xpos, ypos, 0.0, 1.0 },
{ xpos + w, ypos, 1.0, 1.0 },
{xpos, ypos + h, 0.0, 0.0},
{xpos, ypos, 0.0, 1.0},
{xpos + w, ypos, 1.0, 1.0},
{ xpos, ypos + h, 0.0, 0.0 },
{ xpos + w, ypos, 1.0, 1.0 },
{ xpos + w, ypos + h, 1.0, 0.0 }
};
{xpos, ypos + h, 0.0, 0.0},
{xpos + w, ypos, 1.0, 1.0},
{xpos + w, ypos + h, 1.0, 0.0}};
// Render glyph texture over quad
glBindTexture(GL_TEXTURE_2D, ch.texID);
// Update content of VBO memory