fix camera view rotation far from the world origin

Camera::getView built the view with lookAt(position, position + front):
the direction came back as a difference of large floats. Far from the
origin the view turned away from where the camera looks - at 16M blocks
by up to 24 degrees, at 30M the camera looked straight up or down
whatever its yaw (at pitch 0 the direction is a zero vector, the matrix
is NaN). The rotation is built from front itself now, then translated
by -position: the same matrix, without the subtraction.
This commit is contained in:
ostermine 2026-11-11 21:59:06 +03:00
parent 348c058c9b
commit e1f6daabb7
2 changed files with 35 additions and 9 deletions

31
test/window/Camera.cpp Normal file
View file

@ -0,0 +1,31 @@
#include <gtest/gtest.h>
#include <cmath>
#include <glm/ext.hpp>
#include "window/Camera.hpp"
TEST(Camera, ViewRotationFarFromOrigin) {
for (float coord : {0.0f, 1e5f, 1e6f, 3e7f}) {
for (bool rotated : {false, true}) {
Camera camera(glm::vec3(coord, 64.0f, coord), 1.0f);
if (rotated) {
camera.rotate(glm::radians(-20.0f), glm::radians(35.0f), 0.0f);
}
glm::mat4 rotation = camera.getView(false);
glm::mat4 view = camera.getView();
for (int col = 0; col < 4; col++) {
for (int row = 0; row < 4; row++) {
ASSERT_TRUE(std::isfinite(view[col][row]))
<< "at " << coord;
}
}
for (int col = 0; col < 3; col++) {
for (int row = 0; row < 3; row++) {
EXPECT_NEAR(rotation[col][row], view[col][row], 1e-5f)
<< "at " << coord << ", rotated " << rotated;
}
}
}
}
}