spriteComponent WIP

This commit is contained in:
viktorljung
2016-02-05 16:43:57 +01:00
parent ff26e1235c
commit 8034b42c87
16 changed files with 318 additions and 8 deletions
+7
View File
@@ -62,6 +62,13 @@ void Camera::SetViewMatrix(glm::mat4 val)
m_ViewMatrix = val;
}
glm::mat4 Camera::BillboardMatrix()
{
glm::mat4 matrix = glm::toMat4(m_Orientation);
return matrix;
}
//void Camera::Pitch(float val)
//{
// m_Pitch = val;
+55 -1
View File
@@ -55,6 +55,15 @@ void DrawFinalPass::InitializeShaderPrograms()
m_ExplosionEffectProgram->BindFragDataLocation(1, "bloomColor");
m_ExplosionEffectProgram->Link();
GLERROR("Creating explosion program");
m_SpriteProgram = ResourceManager::Load<ShaderProgram>("#m_SpriteProgram");
m_SpriteProgram->AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/Sprite.vert.glsl")));
m_SpriteProgram->AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/Sprite.frag.glsl")));
m_SpriteProgram->Compile();
m_SpriteProgram->BindFragDataLocation(0, "sceneColor");
m_SpriteProgram->BindFragDataLocation(1, "bloomColor");
m_SpriteProgram->Link();
GLERROR("Creating sprite program");
}
void DrawFinalPass::Draw(RenderScene& scene)
@@ -70,6 +79,8 @@ void DrawFinalPass::Draw(RenderScene& scene)
GLERROR("OpaqueObjects");
DrawModelRenderQueues(scene.TransparentObjects, scene);
GLERROR("TransparentObjects");
DrawSprites(scene.SpriteJobs, scene);
GLERROR("SpriteJobs");
delete state;
GLERROR("END");
@@ -201,6 +212,50 @@ void DrawFinalPass::DrawModelRenderQueues(std::list<std::shared_ptr<RenderJob>>&
}
void DrawFinalPass::DrawSprites(std::list<std::shared_ptr<RenderJob>>&jobs, RenderScene& scene)
{
m_SpriteProgram->Bind();
GLuint shaderHandle = m_SpriteProgram->GetHandle();
for(auto& job : jobs) {
auto spriteJob = std::dynamic_pointer_cast<SpriteJob>(job);
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()));
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);
glActiveTexture(GL_TEXTURE0);
if (spriteJob->DiffuseTexture != nullptr) {
glBindTexture(GL_TEXTURE_2D, spriteJob->DiffuseTexture->m_Texture);
} else {
glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture);
}
glActiveTexture(GL_TEXTURE1);
if (spriteJob->IncandescenceTexture != nullptr) {
glBindTexture(GL_TEXTURE_2D, spriteJob->IncandescenceTexture->m_Texture);
} else {
glBindTexture(GL_TEXTURE_2D, m_BlackTexture->m_Texture);
}
glBindVertexArray(spriteJob->Model->VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, spriteJob->Model->ElementBuffer);
glDrawElements(GL_TRIANGLES, spriteJob->EndIndex - spriteJob->StartIndex + 1, GL_UNSIGNED_INT, (void*)(spriteJob->StartIndex*sizeof(unsigned int)));
}
}
// m_SpriteProgram->Unbind();
}
void DrawFinalPass::BindExplosionUniforms(GLuint shaderHandle, std::shared_ptr<ExplosionEffectJob>& job, RenderScene& scene)
{
glUniformMatrix4fv(glGetUniformLocation(shaderHandle, "M"), 1, GL_FALSE, glm::value_ptr(job->Matrix));
@@ -245,7 +300,6 @@ void DrawFinalPass::BindModelUniforms(GLuint shaderHandle, std::shared_ptr<Model
GLERROR("END");
}
void DrawFinalPass::BindExplosionTextures(std::shared_ptr<ExplosionEffectJob>& job)
{
glActiveTexture(GL_TEXTURE0);
+51 -2
View File
@@ -31,6 +31,56 @@ 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);
// 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))) {
continue;
}
std::string diffuseResource = cSprite["DiffuseTexture"];
std::string glowResource = cSprite["GlowMap"];
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"];
}
glm::mat4 modelMatrix = Transform::ModelMatrix(entity.ID, world);
//modelMatrix *= m_Camera->BillboardMatrix();
std::shared_ptr<SpriteJob> spriteJob = std::shared_ptr<SpriteJob>(new SpriteJob(cSprite, m_Camera, modelMatrix, world, fillColor, fillPercentage));
jobs.push_back(spriteJob);
}
}
bool RenderSystem::isChildOfACamera(EntityWrapper entity)
{
return entity.FirstParentWithComponent("Camera").Valid();
@@ -167,7 +217,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,7 +238,6 @@ 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");
@@ -255,6 +303,7 @@ void RenderSystem::Update(double dt)
fillPointLights(scene.PointLightJobs, m_World);
fillDirectionalLights(scene.DirectionalLightJobs, m_World);
fillText(scene.TextJobs, m_World);
fillSprites(scene.SpriteJobs, m_World);
m_RenderFrame->Add(scene);
}
+5 -2
View File
@@ -2,14 +2,17 @@
Texture::Texture(std::string path)
{
PNG image(path);
if (image.Width == 0 && image.Height == 0 || image.Format == Image::ImageFormat::Unknown) {
image = PNG("Textures/Core/ErrorTexture.png");
//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;
}
}*/
}
this->Width = image.Width;
+2 -1
View File
@@ -14,6 +14,7 @@
#include "Game/Systems/PlayerHUD.h"
#include "Game/Systems/LifetimeSystem.h"
#include "../Engine/Rendering/AnimationSystem.h"
#include "../Engine/Core/UniformScaleSystem.h"
Game::Game(int argc, char* argv[])
{
@@ -97,7 +98,7 @@ Game::Game(int argc, char* argv[])
m_SystemPipeline->AddSystem<CollidableOctreeSystem>(updateOrderLevel, m_OctreeTrigger, "Player");
m_SystemPipeline->AddSystem<PlayerHUD>(updateOrderLevel);
m_SystemPipeline->AddSystem<AnimationSystem>(updateOrderLevel);
m_SystemPipeline->AddSystem<UniformScaleSystem>(updateOrderLevel);
// Collision and TriggerSystem should update after player.
++updateOrderLevel;
m_SystemPipeline->AddSystem<CollisionSystem>(updateOrderLevel, m_OctreeCollision);