rays fixed
This commit is contained in:
parent
5bc75c3f2d
commit
1e39b5d05d
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,6 +5,7 @@ Debug/voxel_engine
|
|||||||
|
|
||||||
/build
|
/build
|
||||||
/screenshots
|
/screenshots
|
||||||
|
/out
|
||||||
|
|
||||||
/world
|
/world
|
||||||
/worlds/**/*
|
/worlds/**/*
|
||||||
|
|||||||
@ -34,33 +34,28 @@ RayRelation Rays::rayIntersectAAFace<AAFaceKind::Xperp>(
|
|||||||
const rayvec3& rayDir,
|
const rayvec3& rayDir,
|
||||||
const rayvec3& faceMin,
|
const rayvec3& faceMin,
|
||||||
const rayvec2& faceOppositeCorner, //y and z global coords of opposite corner
|
const rayvec2& faceOppositeCorner, //y and z global coords of opposite corner
|
||||||
rayvec3& intersectPoint_ret
|
glm::ivec3& normal_ret,
|
||||||
|
scalar_t& distance_ret //sinonym of rayCoef
|
||||||
){
|
){
|
||||||
if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8){ //precision
|
if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8){ //precision
|
||||||
return RayRelation::Parallel;
|
return RayRelation::Parallel;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::float64 rayCoef = (faceMin.x - rayOrigin.x) / (rayDir.x);
|
scalar_t rayCoef = (faceMin.x - rayOrigin.x) / (rayDir.x);// equivalent to distance if raydir normalized
|
||||||
intersectPoint_ret = {faceMin.x,
|
if (rayCoef < 0) return RayRelation::None;
|
||||||
|
rayvec3 intersectPoint = {faceMin.x,
|
||||||
rayCoef*rayDir.y + rayOrigin.y,
|
rayCoef*rayDir.y + rayOrigin.y,
|
||||||
rayCoef*rayDir.z + rayOrigin.z};
|
rayCoef*rayDir.z + rayOrigin.z};
|
||||||
|
|
||||||
if (rayDir.x > 0){
|
if (intersectPoint.y >= faceMin.y
|
||||||
if (intersectPoint_ret.y >= faceMin.y
|
&& intersectPoint.y <= faceOppositeCorner[0]
|
||||||
&& intersectPoint_ret.y <= faceOppositeCorner[0]
|
&& intersectPoint.z >= faceMin.z
|
||||||
&& intersectPoint_ret.z >= faceMin.z
|
&& intersectPoint.z <= faceOppositeCorner[1]){
|
||||||
&& intersectPoint_ret.z <= faceOppositeCorner[1]){
|
distance_ret = rayCoef; // believe that raydir normalized
|
||||||
|
if (rayDir.x > 0) normal_ret = -X_AXIS;
|
||||||
|
else normal_ret = X_AXIS;
|
||||||
return RayRelation::Intersect;
|
return RayRelation::Intersect;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else{
|
|
||||||
if (intersectPoint_ret.y <= faceMin.y
|
|
||||||
&& intersectPoint_ret.y >= faceOppositeCorner[0]
|
|
||||||
&& intersectPoint_ret.z <= faceMin.z
|
|
||||||
&& intersectPoint_ret.z >= faceOppositeCorner[1]){
|
|
||||||
return RayRelation::Intersect;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return RayRelation::None;
|
return RayRelation::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,33 +65,28 @@ RayRelation Rays::rayIntersectAAFace<AAFaceKind::Yperp>(
|
|||||||
const rayvec3& rayDir,
|
const rayvec3& rayDir,
|
||||||
const rayvec3& faceMin,
|
const rayvec3& faceMin,
|
||||||
const rayvec2& faceOppositeCorner, //x and z global coords of opposite corner
|
const rayvec2& faceOppositeCorner, //x and z global coords of opposite corner
|
||||||
rayvec3& intersectPoint_ret
|
glm::ivec3& normal_ret,
|
||||||
|
scalar_t& distance_ret
|
||||||
){
|
){
|
||||||
if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8){ //precision
|
if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8){ //precision
|
||||||
return RayRelation::Parallel;
|
return RayRelation::Parallel;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::float64 rayCoef = (faceMin.y - rayOrigin.y) / (rayDir.y);
|
scalar_t rayCoef = (faceMin.y - rayOrigin.y) / (rayDir.y);// equivalent to distance if raydir normalized
|
||||||
intersectPoint_ret = {rayCoef*rayDir.x + rayOrigin.x,
|
if (rayCoef < 0) return RayRelation::None;
|
||||||
|
rayvec3 intersectPoint = { rayCoef *rayDir.x + rayOrigin.x,
|
||||||
faceMin.y,
|
faceMin.y,
|
||||||
rayCoef*rayDir.z + rayOrigin.z};
|
rayCoef*rayDir.z + rayOrigin.z};
|
||||||
|
|
||||||
if (rayDir.y > 0){
|
if (intersectPoint.x >= faceMin.x //Face-hit check
|
||||||
if (intersectPoint_ret.x >= faceMin.x //Face-hit check
|
&& intersectPoint.x <= faceOppositeCorner[0]
|
||||||
&& intersectPoint_ret.x <= faceOppositeCorner[0]
|
&& intersectPoint.z >= faceMin.z
|
||||||
&& intersectPoint_ret.z >= faceMin.z
|
&& intersectPoint.z <= faceOppositeCorner[1] ){
|
||||||
&& intersectPoint_ret.z <= faceOppositeCorner[1] ){
|
distance_ret = rayCoef; // believe that raydir normalized
|
||||||
|
if (rayDir.y > 0) normal_ret = -Y_AXIS;
|
||||||
|
else normal_ret = Y_AXIS;
|
||||||
return RayRelation::Intersect;
|
return RayRelation::Intersect;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else{
|
|
||||||
if (intersectPoint_ret.x <= faceMin.x //Face-hit check for negative dir.
|
|
||||||
&& intersectPoint_ret.x >= faceOppositeCorner[0]
|
|
||||||
&& intersectPoint_ret.z <= faceMin.z
|
|
||||||
&& intersectPoint_ret.z >= faceOppositeCorner[1]){
|
|
||||||
return RayRelation::Intersect;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return RayRelation::None;
|
return RayRelation::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,85 +96,54 @@ RayRelation Rays::rayIntersectAAFace<AAFaceKind::Zperp>(
|
|||||||
const rayvec3& rayDir,
|
const rayvec3& rayDir,
|
||||||
const rayvec3& faceMin,
|
const rayvec3& faceMin,
|
||||||
const rayvec2& faceOppositeCorner, //x and y global coords of opposite corner
|
const rayvec2& faceOppositeCorner, //x and y global coords of opposite corner
|
||||||
rayvec3& intersectPoint_ret
|
glm::ivec3& normal_ret,
|
||||||
|
scalar_t& distance_ret
|
||||||
){
|
){
|
||||||
if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8){ //precision
|
if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8){ //precision
|
||||||
return RayRelation::Parallel;
|
return RayRelation::Parallel;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::float64 rayCoef = (faceMin.z - rayOrigin.z) / (rayDir.z);
|
scalar_t rayCoef = (faceMin.z - rayOrigin.z) / (rayDir.z); // equivalent to distance if raydir normalized
|
||||||
intersectPoint_ret = {rayCoef*rayDir.x + rayOrigin.x,
|
if (rayCoef < 0) return RayRelation::None;
|
||||||
|
rayvec3 intersectPoint = { rayCoef *rayDir.x + rayOrigin.x,
|
||||||
rayCoef*rayDir.y + rayOrigin.y,
|
rayCoef*rayDir.y + rayOrigin.y,
|
||||||
faceMin.z};
|
faceMin.z};
|
||||||
|
|
||||||
if (rayDir.z > 0){
|
if (intersectPoint.x >= faceMin.x //Face-hit check
|
||||||
if (intersectPoint_ret.x >= faceMin.x //Face-hit check
|
&& intersectPoint.x <= faceOppositeCorner[0]
|
||||||
&& intersectPoint_ret.x <= faceOppositeCorner[0]
|
&& intersectPoint.y >= faceMin.y
|
||||||
&& intersectPoint_ret.y >= faceMin.y
|
&& intersectPoint.y <= faceOppositeCorner[1] ){
|
||||||
&& intersectPoint_ret.y <= faceOppositeCorner[1] ){
|
distance_ret = rayCoef; // believe that raydir normalized
|
||||||
|
if (rayDir.z > 0) normal_ret = -Z_AXIS;
|
||||||
|
else normal_ret = Z_AXIS;
|
||||||
return RayRelation::Intersect;
|
return RayRelation::Intersect;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else{
|
|
||||||
if (intersectPoint_ret.x <= faceMin.x //Face-hit check
|
|
||||||
&& intersectPoint_ret.x >= faceOppositeCorner[0]
|
|
||||||
&& intersectPoint_ret.y <= faceMin.y
|
|
||||||
&& intersectPoint_ret.y >= faceOppositeCorner[1] ){
|
|
||||||
return RayRelation::Intersect;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return RayRelation::None;
|
return RayRelation::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
double Rays::updateNormal(
|
|
||||||
double newDistApprox,
|
|
||||||
const glm::ivec3& newNormal,
|
|
||||||
double currentDistApprox,
|
|
||||||
glm::ivec3& normal_ret
|
|
||||||
){
|
|
||||||
if (newDistApprox < currentDistApprox){
|
|
||||||
currentDistApprox = newDistApprox;
|
|
||||||
normal_ret = newNormal;
|
|
||||||
}
|
|
||||||
return currentDistApprox;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
RayRelation Rays::isRayIntersectsAAFace<AAFaceKind::Xperp>(
|
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,
|
|
||||||
double& currentDistApprox
|
|
||||||
){
|
){
|
||||||
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"
|
||||||
return RayRelation::Parallel;
|
return RayRelation::Parallel;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::float64 rayCoef = (faceMin.x - rayOrigin.x);
|
scalar_t rayCoef = (faceMin.x - rayOrigin.x) / (rayDir.x);
|
||||||
rayvec3 intersectPointMult = {faceMin.x * rayDir.x,
|
if (rayCoef < 0) return RayRelation::None;
|
||||||
rayCoef*rayDir.y + rayOrigin.y * rayDir.x,
|
rayvec3 intersectPoint = { faceMin.x,
|
||||||
rayCoef*rayDir.z + rayOrigin.z * rayDir.x};
|
rayCoef * rayDir.y + rayOrigin.y,
|
||||||
|
rayCoef * rayDir.z + rayOrigin.z };
|
||||||
|
|
||||||
if (rayDir.x > 0){
|
if (intersectPoint.y >= faceMin.y
|
||||||
if (intersectPointMult.y >= faceMin.y * rayDir.x //Face-hit check
|
&& intersectPoint.y <= faceOppositeCorner[0]
|
||||||
&& intersectPointMult.y <= faceOppositeCorner[0] * rayDir.x
|
&& intersectPoint.z >= faceMin.z
|
||||||
&& intersectPointMult.z >= faceMin.z * rayDir.x
|
&& intersectPoint.z <= faceOppositeCorner[1]) {
|
||||||
&& intersectPointMult.z <= faceOppositeCorner[1] * rayDir.x){
|
|
||||||
currentDistApprox = updateNormal(fabs(rayCoef * rayDir.y * rayDir.z), -X_AXIS, currentDistApprox, normal_ret);
|
|
||||||
return RayRelation::Intersect;
|
return RayRelation::Intersect;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else{
|
|
||||||
if (intersectPointMult.y <= faceMin.y * rayDir.x //Face-hit check for negative dir.
|
|
||||||
&& intersectPointMult.y >= faceOppositeCorner[0] * rayDir.x
|
|
||||||
&& intersectPointMult.z <= faceMin.z * rayDir.x
|
|
||||||
&& intersectPointMult.z >= faceOppositeCorner[1] * rayDir.x){
|
|
||||||
currentDistApprox = updateNormal(fabs(rayCoef * rayDir.y * rayDir.z), X_AXIS, currentDistApprox, normal_ret);
|
|
||||||
return RayRelation::Intersect;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return RayRelation::None;
|
return RayRelation::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,37 +152,24 @@ 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,
|
|
||||||
double& currentDistApprox
|
|
||||||
) {
|
) {
|
||||||
if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8) { //precision of "parallelity"
|
if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8) { //precision of "parallelity"
|
||||||
return RayRelation::Parallel;
|
return RayRelation::Parallel;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::float64 rayCoef = (faceMin.y - rayOrigin.y);
|
scalar_t rayCoef = (faceMin.y - rayOrigin.y) / (rayDir.y);
|
||||||
rayvec3 intersectPointMult = {rayCoef*rayDir.x + rayOrigin.x * rayDir.y,
|
if (rayCoef < 0) return RayRelation::None;
|
||||||
faceMin.y * rayDir.y,
|
rayvec3 intersectPoint = { rayCoef * rayDir.x + rayOrigin.x,
|
||||||
rayCoef*rayDir.z + rayOrigin.z * rayDir.y};
|
faceMin.y,
|
||||||
|
rayCoef * rayDir.z + rayOrigin.z };
|
||||||
|
|
||||||
if (rayDir.y > 0){
|
if (intersectPoint.x >= faceMin.x //Face-hit check
|
||||||
if (intersectPointMult.x >= faceMin.x * rayDir.y //Face-hit check
|
&& intersectPoint.x <= faceOppositeCorner[0]
|
||||||
&& intersectPointMult.x <= faceOppositeCorner[0] * rayDir.y
|
&& intersectPoint.z >= faceMin.z
|
||||||
&& intersectPointMult.z >= faceMin.z * rayDir.y
|
&& intersectPoint.z <= faceOppositeCorner[1]) {
|
||||||
&& intersectPointMult.z <= faceOppositeCorner[1] * rayDir.y){
|
|
||||||
currentDistApprox = updateNormal(fabs(rayCoef * rayDir.x * rayDir.z), -Y_AXIS, currentDistApprox, normal_ret);
|
|
||||||
return RayRelation::Intersect;
|
return RayRelation::Intersect;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else{
|
|
||||||
if (intersectPointMult.x <= faceMin.x * rayDir.y //Face-hit check for negative dir.
|
|
||||||
&& intersectPointMult.x >= faceOppositeCorner[0] * rayDir.y
|
|
||||||
&& intersectPointMult.z <= faceMin.z * rayDir.y
|
|
||||||
&& intersectPointMult.z >= faceOppositeCorner[1] * rayDir.y){
|
|
||||||
currentDistApprox = updateNormal(fabs(rayCoef * rayDir.x * rayDir.z), Y_AXIS, currentDistApprox, normal_ret);
|
|
||||||
return RayRelation::Intersect;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return RayRelation::None;
|
return RayRelation::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -232,37 +178,24 @@ 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,
|
|
||||||
double& currentDistApprox
|
|
||||||
) {
|
) {
|
||||||
if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8) { //precision of "parallelity"
|
if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8) { //precision of "parallelity"
|
||||||
return RayRelation::Parallel;
|
return RayRelation::Parallel;
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::float64 rayCoef = (faceMin.z - rayOrigin.z);
|
scalar_t rayCoef = (faceMin.z - rayOrigin.z) / (rayDir.z);
|
||||||
rayvec3 intersectPointMult = {rayCoef*rayDir.x + rayOrigin.x * rayDir.z,
|
if (rayCoef < 0) return RayRelation::None;
|
||||||
rayCoef*rayDir.y + rayOrigin.y * rayDir.z,
|
rayvec3 intersectPoint = { rayCoef * rayDir.x + rayOrigin.x,
|
||||||
faceMin.z * rayDir.z};
|
rayCoef * rayDir.y + rayOrigin.y,
|
||||||
|
faceMin.z };
|
||||||
|
|
||||||
if (rayDir.z > 0){
|
if (intersectPoint.x >= faceMin.x //Face-hit check
|
||||||
if (intersectPointMult.x >= faceMin.x * rayDir.z //Face-hit check
|
&& intersectPoint.x <= faceOppositeCorner[0]
|
||||||
&& intersectPointMult.x <= faceOppositeCorner[0] * rayDir.z
|
&& intersectPoint.y >= faceMin.y
|
||||||
&& intersectPointMult.y >= faceMin.y * rayDir.z
|
&& intersectPoint.y <= faceOppositeCorner[1]) {
|
||||||
&& intersectPointMult.y <= faceOppositeCorner[1] * rayDir.z){
|
|
||||||
currentDistApprox = updateNormal(fabs(rayCoef * rayDir.x * rayDir.y), -Z_AXIS, currentDistApprox, normal_ret);
|
|
||||||
return RayRelation::Intersect;
|
return RayRelation::Intersect;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else{
|
|
||||||
if (intersectPointMult.x <= faceMin.x * rayDir.z //Face-hit check
|
|
||||||
&& intersectPointMult.x >= faceOppositeCorner[0] * rayDir.z
|
|
||||||
&& intersectPointMult.y <= faceMin.y * rayDir.z
|
|
||||||
&& intersectPointMult.y >= faceOppositeCorner[1] * rayDir.z){
|
|
||||||
currentDistApprox = updateNormal(fabs(rayCoef * rayDir.x * rayDir.y), Z_AXIS, currentDistApprox, normal_ret);
|
|
||||||
return RayRelation::Intersect;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return RayRelation::None;
|
return RayRelation::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -272,23 +205,22 @@ RayRelation Rays::rayIntersectAABB(
|
|||||||
const rayvec3& boxPos,
|
const rayvec3& boxPos,
|
||||||
const AABB& box,
|
const AABB& box,
|
||||||
float maxDist,
|
float maxDist,
|
||||||
rayvec3& pointIn_ret,
|
glm::ivec3& normal_ret,
|
||||||
rayvec3& pointOut_ret,
|
scalar_t& distance_ret){
|
||||||
glm::ivec3& normal_ret){
|
|
||||||
if constexpr (IS_RAYS_BOX_CACHE_ON){
|
if constexpr (IS_RAYS_BOX_CACHE_ON){
|
||||||
|
|
||||||
if (raysBoxCache_.find(boxPos) != raysBoxCache_.end()){
|
if (raysBoxCache_.find(boxPos) != raysBoxCache_.end()){
|
||||||
const AABBFaces& boxFaces = raysBoxCache_[boxPos];
|
const AABBFaces& boxFaces = raysBoxCache_[boxPos];
|
||||||
return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, pointIn_ret, pointOut_ret, normal_ret);
|
return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, normal_ret, distance_ret);
|
||||||
} else {
|
} else {
|
||||||
const AABBFaces& boxFaces = AABBFaces(boxPos, box);
|
const AABBFaces& boxFaces = AABBFaces(boxPos, box);
|
||||||
raysBoxCache_[boxPos] = boxFaces;
|
raysBoxCache_[boxPos] = boxFaces;
|
||||||
return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, pointIn_ret, pointOut_ret, normal_ret);
|
return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, normal_ret, distance_ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
const AABBFaces& boxFaces = AABBFaces(boxPos, box);
|
const AABBFaces& boxFaces = AABBFaces(boxPos, box);
|
||||||
return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, pointIn_ret, pointOut_ret, normal_ret);
|
return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, normal_ret, distance_ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -298,54 +230,63 @@ RayRelation Rays::rayIntersectAABBFaces(
|
|||||||
const rayvec3& rayDir,
|
const rayvec3& rayDir,
|
||||||
const AABBFaces& boxFaces,
|
const AABBFaces& boxFaces,
|
||||||
float maxDist,
|
float maxDist,
|
||||||
rayvec3& pointIn_ret,
|
glm::ivec3& normal_ret,
|
||||||
rayvec3& pointOut_ret,
|
scalar_t& distance_ret){//TODO: points returning
|
||||||
glm::ivec3& normal_ret){//TODO: points returning
|
|
||||||
RayRelation rel;
|
scalar_t faceDist;
|
||||||
double faceDistApprox = maxDist;
|
distance_ret = maxDist;
|
||||||
unsigned char intersectedCount = 0; //this code is very uncomfortable, DONT LEARN IT!
|
glm::ivec3 bufNormal;
|
||||||
rel = isRayIntersectsAAFace<AABBFaces::KINDS_ORDER[0]>(
|
//unsigned char intersectedCount = 0; //this code is very uncomfortable, DONT LEARN IT!
|
||||||
rayOrigin, rayDir, boxFaces.faces[0].first, boxFaces.faces[0].second, normal_ret, faceDistApprox
|
bool isIntersect = false;
|
||||||
);
|
|
||||||
if (rel > RayRelation::None){
|
if (rayIntersectAAFace<AABBFaces::KINDS_ORDER[0]>(
|
||||||
++intersectedCount;
|
rayOrigin, rayDir, boxFaces.faces[0].first, boxFaces.faces[0].second, bufNormal, faceDist
|
||||||
|
) > RayRelation::None && faceDist < distance_ret){
|
||||||
|
isIntersect = true;
|
||||||
|
normal_ret = bufNormal;
|
||||||
|
distance_ret = faceDist;
|
||||||
}
|
}
|
||||||
|
|
||||||
rel = isRayIntersectsAAFace<AABBFaces::KINDS_ORDER[1]>(
|
if (rayIntersectAAFace<AABBFaces::KINDS_ORDER[1]>(
|
||||||
rayOrigin, rayDir, boxFaces.faces[1].first, boxFaces.faces[1].second, normal_ret, faceDistApprox
|
rayOrigin, rayDir, boxFaces.faces[1].first, boxFaces.faces[1].second, bufNormal, faceDist
|
||||||
);
|
) > RayRelation::None && faceDist < distance_ret) {
|
||||||
if (rel > RayRelation::None){
|
isIntersect = true;
|
||||||
++intersectedCount;
|
normal_ret = bufNormal;
|
||||||
|
distance_ret = faceDist;
|
||||||
}
|
}
|
||||||
|
|
||||||
rel = isRayIntersectsAAFace<AABBFaces::KINDS_ORDER[2]>(
|
if (rayIntersectAAFace<AABBFaces::KINDS_ORDER[2]>(
|
||||||
rayOrigin, rayDir, boxFaces.faces[2].first, boxFaces.faces[2].second, normal_ret, faceDistApprox
|
rayOrigin, rayDir, boxFaces.faces[2].first, boxFaces.faces[2].second, bufNormal, faceDist
|
||||||
);
|
) > RayRelation::None && faceDist < distance_ret) {
|
||||||
if (rel > RayRelation::None){
|
isIntersect = true;
|
||||||
++intersectedCount;
|
normal_ret = bufNormal;
|
||||||
|
distance_ret = faceDist;
|
||||||
}
|
}
|
||||||
|
|
||||||
rel = isRayIntersectsAAFace<AABBFaces::KINDS_ORDER[3]>(
|
if (rayIntersectAAFace<AABBFaces::KINDS_ORDER[3]>(
|
||||||
rayOrigin, rayDir, boxFaces.faces[3].first, boxFaces.faces[3].second, normal_ret, faceDistApprox
|
rayOrigin, rayDir, boxFaces.faces[3].first, boxFaces.faces[3].second, bufNormal, faceDist
|
||||||
);
|
) > RayRelation::None && faceDist < distance_ret) {
|
||||||
if (rel > RayRelation::None){
|
isIntersect = true;
|
||||||
++intersectedCount;
|
normal_ret = bufNormal;
|
||||||
|
distance_ret = faceDist;
|
||||||
}
|
}
|
||||||
|
|
||||||
rel = isRayIntersectsAAFace<AABBFaces::KINDS_ORDER[4]>(
|
if (rayIntersectAAFace<AABBFaces::KINDS_ORDER[4]>(
|
||||||
rayOrigin, rayDir, boxFaces.faces[4].first, boxFaces.faces[4].second, normal_ret, faceDistApprox
|
rayOrigin, rayDir, boxFaces.faces[4].first, boxFaces.faces[4].second, bufNormal, faceDist
|
||||||
);
|
) > RayRelation::None && faceDist < distance_ret) {
|
||||||
if (rel > RayRelation::None){
|
isIntersect = true;
|
||||||
++intersectedCount;
|
normal_ret = bufNormal;
|
||||||
|
distance_ret = faceDist;
|
||||||
}
|
}
|
||||||
|
|
||||||
rel = isRayIntersectsAAFace<AABBFaces::KINDS_ORDER[5]>(
|
if (rayIntersectAAFace<AABBFaces::KINDS_ORDER[5]>(
|
||||||
rayOrigin, rayDir, boxFaces.faces[5].first, boxFaces.faces[5].second, normal_ret, faceDistApprox
|
rayOrigin, rayDir, boxFaces.faces[5].first, boxFaces.faces[5].second, bufNormal, faceDist
|
||||||
);
|
) > RayRelation::None && faceDist < distance_ret) {
|
||||||
if (rel > RayRelation::None){
|
isIntersect = true;
|
||||||
++intersectedCount;
|
normal_ret = bufNormal;
|
||||||
|
distance_ret = faceDist;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (intersectedCount > 0) return RayRelation::Intersect;
|
if (isIntersect) return RayRelation::Intersect;
|
||||||
return RayRelation::None;
|
return RayRelation::None;
|
||||||
}
|
}
|
||||||
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
typedef glm::highp_dvec3 rayvec3;
|
typedef glm::highp_dvec3 rayvec3;
|
||||||
typedef glm::highp_dvec2 rayvec2;
|
typedef glm::highp_dvec2 rayvec2;
|
||||||
|
typedef double scalar_t;
|
||||||
|
|
||||||
enum class RayRelation{
|
enum class RayRelation{
|
||||||
Embed=2, Intersect=1, Parallel=0, None=0
|
Embed=2, Intersect=1, Parallel=0, None=0
|
||||||
@ -37,42 +38,57 @@ public:
|
|||||||
template<>
|
template<>
|
||||||
struct std::hash<rayvec3>{
|
struct std::hash<rayvec3>{
|
||||||
std::size_t operator()(const rayvec3& r) const noexcept{
|
std::size_t operator()(const rayvec3& r) const noexcept{
|
||||||
return std::hash<double>{}(r.x) ^ (std::hash<double>{}(r.y) << 1) ^ (std::hash<double>{}(r.z) << 2);
|
return std::hash<scalar_t>{}(r.x) ^ (std::hash<scalar_t>{}(r.y) << 1) ^ (std::hash<scalar_t>{}(r.z) << 2);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Rays{
|
class Rays{
|
||||||
protected:
|
protected:
|
||||||
static const bool IS_RAYS_BOX_CACHE_ON = false;
|
static const bool IS_RAYS_BOX_CACHE_ON = true;
|
||||||
static std::unordered_map<rayvec3, AABBFaces> raysBoxCache_; //[boxPos]: faces array
|
static std::unordered_map<rayvec3, AABBFaces> raysBoxCache_; //[boxPos]: faces array
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
//optimized, NOT returns intersectPoint coordinates and normal vector
|
||||||
|
template <AAFaceKind faceKind>
|
||||||
|
static RayRelation isRayIntersectsAAFace(
|
||||||
|
const rayvec3& rayOrigin,
|
||||||
|
const rayvec3& rayDir,
|
||||||
|
const rayvec3& faceMin,
|
||||||
|
const rayvec2& faceOppositeCorner
|
||||||
|
);
|
||||||
|
|
||||||
|
//returns only normal
|
||||||
template <AAFaceKind faceKind>
|
template <AAFaceKind faceKind>
|
||||||
static RayRelation rayIntersectAAFace(
|
static RayRelation rayIntersectAAFace(
|
||||||
const rayvec3& rayOrigin,
|
const rayvec3& rayOrigin,
|
||||||
const rayvec3& rayDir,
|
const rayvec3& rayDir,
|
||||||
const rayvec3& faceMin,
|
const rayvec3& faceMin,
|
||||||
const rayvec2& faceOppositeCorner,
|
const rayvec2& faceOppositeCorner,
|
||||||
rayvec3& intersectPoint_ret
|
|
||||||
);
|
|
||||||
|
|
||||||
static double updateNormal(
|
|
||||||
double newDistApprox,
|
|
||||||
const glm::ivec3& newNormal,
|
|
||||||
double currentDistApprox,
|
|
||||||
glm::ivec3& normal_ret
|
glm::ivec3& normal_ret
|
||||||
);
|
);
|
||||||
|
|
||||||
//optimized, not returns intersectPoint coordinates
|
//returns normal and distance
|
||||||
template <AAFaceKind faceKind>
|
template <AAFaceKind faceKind>
|
||||||
static RayRelation isRayIntersectsAAFace(
|
static RayRelation rayIntersectAAFace(
|
||||||
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,
|
glm::ivec3& normal_ret,
|
||||||
double& currentDistApprox
|
scalar_t& distance_ret
|
||||||
|
);
|
||||||
|
|
||||||
|
// returns normal, distance and intersection point
|
||||||
|
template <AAFaceKind faceKind>
|
||||||
|
static RayRelation rayIntersectAAFace(
|
||||||
|
const rayvec3& rayOrigin,
|
||||||
|
const rayvec3& rayDir,
|
||||||
|
const rayvec3& faceMin,
|
||||||
|
const rayvec2& faceOppositeCorner,
|
||||||
|
glm::ivec3& normal_ret,
|
||||||
|
scalar_t& distance_ret,
|
||||||
|
rayvec3& intersectPoint_ret
|
||||||
);
|
);
|
||||||
|
|
||||||
static RayRelation rayIntersectAABB(
|
static RayRelation rayIntersectAABB(
|
||||||
@ -81,18 +97,16 @@ static RayRelation rayIntersectAABB(
|
|||||||
const rayvec3& boxPos,
|
const rayvec3& boxPos,
|
||||||
const AABB& box,
|
const AABB& box,
|
||||||
float maxDist,
|
float maxDist,
|
||||||
rayvec3& pointIn_ret,
|
glm::ivec3& normal_ret,
|
||||||
rayvec3& pointOut_ret,
|
scalar_t& distance_ret);
|
||||||
glm::ivec3& normal_ret);
|
|
||||||
|
|
||||||
static RayRelation rayIntersectAABBFaces(
|
static RayRelation rayIntersectAABBFaces( // calculates only normal and distance
|
||||||
const rayvec3& rayOrigin,
|
const rayvec3& rayOrigin,
|
||||||
const rayvec3& rayDir,
|
const rayvec3& rayDir,
|
||||||
const AABBFaces& boxFaces,
|
const AABBFaces& boxFaces,
|
||||||
float maxDist,
|
float maxDist,
|
||||||
rayvec3& pointIn_ret,
|
glm::ivec3& normal_ret,
|
||||||
rayvec3& pointOut_ret,
|
scalar_t& distance_ret);
|
||||||
glm::ivec3& normal_ret);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SRC_VOXNATHS_H_
|
#endif // SRC_VOXNATHS_H_
|
||||||
|
|||||||
@ -234,8 +234,9 @@ voxel* Chunks::rayCast(vec3 start,
|
|||||||
const AABB& box = def->rotatable
|
const AABB& box = def->rotatable
|
||||||
? def->rt.hitboxes[voxel->rotation()]
|
? def->rt.hitboxes[voxel->rotation()]
|
||||||
: def->hitbox;
|
: def->hitbox;
|
||||||
rayvec3 in, out; // <- now not used, but for future...
|
scalar_t distance;
|
||||||
if (Rays::rayIntersectAABB(start, dir, iend, box, maxDist, in, out, norm) > RayRelation::None){
|
if (Rays::rayIntersectAABB(start, dir, iend, box, maxDist, norm, distance) > RayRelation::None){
|
||||||
|
end = start + (dir * vec3(distance));
|
||||||
return voxel;
|
return voxel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user