Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //================================================CFONT.H=============================================
- #ifndef __C_FONT_H__
- #define __C_FONT_H__
- #include <ft2build.h>
- #include FT_FREETYPE_H
- class ShaderProgram;
- class CFont
- {
- private:
- static FT_Library library;
- static ShaderProgram* textShader;
- private:
- unsigned int texID; //The texture id that stores the texture this font uses
- unsigned int vboID; //The vbo id that holds the combined vertex and texture coordinates
- FT_Face face; //The face used by this font
- FT_GlyphSlot slot; //The shortcut to the active glyph in the face
- public:
- CFont(const char* file, unsigned int size);
- ~CFont();
- void SetSize(unsigned int);
- void RenderText(const char* text, float x, float y, float color[4]);
- };
- #endif
- //=============================================CFONT.CPP==========================================
- #include "cfont.h"
- #include "shader_program.h"
- #include "globals.h"
- #include <GL\glew.h>
- #include <stdexcept>
- FT_Library CFont::library = nullptr;
- ShaderProgram* CFont::textShader = nullptr;
- CFont::CFont(const char* file, unsigned int size)
- {
- //Static init
- if (!library)
- {
- int err = FT_Init_FreeType(&library);
- if (err)
- throw std::runtime_error("Failed to load FreeType font library.");
- textShader = new ShaderProgram("data\\shaders\\TextRenderer.vert", "data\\shaders\\TextRenderer.frag");
- }
- //Load face and set size
- int err;
- err = FT_New_Face(library, file, 0, &face);
- if (err)
- throw std::runtime_error("Could not load font " + *file);
- err = FT_Set_Pixel_Sizes(face, 0, size);
- if (err)
- throw std::runtime_error("Could not change fint size.");
- slot = face->glyph;
- //Load graphics stuff
- glGenTextures(1, &texID);
- glBindTexture(GL_TEXTURE_2D, texID);
- 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);
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glBindTexture(GL_TEXTURE_2D, 0);
- glGenBuffers(1, &vboID);
- }
- CFont::~CFont()
- {
- }
- void CFont::SetSize(unsigned int size)
- {
- int err = FT_Set_Pixel_Sizes(face, 0, size);
- if (err)
- throw std::runtime_error("Could not set font size.");
- }
- void CFont::RenderText(const char* text, float x, float y, float color[4])
- {
- float penX = x, penY = y;
- ShaderProgram::SetActive(textShader);
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, texID);
- textShader->Uniform1i("glyphTex", 0);
- textShader->Uniform4f("color", color[0], color[1], color[2], color[3]);
- glBindBuffer(GL_ARRAY_BUFFER, vboID);
- glEnableVertexAttribArray(glGetAttribLocation(textShader->GetProgramID(), "coord"));
- glVertexAttribPointer(glGetAttribLocation(textShader->GetProgramID(), "coord"), 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
- const char* p;
- for (p = text; *p; p++)
- {
- int err = FT_Load_Char(face, *p, FT_LOAD_RENDER);
- if (err)
- continue;
- glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, slot->bitmap.width, slot->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, slot->bitmap.buffer);
- float x2 = penX + slot->bitmap_left * (2.0f / g_screenWidth);
- float y2 = -penY - slot->bitmap_top * (2.0f / g_screenHeight);
- float w = slot->bitmap.width * (2.0f / g_screenWidth);
- float h = slot->bitmap.rows * (2.0f / g_screenHeight);
- GLfloat data[4][4] =
- {
- {x2, -y2, 0, 0},
- {x2 + w, -y2, 1, 0},
- {x2, -y2 - h, 0, 1},
- {x2 + w, -y2 - h, 1, 1},
- };
- glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_DYNAMIC_DRAW);
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- penX += (slot->advance.x >> 6) * (2.0f / g_screenWidth);
- penY += (slot->advance.y >> 6) * (2.0f / g_screenHeight);
- }
- glDisableVertexAttribArray(glGetAttribLocation(textShader->GetProgramID(), "coord"));
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindTexture(GL_TEXTURE_2D, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement