From 9f4d60036d10e5512e92ff1d9e1945dcdecb2f3a Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Mon, 15 Jun 2026 12:06:03 +0300 Subject: [PATCH] Working on camera positioning --- src/Environment.h | 2 +- src/Game.cpp | 23 +++++++++-------------- src/Location.cpp | 9 +++++++-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/Environment.h b/src/Environment.h index 10f18cd..47e91d0 100644 --- a/src/Environment.h +++ b/src/Environment.h @@ -15,7 +15,7 @@ namespace ZL { using std::max; #endif - constexpr float DEFAULT_ZOOM = 6.f; + constexpr float DEFAULT_ZOOM = 8.f; class Environment { public: diff --git a/src/Game.cpp b/src/Game.cpp index 057a10b..210c08a 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -22,17 +22,8 @@ namespace ZL { - /* - void set_Texture(Character& npc, const TextureDataStruct& texture) - { - auto tt = std::make_shared(texture); - npc.setTexture(tt); - } - void set_Texture(Character& npc, const std::string& meshName, const TextureDataStruct& texture) - { - auto tt = std::make_shared(texture); - npc.setTexture(meshName, tt); - }*/ + static const float zoomMin = 6.0f; + static const float zoomMax = 20.0f; #ifdef EMSCRIPTEN const char* CONST_ZIP_FILE = "resources.zip"; @@ -945,8 +936,11 @@ namespace ZL else if (event.wheel.y < 0) { Environment::zoom += zoomstep; } - if (Environment::zoom < zoomstep) { - Environment::zoom = zoomstep; + if (Environment::zoom < zoomMin) { + Environment::zoom = zoomMin; + } + if (Environment::zoom > zoomMax) { + Environment::zoom = zoomMax; } std::cout << "Current zoom: " << Environment::zoom << std::endl; // Tutorial step3 → step4: any mouse-wheel scroll counts as "zoom gesture". @@ -1225,8 +1219,9 @@ namespace ZL float newZoom = pinchStartZoom * (pinchStartDistance / dist); // Match the wheel-zoom lower bound so both gestures clamp the same way. - static const float zoomMin = 2.0f; + if (newZoom < zoomMin) newZoom = zoomMin; + if (newZoom > zoomMax) newZoom = zoomMax; Environment::zoom = newZoom; // Tutorial step3 → step4: detect a significant pinch-zoom (≥ 2 zoom units). diff --git a/src/Location.cpp b/src/Location.cpp index 688e4bc..7b77848 100644 --- a/src/Location.cpp +++ b/src/Location.cpp @@ -615,6 +615,7 @@ namespace ZL renderer.RotateMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(cameraAzimuth, Eigen::Vector3f::UnitY())).toRotationMatrix()); const Eigen::Vector3f& camTarget = player ? player->position : Eigen::Vector3f::Zero(); renderer.TranslateMatrix({ -camTarget.x(), -camTarget.y(), -camTarget.z() }); + renderer.TranslateMatrix({ 0, -1.f, 0 }); //glBindTexture(GL_TEXTURE_2D, roomTexture->getTexID()); //renderer.DrawVertexRenderStruct(roomMesh); @@ -780,11 +781,13 @@ namespace ZL renderer.LoadIdentity(); renderer.TranslateMatrix({ 0,0, -1.0f * Environment::zoom }); - + renderer.RotateMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(cameraInclination, Eigen::Vector3f::UnitX())).toRotationMatrix()); renderer.RotateMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(cameraAzimuth, Eigen::Vector3f::UnitY())).toRotationMatrix()); const Eigen::Vector3f& camTarget = player ? player->position : Eigen::Vector3f::Zero(); renderer.TranslateMatrix({ -camTarget.x(), -camTarget.y(), -camTarget.z() }); + renderer.TranslateMatrix({ 0, -1.f, 0 }); + // Capture the camera view matrix and compute uLightFromCamera cameraViewMatrix = renderer.GetCurrentModelViewMatrix(); @@ -894,6 +897,7 @@ namespace ZL renderer.RotateMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(cameraAzimuth, Eigen::Vector3f::UnitY())).toRotationMatrix()); const Eigen::Vector3f& camTarget = player ? player->position : Eigen::Vector3f::Zero(); renderer.TranslateMatrix({ -camTarget.x(), -camTarget.y(), -camTarget.z() }); + renderer.TranslateMatrix({ 0, -1.f, 0 }); renderer.RenderUniform1f("uAlpha", 1.0f); for (auto& [name, gameObj] : gameObjects) { @@ -1067,6 +1071,7 @@ namespace ZL renderer.RotateMatrix(Eigen::Quaternionf(Eigen::AngleAxisf(cameraAzimuth, Eigen::Vector3f::UnitY())).toRotationMatrix()); const Eigen::Vector3f& camTarget = player ? player->position : Eigen::Vector3f::Zero(); renderer.TranslateMatrix({ -camTarget.x(), -camTarget.y(), -camTarget.z() }); + renderer.TranslateMatrix({ 0, -1.f, 0 }); cameraViewMatrix = renderer.GetCurrentModelViewMatrix(); @@ -1563,7 +1568,7 @@ namespace ZL Eigen::Vector3f camForward(sinAzim * cosIncl, -sinIncl, -cosAzim * cosIncl); Eigen::Vector3f camUp(sinAzim * sinIncl, cosIncl, -cosAzim * sinIncl); const Eigen::Vector3f& playerPos = player ? player->position : Eigen::Vector3f::Zero(); - Eigen::Vector3f camPos = playerPos + Eigen::Vector3f(-sinAzim * cosIncl, sinIncl, cosAzim * cosIncl) * Environment::zoom; + Eigen::Vector3f camPos = playerPos + Eigen::Vector3f(-sinAzim * cosIncl, sinIncl, cosAzim * cosIncl) * Environment::zoom + Eigen::Vector3f(0,1,0); Eigen::Vector3f rayDir = (camForward + camRight * (ndcX * aspect * tanHalfFov) + camUp * (ndcY * tanHalfFov)).normalized();