camera fixing

This commit is contained in:
2023-06-06 13:07:42 +02:00
parent 83ab8c70f0
commit 48841fa4e9
3 changed files with 24 additions and 4 deletions

View File

@@ -15,7 +15,9 @@ struct Camera {
Mat4f InvViewMatrix; Mat4f InvViewMatrix;
Mat4f InvProjectionMatrix; Mat4f InvProjectionMatrix;
Vec3f CamPos; Vec3f CamPos {0, 25, 0};
Vec2f CamLook {};
Vec3f m_Front {0, -1, 0}; Vec3f m_Front {0, -1, 0};
@@ -55,13 +57,14 @@ public:
void SetZoom(float zoom); void SetZoom(float zoom);
void SetCamMovement(const Vec2f& mov); void SetCamMovement(const Vec2f& mov);
void SetCamPos(const Vec3f& newPos); void SetCamLook(const Vec2f& worldPos);
void SetBackgroundColor(const Vec3f& color) { m_BackgroundColor = color; } void SetBackgroundColor(const Vec3f& color) { m_BackgroundColor = color; }
Vec2f GetCursorWorldPos(const Vec2f& cursorPos, float windowWidth, float windowHeight); Vec2f GetCursorWorldPos(const Vec2f& cursorPos, float windowWidth, float windowHeight);
private: private:
void InitShaders(); void InitShaders();
void SetCamPos(const Vec3f& newPos);
}; };
} // namespace render } // namespace render

View File

@@ -88,12 +88,13 @@ void Renderer::Resize(int width, int height) {
void Renderer::SetZoom(float zoom) { void Renderer::SetZoom(float zoom) {
m_Camera.CamPos.y = std::max(1.0f, m_Camera.CamPos.y - zoom); m_Camera.CamPos.y = std::max(1.0f, m_Camera.CamPos.y - zoom);
SetCamLook(m_Camera.CamLook);
} }
void Renderer::SetCamMovement(const Vec2f& mov) { void Renderer::SetCamMovement(const Vec2f& mov) {
m_Camera.m_Pitch -= mov.y / 50.0f; m_Camera.m_Pitch -= mov.y / 50.0f;
m_Camera.m_Yaw += mov.x / 50.0f; m_Camera.m_Yaw += mov.x / 50.0f;
SetCamPos(m_Camera.CamPos); SetCamLook(m_Camera.CamLook);
} }
void Renderer::SetCamPos(const Vec3f& newPos) { void Renderer::SetCamPos(const Vec3f& newPos) {
@@ -114,6 +115,22 @@ void Renderer::SetCamPos(const Vec3f& newPos) {
m_EntityShader->SetViewMatrix(m_Camera.viewMatrix); m_EntityShader->SetViewMatrix(m_Camera.viewMatrix);
} }
void Renderer::SetCamLook(const Vec2f& worldPos) {
m_Camera.CamLook = worldPos;
Vec3f front = {
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)
};
static const float WORLD_HEIGHT = 0;
float lambda = (m_Camera.CamPos.y - WORLD_HEIGHT) / front.y;
SetCamPos({lambda * front.x + m_Camera.CamLook.x, m_Camera.CamPos.y, lambda * front.z + m_Camera.CamLook.y});
}
Vec2f Renderer::GetCursorWorldPos(const Vec2f& cursorPos, float windowWidth, float windowHeight) { Vec2f Renderer::GetCursorWorldPos(const Vec2f& cursorPos, float windowWidth, float windowHeight) {
float relativeX = (cursorPos.x / windowWidth * 2) - 1.0f; float relativeX = (cursorPos.x / windowWidth * 2) - 1.0f;

View File

@@ -160,7 +160,7 @@ void WorldRenderer::Click() {
void WorldRenderer::SetCamPos(float camX, float camY) { void WorldRenderer::SetCamPos(float camX, float camY) {
m_CamPos = { camX, camY }; m_CamPos = { camX, camY };
m_Renderer->SetCamPos({ camX, 25, camY }); m_Renderer->SetCamLook({ camX, camY });
} }
void WorldRenderer::DetectClick() { void WorldRenderer::DetectClick() {