fix mouse picking
This commit is contained in:
@@ -12,6 +12,9 @@
|
||||
#include "misc/Easing.h"
|
||||
#include "misc/Maths.h"
|
||||
|
||||
#include <misc/Log.h>
|
||||
#include <misc/Format.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
@@ -23,14 +26,6 @@ Renderer::~Renderer() {
|
||||
|
||||
}
|
||||
|
||||
void Renderer::UpdateIsometricView() {
|
||||
//float isometricEased = utils::EaseInOutExpo(m_IsometricShade);
|
||||
m_WorldShader->Start();
|
||||
//m_WorldShader->SetIsometricView(isometricEased);
|
||||
m_EntityShader->Start();
|
||||
//m_EntityShader->SetIsometricView(isometricEased);
|
||||
}
|
||||
|
||||
void Renderer::InitShaders() {
|
||||
m_WorldShader = std::make_unique<shader::WorldShader>();
|
||||
m_WorldShader->LoadShader();
|
||||
@@ -75,34 +70,18 @@ void Renderer::RenderModel(const Model& model) {
|
||||
model.vao->Unbind();
|
||||
}
|
||||
|
||||
void Renderer::UpdateIsometricFade() {
|
||||
static std::uint64_t lastTime = utils::GetTime();
|
||||
if (m_IsometricShade != static_cast<float>(m_IsometricView)) {
|
||||
float step = static_cast<float>(utils::GetTime() - lastTime) / 1000.0f * m_AnimationSpeed;
|
||||
if (m_IsometricShade < m_IsometricView) {
|
||||
m_IsometricShade += step;
|
||||
} else {
|
||||
m_IsometricShade -= step;
|
||||
}
|
||||
m_IsometricShade = std::min(m_IsometricShade, 1.0f);
|
||||
m_IsometricShade = std::max(m_IsometricShade, 0.0f);
|
||||
UpdateIsometricView();
|
||||
}
|
||||
lastTime = utils::GetTime();
|
||||
}
|
||||
|
||||
void Renderer::Prepare() {
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glClearColor(m_BackgroundColor.r, m_BackgroundColor.g, m_BackgroundColor.b, 0);
|
||||
UpdateIsometricFade();
|
||||
}
|
||||
|
||||
void Renderer::Resize(int width, int height) {
|
||||
Mat4f projectionMatrix = maths::Perspective(80.0f / 180.0f * M_PI, static_cast<float>(width) / height, 0.1f, 160.0f);
|
||||
m_Camera.projectionMatrix = maths::Perspective(80.0f / 180.0f * M_PI, static_cast<float>(width) / height, 0.1f, 160.0f);
|
||||
m_Camera.InvProjectionMatrix = maths::Inverse(m_Camera.projectionMatrix);
|
||||
m_WorldShader->Start();
|
||||
m_WorldShader->SetProjectionMatrix(projectionMatrix);
|
||||
m_WorldShader->SetProjectionMatrix(m_Camera.projectionMatrix);
|
||||
m_EntityShader->Start();
|
||||
m_WorldShader->SetProjectionMatrix(projectionMatrix);
|
||||
m_WorldShader->SetProjectionMatrix(m_Camera.projectionMatrix);
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
@@ -114,37 +93,39 @@ void Renderer::SetZoom(float zoom) {
|
||||
}
|
||||
|
||||
void Renderer::SetCamMovement(const Vec2f& mov) {
|
||||
m_CamPos.x += mov.x;
|
||||
m_CamPos.y += -mov.y;
|
||||
SetCamPos(m_CamPos);
|
||||
m_Camera.CamPos.x += mov.x;
|
||||
m_Camera.CamPos.y += -mov.y;
|
||||
SetCamPos(m_Camera.CamPos);
|
||||
}
|
||||
|
||||
void Renderer::SetCamPos(const Vec2f& newPos) {
|
||||
m_CamPos = newPos;
|
||||
Mat4f viewMatrix = maths::Look({m_CamPos.x, 50, m_CamPos.y}, {0, -1, -0.0001}, {0, 1, 0});
|
||||
void Renderer::SetCamPos(const Vec3f& newPos) {
|
||||
m_Camera.CamPos = newPos;
|
||||
m_Camera.viewMatrix = maths::Look(m_Camera.CamPos, {0, -1, -0.0001}, {0, 1, 0});
|
||||
m_Camera.InvViewMatrix = maths::Inverse(m_Camera.viewMatrix);
|
||||
m_WorldShader->Start();
|
||||
m_WorldShader->SetViewMatrix(viewMatrix);
|
||||
m_WorldShader->SetViewMatrix(m_Camera.viewMatrix);
|
||||
m_EntityShader->Start();
|
||||
m_EntityShader->SetViewMatrix(viewMatrix);
|
||||
m_EntityShader->SetViewMatrix(m_Camera.viewMatrix);
|
||||
}
|
||||
|
||||
void Renderer::SetIsometricView(bool isometric) {
|
||||
m_IsometricView = isometric;
|
||||
}
|
||||
Vec2f Renderer::GetCursorWorldPos(const Vec2f& cursorPos, float windowWidth, float windowHeight) {
|
||||
|
||||
Vec2f Renderer::GetCursorWorldPos(const Vec2f& cursorPos, float aspectRatio, float zoom, float windowWidth, float windowHeight) {
|
||||
float isometricEased = utils::EaseInOutExpo(m_IsometricShade);
|
||||
float relativeX = 1 - (cursorPos.x / windowWidth * 2);
|
||||
float relativeY = 1 - (cursorPos.y / windowHeight * 2);
|
||||
|
||||
float relativeX = (cursorPos.x / windowWidth * 2) - 1;
|
||||
float relativeY = (cursorPos.y / windowHeight * 2) - 1;
|
||||
Vec4f rayClip {relativeX, relativeY, -1.0f, 1.0f};
|
||||
|
||||
float deltaX = relativeX * aspectRatio / zoom;
|
||||
float deltaY = relativeY / zoom;
|
||||
Vec4f rayEye = maths::Dot(m_Camera.InvProjectionMatrix , rayClip);
|
||||
rayEye = {rayEye.x, rayEye.y, -1.0f, 0.0f};
|
||||
|
||||
float worldX = m_CamPos.x + deltaX * (1 - isometricEased) + (0.5 * deltaX + deltaY) * isometricEased;
|
||||
float worldY = m_CamPos.y + deltaY * (1 - isometricEased) + (-0.5 * deltaX + deltaY) * isometricEased;
|
||||
Vec4f rayWorld = maths::Dot(m_Camera.InvViewMatrix, rayEye);
|
||||
rayWorld.w = 0;
|
||||
|
||||
maths::Normalize(rayWorld);
|
||||
|
||||
return { worldX, worldY };
|
||||
float lambda = - m_Camera.CamPos.y / rayWorld.y;
|
||||
|
||||
return {lambda * rayWorld.x + m_Camera.CamPos.x, lambda * rayWorld.z + m_Camera.CamPos.z};
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user