diff --git a/include/render/Renderer.h b/include/render/Renderer.h index e71bbde..8d19841 100644 --- a/include/render/Renderer.h +++ b/include/render/Renderer.h @@ -15,7 +15,7 @@ public: struct Model { GL::VertexArray* vao; - Vec2f positon; + Vec3f positon; Vec3f color = { 1, 1, 1 }; }; private: diff --git a/include/render/shaders/EntityShader.h b/include/render/shaders/EntityShader.h index ee9dcc1..25309c6 100644 --- a/include/render/shaders/EntityShader.h +++ b/include/render/shaders/EntityShader.h @@ -21,7 +21,7 @@ public: void SetColorEffect(const Vec3f& color); void SetProjectionMatrix(const maths::Mat4f& proj) const; void SetViewMatrix(const maths::Mat4f& view) const; - void SetModelPos(const Vec2f& pos) const; + void SetModelPos(const Vec3f& pos) const; }; } // namespace shader diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp index 65d5112..c2a6f20 100644 --- a/src/render/Renderer.cpp +++ b/src/render/Renderer.cpp @@ -10,6 +10,7 @@ #include #include "misc/Time.h" #include "misc/Easing.h" +#include "misc/Maths.h" namespace td { namespace render { @@ -35,8 +36,6 @@ void Renderer::InitShaders() { m_WorldShader->LoadShader(); m_EntityShader = std::make_unique(); m_EntityShader->LoadShader(); - SetIsometricView(true); - UpdateIsometricView(); } // TODO : change loader check @@ -69,7 +68,7 @@ void Renderer::RenderVAO(const GL::VertexArray& vao) { void Renderer::RenderModel(const Model& model) { m_EntityShader->Start(); - //m_EntityShader->SetModelPos(model.positon); + m_EntityShader->SetModelPos(model.positon); m_EntityShader->SetColorEffect(model.color); model.vao->Bind(); glDrawArrays(GL_TRIANGLES, 0, static_cast(model.vao->GetVertexCount())); @@ -99,10 +98,11 @@ void Renderer::Prepare() { } void Renderer::Resize(int width, int height) { + maths::Mat4f projectionMatrix = maths::Perspective(80.0f / 180.0f * M_PI, static_cast(width) / height, 0.1f, 160.0f); m_WorldShader->Start(); - //m_WorldShader->SetAspectRatio(static_cast(width) / height); + m_WorldShader->SetProjectionMatrix(projectionMatrix); m_EntityShader->Start(); - //m_EntityShader->SetAspectRatio(static_cast(width) / height); + m_WorldShader->SetProjectionMatrix(projectionMatrix); glViewport(0, 0, width, height); } @@ -114,17 +114,18 @@ void Renderer::SetZoom(float zoom) { } void Renderer::SetCamMovement(const Vec2f& mov) { - m_CamPos.x += mov.x * (1 - m_IsometricView) + (0.5 * mov.x - mov.y) * m_IsometricView; - m_CamPos.y += -mov.y * (1 - m_IsometricView) + (-0.5 * mov.x - mov.y) * m_IsometricView; + m_CamPos.x += mov.x; + m_CamPos.y += -mov.y; SetCamPos(m_CamPos); } void Renderer::SetCamPos(const Vec2f& newPos) { m_CamPos = newPos; + maths::Mat4f viewMatrix = maths::LookAt({m_CamPos.x, 50, m_CamPos.y}, {m_CamPos.x, -1, m_CamPos.y}, {0, 1, 0}); m_WorldShader->Start(); - //m_WorldShader->SetCamPos(newPos); + m_WorldShader->SetViewMatrix(viewMatrix); m_EntityShader->Start(); - //m_EntityShader->SetCamPos(newPos); + m_EntityShader->SetViewMatrix(viewMatrix); } void Renderer::SetIsometricView(bool isometric) { diff --git a/src/render/VertexCache.cpp b/src/render/VertexCache.cpp index b1e0f4c..1bf9124 100644 --- a/src/render/VertexCache.cpp +++ b/src/render/VertexCache.cpp @@ -4,6 +4,8 @@ namespace td { namespace render { +const static int VERTEX_SIZE = 3; + void VertexCache::AddData(std::uint64_t index, std::vector positions, std::vector colors) { m_Indexes.insert({ index, {positions, colors} }); m_VertexCount += colors.size(); // one color per vertex @@ -26,7 +28,7 @@ void VertexCache::UpdateVertexArray() { m_VertexArray = std::make_unique(m_VertexCount); // one color per vertex Vector positions; - positions.reserve(m_VertexCount * 2); + positions.reserve(m_VertexCount * VERTEX_SIZE); Vector colors; colors.reserve(m_VertexCount); @@ -38,8 +40,8 @@ void VertexCache::UpdateVertexArray() { colors.insert(colors.end(), data.color.begin(), data.color.end()); } - GL::VertexBuffer positionsBuffer(positions, 2); - positionsBuffer.AddVertexAttribPointer(0, 2, 0); + GL::VertexBuffer positionsBuffer(positions, VERTEX_SIZE); + positionsBuffer.AddVertexAttribPointer(0, VERTEX_SIZE, 0); GL::VertexBuffer colorsBuffer(colors, 1); colorsBuffer.AddVertexAttribPointer(1, 1, 0); diff --git a/src/render/WorldRenderer.cpp b/src/render/WorldRenderer.cpp index 29f8be9..2293b97 100644 --- a/src/render/WorldRenderer.cpp +++ b/src/render/WorldRenderer.cpp @@ -86,7 +86,7 @@ void WorldRenderer::RenderMobs() const { for (game::MobPtr mob : m_World->GetMobList()) { Renderer::Model model; model.vao = m_MobVao.get(); - model.positon = { mob->GetCenterX(), mob->GetCenterY() }; + model.positon = { mob->GetCenterX(), 0, mob->GetCenterY() }; model.color = mob->HasTakenDamage() ? Vec3f{ 1, 0.5, 0.5 } : Vec3f{ 1, 1, 1 }; m_Renderer->RenderModel(model); } @@ -104,7 +104,7 @@ void WorldRenderer::RenderTileSelect() const { Renderer::Model tileSelectModel; tileSelectModel.vao = m_SelectTileVao.get(); - tileSelectModel.positon = { std::floor(m_CursorPos.x), std::floor(m_CursorPos.y) }; + tileSelectModel.positon = { std::floor(m_CursorPos.x), 0, std::floor(m_CursorPos.y) }; m_Renderer->RenderModel(tileSelectModel); } diff --git a/src/render/loader/WorldLoader.cpp b/src/render/loader/WorldLoader.cpp index b805d29..7716c27 100644 --- a/src/render/loader/WorldLoader.cpp +++ b/src/render/loader/WorldLoader.cpp @@ -10,15 +10,17 @@ namespace render { namespace WorldLoader { +const static int VERTEX_SIZE = 3; + GL::VertexArray LoadMobModel() { std::vector positions = { - -0.5, -0.5, - 0.5, -0.5, - -0.5, 0.5, + -0.5, 0, -0.5, + 0.5, 0, -0.5, + -0.5, 0, 0.5, - 0.5, -0.5, - -0.5, 0.5, - 0.5, 0.5 + 0.5, 0, -0.5, + -0.5, 0, 0.5, + 0.5, 0, 0.5 }; float yellowFloat; @@ -35,8 +37,8 @@ GL::VertexArray LoadMobModel() { yellowFloat }; - GL::VertexBuffer positionVBO(positions, 2); - positionVBO.AddVertexAttribPointer(0, 2, 0); + GL::VertexBuffer positionVBO(positions, VERTEX_SIZE); + positionVBO.AddVertexAttribPointer(0, VERTEX_SIZE, 0); GL::VertexBuffer colorVBO(colors, 1); colorVBO.AddVertexAttribPointer(1, 1, 0); @@ -69,13 +71,13 @@ GL::VertexArray LoadWorldModel(const td::game::World* world) { continue; positions.insert(positions.end(), { - static_cast(chunkX + tileX), static_cast(chunkY + tileY), - static_cast(chunkX + tileX + 1), static_cast(chunkY + tileY), - static_cast(chunkX + tileX), static_cast(chunkY + tileY + 1), + static_cast(chunkX + tileX), 0, static_cast(chunkY + tileY), + static_cast(chunkX + tileX + 1), 0, static_cast(chunkY + tileY), + static_cast(chunkX + tileX), 0, static_cast(chunkY + tileY + 1), - static_cast(chunkX + tileX + 1), static_cast(chunkY + tileY), - static_cast(chunkX + tileX), static_cast(chunkY + tileY + 1), - static_cast(chunkX + tileX + 1), static_cast(chunkY + tileY + 1), + static_cast(chunkX + tileX + 1), 0, static_cast(chunkY + tileY), + static_cast(chunkX + tileX), 0, static_cast(chunkY + tileY + 1), + static_cast(chunkX + tileX + 1), 0, static_cast(chunkY + tileY + 1) }); const td::Color* tileColor = world->GetTileColor(tile); @@ -101,13 +103,13 @@ GL::VertexArray LoadWorldModel(const td::game::World* world) { float fromY = spawn.GetTopLeft().GetY(), toY = spawn.GetBottomRight().GetY(); positions.insert(positions.end(), { - fromX, fromY, - fromX, toY, - toX, fromY, + fromX, 0, fromY, + fromX, 0, toY, + toX, 0, fromY, - toX, toY, - fromX, toY, - toX, fromY, + toX, 0, toY, + fromX, 0, toY, + toX, 0, fromY, }); for (int i = 0; i < 6; i++) { @@ -129,13 +131,13 @@ GL::VertexArray LoadWorldModel(const td::game::World* world) { float fromY = castle.GetTopLeft().GetY(), toY = castle.GetBottomRight().GetY(); positions.insert(positions.end(), { - fromX, fromY, - fromX, toY, - toX, fromY, + fromX, 0, fromY, + fromX, 0, toY, + toX, 0, fromY, - toX, toY, - fromX, toY, - toX, fromY, + toX, 0, toY, + fromX, 0, toY, + toX, 0, fromY, }); for (int i = 0; i < 6; i++) { @@ -151,12 +153,12 @@ GL::VertexArray LoadWorldModel(const td::game::World* world) { } } - GL::VertexBuffer positionVBO(positions, 2); - positionVBO.AddVertexAttribPointer(0, 2, 0); + GL::VertexBuffer positionVBO(positions, VERTEX_SIZE); + positionVBO.AddVertexAttribPointer(0, VERTEX_SIZE, 0); GL::VertexBuffer colorVBO(colors, 1); colorVBO.AddVertexAttribPointer(1, 1, 0); - GL::VertexArray worldVao(positions.size() / 2); // each pos = 2 vertecies + GL::VertexArray worldVao(positions.size() / VERTEX_SIZE); // each pos = 3 vertecies worldVao.Bind(); worldVao.BindVertexBuffer(positionVBO); worldVao.BindVertexBuffer(colorVBO); @@ -166,13 +168,13 @@ GL::VertexArray LoadWorldModel(const td::game::World* world) { GL::VertexArray LoadTileSelectModel() { std::vector positions = { - 0, 0, - 1, 0, - 0, 1, + 0, 0, 0, + 1, 0, 0, + 0, 0, 1, - 1, 0, - 0, 1, - 1, 1 + 1, 0, 0, + 0, 0, 1, + 1, 0, 1 }; int color = 255 << 24 | 255 << 16 | 255 << 8 | 150; @@ -182,8 +184,8 @@ GL::VertexArray LoadTileSelectModel() { std::vector colors(6, colorFloat); - GL::VertexBuffer positionVBO(positions, 2); - positionVBO.AddVertexAttribPointer(0, 2, 0); + GL::VertexBuffer positionVBO(positions, VERTEX_SIZE); + positionVBO.AddVertexAttribPointer(0, VERTEX_SIZE, 0); GL::VertexBuffer colorVBO(colors, 1); colorVBO.AddVertexAttribPointer(1, 1, 0); diff --git a/src/render/shaders/EntityShader.cpp b/src/render/shaders/EntityShader.cpp index 1029034..216f106 100644 --- a/src/render/shaders/EntityShader.cpp +++ b/src/render/shaders/EntityShader.cpp @@ -112,7 +112,7 @@ void EntityShader::SetViewMatrix(const maths::Mat4f& view) const { LoadMat4(m_LocationViewMatrix, view); } -void EntityShader::SetModelPos(const Vec2f& pos) const { +void EntityShader::SetModelPos(const Vec3f& pos) const { LoadVector(m_LocationPosition, pos); }