mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-20 09:29:44 +00:00
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:
parent
e1f6daabb7
commit
89bdadd6aa
30 changed files with 283 additions and 127 deletions
|
|
@ -5,6 +5,7 @@
|
|||
#include "graphics/core/DrawContext.hpp"
|
||||
#include "graphics/core/LineBatch.hpp"
|
||||
#include "maths/FrustumCulling.hpp"
|
||||
#include "maths/util.hpp"
|
||||
#include "objects/Entities.hpp"
|
||||
#include "objects/Entity.hpp"
|
||||
#include "objects/EntityDef.hpp"
|
||||
|
|
@ -46,10 +47,12 @@ static bool check_frustum(
|
|||
const Frustum& frustum,
|
||||
const Transform& transform,
|
||||
const Rigidbody& rigidbody,
|
||||
const rigging::Skeleton& skeleton
|
||||
const rigging::Skeleton& skeleton,
|
||||
const glm::ivec3& origin
|
||||
) {
|
||||
glm::vec3 pos = util::to_local(transform.pos, origin);
|
||||
glm::vec3 ext = calc_extent(transform, rigidbody, skeleton);
|
||||
return frustum.isBoxVisible(transform.pos - ext, transform.pos + ext);
|
||||
return frustum.isBoxVisible(pos - ext, pos + ext);
|
||||
}
|
||||
|
||||
EntitiesRenderer::EntitiesRenderer(const Assets& assets, Entities& entities)
|
||||
|
|
@ -57,8 +60,12 @@ EntitiesRenderer::EntitiesRenderer(const Assets& assets, Entities& entities)
|
|||
}
|
||||
|
||||
void EntitiesRenderer::render(
|
||||
ModelBatch& batch, const Frustum* frustum, entityid_t fpsEntity
|
||||
ModelBatch& batch,
|
||||
const glm::ivec3& origin,
|
||||
const Frustum* frustum,
|
||||
entityid_t fpsEntity
|
||||
) {
|
||||
batch.setLightsOffset(origin);
|
||||
for (const auto& [uid, entity] : entities.getEntities()) {
|
||||
if (!entity->getSkeleton() || uid == fpsEntity) {
|
||||
continue;
|
||||
|
|
@ -73,12 +80,17 @@ void EntitiesRenderer::render(
|
|||
if (rigConfig == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (frustum && !check_frustum(*frustum, transform, rigidbody, skeleton)) {
|
||||
if (frustum &&
|
||||
!check_frustum(*frustum, transform, rigidbody, skeleton, origin)) {
|
||||
if (!skeleton.boundsDirty || !skeleton.visible) {
|
||||
continue;
|
||||
}
|
||||
rigConfig->updateBounds(assets, skeleton, transform.rot, pos, size);
|
||||
if (!check_frustum(*frustum, transform, rigidbody, skeleton)) {
|
||||
rigConfig->updateBounds(
|
||||
assets, skeleton, transform.rot, pos, size, origin
|
||||
);
|
||||
if (!check_frustum(
|
||||
*frustum, transform, rigidbody, skeleton, origin
|
||||
)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
|
@ -90,6 +102,7 @@ void EntitiesRenderer::render(
|
|||
transform.rot,
|
||||
pos,
|
||||
size,
|
||||
origin,
|
||||
def.translucent
|
||||
);
|
||||
}
|
||||
|
|
@ -97,6 +110,7 @@ void EntitiesRenderer::render(
|
|||
|
||||
void EntitiesRenderer::renderDebug(
|
||||
LineBatch& batch,
|
||||
const glm::ivec3& origin,
|
||||
const Frustum* frustum,
|
||||
const DrawContext& pctx,
|
||||
entityid_t fpsEntity
|
||||
|
|
@ -113,20 +127,27 @@ void EntitiesRenderer::renderDebug(
|
|||
const auto& rigidbody = entity->getRigidbody();
|
||||
const auto& skeleton = *entity->getSkeleton();
|
||||
const auto& hitbox = rigidbody.hitbox;
|
||||
if (frustum && !check_frustum(*frustum, transform, rigidbody, skeleton)) {
|
||||
if (frustum &&
|
||||
!check_frustum(
|
||||
*frustum, transform, rigidbody, skeleton, origin
|
||||
)) {
|
||||
continue;
|
||||
}
|
||||
batch.box(
|
||||
hitbox.position, hitbox.getHalfSize() * 2.0f, glm::vec4(1.0f)
|
||||
util::to_local(hitbox.position, origin),
|
||||
hitbox.getHalfSize() * 2.0f,
|
||||
glm::vec4(1.0f)
|
||||
);
|
||||
glm::vec3 ext = calc_extent(transform, rigidbody, skeleton);
|
||||
batch.box(
|
||||
transform.pos, ext * 2.0f, glm::vec4(0.0f, 0.0f, 1.0f, 1.0f)
|
||||
util::to_local(transform.pos, origin),
|
||||
ext * 2.0f,
|
||||
glm::vec4(0.0f, 0.0f, 1.0f, 1.0f)
|
||||
);
|
||||
for (auto& sensor : rigidbody.sensors) {
|
||||
if (sensor.type != SensorType::AABB) continue;
|
||||
batch.box(
|
||||
sensor.calculated.aabb.center(),
|
||||
util::to_local(sensor.calculated.aabb.center(), origin),
|
||||
sensor.calculated.aabb.size(),
|
||||
glm::vec4(1.0f, 1.0f, 0.0f, 1.0f)
|
||||
);
|
||||
|
|
@ -149,7 +170,10 @@ void EntitiesRenderer::renderDebug(
|
|||
if (config == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (frustum && !check_frustum(*frustum, transform, rigidbody, skeleton)) {
|
||||
if (frustum &&
|
||||
!check_frustum(
|
||||
*frustum, transform, rigidbody, skeleton, origin
|
||||
)) {
|
||||
continue;
|
||||
}
|
||||
auto bone = config->getRoot();
|
||||
|
|
@ -159,7 +183,7 @@ void EntitiesRenderer::renderDebug(
|
|||
}
|
||||
|
||||
void EntitiesRenderer::updateCulling(
|
||||
const Frustum* frustum, entityid_t fpsEntity
|
||||
const glm::ivec3& origin, const Frustum* frustum, entityid_t fpsEntity
|
||||
) {
|
||||
for (const auto& [uid, entity] : entities.getEntities()) {
|
||||
if (!entity->getSkeleton()) {
|
||||
|
|
@ -171,7 +195,11 @@ void EntitiesRenderer::updateCulling(
|
|||
continue;
|
||||
}
|
||||
skeleton.culled = !check_frustum(
|
||||
*frustum, entity->getTransform(), entity->getRigidbody(), skeleton
|
||||
*frustum,
|
||||
entity->getTransform(),
|
||||
entity->getRigidbody(),
|
||||
skeleton,
|
||||
origin
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue