mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-23 02:49:46 +00:00
use double precision for camera position and ray casts
A float position has a step of 6 cm at 1M blocks and of 2 m at 30M: the camera moved by jumps and rays started not where the camera is. worldvec3 (glm::dvec3) is the type of a position in the world. Camera::position, the start and the end of block and entity ray casts and the hit position of the player selection use it. Lua numbers are doubles already, so cameras.set_pos/get_pos, block.raycast, entities.raycast and world.raycast only stop rounding to float. Entities::rayCast intersects a hitbox in the coordinates relative to its position instead of a float AABB in the world space.
This commit is contained in:
parent
89bdadd6aa
commit
ad69663b2e
18 changed files with 86 additions and 71 deletions
|
|
@ -180,10 +180,10 @@ void Shadows::generateShadowsMap(
|
||||||
float shadowMapScale = 0.32f / (1 << glm::max(0, quality)) * scale;
|
float shadowMapScale = 0.32f / (1 << glm::max(0, quality)) * scale;
|
||||||
float shadowMapSize = resolution * shadowMapScale;
|
float shadowMapSize = resolution * shadowMapScale;
|
||||||
|
|
||||||
glm::vec3 basePos = glm::floor(camera.position / 4.0f) * 4.0f;
|
worldvec3 basePos = glm::floor(camera.position / 4.0) * 4.0;
|
||||||
glm::vec3 prevPos = shadowCamera.position;
|
worldvec3 prevPos = shadowCamera.position;
|
||||||
shadowCamera = Camera(
|
shadowCamera = Camera(
|
||||||
glm::distance2(prevPos, basePos) > 25.0f ? basePos : prevPos,
|
glm::distance2(prevPos, basePos) > 25.0 ? basePos : prevPos,
|
||||||
shadowMapSize
|
shadowMapSize
|
||||||
);
|
);
|
||||||
shadowCamera.near = 0.1f;
|
shadowCamera.near = 0.1f;
|
||||||
|
|
@ -206,9 +206,9 @@ void Shadows::generateShadowsMap(
|
||||||
glm::radians(0.0f)
|
glm::radians(0.0f)
|
||||||
);
|
);
|
||||||
|
|
||||||
shadowCamera.position -= shadowCamera.front * 500.0f;
|
shadowCamera.position -= worldvec3(shadowCamera.front * 500.0f);
|
||||||
shadowCamera.position += shadowCamera.up * 0.0f;
|
shadowCamera.position += worldvec3(shadowCamera.up * 0.0f);
|
||||||
shadowCamera.position += camera.front * 0.0f;
|
shadowCamera.position += worldvec3(camera.front * 0.0f);
|
||||||
|
|
||||||
auto view = shadowCamera.getView();
|
auto view = shadowCamera.getView();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -118,7 +118,7 @@ void DebugLinesRenderer::render(
|
||||||
|
|
||||||
if (showChunkBorders) {
|
if (showChunkBorders) {
|
||||||
linesShader.uniformMatrix("u_projview", camera.getProjView());
|
linesShader.uniformMatrix("u_projview", camera.getProjView());
|
||||||
glm::vec3 coord = camera.position;
|
worldvec3 coord = camera.position;
|
||||||
if (coord.x < 0) coord.x--;
|
if (coord.x < 0) coord.x--;
|
||||||
if (coord.z < 0) coord.z--;
|
if (coord.z < 0) coord.z--;
|
||||||
int cx = floordiv(static_cast<int>(coord.x), CHUNK_W);
|
int cx = floordiv(static_cast<int>(coord.x), CHUNK_W);
|
||||||
|
|
|
||||||
|
|
@ -120,7 +120,7 @@ void Decorator::updateAcoustics(const Camera& camera) {
|
||||||
glm::vec3 dir = { r * std::cos(phi), r * std::sin(phi), z };
|
glm::vec3 dir = { r * std::cos(phi), r * std::sin(phi), z };
|
||||||
|
|
||||||
dir = glm::normalize(dir);
|
dir = glm::normalize(dir);
|
||||||
glm::vec3 end;
|
worldvec3 end;
|
||||||
glm::ivec3 norm;
|
glm::ivec3 norm;
|
||||||
glm::ivec3 iend;
|
glm::ivec3 iend;
|
||||||
blocks_agent::RaycastSettings raycast {};
|
blocks_agent::RaycastSettings raycast {};
|
||||||
|
|
@ -128,7 +128,7 @@ void Decorator::updateAcoustics(const Camera& camera) {
|
||||||
if (vox == nullptr) {
|
if (vox == nullptr) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
auto distance = glm::distance(start, end);
|
float distance = glm::distance(start, end);
|
||||||
if (distance >= length * 0.98f) {
|
if (distance >= length * 0.98f) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -200,7 +200,7 @@ void CameraControl::update(
|
||||||
refreshPosition();
|
refreshPosition();
|
||||||
|
|
||||||
auto castRay = [](Player& player, Camera& camera, const glm::vec3& front)
|
auto castRay = [](Player& player, Camera& camera, const glm::vec3& front)
|
||||||
-> glm::vec3 {
|
-> worldvec3 {
|
||||||
auto blockEnd = player.chunks->rayCastToObstacle(camera.position, front, 3.0f);
|
auto blockEnd = player.chunks->rayCastToObstacle(camera.position, front, 3.0f);
|
||||||
|
|
||||||
Entities::RaycastSettings raycast {};
|
Entities::RaycastSettings raycast {};
|
||||||
|
|
@ -212,7 +212,7 @@ void CameraControl::update(
|
||||||
);
|
);
|
||||||
if (entityRay.has_value() &&
|
if (entityRay.has_value() &&
|
||||||
entityRay->distance < glm::distance(camera.position, blockEnd)) {
|
entityRay->distance < glm::distance(camera.position, blockEnd)) {
|
||||||
return (camera.position + front * entityRay->distance);
|
return camera.position + worldvec3(front * entityRay->distance);
|
||||||
} else {
|
} else {
|
||||||
return blockEnd;
|
return blockEnd;
|
||||||
}
|
}
|
||||||
|
|
@ -220,14 +220,14 @@ void CameraControl::update(
|
||||||
|
|
||||||
camera->updateVectors();
|
camera->updateVectors();
|
||||||
if (player.currentCamera == spCamera) {
|
if (player.currentCamera == spCamera) {
|
||||||
spCamera->position =
|
spCamera->position = castRay(player, *camera, camera->front) -
|
||||||
castRay(player, *camera, camera->front) - 0.4f * camera->front;
|
worldvec3(0.4f * camera->front);
|
||||||
spCamera->dir = -camera->dir;
|
spCamera->dir = -camera->dir;
|
||||||
spCamera->front = -camera->front;
|
spCamera->front = -camera->front;
|
||||||
spCamera->right = -camera->right;
|
spCamera->right = -camera->right;
|
||||||
} else if (player.currentCamera == tpCamera) {
|
} else if (player.currentCamera == tpCamera) {
|
||||||
tpCamera->position = castRay(player, *camera, camera->front * -1.0f) +
|
tpCamera->position = castRay(player, *camera, camera->front * -1.0f) +
|
||||||
0.4f * camera->front;
|
worldvec3(0.4f * camera->front);
|
||||||
tpCamera->dir = camera->dir;
|
tpCamera->dir = camera->dir;
|
||||||
tpCamera->front = camera->front;
|
tpCamera->front = camera->front;
|
||||||
tpCamera->right = camera->right;
|
tpCamera->right = camera->right;
|
||||||
|
|
@ -366,7 +366,7 @@ voxel* PlayerController::updateSelection(float maxDistance) {
|
||||||
auto camera = player.fpCamera.get();
|
auto camera = player.fpCamera.get();
|
||||||
auto& selection = player.selection;
|
auto& selection = player.selection;
|
||||||
|
|
||||||
glm::vec3 end;
|
worldvec3 end;
|
||||||
glm::ivec3 iend;
|
glm::ivec3 iend;
|
||||||
glm::ivec3 norm;
|
glm::ivec3 norm;
|
||||||
voxel* vox = chunks.rayCast(
|
voxel* vox = chunks.rayCast(
|
||||||
|
|
@ -393,7 +393,7 @@ voxel* PlayerController::updateSelection(float maxDistance) {
|
||||||
)) {
|
)) {
|
||||||
selection.entity = result->entity;
|
selection.entity = result->entity;
|
||||||
selection.hitPosition =
|
selection.hitPosition =
|
||||||
camera->position + camera->front * result->distance;
|
camera->position + worldvec3(camera->front * result->distance);
|
||||||
selection.position = selection.hitPosition;
|
selection.position = selection.hitPosition;
|
||||||
selection.actualPosition = selection.position;
|
selection.actualPosition = selection.position;
|
||||||
selection.normal = result->normal;
|
selection.normal = result->normal;
|
||||||
|
|
|
||||||
|
|
@ -548,7 +548,7 @@ static int l_destruct(lua::State* L) {
|
||||||
static int l_raycast(lua::State* L) {
|
static int l_raycast(lua::State* L) {
|
||||||
const auto& level = require_level();
|
const auto& level = require_level();
|
||||||
|
|
||||||
auto start = lua::tovec<3>(L, 1);
|
auto start = lua::tovec<3, double>(L, 1);
|
||||||
auto dir = lua::tovec<3>(L, 2);
|
auto dir = lua::tovec<3>(L, 2);
|
||||||
auto maxDistance = lua::tonumber(L, 3);
|
auto maxDistance = lua::tonumber(L, 3);
|
||||||
|
|
||||||
|
|
@ -576,7 +576,7 @@ static int l_raycast(lua::State* L) {
|
||||||
if (luaStackSize >= 6) {
|
if (luaStackSize >= 6) {
|
||||||
raycast.includeNonSelectable = lua::toboolean(L, 6);
|
raycast.includeNonSelectable = lua::toboolean(L, 6);
|
||||||
}
|
}
|
||||||
glm::vec3 end;
|
worldvec3 end;
|
||||||
glm::ivec3 normal;
|
glm::ivec3 normal;
|
||||||
glm::ivec3 iend;
|
glm::ivec3 iend;
|
||||||
|
|
||||||
|
|
@ -589,7 +589,7 @@ static int l_raycast(lua::State* L) {
|
||||||
lua::createtable(L, 0, 5);
|
lua::createtable(L, 0, 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
lua::pushvec3(L, end);
|
lua::pushvec(L, end);
|
||||||
lua::setfield(L, "endpoint");
|
lua::setfield(L, "endpoint");
|
||||||
|
|
||||||
lua::pushvec3(L, normal);
|
lua::pushvec3(L, normal);
|
||||||
|
|
|
||||||
|
|
@ -47,11 +47,11 @@ static int l_name(lua::State* L) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int getter_pos(lua::State* L, const Camera& camera) {
|
static int getter_pos(lua::State* L, const Camera& camera) {
|
||||||
return lua::pushvec3(L, camera.position);
|
return lua::pushvec(L, camera.position);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setter_pos(lua::State* L, Camera& camera, int idx) {
|
static void setter_pos(lua::State* L, Camera& camera, int idx) {
|
||||||
camera.position = lua::tovec<3>(L, idx);
|
camera.position = lua::tovec<3, double>(L, idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int getter_rot(lua::State* L, const Camera& camera) {
|
static int getter_rot(lua::State* L, const Camera& camera) {
|
||||||
|
|
@ -104,9 +104,11 @@ static int getter_up(lua::State* L, const Camera& camera) {
|
||||||
static int l_look_at(lua::State* L) {
|
static int l_look_at(lua::State* L) {
|
||||||
size_t index = static_cast<size_t>(lua::tointeger(L, 1));
|
size_t index = static_cast<size_t>(lua::tointeger(L, 1));
|
||||||
auto& camera = *require_level().cameras.at(index);
|
auto& camera = *require_level().cameras.at(index);
|
||||||
auto center = lua::tovec<3>(L, 2);
|
auto center = lua::tovec<3, double>(L, 2);
|
||||||
auto matrix = glm::inverse(
|
auto matrix = glm::inverse(
|
||||||
glm::lookAt(glm::vec3(), center - camera.position, glm::vec3(0, 1, 0))
|
glm::lookAt(
|
||||||
|
glm::vec3(), glm::vec3(center - camera.position), glm::vec3(0, 1, 0)
|
||||||
|
)
|
||||||
);
|
);
|
||||||
if (lua::isnumber(L, 3)) {
|
if (lua::isnumber(L, 3)) {
|
||||||
matrix = glm::mat4_cast(glm::slerp(
|
matrix = glm::mat4_cast(glm::slerp(
|
||||||
|
|
|
||||||
|
|
@ -160,9 +160,9 @@ static int l_push_ray(
|
||||||
lua::State* L,
|
lua::State* L,
|
||||||
const std::optional<Entities::RaycastResult>& ray,
|
const std::optional<Entities::RaycastResult>& ray,
|
||||||
blockid_t block,
|
blockid_t block,
|
||||||
const glm::vec3& start,
|
const worldvec3& start,
|
||||||
const glm::vec3& dir,
|
const glm::vec3& dir,
|
||||||
const glm::vec3& end,
|
const worldvec3& end,
|
||||||
const glm::ivec3& iend,
|
const glm::ivec3& iend,
|
||||||
const glm::ivec3& normal
|
const glm::ivec3& normal
|
||||||
) {
|
) {
|
||||||
|
|
@ -175,7 +175,7 @@ static int l_push_ray(
|
||||||
lua::createtable(L, 0, 6);
|
lua::createtable(L, 0, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
lua::pushvec3(L, ray ? (start + dir * ray->distance) : end);
|
lua::pushvec(L, ray ? (start + worldvec3(dir * ray->distance)) : end);
|
||||||
lua::setfield(L, "endpoint");
|
lua::setfield(L, "endpoint");
|
||||||
|
|
||||||
lua::pushvec3(L, ray ? ray->normal : normal);
|
lua::pushvec3(L, ray ? ray->normal : normal);
|
||||||
|
|
@ -202,14 +202,14 @@ static int l_push_ray(
|
||||||
|
|
||||||
static int l_raycast(
|
static int l_raycast(
|
||||||
lua::State* L,
|
lua::State* L,
|
||||||
const glm::vec3& start,
|
const worldvec3& start,
|
||||||
const glm::vec3& dir,
|
const glm::vec3& dir,
|
||||||
float maxDistance,
|
float maxDistance,
|
||||||
const blocks_agent::RaycastSettings& voxelRaycast,
|
const blocks_agent::RaycastSettings& voxelRaycast,
|
||||||
const Entities::RaycastSettings& entityRaycast,
|
const Entities::RaycastSettings& entityRaycast,
|
||||||
bool entities
|
bool entities
|
||||||
) {
|
) {
|
||||||
glm::vec3 end;
|
worldvec3 end;
|
||||||
glm::ivec3 normal;
|
glm::ivec3 normal;
|
||||||
glm::ivec3 iend;
|
glm::ivec3 iend;
|
||||||
|
|
||||||
|
|
@ -276,7 +276,8 @@ static int l_world_raycast(lua::State* L) {
|
||||||
std::set<blockid_t> filteredBlocks {};
|
std::set<blockid_t> filteredBlocks {};
|
||||||
std::set<entitydefid_t> filteredEntities {};
|
std::set<entitydefid_t> filteredEntities {};
|
||||||
|
|
||||||
glm::vec3 start, dir;
|
worldvec3 start;
|
||||||
|
glm::vec3 dir;
|
||||||
float maxDistance;
|
float maxDistance;
|
||||||
bool entities = true;
|
bool entities = true;
|
||||||
|
|
||||||
|
|
@ -285,7 +286,7 @@ static int l_world_raycast(lua::State* L) {
|
||||||
}
|
}
|
||||||
|
|
||||||
lua::requirefield(L, "start", 1);
|
lua::requirefield(L, "start", 1);
|
||||||
start = lua::tovec3(L, -1);
|
start = lua::tovec<3, double>(L, -1);
|
||||||
|
|
||||||
lua::requirefield(L, "dir", 1);
|
lua::requirefield(L, "dir", 1);
|
||||||
dir = lua::tovec3(L, -1);
|
dir = lua::tovec3(L, -1);
|
||||||
|
|
@ -341,7 +342,7 @@ static int l_raycast(lua::State* L) {
|
||||||
std::set<blockid_t> filteredBlocks {};
|
std::set<blockid_t> filteredBlocks {};
|
||||||
|
|
||||||
const int luaStackSize = lua::gettop(L);
|
const int luaStackSize = lua::gettop(L);
|
||||||
glm::vec3 start = lua::tovec<3>(L, 1);
|
worldvec3 start = lua::tovec<3, double>(L, 1);
|
||||||
glm::vec3 dir = lua::tovec<3>(L, 2);
|
glm::vec3 dir = lua::tovec<3>(L, 2);
|
||||||
float maxDistance = lua::tonumber(L, 3);
|
float maxDistance = lua::tonumber(L, 3);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "typedefs.hpp"
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
|
@ -17,8 +19,8 @@ namespace util {
|
||||||
|
|
||||||
constexpr inline float EPSILON = 1e-6f;
|
constexpr inline float EPSILON = 1e-6f;
|
||||||
|
|
||||||
inline glm::vec3 to_local(const glm::vec3& pos, const glm::ivec3& origin) {
|
inline glm::vec3 to_local(const worldvec3& pos, const glm::ivec3& origin) {
|
||||||
return glm::dvec3(pos) - glm::dvec3(origin);
|
return glm::vec3(pos - worldvec3(origin));
|
||||||
}
|
}
|
||||||
|
|
||||||
class PseudoRandom {
|
class PseudoRandom {
|
||||||
|
|
|
||||||
|
|
@ -159,7 +159,7 @@ void Entities::loadEntity(const dv::value& map, Entity& entity) {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<Entities::RaycastResult> Entities::rayCast(
|
std::optional<Entities::RaycastResult> Entities::rayCast(
|
||||||
glm::vec3 start,
|
worldvec3 start,
|
||||||
glm::vec3 dir,
|
glm::vec3 dir,
|
||||||
float maxDistance,
|
float maxDistance,
|
||||||
const RaycastSettings& settings
|
const RaycastSettings& settings
|
||||||
|
|
@ -187,7 +187,11 @@ std::optional<Entities::RaycastResult> Entities::rayCast(
|
||||||
glm::ivec3 normal;
|
glm::ivec3 normal;
|
||||||
double distance;
|
double distance;
|
||||||
if (ray.intersectAABB(
|
if (ray.intersectAABB(
|
||||||
glm::vec3(), hitbox.getAABB(), maxDistance, normal, distance
|
hitbox.position,
|
||||||
|
AABB(-hitbox.halfsize, hitbox.halfsize),
|
||||||
|
maxDistance,
|
||||||
|
normal,
|
||||||
|
distance
|
||||||
) > RayRelation::None) {
|
) > RayRelation::None) {
|
||||||
foundUID = eid.uid;
|
foundUID = eid.uid;
|
||||||
foundNormal = normal;
|
foundNormal = normal;
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,7 @@ public:
|
||||||
/// @param maxDistance Max ray length
|
/// @param maxDistance Max ray length
|
||||||
/// @return An optional structure containing entity, normal and distance
|
/// @return An optional structure containing entity, normal and distance
|
||||||
std::optional<RaycastResult> rayCast(
|
std::optional<RaycastResult> rayCast(
|
||||||
glm::vec3 start,
|
worldvec3 start,
|
||||||
glm::vec3 dir,
|
glm::vec3 dir,
|
||||||
float maxDistance,
|
float maxDistance,
|
||||||
const RaycastSettings& settings
|
const RaycastSettings& settings
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ struct CursorSelection {
|
||||||
glm::ivec3 position {};
|
glm::ivec3 position {};
|
||||||
glm::ivec3 actualPosition {};
|
glm::ivec3 actualPosition {};
|
||||||
glm::ivec3 normal {};
|
glm::ivec3 normal {};
|
||||||
glm::vec3 hitPosition;
|
worldvec3 hitPosition;
|
||||||
entityid_t entity = ENTITY_NONE;
|
entityid_t entity = ENTITY_NONE;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <glm/fwd.hpp>
|
||||||
|
|
||||||
using scriptenv = std::shared_ptr<int>;
|
using scriptenv = std::shared_ptr<int>;
|
||||||
|
|
||||||
|
|
@ -12,6 +13,9 @@ using integer_t = int64_t;
|
||||||
/// @brief dynamic floating-point type (64 bit floating point)
|
/// @brief dynamic floating-point type (64 bit floating point)
|
||||||
using number_t = double;
|
using number_t = double;
|
||||||
|
|
||||||
|
/// @brief position in the world (64 bit floating point components)
|
||||||
|
using worldvec3 = glm::dvec3;
|
||||||
|
|
||||||
using uint = unsigned int;
|
using uint = unsigned int;
|
||||||
using u64id_t = uint64_t;
|
using u64id_t = uint64_t;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -211,10 +211,10 @@ void Chunks::set(
|
||||||
}
|
}
|
||||||
|
|
||||||
voxel* Chunks::rayCast(
|
voxel* Chunks::rayCast(
|
||||||
const glm::vec3& start,
|
const worldvec3& start,
|
||||||
const glm::vec3& dir,
|
const glm::vec3& dir,
|
||||||
float maxDist,
|
float maxDist,
|
||||||
glm::vec3& end,
|
worldvec3& end,
|
||||||
glm::ivec3& norm,
|
glm::ivec3& norm,
|
||||||
glm::ivec3& iend,
|
glm::ivec3& iend,
|
||||||
const blocks_agent::RaycastSettings& settings
|
const blocks_agent::RaycastSettings& settings
|
||||||
|
|
@ -224,12 +224,12 @@ voxel* Chunks::rayCast(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 Chunks::rayCastToObstacle(
|
worldvec3 Chunks::rayCastToObstacle(
|
||||||
const glm::vec3& start, const glm::vec3& dir, float maxDist
|
const worldvec3& start, const glm::vec3& dir, float maxDist
|
||||||
) const {
|
) const {
|
||||||
const float px = start.x;
|
const double px = start.x;
|
||||||
const float py = start.y;
|
const double py = start.y;
|
||||||
const float pz = start.z;
|
const double pz = start.z;
|
||||||
|
|
||||||
float dx = dir.x;
|
float dx = dir.x;
|
||||||
float dy = dir.y;
|
float dy = dir.y;
|
||||||
|
|
@ -286,11 +286,11 @@ glm::vec3 Chunks::rayCastToObstacle(
|
||||||
norm,
|
norm,
|
||||||
distance
|
distance
|
||||||
) > RayRelation::None) {
|
) > RayRelation::None) {
|
||||||
return start + (dir * glm::vec3(distance));
|
return start + worldvec3(dir) * distance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return glm::vec3(px + t * dx, py + t * dy, pz + t * dz);
|
return worldvec3(px + t * dx, py + t * dy, pz + t * dz);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -316,7 +316,7 @@ glm::vec3 Chunks::rayCastToObstacle(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return glm::vec3(px + maxDist * dx, py + maxDist * dy, pz + maxDist * dz);
|
return worldvec3(px + maxDist * dx, py + maxDist * dy, pz + maxDist * dz);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunks::setCenter(int32_t x, int32_t z) {
|
void Chunks::setCenter(int32_t x, int32_t z) {
|
||||||
|
|
|
||||||
|
|
@ -110,17 +110,17 @@ public:
|
||||||
void setRotation(int32_t x, int32_t y, int32_t z, uint8_t rotation);
|
void setRotation(int32_t x, int32_t y, int32_t z, uint8_t rotation);
|
||||||
|
|
||||||
voxel* rayCast(
|
voxel* rayCast(
|
||||||
const glm::vec3& start,
|
const worldvec3& start,
|
||||||
const glm::vec3& dir,
|
const glm::vec3& dir,
|
||||||
float maxLength,
|
float maxLength,
|
||||||
glm::vec3& end,
|
worldvec3& end,
|
||||||
glm::ivec3& norm,
|
glm::ivec3& norm,
|
||||||
glm::ivec3& iend,
|
glm::ivec3& iend,
|
||||||
const blocks_agent::RaycastSettings& settings
|
const blocks_agent::RaycastSettings& settings
|
||||||
) const;
|
) const;
|
||||||
|
|
||||||
glm::vec3 rayCastToObstacle(
|
worldvec3 rayCastToObstacle(
|
||||||
const glm::vec3& start, const glm::vec3& dir, float maxDist
|
const worldvec3& start, const glm::vec3& dir, float maxDist
|
||||||
) const;
|
) const;
|
||||||
|
|
||||||
const AABB* isObstacleAt(float x, float y, float z) const;
|
const AABB* isObstacleAt(float x, float y, float z) const;
|
||||||
|
|
|
||||||
|
|
@ -217,18 +217,18 @@ bool blocks_agent::set(
|
||||||
template <class Storage>
|
template <class Storage>
|
||||||
static inline voxel* raycast_blocks(
|
static inline voxel* raycast_blocks(
|
||||||
const Storage& chunks,
|
const Storage& chunks,
|
||||||
const glm::vec3& start,
|
const worldvec3& start,
|
||||||
const glm::vec3& dir,
|
const glm::vec3& dir,
|
||||||
float maxDist,
|
float maxDist,
|
||||||
glm::vec3& end,
|
worldvec3& end,
|
||||||
glm::ivec3& norm,
|
glm::ivec3& norm,
|
||||||
glm::ivec3& iend,
|
glm::ivec3& iend,
|
||||||
const RaycastSettings& settings
|
const RaycastSettings& settings
|
||||||
) {
|
) {
|
||||||
const auto& blocks = chunks.getContentIndices().blocks;
|
const auto& blocks = chunks.getContentIndices().blocks;
|
||||||
float px = start.x;
|
double px = start.x;
|
||||||
float py = start.y;
|
double py = start.y;
|
||||||
float pz = start.z;
|
double pz = start.z;
|
||||||
|
|
||||||
float dx = dir.x;
|
float dx = dir.x;
|
||||||
float dy = dir.y;
|
float dy = dir.y;
|
||||||
|
|
@ -271,7 +271,7 @@ static inline voxel* raycast_blocks(
|
||||||
(def.selectable || includeNonSelectable) &&
|
(def.selectable || includeNonSelectable) &&
|
||||||
((filter == nullptr || filter->find(def.rt.id) == filter->end()) ==
|
((filter == nullptr || filter->find(def.rt.id) == filter->end()) ==
|
||||||
settings.blocksFilterExcludeMode)) {
|
settings.blocksFilterExcludeMode)) {
|
||||||
end = start + t * dir;
|
end = start + worldvec3(t * dir);
|
||||||
iend = {ix, iy, iz};
|
iend = {ix, iy, iz};
|
||||||
|
|
||||||
if (!def.rt.solid) {
|
if (!def.rt.solid) {
|
||||||
|
|
@ -301,7 +301,7 @@ static inline voxel* raycast_blocks(
|
||||||
hit = true;
|
hit = true;
|
||||||
distance = boxDistance;
|
distance = boxDistance;
|
||||||
norm = boxNorm;
|
norm = boxNorm;
|
||||||
end = start + (dir * glm::vec3(distance));
|
end = start + worldvec3(dir) * distance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -342,17 +342,17 @@ static inline voxel* raycast_blocks(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
iend = {ix, iy, iz};
|
iend = {ix, iy, iz};
|
||||||
end = start + t * dir;
|
end = start + worldvec3(t * dir);
|
||||||
norm = {0, 0, 0};
|
norm = {0, 0, 0};
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
voxel* blocks_agent::raycast(
|
voxel* blocks_agent::raycast(
|
||||||
const Chunks& chunks,
|
const Chunks& chunks,
|
||||||
const glm::vec3& start,
|
const worldvec3& start,
|
||||||
const glm::vec3& dir,
|
const glm::vec3& dir,
|
||||||
float maxDist,
|
float maxDist,
|
||||||
glm::vec3& end,
|
worldvec3& end,
|
||||||
glm::ivec3& norm,
|
glm::ivec3& norm,
|
||||||
glm::ivec3& iend,
|
glm::ivec3& iend,
|
||||||
const RaycastSettings& settings
|
const RaycastSettings& settings
|
||||||
|
|
@ -362,10 +362,10 @@ voxel* blocks_agent::raycast(
|
||||||
|
|
||||||
voxel* blocks_agent::raycast(
|
voxel* blocks_agent::raycast(
|
||||||
const GlobalChunks& chunks,
|
const GlobalChunks& chunks,
|
||||||
const glm::vec3& start,
|
const worldvec3& start,
|
||||||
const glm::vec3& dir,
|
const glm::vec3& dir,
|
||||||
float maxDist,
|
float maxDist,
|
||||||
glm::vec3& end,
|
worldvec3& end,
|
||||||
glm::ivec3& norm,
|
glm::ivec3& norm,
|
||||||
glm::ivec3& iend,
|
glm::ivec3& iend,
|
||||||
const RaycastSettings& settings
|
const RaycastSettings& settings
|
||||||
|
|
|
||||||
|
|
@ -447,10 +447,10 @@ struct RaycastSettings {
|
||||||
/// @return voxel pointer or nullptr
|
/// @return voxel pointer or nullptr
|
||||||
voxel* raycast(
|
voxel* raycast(
|
||||||
const Chunks& chunks,
|
const Chunks& chunks,
|
||||||
const glm::vec3& start,
|
const worldvec3& start,
|
||||||
const glm::vec3& dir,
|
const glm::vec3& dir,
|
||||||
float maxDist,
|
float maxDist,
|
||||||
glm::vec3& end,
|
worldvec3& end,
|
||||||
glm::ivec3& norm,
|
glm::ivec3& norm,
|
||||||
glm::ivec3& iend,
|
glm::ivec3& iend,
|
||||||
const RaycastSettings& settings
|
const RaycastSettings& settings
|
||||||
|
|
@ -469,10 +469,10 @@ voxel* raycast(
|
||||||
/// @return voxel pointer or nullptr
|
/// @return voxel pointer or nullptr
|
||||||
voxel* raycast(
|
voxel* raycast(
|
||||||
const GlobalChunks& chunks,
|
const GlobalChunks& chunks,
|
||||||
const glm::vec3& start,
|
const worldvec3& start,
|
||||||
const glm::vec3& dir,
|
const glm::vec3& dir,
|
||||||
float maxDist,
|
float maxDist,
|
||||||
glm::vec3& end,
|
worldvec3& end,
|
||||||
glm::ivec3& norm,
|
glm::ivec3& norm,
|
||||||
glm::ivec3& iend,
|
glm::ivec3& iend,
|
||||||
const RaycastSettings& settings
|
const RaycastSettings& settings
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
inline constexpr double ORIGIN_STEP = 1024.0;
|
inline constexpr double ORIGIN_STEP = 1024.0;
|
||||||
|
|
||||||
Camera::Camera(glm::vec3 position, float fov) : fov(fov), position(position) {
|
Camera::Camera(worldvec3 position, float fov) : fov(fov), position(position) {
|
||||||
updateVectors();
|
updateVectors();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -51,7 +51,7 @@ glm::ivec3 Camera::getOrigin() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 Camera::getLocalPosition() const {
|
glm::vec3 Camera::getLocalPosition() const {
|
||||||
return glm::dvec3(position) - glm::dvec3(getOrigin());
|
return glm::vec3(position - worldvec3(getOrigin()));
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::mat4 Camera::getView(bool pos) const {
|
glm::mat4 Camera::getView(bool pos) const {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "typedefs.hpp"
|
||||||
|
|
||||||
#define GLM_FORCE_RADIANS
|
#define GLM_FORCE_RADIANS
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
|
@ -12,7 +14,7 @@ public:
|
||||||
glm::vec3 right {};
|
glm::vec3 right {};
|
||||||
glm::vec3 dir {};
|
glm::vec3 dir {};
|
||||||
|
|
||||||
glm::vec3 position {};
|
worldvec3 position {};
|
||||||
|
|
||||||
float zoom = 1.0f;
|
float zoom = 1.0f;
|
||||||
glm::mat4 rotation {1.0f};
|
glm::mat4 rotation {1.0f};
|
||||||
|
|
@ -27,7 +29,7 @@ public:
|
||||||
Camera() {
|
Camera() {
|
||||||
updateVectors();
|
updateVectors();
|
||||||
}
|
}
|
||||||
Camera(glm::vec3 position, float fov);
|
Camera(worldvec3 position, float fov);
|
||||||
|
|
||||||
void updateVectors();
|
void updateVectors();
|
||||||
void rotate(float x, float y, float z);
|
void rotate(float x, float y, float z);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue