diff --git a/include/Defines.h b/include/Defines.h index 79cb783..30cfd92 100644 --- a/include/Defines.h +++ b/include/Defines.h @@ -135,4 +135,12 @@ typedef Mat4 Mat4f; typedef Mat4 Mat4i; typedef Mat4 Mat4d; +template +inline bool operator==(const Mat4& mat, const Mat4& other) { + return mat.x0 == other.x0 && mat.y0 == other.y0 && mat.z0 == other.z0 && mat.w0 == other.w0 && + mat.x1 == other.x1 && mat.y1 == other.y1 && mat.z1 == other.z1 && mat.w1 == other.w1 && + mat.x2 == other.x2 && mat.y2 == other.y2 && mat.z2 == other.z2 && mat.w2 == other.w2 && + mat.x3 == other.x3 && mat.y3 == other.y3 && mat.z3 == other.z3 && mat.w3 == other.w3; +} + } // namespace td diff --git a/include/misc/Maths.h b/include/misc/Maths.h index a83842d..783940c 100644 --- a/include/misc/Maths.h +++ b/include/misc/Maths.h @@ -20,6 +20,11 @@ Vec3 operator- (const Vec3& vect) { return { -vect.x, -vect.y, -vect.z }; } +template +Vec4 operator- (const Vec4& vect) { + return { -vect.x, -vect.y, -vect.z, -vect.w }; +} + template Vec3 operator+ (const Vec3& vect, const Vec3& other) { return { vect.x + other.x, vect.y + other.y, vect.z + other.y }; @@ -70,10 +75,10 @@ T Dot(const Vec4& vect, const Vec4& other) { template Vec4 Dot(const Mat4& mat, const Vec4& vect) { return { - Dot(*reinterpret_cast*>(&mat), vect), - Dot(*reinterpret_cast*>(mat.data() + Mat4::MATRIX_SIZE), vect), - Dot(*reinterpret_cast*>(mat.data() + 2 * Mat4::MATRIX_SIZE), vect), - Dot(*reinterpret_cast*>(mat.data() + 3 * Mat4::MATRIX_SIZE), vect), + mat.x0 * vect.x + mat.x1 * vect.y + mat.x2 * vect.z + mat.x3 * vect.w, + mat.y0 * vect.x + mat.y1 * vect.y + mat.y2 * vect.z + mat.y3 * vect.w, + mat.z0 * vect.x + mat.z1 * vect.y + mat.z2 * vect.z + mat.z3 * vect.w, + mat.w0 * vect.x + mat.w1 * vect.y + mat.w2 * vect.z + mat.w3 * vect.w }; } @@ -84,7 +89,7 @@ Mat4 Dot(const Mat4& mat, const Mat4& other) { for (std::size_t i = 0; i < Mat4::MATRIX_SIZE; i++) { for (std::size_t j = 0; j < Mat4::MATRIX_SIZE; j++) { for (std::size_t k = 0; k < Mat4::MATRIX_SIZE; k++) { - result.at(i, j) = mat.at(i, k) * other.at(k, j); + result.at(i, j) += mat.at(i, k) * other.at(k, j); } } } diff --git a/include/render/Renderer.h b/include/render/Renderer.h index e37169c..fe1b60e 100644 --- a/include/render/Renderer.h +++ b/include/render/Renderer.h @@ -17,8 +17,10 @@ struct Camera { Vec3f CamPos; + Vec3f m_Front {0, -1, 0}; + float m_Yaw = -3.141592653f / 2.0f; - constexpr static float m_Pitch = -3.141592653f / 2.0f + 0.0000001f; + float m_Pitch = -3.141592653f / 2.0f - 0.0000001f; }; class Renderer { diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cpp index 428b382..e546a17 100644 --- a/src/render/Renderer.cpp +++ b/src/render/Renderer.cpp @@ -82,7 +82,7 @@ void Renderer::Resize(int width, int height) { m_WorldShader->SetProjectionMatrix(m_Camera.projectionMatrix); m_EntityShader->Start(); m_WorldShader->SetProjectionMatrix(m_Camera.projectionMatrix); - m_WindowSize = {width, height}; + m_WindowSize = { width, height }; glViewport(0, 0, width, height); } @@ -91,22 +91,23 @@ void Renderer::SetZoom(float zoom) { } void Renderer::SetCamMovement(const Vec2f& mov) { - Vec2f cursor = {static_cast(m_WindowSize.x) / 2.0f - mov.x, static_cast(m_WindowSize.y) / 2.0f - mov.y}; - Vec2f worldMovement = GetCursorWorldPos(cursor, m_WindowSize.x, m_WindowSize.y); - SetCamPos({worldMovement.x, m_Camera.CamPos.y, worldMovement.y}); + m_Camera.m_Pitch -= mov.y / 50.0f; + m_Camera.m_Yaw += mov.x / 50.0f; + SetCamPos(m_Camera.CamPos); } void Renderer::SetCamPos(const Vec3f& newPos) { Vec3f front = { - std::cos(m_Camera.m_Yaw) * std::cos(m_Camera.m_Pitch), + std::cos(m_Camera.m_Yaw) * std::cos(m_Camera.m_Pitch), std::sin(m_Camera.m_Pitch), std::sin(m_Camera.m_Yaw) * std::cos(m_Camera.m_Pitch) }; m_Camera.CamPos = newPos; - m_Camera.viewMatrix = maths::Look(m_Camera.CamPos, front, {0, 1, 0}); + m_Camera.viewMatrix = maths::Look(m_Camera.CamPos, front, { 0, 1, 0 }); m_Camera.InvViewMatrix = maths::Inverse(m_Camera.viewMatrix); + m_WorldShader->Start(); m_WorldShader->SetViewMatrix(m_Camera.viewMatrix); m_EntityShader->Start(); @@ -115,19 +116,20 @@ void Renderer::SetCamPos(const Vec3f& newPos) { Vec2f Renderer::GetCursorWorldPos(const Vec2f& cursorPos, float windowWidth, float windowHeight) { - float relativeX = 1 - (cursorPos.x / windowWidth * 2); - float relativeY = 1 - (cursorPos.y / windowHeight * 2); + float relativeX = (cursorPos.x / windowWidth * 2) - 1.0f; + float relativeY = 1.0f - (cursorPos.y / windowHeight * 2); - Vec4f rayClip {relativeX, relativeY, -1.0f, 1.0f}; + Vec4f rayClip{ relativeX, relativeY, -1.0f, 1.0f }; - Vec4f rayEye = maths::Dot(m_Camera.InvProjectionMatrix , rayClip); - rayEye = {rayEye.x, rayEye.y, -1.0f, 0.0f}; + Vec4f rayEye = maths::Dot(m_Camera.InvProjectionMatrix, rayClip); - Vec4f rayWorld = maths::Dot(m_Camera.InvViewMatrix, rayEye); + rayEye = { rayEye.x, rayEye.y, -1.0f, 0.0f }; - float lambda = - m_Camera.CamPos.y / rayWorld.y; + Vec4f rayWorld = maths::Dot(maths::Transpose(m_Camera.InvViewMatrix), rayEye); // why transpose ? I don't know - return {lambda * rayWorld.x + m_Camera.CamPos.x, lambda * rayWorld.z + m_Camera.CamPos.z}; + float lambda = -m_Camera.CamPos.y / rayWorld.y; + + return { lambda * rayWorld.x + m_Camera.CamPos.x, lambda * rayWorld.z + m_Camera.CamPos.z }; } diff --git a/src/render/WorldRenderer.cpp b/src/render/WorldRenderer.cpp index 31d5f87..b6264fb 100644 --- a/src/render/WorldRenderer.cpp +++ b/src/render/WorldRenderer.cpp @@ -160,7 +160,7 @@ void WorldRenderer::Click() { void WorldRenderer::SetCamPos(float camX, float camY) { m_CamPos = { camX, camY }; - m_Renderer->SetCamPos({ camX, 50, camY }); + m_Renderer->SetCamPos({ camX, 25, camY }); } void WorldRenderer::DetectClick() {