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
+2
View File
@@ -33,6 +33,8 @@ public:
glm::mat4 ViewMatrix() const { return m_ViewMatrix; }
void SetViewMatrix(glm::mat4 val);
glm::mat4 BillboardMatrix();
float AspectRatio() const { return m_AspectRatio; }
void SetAspectRatio(float val);
+2
View File
@@ -31,6 +31,7 @@ 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<std::shared_ptr<RenderJob>>& job, RenderScene& scene);
void DrawSprites(std::list<std::shared_ptr<RenderJob>>&jobs, RenderScene& scene);
void BindExplosionUniforms(GLuint shaderHandle, std::shared_ptr<ExplosionEffectJob>& job, RenderScene& scene);
void BindModelUniforms(GLuint shaderHandle, std::shared_ptr<ModelJob>& job, RenderScene& scene);
@@ -53,6 +54,7 @@ private:
ShaderProgram* m_ForwardPlusProgram;
ShaderProgram* m_ExplosionEffectProgram;
ShaderProgram* m_SpriteProgram;
};
#endif
+4
View File
@@ -15,6 +15,7 @@
#include "PointLightJob.h"
#include "DirectionalLightJob.h"
#include "ExplosionEffectJob.h"
#include "SpriteJob.h"
struct RenderScene
{
@@ -24,6 +25,8 @@ struct RenderScene
std::list<std::shared_ptr<RenderJob>> PointLightJobs;
std::list<std::shared_ptr<RenderJob>> TextJobs;
std::list<std::shared_ptr<RenderJob>> DirectionalLightJobs;
std::list<std::shared_ptr<RenderJob>> SpriteJobs;
Rectangle Viewport;
bool ClearDepth = false;
glm::vec4 AmbientColor;
@@ -35,6 +38,7 @@ struct RenderScene
PointLightJobs.clear();
TextJobs.clear();
DirectionalLightJobs.clear();
SpriteJobs.clear();
}
};
+1
View File
@@ -45,6 +45,7 @@ private:
void fillPointLights(std::list<std::shared_ptr<RenderJob>>& jobs, World* world);
void fillDirectionalLights(std::list<std::shared_ptr<RenderJob>>& jobs, World* world);
void fillLight(std::list<std::shared_ptr<RenderJob>>& jobs);
void fillSprites(std::list<std::shared_ptr<RenderJob>>& jobs, World* world);
bool isChildOfACamera(EntityWrapper entity);
bool isChildOfCurrentCamera(EntityWrapper entity);
+75
View File
@@ -0,0 +1,75 @@
#ifndef SpriteJob_h__
#define SpriteJob_h__
#include <cstdint>
#include "../Common.h"
#include "../GLM.h"
#include "../Core/ComponentWrapper.h"
#include "Texture.h"
#include "Model.h"
#include "RenderJob.h"
#include "../Core/ResourceManager.h"
#include "Camera.h"
#include "../Core/World.h"
#include "../Core/Transform.h"
#include "Skeleton.h"
struct SpriteJob : RenderJob
{
SpriteJob(ComponentWrapper cSprite, Camera* camera, glm::mat4 matrix, World* world, glm::vec4 fillColor, float fillPercentage)
: RenderJob()
{
Model = ResourceManager::Load<::Model>("Models/Core/UnitQuad.mesh");
::RawModel::MaterialGroup matGroup = Model->MaterialGroups().front();
TextureID = (matGroup.Texture) ? matGroup.Texture->ResourceID : 0;
if (cSprite["DiffuseTexture"]) {
DiffuseTexture = ResourceManager::Load<Texture>(cSprite["DiffuseTexture"]);
} else {
DiffuseTexture = nullptr;
}
if (cSprite["GlowMap"]) {
IncandescenceTexture = ResourceManager::Load<Texture>(cSprite["GlowMap"]);
} else {
IncandescenceTexture = nullptr;
}
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));
Depth = viewpos.z;
World = world;
FillColor = fillColor;
FillPercentage = fillPercentage;
};
unsigned int TextureID;
EntityID Entity;
glm::mat4 Matrix;
const Texture* DiffuseTexture;
const Texture* NormalTexture;
const Texture* SpecularTexture;
const Texture* IncandescenceTexture;
float Shininess = 0.f;
glm::vec4 Color;
const ::Model* Model = nullptr;
unsigned int StartIndex = 0;
unsigned int EndIndex = 0;
World* World;
glm::vec4 FillColor = glm::vec4(0);
float FillPercentage = 0.0;
void CalculateHash() override
{
Hash = TextureID;
}
};
#endif
+1
View File
@@ -31,4 +31,5 @@
<xs:include schemaLocation="Components/Lifetime.xsd"/>
<xs:include schemaLocation="Components/HiddenForLocalPlayer.xsd"/>
<xs:include schemaLocation="Components/DashAbility.xsd"/>
<xs:include schemaLocation="Components/Sprite.xsd"/>
</xs:schema>
+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<Sprite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Sprite.xsd">
<DiffuseTexture></DiffuseTexture>
<GlowMap></GlowMap>
<Color R="1" G="1" B="1" A="1"/>
<Visible>true</Visible>
</Sprite>
+27
View File
@@ -0,0 +1,27 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="types">
<xs:import schemaLocation="../Types.xsd" namespace="types"/>
<xs:element name="Sprite">
<xs:annotation>
<xs:documentation>A sprite that will be facing the camera</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:all>
<xs:element name="DiffuseTexture" type="t:string" minOccurs="0">
<xs:annotation><xs:documentation>Diffuse Texture file</xs:documentation></xs:annotation>
</xs:element>
<xs:element name="GlowMap" type="t:string" minOccurs="0">
<xs:annotation><xs:documentation>GlowMap file</xs:documentation></xs:annotation>
</xs:element>
<xs:element name="Color" type="t:Color" minOccurs="0">
<xs:annotation><xs:documentation>Color tint</xs:documentation></xs:annotation>
</xs:element>
<xs:element name="Visible" type="t:bool" minOccurs="0">
<xs:annotation><xs:documentation>Whether the model is visible or not</xs:documentation></xs:annotation>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
+14 -2
View File
@@ -52,11 +52,11 @@
<Children>
<Entity name="Hexagon">
<Components>
<c:HealthHUD/>
<c:Fill>
<Percentage>0.65990006923675537</Percentage>
<Color A="0" B="0.659900069" G="0" R="0.340099931"/>
</c:Fill>
<c:HealthHUD/>
<c:Model>
<Resource>Models/Core/UnitHexagon.mesh</Resource>
<Color A="0.313725501" B="0.70588237" G="0.70588237" R="0.70588237"/>
@@ -161,7 +161,7 @@
<Axis X="0.200000003" Y="3.4000001" Z="0.5"/>
</c:RaptorCopter>
<c:Transform>
<Orientation X="758.058899" Y="20632.8691" Z="1320.68018"/>
<Orientation X="758.059998" Y="20632.8887" Z="1320.68311"/>
</c:Transform>
</Components>
<Children>
@@ -346,6 +346,18 @@
</Components>
<Children/>
</Entity>
<Entity name="Sprite">
<Components>
<c:Sprite>
<DiffuseTexture>Textures/HexmapDiff.png</DiffuseTexture>
<GlowMap>Textures/GlowFrame.png</GlowMap>
</c:Sprite>
<c:Transform>
<Position X="0" Y="0" Z="-3.41100025"/>
</c:Transform>
</Components>
<Children/>
</Entity>
</Children>
</Entity>
+41
View File
@@ -0,0 +1,41 @@
#version 430
uniform vec4 Color;
uniform vec4 FillColor;
uniform float FillPercentage;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
layout (binding = 0) uniform sampler2D DiffuseTexture;
layout (binding = 1) uniform sampler2D GlowMapTexture;
in VertexData{
vec3 Position;
vec3 Normal;
vec2 TextureCoordinate;
}Input;
out vec4 sceneColor;
out vec4 bloomColor;
void main()
{
vec4 diffuseTexel = texture2D(DiffuseTexture, Input.TextureCoordinate);
vec4 glowTexel = texture2D(GlowMapTexture, Input.TextureCoordinate);
vec4 color_result = Color * diffuseTexel;
float pos = ((P * vec4(Input.Position, 1)).y + 1.0)/2.0;
if(pos <= FillPercentage) {
color_result += FillColor;
}
sceneColor = vec4(color_result.xyz, clamp(color_result.a, 0, 1));
color_result += glowTexel*3;
bloomColor = vec4(clamp(color_result.xyz - 1.0, 0, 100), 1.0);
}
+24
View File
@@ -0,0 +1,24 @@
#version 430
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
layout(location = 0) in vec3 Position;
layout(location = 1) in vec3 Normal;
layout(location = 4) in vec2 TextureCoords;
out VertexData{
vec3 Position;
vec3 Normal;
vec2 TextureCoordinate;
}Output;
void main()
{
gl_Position = P * M * vec4(Position, 1.0);
Output.Position = Position;
Output.TextureCoordinate = TextureCoords;
Output.Normal = Normal;
}
+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);