render the world relative to an origin near the camera

World coordinates were passed to GPU as floats: u_model of a chunk and
of a model had the world translation, vertices of translucent blocks,
particles, precipitation, block wrappers, lines and 3D texts were built
in the world space, and the camera position was subtracted in shaders.
The float step grows with the distance from the world origin (6 cm at
1M blocks, 2 m at 30M): blocks got skewed, then stuck together.

Camera::getOrigin() is an integer point near the camera (X and Z are
rounded to 1024 blocks, Y is 0). Camera::getView() is relative to it,
and every position passed to GPU is "world - origin" calculated on CPU,
so the shaders keep working with small numbers and do not change:
u_cameraPos, a_modelpos, u_inverseView and the shadows matrices are in
the same space as before, shifted by the origin. Matrices of a shadow
map are converted from the origin of its camera to the origin of the
player camera. The clouds shader gets the origin wrapped by the period
of its waves, the waves stay bound to the world.

Vertices of translucent blocks are local to the chunk now, as vertices
of opaque ones, and drawn with u_model of the chunk.
This commit is contained in:
ostermine 2026-11-12 00:48:30 +03:00
parent e1f6daabb7
commit 89bdadd6aa
30 changed files with 283 additions and 127 deletions

View file

@ -29,3 +29,24 @@ TEST(Camera, ViewRotationFarFromOrigin) {
}
}
}
TEST(Camera, ViewRelativeToOrigin) {
for (float coord : {0.0f, 500.0f, -1e5f, 1e6f, 3e7f}) {
Camera camera(glm::vec3(coord, 64.0f, -coord), 1.0f);
camera.rotate(glm::radians(-20.0f), glm::radians(35.0f), 0.0f);
glm::dvec3 origin = camera.getOrigin();
glm::vec3 local = camera.getLocalPosition();
EXPECT_LE(glm::abs(local.x), 512.0f) << "at " << coord;
EXPECT_LE(glm::abs(local.z), 512.0f) << "at " << coord;
EXPECT_EQ(glm::dvec3(camera.position), origin + glm::dvec3(local));
glm::dvec3 point =
glm::dvec3(camera.position) + glm::dvec3(camera.front) * 10.0;
glm::vec4 viewPoint =
camera.getView() * glm::vec4(glm::vec3(point - origin), 1.0f);
EXPECT_NEAR(viewPoint.x, 0.0f, 1e-3f) << "at " << coord;
EXPECT_NEAR(viewPoint.y, 0.0f, 1e-3f) << "at " << coord;
EXPECT_NEAR(viewPoint.z, -10.0f, 1e-3f) << "at " << coord;
}
}