diff --git a/fontAsset.h b/fontAsset.h index 6c6dbb8..19ae3b2 100644 --- a/fontAsset.h +++ b/fontAsset.h @@ -3,10 +3,18 @@ #include "asset.h" +#include +#include +#include +#include FT_FREETYPE_H + namespace vb01{ - struct FontAsset : public Asset{ - FT_Face face; - }; + class Texture; + + struct FontAsset : public Asset{ + FT_Face face; + std::vector> glyphTextures; + }; } #endif diff --git a/root.cpp b/root.cpp index 0c0c1d5..f097bf5 100755 --- a/root.cpp +++ b/root.cpp @@ -56,14 +56,49 @@ namespace vb01{ brdfLutPlane = new Quad(Vector3(1, 1, 1) * 2); iblBox = new Box(Vector3::VEC_IJK); + blurShader = new Shader(libPath + "blur"); + shader = new Shader(Root::getSingleton()->getLibPath() + "line3D"); + + for(int i = 0; i < 2; i++) + pingPongTextures[i] = new Texture(width, height, false); + Texture *fragTexture = new Texture(width, height, false); Texture *brightTexture = new Texture(width, height, false); - initMainFramebuffer(fragTexture, brightTexture); - initBloomFramebuffer(); + initMainFramebuffer(fragTexture, nullptr); initGuiPlane(fragTexture, brightTexture); + } - shader = new Shader(Root::getSingleton()->getLibPath() + "line3D"); + void Root::toggleHDR(bool hdr){ + glDeleteFramebuffers((hdr ? 2 : 1), &FBO); + glDeleteRenderbuffers(1, &RBO); + + Texture *fragTexture = ((Material::TextureUniform*)guiPlane->getMaterial()->getUniform("frag"))->value; + Texture *brightTexture = ((Material::TextureUniform*)guiPlane->getMaterial()->getUniform("bright"))->value; + initMainFramebuffer(fragTexture, (hdr ? brightTexture : nullptr)); + + this->hdr = hdr; + } + + void Root::initBloomFramebuffer(){ + glGenFramebuffers(2, pingpongBuffers); + + for(int i = 0; i < 2; i++){ + glBindFramebuffer(GL_FRAMEBUFFER, pingpongBuffers[i]); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *(pingPongTextures[i]->getTexture()), 0); + + if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + cout << "Not complete\n"; + } + } + + void Root::toggleBloom(bool bloom){ + this->bloom = bloom; + + if(bloom) + initBloomFramebuffer(); + else + glDeleteFramebuffers(2, pingpongBuffers); } void Root::initWindow(string name){ @@ -101,11 +136,14 @@ namespace vb01{ glBindFramebuffer(GL_FRAMEBUFFER, FBO); u32 colorAttachments[]{GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1}; + glFramebufferTexture2D(GL_FRAMEBUFFER, colorAttachments[0], GL_TEXTURE_2D, *fragTexture->getTexture(), 0); - for(int i = 0; i < 2; i++) - glFramebufferTexture2D(GL_FRAMEBUFFER, colorAttachments[i], GL_TEXTURE_2D, *((i == 0 ? fragTexture : brightTexture)->getTexture()), 0); - - glDrawBuffers(2, colorAttachments); + if(brightTexture){ + glFramebufferTexture2D(GL_FRAMEBUFFER, colorAttachments[1], GL_TEXTURE_2D, *brightTexture->getTexture(), 0); + glDrawBuffers(2, colorAttachments); + } + else + glDrawBuffers(1, colorAttachments); glGenRenderbuffers(1, &RBO); glBindRenderbuffer(GL_RENDERBUFFER, RBO); @@ -118,20 +156,6 @@ namespace vb01{ glBindFramebuffer(GL_FRAMEBUFFER, 0); } - void Root::initBloomFramebuffer(){ - blurShader = new Shader(libPath + "blur"); - glGenFramebuffers(2, pingpongBuffers); - - for(int i = 0; i < 2; i++){ - glBindFramebuffer(GL_FRAMEBUFFER, pingpongBuffers[i]); - pingPongTextures[i] = new Texture(width, height, false); - glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *(pingPongTextures[i]->getTexture()), 0); - - if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) - cout << "Not complete\n"; - } - } - void Root::initGuiPlane(Texture *fragTexture, Texture *brightTexture){ guiPlane = new Quad(Vector3(width, height, -1), false); Material *mat = new Material(libPath + "postProcess"); @@ -167,7 +191,8 @@ namespace vb01{ glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); - updateBloomFramebuffer(); + if(bloom) updateBloomFramebuffer(); + updateGuiPlane(); glEnable(GL_DEPTH_TEST); @@ -259,10 +284,14 @@ namespace vb01{ shader->setFloat(exposure, "exposure"); shader->setFloat(gamma, "gamma"); shader->setInt(0, "frag"); - shader->setInt(1, "bright"); ((Material::TextureUniform*)material->getUniform("frag"))->value->select(0); - ((Material::TextureUniform*)material->getUniform("bright"))->value->select(1); + + if(hdr){ + shader->setInt(1, "bright"); + ((Material::TextureUniform*)material->getUniform("bright"))->value->select(1); + } + guiPlane->render(); } diff --git a/root.h b/root.h index 3c87e7e..614a76c 100755 --- a/root.h +++ b/root.h @@ -23,6 +23,8 @@ namespace vb01{ public: void update(); void start(int, int, std::string, std::string); + void toggleHDR(bool); + void toggleBloom(bool); inline Camera* getCamera(){return camera;} inline Node* getRootNode(){return rootNode;} inline Node* getGuiNode(){return guiNode;} @@ -31,10 +33,8 @@ namespace vb01{ inline u32* getFBO(){return &FBO;} inline u32* getRBO(){return &RBO;} inline GLFWwindow* getWindow(){return window;} - inline void setHDREnabled(bool hdr){this->hdr = hdr;} inline void setExposure(float exposure){this->exposure = exposure;} inline void setGamma(float gamma){this->gamma = gamma;} - inline void setBloom(bool bloom){this->bloom = bloom;} inline void setBlurLevel(bool level){this->blurLevel = level;} inline Box* getSkybox(){return skybox;} inline Box* getIblBox(){return iblBox;} diff --git a/text.cpp b/text.cpp index b04db8d..6e12df8 100755 --- a/text.cpp +++ b/text.cpp @@ -4,7 +4,6 @@ #include "texture.h" #include "node.h" #include "material.h" -#include "fontAsset.h" #include "assetManager.h" #include "glad.h" @@ -23,17 +22,20 @@ namespace vb01{ } Text::~Text(){ - clearFont(); - glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); } + //TODO ensure destruction of textures when unloading the asset void Text::applyFont(string fontPath, u16 firstChar, u16 lastChar){ - clearFont(); + characters.clear(); - FontAsset *font = (FontAsset*)AssetManager::getSingleton()->getAsset(fontPath); + font = (FontAsset*)AssetManager::getSingleton()->getAsset(fontPath); FT_Face face = font->face; + vector keys; + + for (pair p : font->glyphTextures) + keys.push_back(p.first); for(u16 i = firstChar; i < lastChar; i++){ if(FT_Load_Char(face, i, FT_LOAD_RENDER)){ @@ -43,21 +45,18 @@ namespace vb01{ Glyph c; c.ch = i; - c.texture = new Texture(face); c.size = Vector2(face->glyph->bitmap.width, face->glyph->bitmap.rows); c.bearing = Vector2(face->glyph->bitmap_left, face->glyph->bitmap_top); c.advance = face->glyph->advance.x; + c.texId = i; + + if(find(keys.begin(), keys.end(), i) == keys.end()) + font->glyphTextures.push_back(make_pair(i, new Texture(face))); + characters.push_back(c); } } - void Text::clearFont(){ - while(!characters.empty()){ - delete characters[characters.size() - 1].texture; - characters.pop_back(); - } - } - void Text::update(){ Root *root = Root::getSingleton(); int width = root->getWidth(), height = root->getHeight(); @@ -72,11 +71,17 @@ namespace vb01{ for(int i = (leftToRight ? 0 : entry.length() - 1); (leftToRight ? (i < entry.length()) : (i >= 0)); (leftToRight ? i++ : i--)){ Glyph *glyphPtr = getGlyph(entry[i]); - if(!glyphPtr) - continue; + if(!glyphPtr) continue; + + int glyphTexId = -1; + for(int j = 0; j < font->glyphTextures.size(); j++) + if(font->glyphTextures[j].first == glyphPtr->texId){ + glyphTexId = j; + break; + } Glyph glyph = *glyphPtr; - prepareGlyphs(glyph, advanceOffset); + prepareGlyphs(glyph, glyphTexId, advanceOffset); Vector2 size = glyph.size, bearing = glyph.bearing; if(horizontal) @@ -86,7 +91,7 @@ namespace vb01{ } } - void Text::prepareGlyphs(Glyph glyph, Vector2 advanceOffset){ + void Text::prepareGlyphs(Glyph glyph, int glyphTexId, Vector2 advanceOffset){ Vector3 nodePos = node->getPosition(); Vector2 origin = Vector2(nodePos.x, nodePos.y) + (advanceOffset * scale); Vector3 scale = node->getScale(); @@ -104,17 +109,13 @@ namespace vb01{ origin.x + bearing.x, origin.y - bearing.y, 0, 0 }; - renderGlyphs(glyph, data, sizeof(data)); - } - - void Text::renderGlyphs(Glyph glyph, float data[], u32 dataSize){ glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferSubData(GL_ARRAY_BUFFER, 0, dataSize, data); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(data), data); int id = 2; material->getShader()->setInt(id, "textures[1].pastTexture"); - glyph.texture->select(id); + font->glyphTextures[glyphTexId].second->select(id); glDrawArrays(GL_TRIANGLES, 0, 6); } diff --git a/text.h b/text.h index c48cc22..9d1780d 100755 --- a/text.h +++ b/text.h @@ -4,13 +4,13 @@ #include #include +#include "fontAsset.h" #include "attachable.h" #include "vector.h" #include "util.h" namespace vb01{ class Node; - class Texture; class Shader; class Material; @@ -18,7 +18,7 @@ namespace vb01{ public: struct Glyph{ u16 ch; - Texture *texture = nullptr; + int texId; u32 advance; Vector2 size, bearing; }; @@ -41,9 +41,9 @@ namespace vb01{ private: void applyFont(std::string, u16, u16 = 256); void clearFont(); - void prepareGlyphs(Glyph, Vector2); - void renderGlyphs(Glyph, float[], u32); + void prepareGlyphs(Glyph, int, Vector2); + FontAsset *font = nullptr; Material *material = nullptr; std::wstring entry; std::vector characters; diff --git a/texture.cpp b/texture.cpp index 504d7a5..503e822 100755 --- a/texture.cpp +++ b/texture.cpp @@ -15,7 +15,7 @@ using namespace std; namespace vb01{ - Texture::Texture(int width, int height, bool shadowMap) : Animatable(Animatable::TEXTURE){ + Texture::Texture(int width, int height, bool shadowMap, bool hiRes) : Animatable(Animatable::TEXTURE){ this->width = width; this->height = height; texture = new u32; @@ -33,8 +33,7 @@ namespace vb01{ glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); } else{ - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, NULL); - //glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA16F,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,NULL); + glTexImage2D(GL_TEXTURE_2D, 0, (hiRes ? GL_RGB16F : GL_RGB), width, height, 0, GL_RGB, GL_FLOAT, NULL); 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); diff --git a/texture.h b/texture.h index d6f88e8..bb00acc 100755 --- a/texture.h +++ b/texture.h @@ -15,7 +15,7 @@ namespace vb01{ class Texture : public Animatable{ public: ~Texture(); - Texture(int, int, bool = true); + Texture(int, int, bool = true, bool = false); Texture(std::string[], int, bool, int = 0, bool = false, std::string = ""); Texture(int, bool = true, int = 0, std::string = ""); Texture(FT_Face);