diff --git a/include/Core/Engine.h b/include/Core/Engine.h index 7241721..04b3693 100755 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -31,6 +31,7 @@ #include "World.h" #include "CTransform.h" #include "Rendering/CModel.h" +#include "Rendering/CSprite.h" #include "CTemplate.h" #include "Transform/TransformSystem.h" @@ -58,15 +59,17 @@ public: m_World->SystemFactory.Register([this]() { return new Systems::TransformSystem(m_World.get(), m_EventBroker); }); m_World->AddSystem(); m_World->ComponentFactory.Register(); + m_World->ComponentFactory.Register(); m_World->ComponentFactory.Register(); m_World->Initialize(); - auto ent = m_World->CreateEntity(); - std::shared_ptr transform = m_World->AddComponent(ent); - transform->Position = glm::vec3(0.f, 0.f, -10.f); - std::shared_ptr model = m_World->AddComponent(ent); - model->ModelFile = "Models/Core/UnitSphere.obj"; - +// { +// auto ent = m_World->CreateEntity(); +// std::shared_ptr transform = m_World->AddComponent(ent); +// transform->Position = glm::vec3(0.f, 0.f, -10.f); +// auto sprite = m_World->AddComponent(ent); +// sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; +// } m_LastTime = glfwGetTime(); } @@ -141,6 +144,21 @@ public: EnqueueModel(modelAsset, modelMatrix, modelComponent->Transparent, modelComponent->Color); } } + + auto spriteComponent = m_World->GetComponent(entity); + if (spriteComponent) + { + auto textureAsset = ResourceManager::Load(spriteComponent->SpriteFile); + if (textureAsset) + { + Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity); + glm::quat orientation2D = glm::angleAxis(glm::eulerAngles(absoluteTransform.Orientation).z, glm::vec3(0, 0, -1)); + glm::mat4 modelMatrix = glm::translate(absoluteTransform.Position) + * glm::toMat4(orientation2D) + * glm::scale(absoluteTransform.Scale); + EnqueueSprite(textureAsset, modelMatrix, spriteComponent->Color); + } + } } m_RendererQueue.Sort(); @@ -167,6 +185,18 @@ public: } } + // TODO: Get this out of engine.h + void EnqueueSprite(Texture* texture, glm::mat4 modelMatrix, glm::vec4 color) + { + SpriteJob job; + job.TextureID = texture->ResourceID; + job.Texture = *texture; + job.ModelMatrix = modelMatrix; + job.Color = color; + + m_RendererQueue.Forward.Add(job); + } + private: std::shared_ptr m_ResourceManager; std::shared_ptr m_EventBroker; diff --git a/src/game/Core/Renderer.cpp b/src/game/Core/Renderer.cpp old mode 100644 new mode 100755 index 5af1892..69aee81 --- a/src/game/Core/Renderer.cpp +++ b/src/game/Core/Renderer.cpp @@ -322,6 +322,26 @@ void dd::Renderer::DrawScene(RenderQueue &objects, ShaderProgram &program) glBindVertexArray(modelJob->VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, modelJob->ElementBuffer); glDrawElementsBaseVertex(GL_TRIANGLES, modelJob->EndIndex - modelJob->StartIndex + 1, GL_UNSIGNED_INT, 0, modelJob->StartIndex); + + continue; + } + + auto spriteJob = std::dynamic_pointer_cast(job); + if (spriteJob) + { + glm::mat4 modelMatrix = spriteJob->ModelMatrix; + MVP = PV * modelMatrix; + glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "V"), 1, GL_FALSE, glm::value_ptr(viewMatrix)); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, spriteJob->Texture); + + glBindVertexArray(m_UnitQuad); + glDrawArrays(GL_TRIANGLES, 0, 6); + + continue; } } } @@ -396,9 +416,9 @@ GLuint dd::Renderer::CreateQuad() glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, 0); + glVertexAttribPointer(4, 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(0); - glEnableVertexAttribArray(2); + glEnableVertexAttribArray(4); glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); diff --git a/src/game/Core/Shaders/Forward/Vertex.glsl b/src/game/Core/Shaders/Forward/Vertex.glsl old mode 100644 new mode 100755 index 4b4e649..46ccf5a --- a/src/game/Core/Shaders/Forward/Vertex.glsl +++ b/src/game/Core/Shaders/Forward/Vertex.glsl @@ -65,7 +65,8 @@ void main() + BoneWeights2[3] * Bones[int(BoneIndices2[3])]; } - gl_Position = MVP * boneTransform * vec4(Position, 1.0); + //gl_Position = MVP * boneTransform * vec4(Position, 1.0); + gl_Position = MVP * vec4(Position, 1.0); Output.Position = (V * M * boneTransform * vec4(Position, 1.0)).xyz; Output.Normal = (inverse(transpose(V * M)) * boneTransform * vec4(Normal, 0.0)).xyz;