small fixes

This commit is contained in:
Ara 2023-12-09 22:36:36 +06:00
parent c5ab5996a3
commit f5b5130e8b
8 changed files with 85 additions and 134 deletions

View File

@ -93,8 +93,8 @@ namespace gui {
void back(); void back();
void clearHistory(); void clearHistory();
void reset(); void reset();
//comment or erase pragma if you hate notes..
#pragma message("gui::PagesControl::current() returns Page by-value! (can initiate unexpected behaviour)") //FIXME "gui::PagesControl::current() returns Page by-value! (can initiate unexpected behaviour)")
Page current(); Page current();
}; };
} }

View File

@ -110,7 +110,7 @@ void CameraControl::update(PlayerInput& input, float delta) {
vec3 PlayerController::selectedBlockPosition; vec3 PlayerController::selectedBlockPosition;
vec3 PlayerController::selectedPointPosition; vec3 PlayerController::selectedPointPosition;
vec3 PlayerController::selectedBlockNormal; ivec3 PlayerController::selectedBlockNormal;
int PlayerController::selectedBlockId = -1; int PlayerController::selectedBlockId = -1;
int PlayerController::selectedBlockStates = 0; int PlayerController::selectedBlockStates = 0;
@ -192,7 +192,8 @@ void PlayerController::updateInteraction(){
Lighting* lighting = level->lighting; Lighting* lighting = level->lighting;
Camera* camera = player->camera; Camera* camera = player->camera;
vec3 end; vec3 end;
vec3 norm; ivec3 iend;
ivec3 norm;
bool xkey = Events::pressed(keycode::X); bool xkey = Events::pressed(keycode::X);
bool lclick = Events::jactive(BIND_PLAYER_ATTACK) || bool lclick = Events::jactive(BIND_PLAYER_ATTACK) ||
@ -203,7 +204,7 @@ void PlayerController::updateInteraction(){
if (xkey) { if (xkey) {
maxDistance *= 20.0f; maxDistance *= 20.0f;
} }
vec3 iend;
voxel* vox = chunks->rayCast(camera->position, voxel* vox = chunks->rayCast(camera->position,
camera->front, camera->front,
maxDistance, maxDistance,
@ -215,9 +216,9 @@ void PlayerController::updateInteraction(){
selectedBlockPosition = iend; selectedBlockPosition = iend;
selectedPointPosition = end; selectedPointPosition = end;
selectedBlockNormal = norm; selectedBlockNormal = norm;
int x = (int)iend.x; int x = iend.x;
int y = (int)iend.y; int y = iend.y;
int z = (int)iend.z; int z = iend.z;
uint8_t states = 0; uint8_t states = 0;
Block* def = contentIds->getBlockDef(player->choosenBlock); Block* def = contentIds->getBlockDef(player->choosenBlock);
@ -250,9 +251,9 @@ void PlayerController::updateInteraction(){
} }
if (rclick){ if (rclick){
if (block->model != BlockModel::xsprite){ if (block->model != BlockModel::xsprite){
x = (int)(iend.x)+(int)(norm.x); x = (iend.x)+(norm.x);
y = (int)(iend.y)+(int)(norm.y); y = (iend.y)+(norm.y);
z = (int)(iend.z)+(int)(norm.z); z = (iend.z)+(norm.z);
} }
vox = chunks->get(x, y, z); vox = chunks->get(x, y, z);
if (vox && (block = contentIds->getBlockDef(vox->id))->replaceable) { if (vox && (block = contentIds->getBlockDef(vox->id))->replaceable) {

View File

@ -37,7 +37,7 @@ class PlayerController {
void updateInteraction(); void updateInteraction();
public: public:
static glm::vec3 selectedBlockPosition; static glm::vec3 selectedBlockPosition;
static glm::vec3 selectedBlockNormal; static glm::ivec3 selectedBlockNormal;
static glm::vec3 selectedPointPosition; static glm::vec3 selectedPointPosition;
static int selectedBlockId; static int selectedBlockId;
static int selectedBlockStates; static int selectedBlockStates;

View File

@ -4,22 +4,18 @@
#include "glm/glm.hpp" #include "glm/glm.hpp"
constexpr std::array<AAFaceKind,AABBFACES_COUNT> AABBFACES_KINDS_ORDER = {
AAFaceKind::Xperp, AAFaceKind::Xperp,
AAFaceKind::Yperp, AAFaceKind::Yperp,
AAFaceKind::Zperp, AAFaceKind::Zperp};
std::unordered_map<rayvec3, AABBFaces> Rays::raysBoxCache_ = {}; std::unordered_map<rayvec3, AABBFaces> Rays::raysBoxCache_ = {};
const rayvec3 X_AXIS = rayvec3(1,0,0), Y_AXIS = rayvec3(0,1,0), Z_AXIS = rayvec3(0,0,1); const rayvec3 X_AXIS = rayvec3(1,0,0), Y_AXIS = rayvec3(0,1,0), Z_AXIS = rayvec3(0,0,1);
//make edges from AABB //make faces from AABB
AABBFaces::AABBFaces(const rayvec3& parentBoxPos, const AABB& parentBox){ AABBFaces::AABBFaces(const rayvec3& parentBoxPos, const AABB& parentBox){
rayvec3 pbMin = parentBox.min(), rayvec3 pbMin = parentBox.min(), // every face is min-point and opposite corner point
pbMax = parentBox.max(), pbMax = parentBox.max(),
pbRealPos = parentBoxPos + pbMin; pbRealPos = parentBoxPos + pbMin;
rayvec2 yzMax = rayvec2(parentBoxPos.y + pbMax.y, parentBoxPos.z + pbMax.z ), rayvec2 yzMax = rayvec2(parentBoxPos.y + pbMax.y, parentBoxPos.z + pbMax.z ),
xzMax = rayvec2(parentBoxPos.x + pbMax.x, parentBoxPos.z + pbMax.z ), xzMax = rayvec2(parentBoxPos.x + pbMax.x, parentBoxPos.z + pbMax.z ),
xyMax = rayvec2(parentBoxPos.x + pbMax.x, parentBoxPos.y + pbMax.y ); xyMax = rayvec2(parentBoxPos.x + pbMax.x, parentBoxPos.y + pbMax.y );
faces[0] = {pbRealPos, yzMax}; faces[0] = {pbRealPos, yzMax}; //in order of AABBFaces::KINDS_ORDER!
faces[1] = {parentBoxPos + rayvec3(pbMax.x, pbMin.y, pbMin.z), yzMax}; faces[1] = {parentBoxPos + rayvec3(pbMax.x, pbMin.y, pbMin.z), yzMax};
@ -41,9 +37,6 @@ RayRelation Rays::rayIntersectAAFace<AAFaceKind::Xperp>(
rayvec3& intersectPoint_ret rayvec3& intersectPoint_ret
){ ){
if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8){ //precision if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8){ //precision
if (rayOrigin.x == faceMin.x) {
return RayRelation::Embed; // there can be check of hit, but not necessarily
}
return RayRelation::Parallel; return RayRelation::Parallel;
} }
@ -80,9 +73,6 @@ RayRelation Rays::rayIntersectAAFace<AAFaceKind::Yperp>(
rayvec3& intersectPoint_ret rayvec3& intersectPoint_ret
){ ){
if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8){ //precision if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8){ //precision
if (rayOrigin.y == faceMin.y) {
return RayRelation::Embed; // there can be check of hit, but not necessarily
}
return RayRelation::Parallel; return RayRelation::Parallel;
} }
@ -119,9 +109,6 @@ RayRelation Rays::rayIntersectAAFace<AAFaceKind::Zperp>(
rayvec3& intersectPoint_ret rayvec3& intersectPoint_ret
){ ){
if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8){ //precision if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8){ //precision
if (rayOrigin.z == faceMin.z) {
return RayRelation::Embed; // there can be check of hit, but not necessarily
}
return RayRelation::Parallel; return RayRelation::Parallel;
} }
@ -154,12 +141,10 @@ RayRelation Rays::isRayIntersectsAAFace<AAFaceKind::Xperp>(
const rayvec3& rayOrigin, const rayvec3& rayOrigin,
const rayvec3& rayDir, const rayvec3& rayDir,
const rayvec3& faceMin, const rayvec3& faceMin,
const rayvec2& faceOppositeCorner const rayvec2& faceOppositeCorner,
glm::ivec3& normal_ret
){ ){
if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8){ //precision of "parallelity" if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8){ //precision of "parallelity"
if (rayOrigin.x == faceMin.x) {
return RayRelation::Embed; // there can be check of hit, but not necessarily
}
return RayRelation::Parallel; return RayRelation::Parallel;
} }
@ -192,12 +177,10 @@ RayRelation Rays::isRayIntersectsAAFace<AAFaceKind::Yperp>(
const rayvec3& rayOrigin, const rayvec3& rayOrigin,
const rayvec3& rayDir, const rayvec3& rayDir,
const rayvec3& faceMin, const rayvec3& faceMin,
const rayvec2& faceOppositeCorner const rayvec2& faceOppositeCorner,
glm::ivec3& normal_ret
){ ){
if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8){ //precision if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8){ //precision of "parallelity"
if (rayOrigin.y == faceMin.y) {
return RayRelation::Embed; // there can be check of hit, but not necessarily
}
return RayRelation::Parallel; return RayRelation::Parallel;
} }
@ -230,12 +213,10 @@ RayRelation Rays::isRayIntersectsAAFace<AAFaceKind::Zperp>(
const rayvec3& rayOrigin, const rayvec3& rayOrigin,
const rayvec3& rayDir, const rayvec3& rayDir,
const rayvec3& faceMin, const rayvec3& faceMin,
const rayvec2& faceOppositeCorner const rayvec2& faceOppositeCorner,
glm::ivec3& normal_ret
){ ){
if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8){ //precision if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8){ //precision of "parallelity"
if (rayOrigin.z == faceMin.z) {
return RayRelation::Embed; // there can be check of hit, but not necessarily
}
return RayRelation::Parallel; return RayRelation::Parallel;
} }
@ -295,39 +276,51 @@ RayRelation Rays::rayIntersectAABBFaces(
const AABBFaces& boxFaces, const AABBFaces& boxFaces,
rayvec3& pointIn_ret, rayvec3& pointIn_ret,
rayvec3& pointOut_ret, rayvec3& pointOut_ret,
glm::ivec3& normal_ret){//TODO: refs update glm::ivec3& normal_ret){//TODO: points returning
RayRelation rel; RayRelation rel;
unsigned char intersectedCount = 0; unsigned char intersectedCount = 0; //this code is very uncomfortable, DONT LEARN IT!
rel = isRayIntersectsAAFace<AABBFACES_KINDS_ORDER[0]>( rel = isRayIntersectsAAFace<AABBFaces::KINDS_ORDER[0]>(
rayOrigin, rayDir, boxFaces.faces[0].first, boxFaces.faces[0].second rayOrigin, rayDir, boxFaces.faces[0].first, boxFaces.faces[0].second, normal_ret
); );
intersectedCount+= (bool)rel; if (rel > RayRelation::None){
++intersectedCount;
}
rel = isRayIntersectsAAFace<AABBFACES_KINDS_ORDER[1]>( rel = isRayIntersectsAAFace<AABBFaces::KINDS_ORDER[1]>(
rayOrigin, rayDir, boxFaces.faces[1].first, boxFaces.faces[1].second rayOrigin, rayDir, boxFaces.faces[1].first, boxFaces.faces[1].second, normal_ret
); );
intersectedCount+= (bool)rel; if (rel > RayRelation::None){
++intersectedCount;
}
rel = isRayIntersectsAAFace<AABBFACES_KINDS_ORDER[2]>( rel = isRayIntersectsAAFace<AABBFaces::KINDS_ORDER[2]>(
rayOrigin, rayDir, boxFaces.faces[2].first, boxFaces.faces[2].second rayOrigin, rayDir, boxFaces.faces[2].first, boxFaces.faces[2].second, normal_ret
); );
intersectedCount+= (bool)rel; if (rel > RayRelation::None){
++intersectedCount;
}
rel = isRayIntersectsAAFace<AABBFACES_KINDS_ORDER[3]>( rel = isRayIntersectsAAFace<AABBFaces::KINDS_ORDER[3]>(
rayOrigin, rayDir, boxFaces.faces[3].first, boxFaces.faces[3].second rayOrigin, rayDir, boxFaces.faces[3].first, boxFaces.faces[3].second, normal_ret
); );
intersectedCount+= (bool)rel; if (rel > RayRelation::None){
++intersectedCount;
}
rel = isRayIntersectsAAFace<AABBFACES_KINDS_ORDER[4]>( rel = isRayIntersectsAAFace<AABBFaces::KINDS_ORDER[4]>(
rayOrigin, rayDir, boxFaces.faces[4].first, boxFaces.faces[4].second rayOrigin, rayDir, boxFaces.faces[4].first, boxFaces.faces[4].second, normal_ret
); );
intersectedCount+= (bool)rel; if (rel > RayRelation::None){
++intersectedCount;
rel = isRayIntersectsAAFace<AABBFACES_KINDS_ORDER[5]>( }
rayOrigin, rayDir, boxFaces.faces[5].first, boxFaces.faces[5].second
);
intersectedCount+= (bool)rel;
rel = isRayIntersectsAAFace<AABBFaces::KINDS_ORDER[5]>(
rayOrigin, rayDir, boxFaces.faces[5].first, boxFaces.faces[5].second, normal_ret
);
if (rel > RayRelation::None){
++intersectedCount;
}
if (intersectedCount > 0) return RayRelation::Intersect; if (intersectedCount > 0) return RayRelation::Intersect;
return RayRelation::None; return RayRelation::None;
} }

View File

@ -23,6 +23,11 @@ class AABBFaces{
public: public:
std::array<std::pair<rayvec3, rayvec2>, AABBFACES_COUNT> faces; // every face is min-point and opposite corner point std::array<std::pair<rayvec3, rayvec2>, AABBFACES_COUNT> faces; // every face is min-point and opposite corner point
static constexpr std::array<AAFaceKind,AABBFACES_COUNT> KINDS_ORDER = {
AAFaceKind::Xperp, AAFaceKind::Xperp,
AAFaceKind::Yperp, AAFaceKind::Yperp,
AAFaceKind::Zperp, AAFaceKind::Zperp};
AABBFaces(){}; AABBFaces(){};
AABBFaces(const rayvec3& parentBoxPos, const AABB& parentBox); AABBFaces(const rayvec3& parentBoxPos, const AABB& parentBox);
@ -57,7 +62,8 @@ static RayRelation isRayIntersectsAAFace(
const rayvec3& rayOrigin, const rayvec3& rayOrigin,
const rayvec3& rayDir, const rayvec3& rayDir,
const rayvec3& faceMin, const rayvec3& faceMin,
const rayvec2& faceOppositeCorner const rayvec2& faceOppositeCorner,
glm::ivec3& normal_ret
); );
static RayRelation rayIntersectAABB( static RayRelation rayIntersectAABB(

View File

@ -16,7 +16,7 @@ int64_t timeutil::Timer::stop() {
timeutil::ScopeLogTimer::ScopeLogTimer(long long id) : scopeid_(id) {} timeutil::ScopeLogTimer::ScopeLogTimer(long long id) : scopeid_(id) {}
timeutil::ScopeLogTimer::~ScopeLogTimer() { timeutil::ScopeLogTimer::~ScopeLogTimer() {
std::cout << "Scope "<< scopeid_ <<" finished in "<< ScopeLogTimer::stop() << " micros."<<std::endl; std::cout << "Scope "<< scopeid_ <<" finished in "<< ScopeLogTimer::stop() << " micros. \n";
} }
float timeutil::time_value(float hour, float minute, float second) { float timeutil::time_value(float hour, float minute, float second) {

View File

@ -17,6 +17,7 @@
#include <limits.h> #include <limits.h>
using glm::vec3; using glm::vec3;
using glm::ivec3;
using std::shared_ptr; using std::shared_ptr;
Chunks::Chunks(int w, int d, Chunks::Chunks(int w, int d,
@ -177,13 +178,12 @@ void Chunks::set(int x, int y, int z, int id, uint8_t states){
chunk->setModified(true); chunk->setModified(true);
} }
#include "../util/timeutil.h" #include "../util/timeutil.h"
#include <iostream>
voxel* Chunks::rayCast(vec3 start, voxel* Chunks::rayCast(vec3 start,
vec3 dir, vec3 dir,
float maxDist, float maxDist,
vec3& end, vec3& end,
vec3& norm, ivec3& norm,
vec3& iend) { ivec3& iend) {
float px = start.x; float px = start.x;
float py = start.y; float py = start.y;
float pz = start.z; float pz = start.z;
@ -197,9 +197,9 @@ voxel* Chunks::rayCast(vec3 start,
int iy = floor(py); int iy = floor(py);
int iz = floor(pz); int iz = floor(pz);
float stepx = (dx > 0.0f) ? 1.0f : -1.0f; int stepx = (dx > 0.0f) ? 1 : -1;
float stepy = (dy > 0.0f) ? 1.0f : -1.0f; int stepy = (dy > 0.0f) ? 1 : -1;
float stepz = (dz > 0.0f) ? 1.0f : -1.0f; int stepz = (dz > 0.0f) ? 1 : -1;
constexpr float infinity = std::numeric_limits<float>::infinity(); constexpr float infinity = std::numeric_limits<float>::infinity();
@ -223,7 +223,7 @@ voxel* Chunks::rayCast(vec3 start,
const Block* def = contentIds->getBlockDef(voxel->id); const Block* def = contentIds->getBlockDef(voxel->id);
if (def->selectable){ if (def->selectable){
timeutil::ScopeLogTimer lg((long long)def); //timeutil::ScopeLogTimer lg((long long)def);
end.x = px + t * dx; end.x = px + t * dx;
end.y = py + t * dy; end.y = py + t * dy;
end.z = pz + t * dz; end.z = pz + t * dz;
@ -231,61 +231,12 @@ voxel* Chunks::rayCast(vec3 start,
iend.y = iy; iend.y = iy;
iend.z = iz; iend.z = iz;
if (def && !def->rt.solid) { if (!def->rt.solid) {
const int gridSize = BLOCK_AABB_GRID * 2;
const AABB& box = def->rotatable const AABB& box = def->rotatable
? def->rt.hitboxes[voxel->rotation()] ? def->rt.hitboxes[voxel->rotation()]
: def->hitbox; : def->hitbox;
/*const int subs = gridSize; rayvec3 in, out; // <- now not used, but for future...
iend = vec3(ix, iy, iz); if (Rays::rayIntersectAABB(start, dir, iend, box, in, out, norm) > RayRelation::None){
end -= iend; // in-block coordinates
int six = end.x * gridSize;
int siy = end.y * gridSize;
int siz = end.z * gridSize;
float stxMax = (txDelta < infinity) ? txDelta * xdist : infinity;
float styMax = (tyDelta < infinity) ? tyDelta * ydist : infinity;
float stzMax = (tzDelta < infinity) ? tzDelta * zdist : infinity;
for (int i = 0; i < subs*3; i++) {
end.x = six / float(gridSize);
end.y = siy / float(gridSize);
end.z = siz / float(gridSize);
if (box.inside(end)) {
end += iend;
norm.x = norm.y = norm.z = 0.0f;
if (steppedIndex == 0) norm.x = -stepx;
if (steppedIndex == 1) norm.y = -stepy;
if (steppedIndex == 2) norm.z = -stepz;
return voxel;
}
if (stxMax < styMax) {
if (stxMax < stzMax) {
six += stepx;
stxMax += txDelta;
steppedIndex = 0;
} else {
siz += stepz;
stzMax += tzDelta;
steppedIndex = 2;
}
} else {
if (styMax < stzMax) {
siy += stepy;
styMax += tyDelta;
steppedIndex = 1;
} else {
siz += stepz;
stzMax += tzDelta;
steppedIndex = 2;
}
}
}*/
rayvec3 in, out;
glm::ivec3 normal;
if ((bool)Rays::rayIntersectAABB(start, dir, iend, box, in, out, normal)){
norm.x = norm.y = norm.z = 0.0f;
if (steppedIndex == 0) norm.x = -stepx;
if (steppedIndex == 1) norm.y = -stepy;
if (steppedIndex == 2) norm.z = -stepz;
return voxel; return voxel;
} }
@ -294,7 +245,7 @@ voxel* Chunks::rayCast(vec3 start,
iend.y = iy; iend.y = iy;
iend.z = iz; iend.z = iz;
norm.x = norm.y = norm.z = 0.0f; norm.x = norm.y = norm.z = 0;
if (steppedIndex == 0) norm.x = -stepx; if (steppedIndex == 0) norm.x = -stepx;
if (steppedIndex == 1) norm.y = -stepy; if (steppedIndex == 1) norm.y = -stepy;
if (steppedIndex == 2) norm.z = -stepz; if (steppedIndex == 2) norm.z = -stepz;
@ -334,7 +285,7 @@ voxel* Chunks::rayCast(vec3 start,
end.x = px + t * dx; end.x = px + t * dx;
end.y = py + t * dy; end.y = py + t * dy;
end.z = pz + t * dz; end.z = pz + t * dz;
norm.x = norm.y = norm.z = 0.0f; norm.x = norm.y = norm.z = 0;
return nullptr; return nullptr;
} }

View File

@ -48,8 +48,8 @@ public:
glm::vec3 dir, glm::vec3 dir,
float maxLength, float maxLength,
glm::vec3& end, glm::vec3& end,
glm::vec3& norm, glm::ivec3& norm,
glm::vec3& iend); glm::ivec3& iend);
const AABB* isObstacle(float x, float y, float z); const AABB* isObstacle(float x, float y, float z);