Merge branch 'master' into Shadows

# Conflicts:
#	assets
#	include/Engine/Rendering/DrawFinalPass.h
#	include/Engine/Rendering/Renderer.h
#	resources/Shaders/ForwardPlus.frag.glsl
#	resources/Shaders/ForwardPlus.vert.glsl
#	src/Engine/Rendering/DrawFinalPass.cpp
#	src/Engine/Rendering/FrameBuffer.cpp
#	src/Engine/Rendering/Renderer.cpp
This commit is contained in:
FakeShemp
2016-02-29 18:03:00 +01:00
367 changed files with 29718 additions and 3885 deletions
+233 -37
View File
@@ -1,10 +1,12 @@
#include "Rendering/RenderSystem.h"
#include "Collision/Collision.h"
#include "Core/Frustum.h"
RenderSystem::RenderSystem(World* world, EventBroker* eventBroker, const IRenderer* renderer, RenderFrame* renderFrame)
: System(world, eventBroker)
RenderSystem::RenderSystem(SystemParams params, const IRenderer* renderer, RenderFrame* renderFrame, Octree<EntityAABB>* frustumCullOctree)
: System(params)
, m_Renderer(renderer)
, m_RenderFrame(renderFrame)
, m_World(world)
, m_Octree(frustumCullOctree)
{
EVENT_SUBSCRIBE_MEMBER(m_ESetCamera, &RenderSystem::OnSetCamera);
EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &RenderSystem::OnInputCommand);
@@ -31,6 +33,160 @@ bool RenderSystem::OnSetCamera(Events::SetCamera& e)
return true;
}
void RenderSystem::fillSprites(std::list<std::shared_ptr<RenderJob>>& jobs, World* world)
{
auto sprites = world->GetComponents("Sprite");
if (sprites == nullptr) {
return;
}
for (auto& cSprite : *sprites) {
bool visible = cSprite["Visible"];
if (!visible) {
continue;
}
EntityWrapper entity(world, cSprite.EntityID);
if (!isEntityVisible(entity)) {
continue;
}
glm::mat4 modelMatrix;
// See a sprite is an SpriteIndicator
bool isIndicator = false;
if (world->HasComponent(entity.ID, "SpriteIndicator"))
{
auto indicator = entity["SpriteIndicator"];
float minScale = (float)(double)indicator["MinScale"];
bool hasTeam = indicator["VisibleForSingleTeamOnly"];
isIndicator = true;
glm::vec3 pos = Transform::AbsolutePosition(entity);
EntityWrapper entityTeam;
if (hasTeam && (entity.HasComponent("Team") || entity.FirstParentWithComponent("Team").Valid()) && m_LocalPlayer.World != nullptr) {
if (!entity.HasComponent("Team")) {
entityTeam = entity.FirstParentWithComponent("Team");
}
else {
entityTeam = entity;
}
ComponentWrapper& entityTeamComponent = entityTeam["Team"];
ComponentWrapper& localComponent = m_LocalPlayer["Team"];
int entityTeamInt = entityTeamComponent["Team"];
int localComponentInt = localComponent["Team"];
int SpectatorInt = localComponent["Team"].Enum("Spectator");
if (entityTeamInt != localComponentInt && localComponentInt != SpectatorInt) {
continue;
}
}
// Code for check if sprite is inside or outside of screen
//glm::vec4 projectedPos = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix() * glm::vec4(pos, 1.0f);
//projectedPos /= projectedPos.w;
//// Check if inside of outside of screen.
//if (projectedPos.x < -1.0f || projectedPos.x > 1.0f || projectedPos.y < -1.0f || projectedPos.y > 1.0f) {
// // is outside of screen
//} else {
// // is inside of screen
//}
glm::vec3 zAxis = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 normal = pos - m_Camera->Position();
//float distance = glm::length(normal);
//if (distance < minDistance) {
// pos = pos - glm::normalize(normal) * (distance - minDistance);
//} else if (distance > maxDistance) {
// pos = pos - glm::normalize(normal) * (distance - maxDistance);
//}
normal.y = 0;
normal = glm::normalize(normal);
glm::vec3 right = glm::cross(normal, zAxis);
glm::vec3 up = glm::cross(right, normal);
modelMatrix[0][0] = right.x;
modelMatrix[0][1] = right.y;
modelMatrix[0][2] = right.z;
modelMatrix[0][3] = 0.0f;
modelMatrix[1][0] = zAxis.x;
modelMatrix[1][1] = zAxis.y;
modelMatrix[1][2] = zAxis.z;
modelMatrix[1][3] = 0.0f;
modelMatrix[2][0] = normal.x;
modelMatrix[2][1] = normal.y;
modelMatrix[2][2] = normal.z;
modelMatrix[2][3] = 0.0f;
modelMatrix[3][0] = pos.x;
modelMatrix[3][1] = pos.y;
modelMatrix[3][2] = pos.z;
modelMatrix[3][3] = 1.0f;
glm::mat4 tranformationMatrix = modelMatrix * glm::scale(Transform::AbsoluteScale(entity));
glm::vec4 tmp = tranformationMatrix * glm::vec4(glm::vec3(0.5, 0.5, 0), 1.0f);
glm::vec2 projectedTopRight = m_Camera->WorldToScreen(glm::vec3(tmp), m_Renderer->GetViewportSize());
tmp = tranformationMatrix * glm::vec4(glm::vec3(-0.5, -0.5, 0), 1.0f);
glm::vec2 projectedBottomLeft = m_Camera->WorldToScreen(glm::vec3(tmp), m_Renderer->GetViewportSize());
float diag = glm::length(projectedBottomLeft - projectedTopRight);
if (diag < minScale) {
tranformationMatrix = tranformationMatrix * glm::scale(glm::vec3(minScale / diag, minScale / diag, minScale / diag));
}
modelMatrix = tranformationMatrix;
}
else {
modelMatrix = Transform::ModelMatrix(entity.ID, world);
}
std::string diffuseResource = cSprite["DiffuseTexture"];
std::string glowResource = cSprite["GlowMap"];
bool depthSorted = cSprite["DepthSort"];
if (diffuseResource.empty() && glowResource.empty()) {
continue;
}
float fillPercentage = 0.f;
glm::vec4 fillColor = glm::vec4(0);
if (world->HasComponent(entity.ID, "Fill")) {
auto fillComponent = world->GetComponent(entity.ID, "Fill");
fillPercentage = (float)(double)fillComponent["Percentage"];
fillColor = (glm::vec4)fillComponent["Color"];
}
std::shared_ptr<SpriteJob> spriteJob = std::shared_ptr<SpriteJob>(new SpriteJob(cSprite, m_Camera, modelMatrix, world, fillColor, fillPercentage, depthSorted, isIndicator));
jobs.push_back(spriteJob);
}
}
bool RenderSystem::isEntityVisible(EntityWrapper& entity)
{
// Only render children of a camera if that camera is currently active
if (isChildOfACamera(entity) && !isChildOfCurrentCamera(entity)) {
return false;
}
// Hide things parented to local player if they have the HiddenFromLocalPlayer component
bool outOfBodyExperience = ResourceManager::Load<ConfigFile>("Config.ini")->Get<bool>("Debug.OutOfBodyExperience", false);
if (
(entity.HasComponent("HiddenForLocalPlayer") || entity.FirstParentWithComponent("HiddenForLocalPlayer").Valid())
&& (entity == m_LocalPlayer || entity.IsChildOf(m_LocalPlayer))
&& !outOfBodyExperience
) {
return false;
}
return true;
}
bool RenderSystem::isChildOfACamera(EntityWrapper entity)
{
return entity.FirstParentWithComponent("Camera").Valid();
@@ -40,14 +196,15 @@ bool RenderSystem::isChildOfCurrentCamera(EntityWrapper entity)
return entity == m_CurrentCamera || entity.IsChildOf(m_CurrentCamera);
}
void RenderSystem::fillModels(std::list<std::shared_ptr<RenderJob>>& opaqueJobs, std::list<std::shared_ptr<RenderJob>>& transparentJobs)
void RenderSystem::fillModels(RenderScene::Queues &Jobs)
{
auto models = m_World->GetComponents("Model");
if (models == nullptr) {
return;
}
Frustum frustum(m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix());
std::vector<EntityAABB> seenEntities;
m_Octree->ObjectsInFrustum(frustum, seenEntities);
for (auto& cModel : *models) {
for (auto& seenEntity : seenEntities) {
EntityWrapper entity = seenEntity.Entity;
ComponentWrapper cModel = entity["Model"];
bool visible = cModel["Visible"];
if (!visible) {
continue;
@@ -57,15 +214,7 @@ void RenderSystem::fillModels(std::list<std::shared_ptr<RenderJob>>& opaqueJobs,
continue;
}
EntityWrapper entity(m_World, cModel.EntityID);
// Only render children of a camera if that camera is currently active
if (isChildOfACamera(entity) && !isChildOfCurrentCamera(entity)) {
continue;
}
// Hide things parented to local player if they have the HiddenFromLocalPlayer component
if (entity.HasComponent("HiddenForLocalPlayer") && (entity == m_LocalPlayer || entity.IsChildOf(m_LocalPlayer))) {
if (!isEntityVisible(entity)) {
continue;
}
@@ -92,7 +241,9 @@ void RenderSystem::fillModels(std::list<std::shared_ptr<RenderJob>>& opaqueJobs,
}
glm::mat4 modelMatrix = Transform::ModelMatrix(cModel.EntityID, m_World);
//Loop through all materialgroups of a model
for (auto matGroup : model->MaterialGroups()) {
//If the model has an explosioneffect component, we will add an explosioneffectjob
if (m_World->HasComponent(cModel.EntityID, "ExplosionEffect")) {
auto explosionEffectComponent = m_World->GetComponent(cModel.EntityID, "ExplosionEffect");
std::shared_ptr<ExplosionEffectJob> explosionEffectJob = std::shared_ptr<ExplosionEffectJob>(new ExplosionEffectJob(
@@ -106,14 +257,33 @@ void RenderSystem::fillModels(std::list<std::shared_ptr<RenderJob>>& opaqueJobs,
fillColor,
fillPercentage
));
if(explosionEffectJob->Color.a != 1.f || explosionEffectJob->EndColor.a != 1.f || explosionEffectJob->DiffuseColor.a != 1.f) {
cModel["Transparent"] = true;
}
if (m_World->HasComponent(cModel.EntityID, "Shield")){
explosionEffectJob->CalculateHash();
Jobs.ShieldObjects.push_back(explosionEffectJob);
} else if (m_World->HasComponent(cModel.EntityID, "Shielded")
|| m_World->HasComponent(cModel.EntityID, "Player")) {
if (cModel["Transparent"]) {
transparentJobs.push_back(explosionEffectJob);
if (explosionEffectJob->Color.a != 1.f || explosionEffectJob->EndColor.a != 1.f || explosionEffectJob->DiffuseColor.a != 1.f) {
cModel["Transparent"] = true;
}
if (cModel["Transparent"]) {
Jobs.TransparentShieldedObjects.push_back(explosionEffectJob);
} else {
explosionEffectJob->CalculateHash();
Jobs.OpaqueShieldedObjects.push_back(explosionEffectJob);
}
} else {
opaqueJobs.push_back(explosionEffectJob);
if (explosionEffectJob->Color.a != 1.f || explosionEffectJob->EndColor.a != 1.f || explosionEffectJob->DiffuseColor.a != 1.f) {
cModel["Transparent"] = true;
}
if (cModel["Transparent"]) {
Jobs.TransparentObjects.push_back(explosionEffectJob);
} else {
explosionEffectJob->CalculateHash();
Jobs.OpaqueObjects.push_back(explosionEffectJob);
}
}
} else {
std::shared_ptr<ModelJob> modelJob = std::shared_ptr<ModelJob>(new ModelJob(
@@ -126,13 +296,33 @@ void RenderSystem::fillModels(std::list<std::shared_ptr<RenderJob>>& opaqueJobs,
fillColor,
fillPercentage
));
if (modelJob->Color.a != 1.f || modelJob->DiffuseColor.a != 1.f) {
cModel["Transparent"] = true;
}
if (cModel["Transparent"]) {
transparentJobs.push_back(modelJob);
if (m_World->HasComponent(cModel.EntityID, "Shield")) {
modelJob->CalculateHash();
Jobs.ShieldObjects.push_back(modelJob);
} else if (m_World->HasComponent(cModel.EntityID, "Shielded")
|| m_World->HasComponent(cModel.EntityID, "Player")) {
if (modelJob->Color.a != 1.f || modelJob->DiffuseColor.a != 1.f) {
cModel["Transparent"] = true;
}
if (cModel["Transparent"]) {
Jobs.TransparentShieldedObjects.push_back(modelJob);
} else {
modelJob->CalculateHash();
Jobs.OpaqueShieldedObjects.push_back(modelJob);
}
} else {
opaqueJobs.push_back(modelJob);
if (modelJob->Color.a != 1.f || modelJob->DiffuseColor.a != 1.f) {
cModel["Transparent"] = true;
}
if (cModel["Transparent"]) {
Jobs.TransparentObjects.push_back(modelJob);
} else {
modelJob->CalculateHash();
Jobs.OpaqueObjects.push_back(modelJob);
}
}
}
}
@@ -167,7 +357,6 @@ void RenderSystem::fillPointLights(std::list<std::shared_ptr<RenderJob>>& jobs,
}
}
void RenderSystem::fillDirectionalLights(std::list<std::shared_ptr<RenderJob>>& jobs, World* world)
{
auto directionalLights = world->GetComponents("DirectionalLight");
@@ -189,14 +378,13 @@ void RenderSystem::fillDirectionalLights(std::list<std::shared_ptr<RenderJob>>&
}
}
void RenderSystem::fillText(std::list<std::shared_ptr<RenderJob>>& jobs, World* world)
{
auto texts = world->GetComponents("Text");
if (texts == nullptr) {
return;
}
for (auto& textComponent : *texts) {
bool visible = textComponent["Visible"];
if (!visible) {
@@ -207,6 +395,11 @@ void RenderSystem::fillText(std::list<std::shared_ptr<RenderJob>>& jobs, World*
continue;
}
EntityWrapper entity(world, textComponent.EntityID);
if (!isEntityVisible(entity)) {
continue;
}
Font* font;
try {
font = ResourceManager::Load<Font>(resource);
@@ -251,10 +444,13 @@ void RenderSystem::Update(double dt)
scene.AmbientColor = (glm::vec4)(*cSceneLight->begin())["AmbientColor"];
}
fillModels(scene.OpaqueObjects, scene.TransparentObjects);
fillPointLights(scene.PointLightJobs, m_World);
fillDirectionalLights(scene.DirectionalLightJobs, m_World);
fillText(scene.TextJobs, m_World);
fillModels(scene.Jobs);
fillPointLights(scene.Jobs.PointLight, m_World);
//TODO: Make sure all objects needed are also sorted.
scene.Jobs.OpaqueObjects.sort();
fillSprites(scene.Jobs.SpriteJob, m_World);
fillDirectionalLights(scene.Jobs.DirectionalLight, m_World);
fillText(scene.Jobs.Text, m_World);
m_RenderFrame->Add(scene);
}