From cbb17d22d49e2abe7cb414d2bbdd0af9857de28b Mon Sep 17 00:00:00 2001 From: Tleety Date: Wed, 27 Jan 2016 12:10:00 +0100 Subject: [PATCH 1/6] WIP --- include/Engine/Rendering/DrawFinalPass.h | 4 + include/Engine/Rendering/RenderQueue.h | 6 +- include/Engine/Rendering/RenderSystem.h | 2 +- resources/Schema/Components/Model.xml | 1 + resources/Schema/Components/Model.xsd | 3 + src/Engine/Editor/EditorRenderSystem.cpp | 6 +- src/Engine/Rendering/DrawFinalPass.cpp | 291 ++++++++++++++--------- src/Engine/Rendering/PickingPass.cpp | 48 +++- src/Engine/Rendering/RenderSystem.cpp | 17 +- src/Engine/Rendering/Renderer.cpp | 2 +- 10 files changed, 255 insertions(+), 125 deletions(-) diff --git a/include/Engine/Rendering/DrawFinalPass.h b/include/Engine/Rendering/DrawFinalPass.h index 6bd8acec..041f7999 100644 --- a/include/Engine/Rendering/DrawFinalPass.h +++ b/include/Engine/Rendering/DrawFinalPass.h @@ -30,6 +30,10 @@ public: private: void GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type) const; void GenerateMipMapTexture(GLuint* texture, GLenum wrapping, glm::vec2 dimensions, GLint format, GLenum type, GLint numMipMaps) const; + void DrawModelRenderQueues(std::list>& job); + + void BindExplosionUniforms(); + void BindExplosionTextures(); Texture* m_WhiteTexture; Texture* m_BlackTexture; diff --git a/include/Engine/Rendering/RenderQueue.h b/include/Engine/Rendering/RenderQueue.h index 08a67ac7..57371146 100644 --- a/include/Engine/Rendering/RenderQueue.h +++ b/include/Engine/Rendering/RenderQueue.h @@ -19,7 +19,8 @@ struct RenderScene { ::Camera* Camera = nullptr; - std::list> ForwardJobs; + std::list> OpaqueObjects; + std::list> TransparentObjects; std::list> PointLightJobs; std::list> TextJobs; std::list> DirectionalLightJobs; @@ -28,7 +29,8 @@ struct RenderScene void Clear() { - ForwardJobs.clear(); + OpaqueObjects.clear(); + TransparentObjects.clear(); PointLightJobs.clear(); TextJobs.clear(); DirectionalLightJobs.clear(); diff --git a/include/Engine/Rendering/RenderSystem.h b/include/Engine/Rendering/RenderSystem.h index 37f99341..d4915e93 100644 --- a/include/Engine/Rendering/RenderSystem.h +++ b/include/Engine/Rendering/RenderSystem.h @@ -41,7 +41,7 @@ private: EventRelay m_EInputCommand; bool OnInputCommand(const Events::InputCommand& e); - void fillModels(std::list>& jobs); + void fillModels(std::list>& opaqueJobs, std::list>& transparentJobs); void fillLight(std::list>& jobs); EventRelay m_EPlayerSpawned; diff --git a/resources/Schema/Components/Model.xml b/resources/Schema/Components/Model.xml index 8f78b9ee..b25c8828 100644 --- a/resources/Schema/Components/Model.xml +++ b/resources/Schema/Components/Model.xml @@ -2,5 +2,6 @@ + false true \ No newline at end of file diff --git a/resources/Schema/Components/Model.xsd b/resources/Schema/Components/Model.xsd index fb0774f8..0ea04d72 100644 --- a/resources/Schema/Components/Model.xsd +++ b/resources/Schema/Components/Model.xsd @@ -15,6 +15,9 @@ Color tint + + Wether the model is transparent or not + Wether the model is visible or not diff --git a/src/Engine/Editor/EditorRenderSystem.cpp b/src/Engine/Editor/EditorRenderSystem.cpp index 2aad1340..14b668d8 100644 --- a/src/Engine/Editor/EditorRenderSystem.cpp +++ b/src/Engine/Editor/EditorRenderSystem.cpp @@ -49,7 +49,11 @@ void EditorRenderSystem::Update(double dt) glm::mat4 modelMatrix = Transform::ModelMatrix(entity.ID, entity.World); for (auto matGroup : model->MaterialGroups()) { std::shared_ptr modelJob = std::make_shared(model, scene.Camera, modelMatrix, matGroup, cModel, entity.World, glm::vec4(0), 0.f); - scene.ForwardJobs.push_back(modelJob); + if(cModel["Transparent"]) { + scene.TransparentObjects.push_back(modelJob); + } else { + scene.OpaqueObjects.push_back(modelJob); + } } } } diff --git a/src/Engine/Rendering/DrawFinalPass.cpp b/src/Engine/Rendering/DrawFinalPass.cpp index a85a80d6..b817d636 100644 --- a/src/Engine/Rendering/DrawFinalPass.cpp +++ b/src/Engine/Rendering/DrawFinalPass.cpp @@ -58,124 +58,38 @@ void DrawFinalPass::Draw(RenderScene& scene) GLERROR("DrawFinalPass::Draw: Pre"); DrawFinalPassState* state = new DrawFinalPassState(m_FinalPassFrameBuffer.GetHandle()); - GLuint shaderHandle; + + DrawModelRenderQueues(scene.OpaqueObjects); + GLERROR("DrawFinalPass::Draw: OpaqueObjects"); + DrawModelRenderQueues(scene.TransparentObjects); + GLERROR("DrawFinalPass::Draw: TransparentObjects"); + + + //Draw opaque + if(explosion) { + //uniforms + } else { + //uniforms + } + //draw transparent + if(explosion) { + + } else { + + } + + + + //Explosion uniforms + + + //Normal Uniforms //TODO: Render: Add code for more jobs than modeljobs. for (auto &job : scene.ForwardJobs) { - auto explosionEffectJob = std::dynamic_pointer_cast(job); - if (explosionEffectJob) { - m_ExplosionEffectProgram->Bind(); - shaderHandle = m_ExplosionEffectProgram->GetHandle(); //--- + - GLERROR("DrawFinalPass::ExplosionEffect: 1"); - //TODO: Kolla upp "header/include/common" shader saken så man slipper skicka in asmycket uniforms - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(explosionEffectJob->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())); - glUniform4fv(glGetUniformLocation(shaderHandle, "Color"), 1, glm::value_ptr(explosionEffectJob->Color)); - glUniform3fv(glGetUniformLocation(shaderHandle, "ExplosionOrigin"), 1, glm::value_ptr(explosionEffectJob->ExplosionOrigin)); - glUniform1f(glGetUniformLocation(shaderHandle, "TimeSinceDeath"), explosionEffectJob->TimeSinceDeath); - glUniform1f(glGetUniformLocation(shaderHandle, "ExplosionDuration"), explosionEffectJob->ExplosionDuration); - glUniform4fv(glGetUniformLocation(shaderHandle, "EndColor"), 1, glm::value_ptr(explosionEffectJob->EndColor)); - glUniform1i(glGetUniformLocation(shaderHandle, "Randomness"), explosionEffectJob->Randomness); - glUniform1fv(glGetUniformLocation(shaderHandle, "RandomNumbers"), 50, explosionEffectJob->RandomNumbers.data()); - glUniform1f(glGetUniformLocation(shaderHandle, "RandomnessScalar"), explosionEffectJob->RandomnessScalar); - glUniform2fv(glGetUniformLocation(shaderHandle, "Velocity"), 1, glm::value_ptr(explosionEffectJob->Velocity)); - glUniform1i(glGetUniformLocation(shaderHandle, "ColorByDistance"), explosionEffectJob->ColorByDistance); - glUniform1i(glGetUniformLocation(shaderHandle, "ExponentialAccelaration"), explosionEffectJob->ExponentialAccelaration); - glUniform4fv(glGetUniformLocation(shaderHandle, "DiffuseColor"), 1, glm::value_ptr(explosionEffectJob->DiffuseColor)); - GLERROR("DrawFinalPass::ExplosionEffect: 2"); - - if (explosionEffectJob->Model->m_RawModel->m_Skeleton != nullptr) { - - if (explosionEffectJob->Animation != nullptr) { - std::vector frameBones = explosionEffectJob->Skeleton->GetFrameBones(*explosionEffectJob->Animation, explosionEffectJob->AnimationTime); - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); - } - } - - //TODO: Renderer: bättre textur felhantering samt fler texturer stöd - if (explosionEffectJob->DiffuseTexture != nullptr) { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, explosionEffectJob->DiffuseTexture->m_Texture); - } else { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); - } - glBindVertexArray(explosionEffectJob->Model->VAO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, explosionEffectJob->Model->ElementBuffer); - glDrawElements(GL_TRIANGLES, explosionEffectJob->EndIndex - explosionEffectJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(explosionEffectJob->StartIndex*sizeof(unsigned int))); - GLERROR("DrawFinalPass::ExplosionEffect: END"); - //m_ExplosionEffectProgram->Unbind(); - - } else { - auto modelJob = std::dynamic_pointer_cast(job); - if (modelJob) { - m_ForwardPlusProgram->Bind(); - GLERROR("1.1"); - shaderHandle = m_ForwardPlusProgram->GetHandle(); - - if (scene.ClearDepth) { - glClear(GL_DEPTH_BUFFER_BIT); - } - - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_LightCullingPass->LightSSBO()); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_LightCullingPass->LightGridSSBO()); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, m_LightCullingPass->LightIndexSSBO()); - 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())); - glUniform2f(glGetUniformLocation(shaderHandle, "ScreenDimensions"), m_Renderer->Resolution().Width, m_Renderer->Resolution().Height); - - //TODO: Kolla upp "header/include/common" shader saken så man slipper skicka in asmycket uniforms - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix)); - glUniform4fv(glGetUniformLocation(shaderHandle, "Color"), 1, glm::value_ptr(modelJob->Color)); - glUniform4fv(glGetUniformLocation(shaderHandle, "DiffuseColor"), 1, glm::value_ptr(modelJob->DiffuseColor)); - - glUniform4fv(glGetUniformLocation(shaderHandle, "FillColor"), 1, glm::value_ptr(modelJob->FillColor)); - glUniform1f(glGetUniformLocation(shaderHandle, "FillPercentage"), modelJob->FillPercentage); - - - if (modelJob->DiffuseTexture != nullptr) { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture->m_Texture); - } else { - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); - } - glActiveTexture(GL_TEXTURE1); - if (modelJob->IncandescenceTexture != nullptr) { - glBindTexture(GL_TEXTURE_2D, modelJob->IncandescenceTexture->m_Texture); - } else { - glBindTexture(GL_TEXTURE_2D, m_BlackTexture->m_Texture); - } - - /*if(modelJob->GlowMap != nullptr) { - glBindTexture(GL_TEXTURE_2D, modelJob->GlowMap->m_Texture); - } else { - glBindTexture(GL_TEXTURE_2D, m_BlackTexture->m_Texture); - }*/ - - //TODO: Fixa så att modelsJobs kan spela upp olika animationer och så att den kan få in en tid istället för 1.0f - Hälsningar Johan och Andreas :) - if (modelJob->Model->m_RawModel->m_Skeleton != nullptr) { - - if (modelJob->Animation != nullptr) { - std::vector frameBones = modelJob->Skeleton->GetFrameBones(*modelJob->Animation, modelJob->AnimationTime); - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); - } - } - - glBindVertexArray(modelJob->Model->VAO); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); - glDrawElements(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(modelJob->StartIndex*sizeof(unsigned int))); - GLERROR("DrawFinalPass::Model: END"); - // m_ForwardPlusProgram->Unbind(); - - } - } - - } GLERROR("DrawFinalPass::Draw: END"); delete state; } @@ -213,4 +127,153 @@ void DrawFinalPass::GenerateMipMapTexture(GLuint* texture, GLenum wrapping, glm: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); GLERROR("MipMap Texture initialization failed"); -} \ No newline at end of file +} + +void DrawFinalPass::DrawModelRenderQueues(std::list>& jobs) +{ + for(auto &job : jobs) + { + auto explosionEffectJob = std::dynamic_pointer_cast(job); + if(explosionEffectJob) { + //Bind program + m_ExplosionEffectProgram->Bind(); + GLuint shaderHandle = m_ExplosionEffectProgram->GetHandle(); + + //Bind uniforms + BindExplosionUniforms(); + + //bind textures + + //draw + } else { + //bind forward program + //shaderhandle + + //bind uniforms + + //bind textures + + //draw + + } + + + } + + auto explosionEffectJob = std::dynamic_pointer_cast(job); + if (explosionEffectJob) { + m_ExplosionEffectProgram->Bind(); + shaderHandle = m_ExplosionEffectProgram->GetHandle(); //--- + + GLERROR("DrawFinalPass::ExplosionEffect: 1"); + //TODO: Kolla upp "header/include/common" shader saken så man slipper skicka in asmycket uniforms + + GLERROR("DrawFinalPass::ExplosionEffect: 2"); + + if (explosionEffectJob->Model->m_RawModel->m_Skeleton != nullptr) { + + if (explosionEffectJob->Animation != nullptr) { + std::vector frameBones = explosionEffectJob->Skeleton->GetFrameBones(*explosionEffectJob->Animation, explosionEffectJob->AnimationTime); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } + } + + //TODO: Renderer: bättre textur felhantering samt fler texturer stöd + glActiveTexture(GL_TEXTURE0); + if (explosionEffectJob->DiffuseTexture != nullptr) { + glBindTexture(GL_TEXTURE_2D, explosionEffectJob->DiffuseTexture->m_Texture); + } else { + glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); + } + + + glBindVertexArray(explosionEffectJob->Model->VAO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, explosionEffectJob->Model->ElementBuffer); + glDrawElements(GL_TRIANGLES, explosionEffectJob->EndIndex - explosionEffectJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(explosionEffectJob->StartIndex*sizeof(unsigned int))); + GLERROR("DrawFinalPass::ExplosionEffect: END"); + //m_ExplosionEffectProgram->Unbind(); + + } else { + auto modelJob = std::dynamic_pointer_cast(job); + if (modelJob) { + m_ForwardPlusProgram->Bind(); + GLERROR("1.1"); + shaderHandle = m_ForwardPlusProgram->GetHandle(); + + if (scene.ClearDepth) { + glClear(GL_DEPTH_BUFFER_BIT); + } + + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_LightCullingPass->LightSSBO()); + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_LightCullingPass->LightGridSSBO()); + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, m_LightCullingPass->LightIndexSSBO()); + 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())); + glUniform2f(glGetUniformLocation(shaderHandle, "ScreenDimensions"), m_Renderer->Resolution().Width, m_Renderer->Resolution().Height); + GLERROR("DrawFinalPass::Draw: Uniforms"); + + DrawModelRenderQueues(scene.OpaqueObjects, shaderHandle); + GLERROR("DrawFinalPass::Draw: OpaqueObjects"); + DrawModelRenderQueues(scene.TransparentObjects, shaderHandle); + GLERROR("DrawFinalPass::Draw: TransparentObjects"); + } + + for (auto &job : job) { + if (modelJob) { + //TODO: Kolla upp "header/include/common" shader saken så man slipper skicka in asmycket uniforms + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix)); + glUniform4fv(glGetUniformLocation(shaderHandle, "Color"), 1, glm::value_ptr(modelJob->Color)); + glUniform4fv(glGetUniformLocation(shaderHandle, "DiffuseColor"), 1, glm::value_ptr(modelJob->DiffuseColor)); + glUniform4fv(glGetUniformLocation(shaderHandle, "FillColor"), 1, glm::value_ptr(modelJob->FillColor)); + glUniform1f(glGetUniformLocation(shaderHandle, "FillPercentage"), modelJob->FillPercentage); + + glActiveTexture(GL_TEXTURE0); + if (modelJob->DiffuseTexture != nullptr) { + glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture->m_Texture); + } else { + glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); + } + glActiveTexture(GL_TEXTURE1); + if (modelJob->IncandescenceTexture != nullptr) { + glBindTexture(GL_TEXTURE_2D, modelJob->IncandescenceTexture->m_Texture); + } else { + glBindTexture(GL_TEXTURE_2D, m_BlackTexture->m_Texture); + } + + //TODO: Fixa så att modelsJobs kan spela upp olika animationer och så att den kan få in en tid istället för 1.0f - Hälsningar Johan och Andreas :) + if (modelJob->Model->m_RawModel->m_Skeleton != nullptr) { + if (modelJob->Animation != nullptr) { + std::vector frameBones = modelJob->Skeleton->GetFrameBones(*modelJob->Animation, modelJob->AnimationTime); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } + } + + glBindVertexArray(modelJob->Model->VAO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); + glDrawElements(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(modelJob->StartIndex*sizeof(unsigned int))); + GLERROR("DrawFinalPass::Model: END"); + // m_ForwardPlusProgram->Unbind(); + + } + } +} + +void DrawFinalPass::BindExplosionUniforms() +{ + 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())); + + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(explosionEffectJob->Matrix)); + glUniform4fv(glGetUniformLocation(shaderHandle, "Color"), 1, glm::value_ptr(explosionEffectJob->Color)); + glUniform3fv(glGetUniformLocation(shaderHandle, "ExplosionOrigin"), 1, glm::value_ptr(explosionEffectJob->ExplosionOrigin)); + glUniform1f(glGetUniformLocation(shaderHandle, "TimeSinceDeath"), explosionEffectJob->TimeSinceDeath); + glUniform1f(glGetUniformLocation(shaderHandle, "ExplosionDuration"), explosionEffectJob->ExplosionDuration); + glUniform4fv(glGetUniformLocation(shaderHandle, "EndColor"), 1, glm::value_ptr(explosionEffectJob->EndColor)); + glUniform1i(glGetUniformLocation(shaderHandle, "Randomness"), explosionEffectJob->Randomness); + glUniform1f(glGetUniformLocation(shaderHandle, "RandomnessScalar"), explosionEffectJob->RandomnessScalar); + glUniform2fv(glGetUniformLocation(shaderHandle, "Velocity"), 1, glm::value_ptr(explosionEffectJob->Velocity)); + glUniform1i(glGetUniformLocation(shaderHandle, "ColorByDistance"), explosionEffectJob->ColorByDistance); + glUniform1i(glGetUniformLocation(shaderHandle, "ExponentialAccelaration"), explosionEffectJob->ExponentialAccelaration); + glUniform4fv(glGetUniformLocation(shaderHandle, "DiffuseColor"), 1, glm::value_ptr(explosionEffectJob->DiffuseColor)); + glUniform1fv(glGetUniformLocation(shaderHandle, "RandomNumbers"), 50, explosionEffectJob->RandomNumbers.data()); +} diff --git a/src/Engine/Rendering/PickingPass.cpp b/src/Engine/Rendering/PickingPass.cpp index c7e23479..70650a9f 100644 --- a/src/Engine/Rendering/PickingPass.cpp +++ b/src/Engine/Rendering/PickingPass.cpp @@ -56,7 +56,7 @@ void PickingPass::Draw(RenderScene& scene) } m_Camera = scene.Camera; - for (auto &job : scene.ForwardJobs) { + for (auto &job : scene.OpaqueObjects) { auto modelJob = std::dynamic_pointer_cast(job); if (modelJob) { @@ -101,6 +101,52 @@ void PickingPass::Draw(RenderScene& scene) glDrawElements(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(modelJob->StartIndex * sizeof(unsigned int))); } } + + for (auto &job : scene.TransparentObjects) { + auto modelJob = std::dynamic_pointer_cast(job); + + if (modelJob) { + int pickColor[2] = { m_ColorCounter[0], m_ColorCounter[1] }; + + PickingInfo pickInfo; + pickInfo.Entity = modelJob->Entity; + pickInfo.World = modelJob->World; + pickInfo.Camera = scene.Camera; + + auto color = m_EntityColors.find(std::make_tuple(pickInfo.Entity, pickInfo.World, pickInfo.Camera)); + if (color != m_EntityColors.end()) { + pickColor[0] = color->second[0]; + pickColor[1] = color->second[1]; + } else { + m_EntityColors[std::make_tuple(pickInfo.Entity, pickInfo.World, pickInfo.Camera)] = glm::ivec2(pickColor[0], pickColor[1]); + if (m_ColorCounter[0] > 255) { + m_ColorCounter[0] = 0; + m_ColorCounter[1] += 5; + } else { + m_ColorCounter[0] += 50; + } + } + + m_PickingColorsToEntity[glm::ivec2(pickColor[0], pickColor[1])] = pickInfo; + + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->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())); + glUniform2fv(glGetUniformLocation(shaderHandle, "PickingColor"), 1, glm::value_ptr(glm::vec2(pickColor[0], pickColor[1]))); + + if (modelJob->Model->m_RawModel->m_Skeleton != nullptr) { + + if (modelJob->Animation != nullptr) { + std::vector frameBones = modelJob->Skeleton->GetFrameBones(*modelJob->Animation, modelJob->AnimationTime); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } + } + + glBindVertexArray(modelJob->Model->VAO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); + glDrawElements(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(modelJob->StartIndex * sizeof(unsigned int))); + } + } m_PickingBuffer.Unbind(); GLERROR("PickingPass Error"); diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index 6d40ed29..9dbc0341 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -31,7 +31,7 @@ bool RenderSystem::OnSetCamera(Events::SetCamera& e) return true; } -void RenderSystem::fillModels(std::list>& jobs) +void RenderSystem::fillModels(std::list>& opaqueJobs, std::list>& transparentJobs) { auto models = m_World->GetComponents("Model"); if (models == nullptr) { @@ -75,7 +75,6 @@ void RenderSystem::fillModels(std::list>& jobs) } } - float fillPercentage = 0.f; glm::vec4 fillColor = glm::vec4(0); if(m_World->HasComponent(modelComponent.EntityID, "Fill")) { @@ -99,7 +98,11 @@ void RenderSystem::fillModels(std::list>& jobs) fillColor, fillPercentage )); - jobs.push_back(explosionEffectJob); + if (modelComponent["Transparent"]) { + transparentJobs.push_back(explosionEffectJob); + } else { + opaqueJobs.push_back(explosionEffectJob); + } } else { std::shared_ptr modelJob = std::shared_ptr(new ModelJob( model, @@ -111,7 +114,11 @@ void RenderSystem::fillModels(std::list>& jobs) fillColor, fillPercentage )); - jobs.push_back(modelJob); + if (modelComponent["Transparent"]) { + transparentJobs.push_back(modelJob); + } else { + opaqueJobs.push_back(modelJob); + } } } } @@ -222,7 +229,7 @@ void RenderSystem::Update(double dt) RenderScene scene; scene.Camera = m_Camera; scene.Viewport = Rectangle(1280, 720); - fillModels(scene.ForwardJobs); + fillModels(scene.OpaqueObjects, scene.TransparentObjects); fillPointLights(scene.PointLightJobs, m_World); fillDirectionalLights(scene.DirectionalLightJobs, m_World); fillText(scene.TextJobs, m_World); diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index af64b5cf..81f1a1cc 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -150,7 +150,7 @@ void Renderer::InitializeTextures() void Renderer::SortRenderJobsByDepth(RenderScene &scene) { //Sort all forward jobs so transparency is good. - scene.ForwardJobs.sort(Renderer::DepthSort); + scene.TransparentObjects.sort(Renderer::DepthSort); } void Renderer::GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type) From 8102f3a2eaa5a32f1e81a15384b2ceac78105f30 Mon Sep 17 00:00:00 2001 From: Tleety Date: Wed, 27 Jan 2016 12:59:44 +0100 Subject: [PATCH 2/6] Working transparency in most common cases. Transparency check is automatically set if the alpha color value is under 1 --- include/Engine/Rendering/DrawFinalPass.h | 9 +- include/Engine/Rendering/ModelJob.h | 1 - src/Engine/Rendering/DrawFinalPass.cpp | 267 ++++++++++------------- src/Engine/Rendering/RenderSystem.cpp | 7 + 4 files changed, 127 insertions(+), 157 deletions(-) diff --git a/include/Engine/Rendering/DrawFinalPass.h b/include/Engine/Rendering/DrawFinalPass.h index 041f7999..d57770e7 100644 --- a/include/Engine/Rendering/DrawFinalPass.h +++ b/include/Engine/Rendering/DrawFinalPass.h @@ -30,10 +30,13 @@ public: private: void GenerateTexture(GLuint* texture, GLenum wrapping, GLenum filtering, glm::vec2 dimensions, GLint internalFormat, GLint format, GLenum type) const; void GenerateMipMapTexture(GLuint* texture, GLenum wrapping, glm::vec2 dimensions, GLint format, GLenum type, GLint numMipMaps) const; - void DrawModelRenderQueues(std::list>& job); + void DrawModelRenderQueues(std::list>& job, RenderScene& scene); - void BindExplosionUniforms(); - void BindExplosionTextures(); + void BindExplosionUniforms(GLuint shaderHandle, std::shared_ptr& job, RenderScene& scene); + void BindModelUniforms(GLuint shaderHandle, std::shared_ptr& job, RenderScene& scene); + + void BindExplosionTextures(std::shared_ptr& job); + void BindModelTextures(std::shared_ptr& job); Texture* m_WhiteTexture; Texture* m_BlackTexture; diff --git a/include/Engine/Rendering/ModelJob.h b/include/Engine/Rendering/ModelJob.h index df7f1aec..d5a2d880 100644 --- a/include/Engine/Rendering/ModelJob.h +++ b/include/Engine/Rendering/ModelJob.h @@ -39,7 +39,6 @@ struct ModelJob : RenderJob Depth = worldpos.z; World = world; - FillColor = fillColor; FillPercentage = fillPercentage; Skeleton = Model->m_RawModel->m_Skeleton; diff --git a/src/Engine/Rendering/DrawFinalPass.cpp b/src/Engine/Rendering/DrawFinalPass.cpp index b817d636..4fd0c2aa 100644 --- a/src/Engine/Rendering/DrawFinalPass.cpp +++ b/src/Engine/Rendering/DrawFinalPass.cpp @@ -58,38 +58,15 @@ void DrawFinalPass::Draw(RenderScene& scene) GLERROR("DrawFinalPass::Draw: Pre"); DrawFinalPassState* state = new DrawFinalPassState(m_FinalPassFrameBuffer.GetHandle()); + if (scene.ClearDepth) { + glClear(GL_DEPTH_BUFFER_BIT); + } - DrawModelRenderQueues(scene.OpaqueObjects); + DrawModelRenderQueues(scene.OpaqueObjects, scene); GLERROR("DrawFinalPass::Draw: OpaqueObjects"); - DrawModelRenderQueues(scene.TransparentObjects); + DrawModelRenderQueues(scene.TransparentObjects, scene); GLERROR("DrawFinalPass::Draw: TransparentObjects"); - - //Draw opaque - if(explosion) { - //uniforms - } else { - //uniforms - } - //draw transparent - if(explosion) { - - } else { - - } - - - - //Explosion uniforms - - - //Normal Uniforms - - - //TODO: Render: Add code for more jobs than modeljobs. - for (auto &job : scene.ForwardJobs) { - - GLERROR("DrawFinalPass::Draw: END"); delete state; } @@ -129,151 +106,135 @@ void DrawFinalPass::GenerateMipMapTexture(GLuint* texture, GLenum wrapping, glm: GLERROR("MipMap Texture initialization failed"); } -void DrawFinalPass::DrawModelRenderQueues(std::list>& jobs) +void DrawFinalPass::DrawModelRenderQueues(std::list>& job, RenderScene& scene) { - for(auto &job : jobs) + GLuint forwardHandle = m_ForwardPlusProgram->GetHandle(); + GLuint explosionHandle = m_ExplosionEffectProgram->GetHandle(); + + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_LightCullingPass->LightSSBO()); + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_LightCullingPass->LightGridSSBO()); + glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, m_LightCullingPass->LightIndexSSBO()); + + for(auto &job : job) { auto explosionEffectJob = std::dynamic_pointer_cast(job); if(explosionEffectJob) { //Bind program m_ExplosionEffectProgram->Bind(); - GLuint shaderHandle = m_ExplosionEffectProgram->GetHandle(); //Bind uniforms - BindExplosionUniforms(); + BindExplosionUniforms(explosionHandle, explosionEffectJob, scene); - //bind textures + if (explosionEffectJob->Model->m_RawModel->m_Skeleton != nullptr) { - //draw - } else { - //bind forward program - //shaderhandle - - //bind uniforms - - //bind textures - - //draw - - } - - - } - - auto explosionEffectJob = std::dynamic_pointer_cast(job); - if (explosionEffectJob) { - m_ExplosionEffectProgram->Bind(); - shaderHandle = m_ExplosionEffectProgram->GetHandle(); //--- - - GLERROR("DrawFinalPass::ExplosionEffect: 1"); - //TODO: Kolla upp "header/include/common" shader saken så man slipper skicka in asmycket uniforms - - GLERROR("DrawFinalPass::ExplosionEffect: 2"); - - if (explosionEffectJob->Model->m_RawModel->m_Skeleton != nullptr) { - - if (explosionEffectJob->Animation != nullptr) { - std::vector frameBones = explosionEffectJob->Skeleton->GetFrameBones(*explosionEffectJob->Animation, explosionEffectJob->AnimationTime); - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); - } - } - - //TODO: Renderer: bättre textur felhantering samt fler texturer stöd - glActiveTexture(GL_TEXTURE0); - if (explosionEffectJob->DiffuseTexture != nullptr) { - glBindTexture(GL_TEXTURE_2D, explosionEffectJob->DiffuseTexture->m_Texture); - } else { - glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); - } - - - glBindVertexArray(explosionEffectJob->Model->VAO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, explosionEffectJob->Model->ElementBuffer); - glDrawElements(GL_TRIANGLES, explosionEffectJob->EndIndex - explosionEffectJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(explosionEffectJob->StartIndex*sizeof(unsigned int))); - GLERROR("DrawFinalPass::ExplosionEffect: END"); - //m_ExplosionEffectProgram->Unbind(); - - } else { - auto modelJob = std::dynamic_pointer_cast(job); - if (modelJob) { - m_ForwardPlusProgram->Bind(); - GLERROR("1.1"); - shaderHandle = m_ForwardPlusProgram->GetHandle(); - - if (scene.ClearDepth) { - glClear(GL_DEPTH_BUFFER_BIT); - } - - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, m_LightCullingPass->LightSSBO()); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, m_LightCullingPass->LightGridSSBO()); - glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, m_LightCullingPass->LightIndexSSBO()); - 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())); - glUniform2f(glGetUniformLocation(shaderHandle, "ScreenDimensions"), m_Renderer->Resolution().Width, m_Renderer->Resolution().Height); - GLERROR("DrawFinalPass::Draw: Uniforms"); - - DrawModelRenderQueues(scene.OpaqueObjects, shaderHandle); - GLERROR("DrawFinalPass::Draw: OpaqueObjects"); - DrawModelRenderQueues(scene.TransparentObjects, shaderHandle); - GLERROR("DrawFinalPass::Draw: TransparentObjects"); - } - - for (auto &job : job) { - if (modelJob) { - //TODO: Kolla upp "header/include/common" shader saken så man slipper skicka in asmycket uniforms - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelJob->Matrix)); - glUniform4fv(glGetUniformLocation(shaderHandle, "Color"), 1, glm::value_ptr(modelJob->Color)); - glUniform4fv(glGetUniformLocation(shaderHandle, "DiffuseColor"), 1, glm::value_ptr(modelJob->DiffuseColor)); - glUniform4fv(glGetUniformLocation(shaderHandle, "FillColor"), 1, glm::value_ptr(modelJob->FillColor)); - glUniform1f(glGetUniformLocation(shaderHandle, "FillPercentage"), modelJob->FillPercentage); - - glActiveTexture(GL_TEXTURE0); - if (modelJob->DiffuseTexture != nullptr) { - glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture->m_Texture); - } else { - glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); - } - glActiveTexture(GL_TEXTURE1); - if (modelJob->IncandescenceTexture != nullptr) { - glBindTexture(GL_TEXTURE_2D, modelJob->IncandescenceTexture->m_Texture); - } else { - glBindTexture(GL_TEXTURE_2D, m_BlackTexture->m_Texture); - } - - //TODO: Fixa så att modelsJobs kan spela upp olika animationer och så att den kan få in en tid istället för 1.0f - Hälsningar Johan och Andreas :) - if (modelJob->Model->m_RawModel->m_Skeleton != nullptr) { - if (modelJob->Animation != nullptr) { - std::vector frameBones = modelJob->Skeleton->GetFrameBones(*modelJob->Animation, modelJob->AnimationTime); - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + if (explosionEffectJob->Animation != nullptr) { + std::vector frameBones = explosionEffectJob->Skeleton->GetFrameBones(*explosionEffectJob->Animation, explosionEffectJob->AnimationTime); + glUniformMatrix4fv(glGetUniformLocation(explosionHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); } } - glBindVertexArray(modelJob->Model->VAO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); - glDrawElements(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(modelJob->StartIndex*sizeof(unsigned int))); - GLERROR("DrawFinalPass::Model: END"); - // m_ForwardPlusProgram->Unbind(); + //bind textures + BindExplosionTextures(explosionEffectJob); + //draw + glBindVertexArray(explosionEffectJob->Model->VAO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, explosionEffectJob->Model->ElementBuffer); + glDrawElements(GL_TRIANGLES, explosionEffectJob->EndIndex - explosionEffectJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(explosionEffectJob->StartIndex*sizeof(unsigned int))); + } else { + auto modelJob = std::dynamic_pointer_cast(job); + if (modelJob) { + //bind forward program + m_ForwardPlusProgram->Bind(); + + //bind uniforms + BindModelUniforms(forwardHandle, modelJob, scene); + + //bind textures + BindModelTextures(modelJob); + + if (modelJob->Model->m_RawModel->m_Skeleton != nullptr) { + + if (modelJob->Animation != nullptr) { + std::vector frameBones = modelJob->Skeleton->GetFrameBones(*modelJob->Animation, modelJob->AnimationTime); + glUniformMatrix4fv(glGetUniformLocation(forwardHandle, "Bones"), frameBones.size(), GL_FALSE, glm::value_ptr(frameBones[0])); + } + } + + //draw + glBindVertexArray(modelJob->Model->VAO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->Model->ElementBuffer); + glDrawElements(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(modelJob->StartIndex*sizeof(unsigned int))); + GLERROR("DrawFinalPass::Model: END"); + } } } + } -void DrawFinalPass::BindExplosionUniforms() +void DrawFinalPass::BindExplosionUniforms(GLuint shaderHandle, std::shared_ptr& job, RenderScene& scene) { 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())); + glUniform2f(glGetUniformLocation(shaderHandle, "ScreenDimensions"), m_Renderer->Resolution().Width, m_Renderer->Resolution().Height); - glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(explosionEffectJob->Matrix)); - glUniform4fv(glGetUniformLocation(shaderHandle, "Color"), 1, glm::value_ptr(explosionEffectJob->Color)); - glUniform3fv(glGetUniformLocation(shaderHandle, "ExplosionOrigin"), 1, glm::value_ptr(explosionEffectJob->ExplosionOrigin)); - glUniform1f(glGetUniformLocation(shaderHandle, "TimeSinceDeath"), explosionEffectJob->TimeSinceDeath); - glUniform1f(glGetUniformLocation(shaderHandle, "ExplosionDuration"), explosionEffectJob->ExplosionDuration); - glUniform4fv(glGetUniformLocation(shaderHandle, "EndColor"), 1, glm::value_ptr(explosionEffectJob->EndColor)); - glUniform1i(glGetUniformLocation(shaderHandle, "Randomness"), explosionEffectJob->Randomness); - glUniform1f(glGetUniformLocation(shaderHandle, "RandomnessScalar"), explosionEffectJob->RandomnessScalar); - glUniform2fv(glGetUniformLocation(shaderHandle, "Velocity"), 1, glm::value_ptr(explosionEffectJob->Velocity)); - glUniform1i(glGetUniformLocation(shaderHandle, "ColorByDistance"), explosionEffectJob->ColorByDistance); - glUniform1i(glGetUniformLocation(shaderHandle, "ExponentialAccelaration"), explosionEffectJob->ExponentialAccelaration); - glUniform4fv(glGetUniformLocation(shaderHandle, "DiffuseColor"), 1, glm::value_ptr(explosionEffectJob->DiffuseColor)); - glUniform1fv(glGetUniformLocation(shaderHandle, "RandomNumbers"), 50, explosionEffectJob->RandomNumbers.data()); + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(job->Matrix)); + glUniform4fv(glGetUniformLocation(shaderHandle, "Color"), 1, glm::value_ptr(job->Color)); + glUniform3fv(glGetUniformLocation(shaderHandle, "ExplosionOrigin"), 1, glm::value_ptr(job->ExplosionOrigin)); + glUniform1f(glGetUniformLocation(shaderHandle, "TimeSinceDeath"), job->TimeSinceDeath); + glUniform1f(glGetUniformLocation(shaderHandle, "ExplosionDuration"), job->ExplosionDuration); + glUniform4fv(glGetUniformLocation(shaderHandle, "EndColor"), 1, glm::value_ptr(job->EndColor)); + glUniform1i(glGetUniformLocation(shaderHandle, "Randomness"), job->Randomness); + glUniform1f(glGetUniformLocation(shaderHandle, "RandomnessScalar"), job->RandomnessScalar); + glUniform2fv(glGetUniformLocation(shaderHandle, "Velocity"), 1, glm::value_ptr(job->Velocity)); + glUniform1i(glGetUniformLocation(shaderHandle, "ColorByDistance"), job->ColorByDistance); + glUniform1i(glGetUniformLocation(shaderHandle, "ExponentialAccelaration"), job->ExponentialAccelaration); + glUniform4fv(glGetUniformLocation(shaderHandle, "DiffuseColor"), 1, glm::value_ptr(job->DiffuseColor)); + glUniform1fv(glGetUniformLocation(shaderHandle, "RandomNumbers"), 50, job->RandomNumbers.data()); } + +void DrawFinalPass::BindModelUniforms(GLuint shaderHandle, std::shared_ptr& job, RenderScene& scene) +{ + 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())); + glUniform2f(glGetUniformLocation(shaderHandle, "ScreenDimensions"), m_Renderer->Resolution().Width, m_Renderer->Resolution().Height); + + glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(job->Matrix)); + glUniform4fv(glGetUniformLocation(shaderHandle, "Color"), 1, glm::value_ptr(job->Color)); + glUniform4fv(glGetUniformLocation(shaderHandle, "DiffuseColor"), 1, glm::value_ptr(job->DiffuseColor)); + glUniform4fv(glGetUniformLocation(shaderHandle, "FillColor"), 1, glm::value_ptr(job->FillColor)); + glUniform1f(glGetUniformLocation(shaderHandle, "FillPercentage"), job->FillPercentage); +} + + +void DrawFinalPass::BindExplosionTextures(std::shared_ptr& job) +{ + glActiveTexture(GL_TEXTURE0); + if (job->DiffuseTexture != nullptr) { + glBindTexture(GL_TEXTURE_2D, job->DiffuseTexture->m_Texture); + } else { + glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); + } + glActiveTexture(GL_TEXTURE1); + if (job->IncandescenceTexture != nullptr) { + glBindTexture(GL_TEXTURE_2D, job->IncandescenceTexture->m_Texture); + } else { + glBindTexture(GL_TEXTURE_2D, m_BlackTexture->m_Texture); + } +} + +void DrawFinalPass::BindModelTextures(std::shared_ptr& job) +{ + glActiveTexture(GL_TEXTURE0); + if (job->DiffuseTexture != nullptr) { + glBindTexture(GL_TEXTURE_2D, job->DiffuseTexture->m_Texture); + } else { + glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); + } + glActiveTexture(GL_TEXTURE1); + if (job->IncandescenceTexture != nullptr) { + glBindTexture(GL_TEXTURE_2D, job->IncandescenceTexture->m_Texture); + } else { + glBindTexture(GL_TEXTURE_2D, m_BlackTexture->m_Texture); + } +} + diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index 9dbc0341..ab4ca36c 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -98,6 +98,10 @@ void RenderSystem::fillModels(std::list>& opaqueJobs, fillColor, fillPercentage )); + if(explosionEffectJob->Color.a != 1.f || explosionEffectJob->EndColor.a != 1.f || explosionEffectJob->DiffuseColor.a != 1.f) { + modelComponent["Transparent"] = true; + } + if (modelComponent["Transparent"]) { transparentJobs.push_back(explosionEffectJob); } else { @@ -114,6 +118,9 @@ void RenderSystem::fillModels(std::list>& opaqueJobs, fillColor, fillPercentage )); + if (modelJob->Color.a != 1.f || modelJob->DiffuseColor.a != 1.f) { + modelComponent["Transparent"] = true; + } if (modelComponent["Transparent"]) { transparentJobs.push_back(modelJob); } else { From a954cbbf50768499df34505119926c6846334761 Mon Sep 17 00:00:00 2001 From: Tleety Date: Wed, 27 Jan 2016 13:52:27 +0100 Subject: [PATCH 3/6] Normal map support --- include/Engine/Rendering/DrawFinalPass.h | 1 + resources/Shaders/ForwardPlus.frag.glsl | 31 ++++++++++-------------- resources/Shaders/ForwardPlus.vert.glsl | 4 +++ src/Engine/Rendering/DrawBloomPass.cpp | 2 +- src/Engine/Rendering/DrawFinalPass.cpp | 11 ++++++++- src/Engine/Rendering/Renderer.cpp | 2 +- 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/include/Engine/Rendering/DrawFinalPass.h b/include/Engine/Rendering/DrawFinalPass.h index d57770e7..acb5cd13 100644 --- a/include/Engine/Rendering/DrawFinalPass.h +++ b/include/Engine/Rendering/DrawFinalPass.h @@ -40,6 +40,7 @@ private: Texture* m_WhiteTexture; Texture* m_BlackTexture; + Texture* m_NeutralNormalTexture; FrameBuffer m_FinalPassFrameBuffer; GLuint m_BloomTexture; diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index 42500b44..7d27ca04 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -9,7 +9,8 @@ uniform vec2 ScreenDimensions; uniform vec4 FillColor; uniform float FillPercentage; layout (binding = 0) uniform sampler2D DiffuseTexture; -layout (binding = 1) uniform sampler2D GlowMap; +layout (binding = 1) uniform sampler2D NormalMapTexture; +layout (binding = 2) uniform sampler2D GlowMapTexture; #define TILE_SIZE 16 @@ -49,6 +50,8 @@ layout (std430, binding = 4) buffer LightIndexBuffer in VertexData{ vec3 Position; vec3 Normal; + vec3 Tangent; + vec3 BiTangent; vec2 TextureCoordinate; vec4 ExplosionColor; }Input; @@ -102,13 +105,20 @@ LightResult CalcDirectionalLightSource(vec4 direction, vec4 color, float intensi return result; } +vec4 CalcNormalMappedValue(vec3 normal, vec3 tangent, vec3 bitangent, vec2 textureCoordinate, sampler2D normalMap) +{ + mat3 TBN = mat3(tangent, bitangent, normal); + vec3 NormalMap = texture(normalMap, textureCoordinate).xyz * 2.0 - vec3(1.0); + return vec4(TBN * normalize(NormalMap), 0.0); +} void main() { vec4 diffuseTexel = texture2D(DiffuseTexture, Input.TextureCoordinate); - vec4 glowTexel = texture2D(GlowMap, Input.TextureCoordinate); + vec4 glowTexel = texture2D(GlowMapTexture, Input.TextureCoordinate); vec4 position = V * M * vec4(Input.Position, 1.0); - vec4 normal = normalize(V * vec4(Input.Normal, 0.0)); + vec4 normal = V * CalcNormalMappedValue(Input.Normal, Input.Tangent, Input.BiTangent, Input.TextureCoordinate, NormalMapTexture); + //vec4 normal = normalize(V * vec4(Input.Normal, 0.0)); vec4 viewVec = normalize(-position); vec2 tilePos; @@ -147,25 +157,10 @@ void main() if(pos <= FillPercentage) { color_result += FillColor; } - //bloomColor = vec4(0.3, 0.8, 0.6, 1.0); sceneColor = vec4(color_result.xyz, clamp(color_result.a, 0, 1)); - //These if statements should be removed if they are slow. color_result += glowTexel; - - - bloomColor = vec4(clamp(color_result.xyz - 1.0, 0, 100), 1.0); - /* - if(color_result.x > 1 || color_result.y > 1 || color_result.z > 1) { - bloomColor = vec4(color_result.xyz, 1.0); - } else { - bloomColor = vec4(0.0, 0.0, 0.0, 1.0); - } */ - - - - //Tiled Debug Code /* diff --git a/resources/Shaders/ForwardPlus.vert.glsl b/resources/Shaders/ForwardPlus.vert.glsl index bd7df47e..1a7cca12 100644 --- a/resources/Shaders/ForwardPlus.vert.glsl +++ b/resources/Shaders/ForwardPlus.vert.glsl @@ -16,6 +16,8 @@ layout(location = 6) in vec4 BoneWeights; out VertexData{ vec3 Position; vec3 Normal; + vec3 Tangent; + vec3 BiTangent; vec2 TextureCoordinate; vec4 ExplosionColor; }Output; @@ -37,5 +39,7 @@ void main() Output.Position = (boneTransform * vec4(Position, 1.0)).xyz; Output.TextureCoordinate = TextureCoords; Output.Normal = vec3(M * vec4(Normal, 0.0)); + Output.Tangent = vec3(M * vec4(Tangent, 0.0)); + Output.BiTangent = vec3(M * vec4(BiTangent, 0.0)); Output.ExplosionColor = vec4(0.0); } \ No newline at end of file diff --git a/src/Engine/Rendering/DrawBloomPass.cpp b/src/Engine/Rendering/DrawBloomPass.cpp index 8fe1d807..5599b9e0 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/Blank.png"); + m_WhiteTexture = ResourceManager::Load("Textures/Core/White.png"); } void DrawBloomPass::InitializeShaderPrograms() diff --git a/src/Engine/Rendering/DrawFinalPass.cpp b/src/Engine/Rendering/DrawFinalPass.cpp index 4fd0c2aa..a284a773 100644 --- a/src/Engine/Rendering/DrawFinalPass.cpp +++ b/src/Engine/Rendering/DrawFinalPass.cpp @@ -11,8 +11,9 @@ DrawFinalPass::DrawFinalPass(IRenderer* renderer, LightCullingPass* lightCulling void DrawFinalPass::InitializeTextures() { - m_WhiteTexture = ResourceManager::Load("Textures/Core/Blank.png"); + m_WhiteTexture = ResourceManager::Load("Textures/Core/White.png"); m_BlackTexture = ResourceManager::Load("Textures/Core/Black.png"); + m_NeutralNormalTexture = ResourceManager::Load("Textures/Core/NeutralNormalMap.png"); } void DrawFinalPass::InitializeFrameBuffers() @@ -230,7 +231,15 @@ void DrawFinalPass::BindModelTextures(std::shared_ptr& job) } else { glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture); } + glActiveTexture(GL_TEXTURE1); + if (job->NormalTexture != nullptr) { + glBindTexture(GL_TEXTURE_2D, job->NormalTexture->m_Texture); + } else { + glBindTexture(GL_TEXTURE_2D, m_NeutralNormalTexture->m_Texture); + } + + glActiveTexture(GL_TEXTURE2); if (job->IncandescenceTexture != nullptr) { glBindTexture(GL_TEXTURE_2D, job->IncandescenceTexture->m_Texture); } else { diff --git a/src/Engine/Rendering/Renderer.cpp b/src/Engine/Rendering/Renderer.cpp index 81f1a1cc..d12a1d1d 100644 --- a/src/Engine/Rendering/Renderer.cpp +++ b/src/Engine/Rendering/Renderer.cpp @@ -143,7 +143,7 @@ PickData Renderer::Pick(glm::vec2 screenCoord) void Renderer::InitializeTextures() { m_ErrorTexture = ResourceManager::Load("Textures/Core/ErrorTexture.png"); - m_WhiteTexture = ResourceManager::Load("Textures/Core/Blank.png"); + m_WhiteTexture = ResourceManager::Load("Textures/Core/White.png"); } From 5c9616d4af8466cb683f269f0a0ddb5d940f47bf Mon Sep 17 00:00:00 2001 From: Tleety Date: Wed, 27 Jan 2016 13:55:23 +0100 Subject: [PATCH 4/6] Fixed asset name --- assets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets b/assets index e4dc9529..bf5fd580 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit e4dc9529f2178d373808ddf487165fa50a641a78 +Subproject commit bf5fd580e03cf8cb67ad2627e6cf53af6e5346f9 From 44302bb0f3143d2bee44248d6d3ef98ca757d420 Mon Sep 17 00:00:00 2001 From: Tleety Date: Wed, 27 Jan 2016 15:38:54 +0100 Subject: [PATCH 5/6] Specular support. Some edits to EditorTestWorld.xml aswell as some debugmodes for textures in the model component --- assets | 2 +- include/Engine/Rendering/DrawFinalPass.h | 1 + include/Engine/Rendering/ModelJob.h | 24 +- resources/Schema/Components/Model.xml | 4 + resources/Schema/Components/Model.xsd | 12 + resources/Schema/Entities/EditorTestWorld.xml | 258 +++++++++++++++--- .../Schema/Entities/PointLightWithModel.xml | 20 ++ resources/Shaders/ForwardPlus.frag.glsl | 6 +- src/Engine/Rendering/DrawFinalPass.cpp | 8 + 9 files changed, 291 insertions(+), 44 deletions(-) create mode 100644 resources/Schema/Entities/PointLightWithModel.xml diff --git a/assets b/assets index bf5fd580..9b2fa74a 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit bf5fd580e03cf8cb67ad2627e6cf53af6e5346f9 +Subproject commit 9b2fa74a6d535d512c13429997385f7aa3fb50f5 diff --git a/include/Engine/Rendering/DrawFinalPass.h b/include/Engine/Rendering/DrawFinalPass.h index acb5cd13..74f505fe 100644 --- a/include/Engine/Rendering/DrawFinalPass.h +++ b/include/Engine/Rendering/DrawFinalPass.h @@ -41,6 +41,7 @@ private: Texture* m_WhiteTexture; Texture* m_BlackTexture; Texture* m_NeutralNormalTexture; + Texture* m_GreyTexture; FrameBuffer m_FinalPassFrameBuffer; GLuint m_BloomTexture; diff --git a/include/Engine/Rendering/ModelJob.h b/include/Engine/Rendering/ModelJob.h index d5a2d880..2cb2169c 100644 --- a/include/Engine/Rendering/ModelJob.h +++ b/include/Engine/Rendering/ModelJob.h @@ -22,10 +22,26 @@ struct ModelJob : RenderJob { Model = model; TextureID = (matGroup.Texture) ? matGroup.Texture->ResourceID : 0; - DiffuseTexture = matGroup.Texture.get(); - NormalTexture = matGroup.NormalMap.get(); - SpecularTexture = matGroup.SpecularMap.get(); - IncandescenceTexture = matGroup.IncandescenceMap.get(); + if (modelComponent["DiffuseTexture"]) { + DiffuseTexture = matGroup.Texture.get(); + } else { + DiffuseTexture = nullptr; + } + if (modelComponent["NormalMap"]) { + NormalTexture = matGroup.NormalMap.get(); + } else { + NormalTexture = nullptr; + } + if (modelComponent["SpecularMap"]) { + SpecularTexture = matGroup.SpecularMap.get(); + } else { + SpecularTexture = nullptr; + } + if (modelComponent["GlowMap"]) { + IncandescenceTexture = matGroup.IncandescenceMap.get(); + } else { + IncandescenceTexture = nullptr; + } DiffuseColor = matGroup.DiffuseColor; SpecularColor = matGroup.SpecularColor; IncandescenceColor = matGroup.IncandescenceColor; diff --git a/resources/Schema/Components/Model.xml b/resources/Schema/Components/Model.xml index b25c8828..4b77dacb 100644 --- a/resources/Schema/Components/Model.xml +++ b/resources/Schema/Components/Model.xml @@ -4,4 +4,8 @@ false true + true + true + true + true \ No newline at end of file diff --git a/resources/Schema/Components/Model.xsd b/resources/Schema/Components/Model.xsd index 0ea04d72..178d8b31 100644 --- a/resources/Schema/Components/Model.xsd +++ b/resources/Schema/Components/Model.xsd @@ -21,6 +21,18 @@ Wether the model is visible or not + + Whether the model should use the Diffuse texture or not + + + Whether the model should use the Normalmap or not + + + Whether the model should use the Specularmap or not + + + Whether the model should use the Glowmap or not + diff --git a/resources/Schema/Entities/EditorTestWorld.xml b/resources/Schema/Entities/EditorTestWorld.xml index 5649a630..9565e2d4 100755 --- a/resources/Schema/Entities/EditorTestWorld.xml +++ b/resources/Schema/Entities/EditorTestWorld.xml @@ -6,7 +6,7 @@ - + Models/Core/UnitCube.mesh @@ -18,69 +18,51 @@ - - - - An error - - - - - - - - - - - An error - - - - - - - - - - - + - + - + + 0.80000001192092896 + Models/DirectionalLightWidget.mesh + + 1.0499999523162842 + + - + - + Models/Assault.mesh - + + - + - Models/SecondaryWeapon.fbx + Models/SecondaryWeapon.mesh @@ -91,17 +73,219 @@ - + + + + + + + + + + + + Run + + 1 + + + models/AssaultAnimated.mesh + + + + + + + + + + + Walk + + 1 + + + models/AssaultAnimated.mesh + + + + + + + + Models/Core/UnitSphere.mesh + + true + + + + + + + + + + + + + - Models/Core/UnitSphere.mesh + Models/Log.mesh - + + + + + + + + + + + + + Models/NormalMapSphere.mesh + + + + + + + + + + + Models/SpecularMapSphere.mesh + + + + + + + + + + 1 + + + + + + + + + + + Models/Core/UnitSphere.mesh + + + + 3 + 1.1399998664855957 + + + + + + + + + + + + + + + + Models/IncandescenceMapSphere.mesh + + + + + + + + + + + models/NormSpecIncdMapSphere.mesh + + + + + + + + + 1 + + + + + + + + + + + Models/Core/UnitSphere.mesh + + + + 3 + 1.1399998664855957 + + + + + + + + + + + + Models/Core/UnitSphere.mesh + + + + 5.0100002288818359 + 0.69999998807907104 + + + + + + + + + + + + Models/Core/UnitSphere.mesh + + + + 4 + 0.80000001192092896 + + + + + + + + + + + + diff --git a/resources/Schema/Entities/PointLightWithModel.xml b/resources/Schema/Entities/PointLightWithModel.xml new file mode 100644 index 00000000..17ca950c --- /dev/null +++ b/resources/Schema/Entities/PointLightWithModel.xml @@ -0,0 +1,20 @@ + + + + + + Models/Core/UnitSphere.mesh + + + + 3 + 1.1399998664855957 + + + + + + + + + diff --git a/resources/Shaders/ForwardPlus.frag.glsl b/resources/Shaders/ForwardPlus.frag.glsl index 7d27ca04..de672dd1 100644 --- a/resources/Shaders/ForwardPlus.frag.glsl +++ b/resources/Shaders/ForwardPlus.frag.glsl @@ -10,7 +10,8 @@ uniform vec4 FillColor; uniform float FillPercentage; layout (binding = 0) uniform sampler2D DiffuseTexture; layout (binding = 1) uniform sampler2D NormalMapTexture; -layout (binding = 2) uniform sampler2D GlowMapTexture; +layout (binding = 2) uniform sampler2D SpecularMapTexture; +layout (binding = 3) uniform sampler2D GlowMapTexture; #define TILE_SIZE 16 @@ -116,6 +117,7 @@ void main() { vec4 diffuseTexel = texture2D(DiffuseTexture, Input.TextureCoordinate); vec4 glowTexel = texture2D(GlowMapTexture, Input.TextureCoordinate); + vec4 specularTexel = texture2D(SpecularMapTexture, Input.TextureCoordinate); vec4 position = V * M * vec4(Input.Position, 1.0); vec4 normal = V * CalcNormalMappedValue(Input.Normal, Input.Tangent, Input.BiTangent, Input.TextureCoordinate, NormalMapTexture); //vec4 normal = normalize(V * vec4(Input.Normal, 0.0)); @@ -149,7 +151,7 @@ void main() } - vec4 color_result = (DiffuseColor + Input.ExplosionColor) * (totalLighting.Diffuse + totalLighting.Specular) * diffuseTexel * Color; + vec4 color_result = (DiffuseColor + Input.ExplosionColor) * (totalLighting.Diffuse + (totalLighting.Specular * specularTexel)) * diffuseTexel * Color; float pos = ((P * vec4(Input.Position, 1)).y + 1.0)/2.0; diff --git a/src/Engine/Rendering/DrawFinalPass.cpp b/src/Engine/Rendering/DrawFinalPass.cpp index a284a773..3aff3ae8 100644 --- a/src/Engine/Rendering/DrawFinalPass.cpp +++ b/src/Engine/Rendering/DrawFinalPass.cpp @@ -14,6 +14,7 @@ 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"); } void DrawFinalPass::InitializeFrameBuffers() @@ -240,6 +241,13 @@ void DrawFinalPass::BindModelTextures(std::shared_ptr& job) } glActiveTexture(GL_TEXTURE2); + if (job->SpecularTexture != nullptr) { + glBindTexture(GL_TEXTURE_2D, job->SpecularTexture->m_Texture); + } else { + glBindTexture(GL_TEXTURE_2D, m_GreyTexture->m_Texture); + } + + glActiveTexture(GL_TEXTURE3); if (job->IncandescenceTexture != nullptr) { glBindTexture(GL_TEXTURE_2D, job->IncandescenceTexture->m_Texture); } else { From a63ecb8133b2658b83f7874600ab4180d41c27bb Mon Sep 17 00:00:00 2001 From: Tleety Date: Wed, 27 Jan 2016 15:54:36 +0100 Subject: [PATCH 6/6] Some wierd shader faults and merge error fixe --- resources/Shaders/Gaussian_horiz.frag.glsl | 1 + resources/Shaders/Gaussian_vert.frag.glsl | 1 + src/Engine/Rendering/RenderSystem.cpp | 13 +++++-------- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/resources/Shaders/Gaussian_horiz.frag.glsl b/resources/Shaders/Gaussian_horiz.frag.glsl index a5606372..6a9572c2 100644 --- a/resources/Shaders/Gaussian_horiz.frag.glsl +++ b/resources/Shaders/Gaussian_horiz.frag.glsl @@ -1,4 +1,5 @@ #version 430 +#extension GL_EXT_gpu_shader4 : enable layout (binding = 0) uniform sampler2D Texture; diff --git a/resources/Shaders/Gaussian_vert.frag.glsl b/resources/Shaders/Gaussian_vert.frag.glsl index 34803b08..8b0e861a 100644 --- a/resources/Shaders/Gaussian_vert.frag.glsl +++ b/resources/Shaders/Gaussian_vert.frag.glsl @@ -1,4 +1,5 @@ #version 430 +#extension GL_EXT_gpu_shader4 : enable layout (binding = 0) uniform sampler2D Texture; diff --git a/src/Engine/Rendering/RenderSystem.cpp b/src/Engine/Rendering/RenderSystem.cpp index 71072a5b..a0912a45 100644 --- a/src/Engine/Rendering/RenderSystem.cpp +++ b/src/Engine/Rendering/RenderSystem.cpp @@ -35,15 +35,12 @@ bool RenderSystem::isChildOfACamera(EntityWrapper entity) { return entity.FirstParentWithComponent("Camera").Valid(); } -void RenderSystem::fillModels(std::list>& opaqueJobs, std::list>& transparentJobs) - -place me somewhere - bool RenderSystem::isChildOfCurrentCamera(EntityWrapper entity) { return entity == m_CurrentCamera || entity.IsChildOf(m_CurrentCamera); } +void RenderSystem::fillModels(std::list>& opaqueJobs, std::list>& transparentJobs) { auto models = m_World->GetComponents("Model"); if (models == nullptr) { @@ -110,10 +107,10 @@ bool RenderSystem::isChildOfCurrentCamera(EntityWrapper entity) fillPercentage )); if(explosionEffectJob->Color.a != 1.f || explosionEffectJob->EndColor.a != 1.f || explosionEffectJob->DiffuseColor.a != 1.f) { - modelComponent["Transparent"] = true; + cModel["Transparent"] = true; } - if (modelComponent["Transparent"]) { + if (cModel["Transparent"]) { transparentJobs.push_back(explosionEffectJob); } else { opaqueJobs.push_back(explosionEffectJob); @@ -130,9 +127,9 @@ bool RenderSystem::isChildOfCurrentCamera(EntityWrapper entity) fillPercentage )); if (modelJob->Color.a != 1.f || modelJob->DiffuseColor.a != 1.f) { - modelComponent["Transparent"] = true; + cModel["Transparent"] = true; } - if (modelComponent["Transparent"]) { + if (cModel["Transparent"]) { transparentJobs.push_back(modelJob); } else { opaqueJobs.push_back(modelJob);