working mouse picking

This commit is contained in:
2023-06-06 12:36:05 +02:00
parent a2b5424888
commit ccdcdac7c6
5 changed files with 38 additions and 21 deletions

View File

@@ -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<float>(m_WindowSize.x) / 2.0f - mov.x, static_cast<float>(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 };
}

View File

@@ -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() {