From 53265bbc90c4c2e647be75ddbfe739e34eb9e5c2 Mon Sep 17 00:00:00 2001 From: Tleety Date: Wed, 10 Feb 2016 17:50:17 +0100 Subject: [PATCH] PNG is now a resource. You can now load textures threaded. PNG will now throw exceptions when errors happend SpriteComponent is now working without billboarding. --- include/Engine/GUI/TextureFrame.h | 5 +- include/Engine/Rendering/DrawFinalPass.h | 2 + include/Engine/Rendering/Model.h | 1 + include/Engine/Rendering/PNG.h | 3 +- include/Engine/Rendering/Renderer.h | 1 + include/Engine/Rendering/SpriteJob.h | 19 ++--- .../Engine/Rendering/Util/CommonFunctions.h | 11 +-- .../Schema/Entities/QualityAssurance.xml | 74 +++++++++++++++---- resources/Shaders/Sprite.vert.glsl | 3 +- src/Engine/Rendering/DrawBloomPass.cpp | 2 +- src/Engine/Rendering/DrawFinalPass.cpp | 17 +++-- src/Engine/Rendering/Model.cpp | 8 +- src/Engine/Rendering/PNG.cpp | 22 ++---- src/Engine/Rendering/Renderer.cpp | 17 +++-- src/Engine/Rendering/Texture.cpp | 29 ++++---- src/Engine/Rendering/Util/CommonFunctions.cpp | 17 +++++ src/Game/Game.cpp | 1 + 17 files changed, 149 insertions(+), 83 deletions(-) diff --git a/include/Engine/GUI/TextureFrame.h b/include/Engine/GUI/TextureFrame.h index 2c6d34dd..77967b4d 100644 --- a/include/Engine/GUI/TextureFrame.h +++ b/include/Engine/GUI/TextureFrame.h @@ -3,6 +3,7 @@ #include "Frame.h" #include "../Rendering/Texture.h" +#include "../Rendering/Util/CommonFunctions.h" namespace GUI { @@ -55,10 +56,10 @@ public: return; } - m_Texture = ResourceManager::Load(resourceName); + m_Texture = CommonFunctions::LoadTexture(resourceName, false); m_TextureName = resourceName; if (m_Texture == nullptr) { - m_Texture = ResourceManager::Load("Textures/Core/ErrorTexture.png"); + m_Texture = CommonFunctions::LoadTexture("Textures/Core/ErrorTexture.png", false); } SizeToTexture(); diff --git a/include/Engine/Rendering/DrawFinalPass.h b/include/Engine/Rendering/DrawFinalPass.h index 867ec844..98f986b4 100644 --- a/include/Engine/Rendering/DrawFinalPass.h +++ b/include/Engine/Rendering/DrawFinalPass.h @@ -7,6 +7,7 @@ #include "FrameBuffer.h" #include "ShaderProgram.h" #include "Util/UnorderedMapVec2.h" +#include "Util/CommonFunctions.h" #include "Texture.h" class DrawFinalPass @@ -43,6 +44,7 @@ private: Texture* m_BlackTexture; Texture* m_NeutralNormalTexture; Texture* m_GreyTexture; + Texture* m_ErrorTexture; FrameBuffer m_FinalPassFrameBuffer; GLuint m_BloomTexture; diff --git a/include/Engine/Rendering/Model.h b/include/Engine/Rendering/Model.h index f751a8cc..6812494c 100644 --- a/include/Engine/Rendering/Model.h +++ b/include/Engine/Rendering/Model.h @@ -2,6 +2,7 @@ #define Model_h__ #include "Rendering/RawModelCustom.h" +#include "Util/CommonFunctions.h" //#include "Rendering/RawModelAssimp.h" #include "../OpenGL.h" diff --git a/include/Engine/Rendering/PNG.h b/include/Engine/Rendering/PNG.h index f2cbf157..a45e9e7c 100644 --- a/include/Engine/Rendering/PNG.h +++ b/include/Engine/Rendering/PNG.h @@ -6,9 +6,10 @@ #include #include "../Common.h" +#include "../Core/ResourceManager.h" #include "Image.h" -class PNG : public Image +class PNG : public Image, public Resource { public: PNG(std::string path); diff --git a/include/Engine/Rendering/Renderer.h b/include/Engine/Rendering/Renderer.h index 33a61edf..04754514 100644 --- a/include/Engine/Rendering/Renderer.h +++ b/include/Engine/Rendering/Renderer.h @@ -22,6 +22,7 @@ #include "../Core/Transform.h" #include "imgui/imgui.h" #include "TextPass.h" +#include "Util/CommonFunctions.h" class Renderer : public IRenderer { diff --git a/include/Engine/Rendering/SpriteJob.h b/include/Engine/Rendering/SpriteJob.h index a20d0395..23bf6360 100644 --- a/include/Engine/Rendering/SpriteJob.h +++ b/include/Engine/Rendering/SpriteJob.h @@ -24,23 +24,17 @@ struct SpriteJob : RenderJob ::RawModel::MaterialGroup matGroup = Model->MaterialGroups().front(); TextureID = (matGroup.Texture) ? matGroup.Texture->ResourceID : 0; - if (cSprite["DiffuseTexture"]) { - DiffuseTexture = ResourceManager::Load(cSprite["DiffuseTexture"]); - } else { - DiffuseTexture = nullptr; - } - if (cSprite["GlowMap"]) { - IncandescenceTexture = ResourceManager::Load(cSprite["GlowMap"]); - } else { - IncandescenceTexture = nullptr; - } + DiffuseTexture = CommonFunctions::LoadTexture(cSprite["DiffuseTexture"], true); + + IncandescenceTexture = CommonFunctions::LoadTexture(cSprite["GlowMap"], true); + StartIndex = matGroup.StartIndex; EndIndex = matGroup.EndIndex; Matrix = matrix; Color = cSprite["Color"]; Entity = cSprite.EntityID; - glm::vec3 abspos = Transform::AbsolutePosition(world, cSprite.EntityID); - glm::vec3 viewpos = glm::vec3(camera->ViewMatrix() * glm::vec4(abspos, 1)); + Position = Transform::AbsolutePosition(world, cSprite.EntityID); + glm::vec3 viewpos = glm::vec3(camera->ViewMatrix() * glm::vec4(Position, 1)); Depth = viewpos.z; World = world; @@ -58,6 +52,7 @@ struct SpriteJob : RenderJob const Texture* IncandescenceTexture; float Shininess = 0.f; glm::vec4 Color; + glm::vec3 Position; const ::Model* Model = nullptr; unsigned int StartIndex = 0; unsigned int EndIndex = 0; diff --git a/include/Engine/Rendering/Util/CommonFunctions.h b/include/Engine/Rendering/Util/CommonFunctions.h index b262568c..e178f52d 100644 --- a/include/Engine/Rendering/Util/CommonFunctions.h +++ b/include/Engine/Rendering/Util/CommonFunctions.h @@ -4,14 +4,11 @@ #include "../../Common.h" #include "../../OpenGL.h" #include "../../GLM.h" +#include "../Texture.h" -class CommonFuntions -{ -public: - CommonFuntions() = delete; - -private: - +namespace CommonFunctions +{ +Texture* LoadTexture(std::string path, bool threaded); }; #endif \ No newline at end of file diff --git a/resources/Schema/Entities/QualityAssurance.xml b/resources/Schema/Entities/QualityAssurance.xml index e057cf38..23ebb4a5 100644 --- a/resources/Schema/Entities/QualityAssurance.xml +++ b/resources/Schema/Entities/QualityAssurance.xml @@ -40,7 +40,7 @@ - + @@ -188,7 +188,7 @@ - + @@ -683,7 +683,7 @@ - + @@ -730,7 +730,7 @@ - + @@ -790,7 +790,7 @@ - + @@ -837,7 +837,7 @@ - + @@ -883,7 +883,7 @@ - + @@ -930,7 +930,7 @@ - + @@ -977,7 +977,7 @@ - + @@ -1374,7 +1374,7 @@ - + @@ -1383,7 +1383,7 @@ true - 0.75008034908941568 + 0.75102457088592522 3.7999999523162842 true @@ -1430,7 +1430,7 @@ - + @@ -1439,7 +1439,7 @@ - 1.1999860997035228 + 1.2009303215000324 Models/Assault.mesh @@ -1482,7 +1482,7 @@ - + @@ -1497,7 +1497,7 @@ true - 0.68343188336345406 + 0.68437610515996361 true @@ -1553,6 +1553,50 @@ + + + + + + + + + + + + Textures/FoliageDiff.png + Textures/DefenderGunBlueIncd.png + + + + + + + + + Textures/FoliageDiff.png + Textures/AssaultWeaponBlueGlowMap.png + + + + + + + + + + + Textures/FoliageDiff.png + Textures/GlowTest.png + + + + + + + + + diff --git a/resources/Shaders/Sprite.vert.glsl b/resources/Shaders/Sprite.vert.glsl index e910a26a..c09d745b 100644 --- a/resources/Shaders/Sprite.vert.glsl +++ b/resources/Shaders/Sprite.vert.glsl @@ -16,7 +16,8 @@ out VertexData{ void main() { - gl_Position = P * M * vec4(Position, 1.0); + + gl_Position = P * V * M * vec4(Position, 1.0); Output.Position = Position; Output.TextureCoordinate = TextureCoords; diff --git a/src/Engine/Rendering/DrawBloomPass.cpp b/src/Engine/Rendering/DrawBloomPass.cpp index 5d8b2359..2c9e47b4 100644 --- a/src/Engine/Rendering/DrawBloomPass.cpp +++ b/src/Engine/Rendering/DrawBloomPass.cpp @@ -13,7 +13,7 @@ DrawBloomPass::DrawBloomPass(IRenderer* renderer) void DrawBloomPass::InitializeTextures() { - m_WhiteTexture = ResourceManager::Load("Textures/Core/White.png"); + m_WhiteTexture = CommonFunctions::LoadTexture("Textures/Core/White.png", false); } void DrawBloomPass::InitializeShaderPrograms() diff --git a/src/Engine/Rendering/DrawFinalPass.cpp b/src/Engine/Rendering/DrawFinalPass.cpp index e5919fbe..98f17835 100644 --- a/src/Engine/Rendering/DrawFinalPass.cpp +++ b/src/Engine/Rendering/DrawFinalPass.cpp @@ -11,10 +11,11 @@ DrawFinalPass::DrawFinalPass(IRenderer* renderer, LightCullingPass* lightCulling void DrawFinalPass::InitializeTextures() { - m_WhiteTexture = ResourceManager::Load("Textures/Core/White.png"); - m_BlackTexture = ResourceManager::Load("Textures/Core/Black.png"); - m_NeutralNormalTexture = ResourceManager::Load("Textures/Core/NeutralNormalMap.png"); - m_GreyTexture = ResourceManager::Load("Textures/Core/Grey.png"); + m_WhiteTexture = CommonFunctions::LoadTexture("Textures/Core/White.png", false); + m_BlackTexture = CommonFunctions::LoadTexture("Textures/Core/Black.png", false); + m_NeutralNormalTexture = CommonFunctions::LoadTexture("Textures/Core/NeutralNormalMap.png", false); + m_GreyTexture = CommonFunctions::LoadTexture("Textures/Core/Grey.png", false); + m_ErrorTexture = CommonFunctions::LoadTexture("Textures/Core/ErrorTexture.png", false); } void DrawFinalPass::InitializeFrameBuffers() @@ -56,7 +57,7 @@ void DrawFinalPass::InitializeShaderPrograms() m_ExplosionEffectProgram->Link(); GLERROR("Creating explosion program"); - m_SpriteProgram = ResourceManager::Load("#m_SpriteProgram"); + m_SpriteProgram = ResourceManager::Load("#SpriteProgram"); m_SpriteProgram->AddShader(std::shared_ptr(new VertexShader("Shaders/Sprite.vert.glsl"))); m_SpriteProgram->AddShader(std::shared_ptr(new FragmentShader("Shaders/Sprite.frag.glsl"))); m_SpriteProgram->Compile(); @@ -222,10 +223,12 @@ void DrawFinalPass::DrawSprites(std::list>&jobs, Rend for(auto& job : jobs) { auto spriteJob = std::dynamic_pointer_cast(job); - if(spriteJob) { + if (spriteJob) { + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(spriteJob->Matrix)); glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "V"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ViewMatrix())); glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "P"), 1, GL_FALSE, glm::value_ptr(scene.Camera->ProjectionMatrix())); + glUniform3fv(glGetUniformLocation(shaderHandle, "CameraPos"), 1, glm::value_ptr(scene.Camera->Position())); glUniform4fv(glGetUniformLocation(shaderHandle, "Color"), 1, glm::value_ptr(spriteJob->Color)); glUniform4fv(glGetUniformLocation(shaderHandle, "FillColor"), 1, glm::value_ptr(spriteJob->FillColor)); glUniform1f(glGetUniformLocation(shaderHandle, "FillPercentage"), spriteJob->FillPercentage); @@ -234,7 +237,7 @@ void DrawFinalPass::DrawSprites(std::list>&jobs, Rend if (spriteJob->DiffuseTexture != nullptr) { glBindTexture(GL_TEXTURE_2D, spriteJob->DiffuseTexture->m_Texture); } else { - glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); + glBindTexture(GL_TEXTURE_2D, m_ErrorTexture->m_Texture); } glActiveTexture(GL_TEXTURE1); diff --git a/src/Engine/Rendering/Model.cpp b/src/Engine/Rendering/Model.cpp index cf4923a3..82dd71af 100644 --- a/src/Engine/Rendering/Model.cpp +++ b/src/Engine/Rendering/Model.cpp @@ -7,16 +7,16 @@ Model::Model(std::string fileName) for (auto& group : m_RawModel->MaterialGroups) { if (!group.TexturePath.empty()) { - group.Texture = std::shared_ptr(ResourceManager::Load(group.TexturePath)); + group.Texture = std::shared_ptr(CommonFunctions::LoadTexture(group.TexturePath, false)); } if (!group.NormalMapPath.empty()) { - group.NormalMap = std::shared_ptr(ResourceManager::Load(group.NormalMapPath)); + group.NormalMap = std::shared_ptr(CommonFunctions::LoadTexture(group.NormalMapPath, false)); } if (!group.SpecularMapPath.empty()) { - group.SpecularMap = std::shared_ptr(ResourceManager::Load(group.SpecularMapPath)); + group.SpecularMap = std::shared_ptr(CommonFunctions::LoadTexture(group.SpecularMapPath, false)); } if (!group.IncandescenceMapPath.empty()) { - group.IncandescenceMap = std::shared_ptr(ResourceManager::Load(group.IncandescenceMapPath)); + group.IncandescenceMap = std::shared_ptr(CommonFunctions::LoadTexture(group.IncandescenceMapPath, false)); } } diff --git a/src/Engine/Rendering/PNG.cpp b/src/Engine/Rendering/PNG.cpp index 7ffd5c20..409da9b1 100644 --- a/src/Engine/Rendering/PNG.cpp +++ b/src/Engine/Rendering/PNG.cpp @@ -4,40 +4,35 @@ PNG::PNG(std::string path) { FILE* file = fopen(path.c_str(), "rb"); if (!file) { - LOG_ERROR("Failed to open texture file \"%s\": %s", path.c_str(), const_cast(strerror(errno))); - return; + throw Resource::FailedLoadingException("Failed to open texture file."); } png_byte header[8]; fread(header, 1, 8, file); bool isPNG = !png_sig_cmp(header, 0, 8); if (!isPNG) { - LOG_ERROR("Failed to load texture file \"%s\": File isn't PNG", path.c_str()); fclose(file); - return; + throw Resource::FailedLoadingException("File is not PNG."); } // Initialize libpng png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, (png_error_ptr)&PNG::pngErrorFunction, (png_error_ptr)&PNG::pngErrorFunction); if (!png_ptr) { - LOG_ERROR("libpng: Failed to initialze png_struct"); png_destroy_read_struct(&png_ptr, nullptr, nullptr); fclose(file); - return; + throw Resource::FailedLoadingException("Failed to initialze png_struct."); } png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { - LOG_ERROR("libpng: Failed to initialze png_info"); png_destroy_read_struct(&png_ptr, nullptr, nullptr); fclose(file); - return; + throw Resource::FailedLoadingException("Failed to initialze png_info."); } png_infop info_end_ptr = png_create_info_struct(png_ptr); if (!info_end_ptr) { - LOG_ERROR("libpng: Failed to initialze second png_info"); png_destroy_read_struct(&png_ptr, &info_ptr, nullptr); fclose(file); - return; + throw Resource::FailedLoadingException("Failed to initialze second png_info."); } png_init_io(png_ptr, file); @@ -51,8 +46,8 @@ PNG::PNG(std::string path) unsigned int width, height; png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL); if (bit_depth != 8) { - LOG_ERROR("libpng: Unsupported bit depth \"%i\" of image \"%s\", must be 8", bit_depth, path.c_str()); - return; + throw Resource::FailedLoadingException("Unsupported bit depth. Must be 8"); + } switch (color_type) { case PNG_COLOR_TYPE_RGB: @@ -60,8 +55,7 @@ PNG::PNG(std::string path) Format = Image::ImageFormat::RGBA; break; default: - LOG_ERROR("libpng: Unsupported color format \"%i\" of image \"%s\"", color_type, path.c_str()); - return; + throw Resource::FailedLoadingException("Unsupported color format."); } // Convert RGB to RGBA, since DirectX rather treat them all the same way diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index a63e02a0..c0e4d07c 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -106,16 +106,21 @@ void Renderer::Draw(RenderFrame& frame) for (auto scene : frame.RenderScenes){ SortRenderJobsByDepth(*scene); + GLERROR("SortByDepth"); m_PickingPass->Draw(*scene); + GLERROR("Drawing pickingpass"); m_LightCullingPass->GenerateNewFrustum(*scene); + GLERROR("Generate frustums"); m_LightCullingPass->FillLightList(*scene); + GLERROR("Filling light list"); m_LightCullingPass->CullLights(*scene); + GLERROR("LightCulling"); m_DrawFinalPass->Draw(*scene); + GLERROR("Draw Geometry+Light"); //m_DrawScenePass->Draw(*scene); - GLERROR("Renderer::Draw m_DrawScenePass->Draw"); - m_TextPass->Draw(*scene, *m_DrawFinalPass->FinalPassFrameBuffer()); + GLERROR("Draw Text"); } m_DrawBloomPass->Draw(m_DrawFinalPass->BloomTexture()); @@ -136,7 +141,8 @@ void Renderer::Draw(RenderFrame& frame) } m_ImGuiRenderPass->Draw(); - glfwSwapBuffers(m_Window); + GLERROR("Imgui draw"); + glfwSwapBuffers(m_Window); } PickData Renderer::Pick(glm::vec2 screenCoord) @@ -146,8 +152,8 @@ PickData Renderer::Pick(glm::vec2 screenCoord) void Renderer::InitializeTextures() { - m_ErrorTexture = ResourceManager::Load("Textures/Core/ErrorTexture.png"); - m_WhiteTexture = ResourceManager::Load("Textures/Core/White.png"); + m_ErrorTexture = CommonFunctions::LoadTexture("Textures/Core/ErrorTexture.png", false); + m_WhiteTexture = CommonFunctions::LoadTexture("Textures/Core/White.png", false); } @@ -155,6 +161,7 @@ void Renderer::SortRenderJobsByDepth(RenderScene &scene) { //Sort all forward jobs so transparency is good. scene.TransparentObjects.sort(Renderer::DepthSort); + scene.SpriteJobs.sort(Renderer::DepthSort); } void Renderer::GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type) diff --git a/src/Engine/Rendering/Texture.cpp b/src/Engine/Rendering/Texture.cpp index 492b35be..a4b2d4c9 100644 --- a/src/Engine/Rendering/Texture.cpp +++ b/src/Engine/Rendering/Texture.cpp @@ -2,24 +2,25 @@ Texture::Texture(std::string path) { + PNG* img = ResourceManager::Load(path); //TODO: Make this threaded. Catch exeptions in all other load places. - PNG image(path); + //PNG image(path); - if (image.Width == 0 && image.Height == 0 || image.Format == Image::ImageFormat::Unknown) { - //image = PNG("Textures/Core/ErrorTexture.png"); - return; // Temporary fix to remove crash - /* - if (image.Width == 0 && image.Height == 0 || image.Format == Image::ImageFormat::Unknown) { - LOG_ERROR("Couldn't even load the error texture. This is a dark day indeed."); - return; - }*/ - } + //if (img->Width == 0 && img->Height == 0 || img->Format == Image::ImageFormat::Unknown) { + // //image = PNG("Textures/Core/ErrorTexture.png"); + // //return; // Temporary fix to remove crash - this->Width = image.Width; - this->Height = image.Height; + // if (img->Width == 0 && img->Height == 0 || img->Format == Image::ImageFormat::Unknown) { + // LOG_ERROR("Couldn't even load the error texture. This is a dark day indeed."); + // return; + // } + //} + + this->Width = img->Width; + this->Height = img->Height; GLint format; - switch (image.Format) { + switch (img->Format) { case Image::ImageFormat::RGB: format = GL_RGB; break; @@ -32,7 +33,7 @@ Texture::Texture(std::string path) glGenTextures(1, &m_Texture); glBindTexture(GL_TEXTURE_2D, m_Texture); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexImage2D(GL_TEXTURE_2D, 0, format, image.Width, image.Height, 0, format, GL_UNSIGNED_BYTE, image.Data); + glTexImage2D(GL_TEXTURE_2D, 0, format, img->Width, img->Height, 0, format, GL_UNSIGNED_BYTE, img->Data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); diff --git a/src/Engine/Rendering/Util/CommonFunctions.cpp b/src/Engine/Rendering/Util/CommonFunctions.cpp index 3c81de66..382cb790 100644 --- a/src/Engine/Rendering/Util/CommonFunctions.cpp +++ b/src/Engine/Rendering/Util/CommonFunctions.cpp @@ -1,2 +1,19 @@ #include "Rendering/Util/CommonFunctions.h" +Texture* CommonFunctions::LoadTexture(std::string path, bool threaded) +{ + Texture* img; + try { + if(threaded) { + img = ResourceManager::Load(path); + } else { + img = ResourceManager::Load(path); + } + } catch (const Resource::StillLoadingException&) { + img = ResourceManager::Load("Textures/Core/ErrorTexture.png"); + } catch (const std::exception&) { + img = nullptr; + } + + return img; +} diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index 0cc41a73..780842a6 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -25,6 +25,7 @@ Game::Game(int argc, char* argv[]) ResourceManager::RegisterType("Model"); ResourceManager::RegisterType("RawModel"); ResourceManager::RegisterType("Texture"); + ResourceManager::RegisterType("Png"); ResourceManager::RegisterType("ShaderProgram"); ResourceManager::RegisterType("EntityFile"); ResourceManager::RegisterType("FontFile");