First phase moving from STBTT to FreeType

This commit is contained in:
2023-05-22 11:25:59 -07:00
parent 4a0c817a1c
commit 8328dba55c
22 changed files with 332 additions and 21 deletions

View File

@ -15,7 +15,7 @@ TrueTypeAsset::TrueTypeAsset(AssetManager *assMan, std::string name) :
void TrueTypeAsset::updateSync() {
if(this->state != 0x04) return;
this->font.texture.setSize(this->width, this->height, TEXTURE_FORMAT_RGBA);
this->font.texture.setSize(this->width, this->height, TEXTURE_FORMAT_R);
this->font.texture.buffer(this->pixels);
auto i = this->pixels;
memoryCopy(
@ -71,9 +71,9 @@ void TrueTypeAsset::updateAsync() {
this->state = 0x03;
this->font.fontSize = fontSize;
this->pixels = (struct Color*)(this->buffer + i);
this->pixels = (uint8_t*)(this->buffer + i);
this->characterData = (truetypechar_t*)(
(uint8_t*)this->pixels + (this->width * this->height * sizeof(struct Color))
(uint8_t*)this->pixels + (this->width * this->height * sizeof(uint8_t))
);
this->state = 0x04;
}

View File

@ -14,7 +14,7 @@ namespace Dawn {
AssetLoader loader;
uint8_t *buffer = nullptr;
truetypechar_t *characterData = nullptr;
struct Color *pixels = nullptr;
uint8_t *pixels = nullptr;
int32_t width, height;
public:

View File

@ -10,6 +10,9 @@
#include <stb_truetype.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include <glm/glm.hpp>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>

View File

@ -18,6 +18,81 @@ RenderPipeline::RenderPipeline(RenderManager *renderManager) {
}
void RenderPipeline::init() {
// FT_Face face;
// if(FT_New_Face(ft, "C:\\Windows\\Fonts\\Arial.ttf", 0, &face)) {
// std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
// assertUnreachable();
// }
// // TESTING FONT
// // 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
// // );
// // // 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);
// Mesh glyphMesh;
// FT_Set_Pixel_Sizes(face, 0, 36);
// FT_UInt glyph_index = FT_Get_Char_Index(face, 'B');
// auto error = FT_Load_Glyph(
// face, /* handle to face object */
// glyph_index, /* glyph index */
// FT_LOAD_DEFAULT ); /* load flags, see below */
// if(error) {
// std::cout << "Error loading glyph" << std::endl;
// assertUnreachable();
// }
// /* convert to an anti-aliased bitmap */
// error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL );
// if(error) {
// std::cout << "Error rendering glyph" << std::endl;
// assertUnreachable();
// }
// // Shader
// auto shdr = &this->renderManager->simpleTexturedShader->program;
// shdr->bind();
// shdr->setMatrix(shdr->paramProjection, camera->getProjection());
// shdr->setMatrix(shdr->paramView, camera->transform->getWorldTransform());
// shdr->setMatrix(shdr->paramModel, glm::mat4(1.0f));
// shdr->setColor(shdr->paramColor, COLOR_WHITE);
// // Texture
// Texture texture;
// texture.setSize(
// face->glyph->bitmap.width,
// face->glyph->bitmap.rows,
// TEXTURE_FORMAT_R
// );
// texture.wrapModeX = TEXTURE_WRAP_MODE_CLAMP_TO_EDGE;
// texture.wrapModeY = TEXTURE_WRAP_MODE_CLAMP_TO_EDGE;
// texture.buffer((uint8_t*)face->glyph->bitmap.buffer);
// shdr->setBoolean(shdr->paramHasTexture, true);
// shdr->setTexture(shdr->paramTexture, 0);
// texture.bind(0);
// this->renderManager->setRenderFlags(RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST | RENDER_MANAGER_RENDER_FLAG_BLEND);
// auto faceCloneForDebugging = face;
// QuadMesh::initQuadMesh(&glyphMesh,
// glm::vec2(face->glyph->bitmap.width, face->glyph->bitmap.rows), glm::vec2(1, 0),
// glm::vec2(0, 0), glm::vec2(0, 1),
// 0.0f
// );
// glyphMesh.draw(MESH_DRAW_MODE_TRIANGLES, 0, -1);
}
void RenderPipeline::render() {
@ -196,6 +271,7 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) {
RENDER_TARGET_CLEAR_FLAG_COLOR
);
// Shader items
itPassItem = shaderPassItems.begin();
while(itPassItem != shaderPassItems.end()) {
auto item = *itPassItem;

View File

@ -15,6 +15,7 @@ namespace Dawn {
class DawnGame;
class RenderPipeline;
class ShaderManager;
class FontManager;
class IRenderManager {
protected:
@ -55,6 +56,13 @@ namespace Dawn {
* @return Reference to the shader manager.
*/
virtual ShaderManager * getShaderManager() = 0;
/**
* Returns the font manager that this render manager uses.
*
* @return Reference ot the font manager.
*/
virtual FontManager * getFontManager() = 0;
/**
* Sets the render flags for the render manager to use.

View File

@ -95,5 +95,6 @@ namespace Dawn {
* @return The amount of bytes buffered to the texture.
*/
virtual void buffer(struct Color pixels[]) = 0;
virtual void buffer(uint8_t pixels[]) = 0;
};
}

View File

@ -6,6 +6,7 @@
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
FontManager.cpp
BitmapFont.cpp
ExampleFont.cpp
TrueTypeFont.cpp

View File

@ -0,0 +1,15 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "FontManager.hpp"
using namespace Dawn;
void FontManager::init() {
if(FT_Init_FreeType(&fontLibrary)) {
std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl;
assertUnreachable();
}
}

View File

@ -0,0 +1,19 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
#include "assert/assert.hpp"
namespace Dawn {
class FontManager {
protected:
public:
FT_Library fontLibrary;
void init();
};
}

View File

@ -74,13 +74,12 @@ std::vector<struct ShaderPassItem> UILabel::getPassItems(
this->updateMesh();
struct ShaderPassItem item;
auto shader = &getGame()->renderManager.uiShader->program;
auto shader = &getGame()->renderManager.fontShader->program;
item.shaderProgram = shader;
item.colorValues[shader->paramColor] = textColor;
item.matrixValues[shader->paramProjection] = proj;
item.matrixValues[shader->paramView] = view;
item.matrixValues[shader->paramModel] = this->transform->getWorldTransform();
item.boolValues[shader->paramHasTexture] = true;
item.textureSlots[0] = this->font->getTexture();
item.textureValues[shader->paramTexture] = 0;
item.start = this->startQuad * QUAD_INDICE_COUNT;