format: reformat project

Signed-off-by: Vyacheslav Ivanov <islavaivanov76@gmail.com>
This commit is contained in:
Vyacheslav Ivanov
2024-08-03 19:53:48 +03:00
committed by Pugemon
parent 736cd175d5
commit bbf33e8e4d
202 changed files with 7389 additions and 5609 deletions
+1664 -937
View File
File diff suppressed because it is too large Load Diff
+60 -39
View File
@@ -9,7 +9,6 @@ public:
void update(glm::mat4 projview);
bool isBoxVisible(const glm::vec3& minp, const glm::vec3& maxp) const;
private:
enum Planes {
Left = 0,
@@ -22,16 +21,16 @@ private:
Combinations = Count * (Count - 1) / 2
};
template<Planes i, Planes j>
template <Planes i, Planes j>
struct ij2k {
enum { k = i * (9 - i) / 2 + j - 1 };
};
template<Planes a, Planes b, Planes c>
template <Planes a, Planes b, Planes c>
glm::vec3 intersection(const glm::vec3* crosses) const;
glm::vec4 m_planes[Count];
glm::vec3 m_points[8];
glm::vec4 m_planes[Count];
glm::vec3 m_points[8];
};
inline void Frustum::update(glm::mat4 m) {
@@ -44,22 +43,21 @@ inline void Frustum::update(glm::mat4 m) {
m_planes[Far] = m[3] - m[2];
glm::vec3 crosses[Combinations] = {
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Right])),
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Bottom])),
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Top])),
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Near])),
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Far])),
glm::cross(glm::vec3(m_planes[Right]), glm::vec3(m_planes[Bottom])),
glm::cross(glm::vec3(m_planes[Right]), glm::vec3(m_planes[Top])),
glm::cross(glm::vec3(m_planes[Right]), glm::vec3(m_planes[Near])),
glm::cross(glm::vec3(m_planes[Right]), glm::vec3(m_planes[Far])),
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Right])),
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Bottom])),
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Top])),
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Near])),
glm::cross(glm::vec3(m_planes[Left]), glm::vec3(m_planes[Far])),
glm::cross(glm::vec3(m_planes[Right]), glm::vec3(m_planes[Bottom])),
glm::cross(glm::vec3(m_planes[Right]), glm::vec3(m_planes[Top])),
glm::cross(glm::vec3(m_planes[Right]), glm::vec3(m_planes[Near])),
glm::cross(glm::vec3(m_planes[Right]), glm::vec3(m_planes[Far])),
glm::cross(glm::vec3(m_planes[Bottom]), glm::vec3(m_planes[Top])),
glm::cross(glm::vec3(m_planes[Bottom]), glm::vec3(m_planes[Near])),
glm::cross(glm::vec3(m_planes[Bottom]), glm::vec3(m_planes[Far])),
glm::cross(glm::vec3(m_planes[Top]), glm::vec3(m_planes[Near])),
glm::cross(glm::vec3(m_planes[Top]), glm::vec3(m_planes[Far])),
glm::cross(glm::vec3(m_planes[Near]), glm::vec3(m_planes[Far]))
};
glm::cross(glm::vec3(m_planes[Top]), glm::vec3(m_planes[Near])),
glm::cross(glm::vec3(m_planes[Top]), glm::vec3(m_planes[Far])),
glm::cross(glm::vec3(m_planes[Near]), glm::vec3(m_planes[Far]))};
m_points[0] = intersection<Left, Bottom, Near>(crosses);
m_points[1] = intersection<Left, Top, Near>(crosses);
@@ -69,43 +67,66 @@ inline void Frustum::update(glm::mat4 m) {
m_points[5] = intersection<Left, Top, Far>(crosses);
m_points[6] = intersection<Right, Bottom, Far>(crosses);
m_points[7] = intersection<Right, Top, Far>(crosses);
}
inline bool Frustum::isBoxVisible(const glm::vec3& minp, const glm::vec3& maxp) const {
inline bool Frustum::isBoxVisible(const glm::vec3& minp, const glm::vec3& maxp)
const {
// check box outside/inside of frustum
for (int i = 0; i < Count; i++) {
if ((glm::dot(m_planes[i], glm::vec4(minp.x, minp.y, minp.z, 1.0f)) < 0.0) &&
(glm::dot(m_planes[i], glm::vec4(maxp.x, minp.y, minp.z, 1.0f)) < 0.0) &&
(glm::dot(m_planes[i], glm::vec4(minp.x, maxp.y, minp.z, 1.0f)) < 0.0) &&
(glm::dot(m_planes[i], glm::vec4(maxp.x, maxp.y, minp.z, 1.0f)) < 0.0) &&
(glm::dot(m_planes[i], glm::vec4(minp.x, minp.y, maxp.z, 1.0f)) < 0.0) &&
(glm::dot(m_planes[i], glm::vec4(maxp.x, minp.y, maxp.z, 1.0f)) < 0.0) &&
(glm::dot(m_planes[i], glm::vec4(minp.x, maxp.y, maxp.z, 1.0f)) < 0.0) &&
(glm::dot(m_planes[i], glm::vec4(maxp.x, maxp.y, maxp.z, 1.0f)) < 0.0))
{
if ((glm::dot(m_planes[i], glm::vec4(minp.x, minp.y, minp.z, 1.0f)) <
0.0) &&
(glm::dot(m_planes[i], glm::vec4(maxp.x, minp.y, minp.z, 1.0f)) <
0.0) &&
(glm::dot(m_planes[i], glm::vec4(minp.x, maxp.y, minp.z, 1.0f)) <
0.0) &&
(glm::dot(m_planes[i], glm::vec4(maxp.x, maxp.y, minp.z, 1.0f)) <
0.0) &&
(glm::dot(m_planes[i], glm::vec4(minp.x, minp.y, maxp.z, 1.0f)) <
0.0) &&
(glm::dot(m_planes[i], glm::vec4(maxp.x, minp.y, maxp.z, 1.0f)) <
0.0) &&
(glm::dot(m_planes[i], glm::vec4(minp.x, maxp.y, maxp.z, 1.0f)) <
0.0) &&
(glm::dot(m_planes[i], glm::vec4(maxp.x, maxp.y, maxp.z, 1.0f)) <
0.0)) {
return false;
}
}
// check frustum outside/inside box
int out;
out = 0; for (int i = 0; i < 8; i++) out += ((m_points[i].x > maxp.x) ? 1 : 0); if (out == 8) return false;
out = 0; for (int i = 0; i < 8; i++) out += ((m_points[i].x < minp.x) ? 1 : 0); if (out == 8) return false;
out = 0; for (int i = 0; i < 8; i++) out += ((m_points[i].y > maxp.y) ? 1 : 0); if (out == 8) return false;
out = 0; for (int i = 0; i < 8; i++) out += ((m_points[i].y < minp.y) ? 1 : 0); if (out == 8) return false;
out = 0; for (int i = 0; i < 8; i++) out += ((m_points[i].z > maxp.z) ? 1 : 0); if (out == 8) return false;
out = 0; for (int i = 0; i < 8; i++) out += ((m_points[i].z < minp.z) ? 1 : 0); if (out == 8) return false;
out = 0;
for (int i = 0; i < 8; i++) out += ((m_points[i].x > maxp.x) ? 1 : 0);
if (out == 8) return false;
out = 0;
for (int i = 0; i < 8; i++) out += ((m_points[i].x < minp.x) ? 1 : 0);
if (out == 8) return false;
out = 0;
for (int i = 0; i < 8; i++) out += ((m_points[i].y > maxp.y) ? 1 : 0);
if (out == 8) return false;
out = 0;
for (int i = 0; i < 8; i++) out += ((m_points[i].y < minp.y) ? 1 : 0);
if (out == 8) return false;
out = 0;
for (int i = 0; i < 8; i++) out += ((m_points[i].z > maxp.z) ? 1 : 0);
if (out == 8) return false;
out = 0;
for (int i = 0; i < 8; i++) out += ((m_points[i].z < minp.z) ? 1 : 0);
if (out == 8) return false;
return true;
}
template<Frustum::Planes a, Frustum::Planes b, Frustum::Planes c>
template <Frustum::Planes a, Frustum::Planes b, Frustum::Planes c>
inline glm::vec3 Frustum::intersection(const glm::vec3* crosses) const {
float D = glm::dot(glm::vec3(m_planes[a]), crosses[ij2k<b, c>::k]);
glm::vec3 res = glm::mat3(crosses[ij2k<b, c>::k], -crosses[ij2k<a, c>::k], crosses[ij2k<a, b>::k]) *
glm::vec3(m_planes[a].w, m_planes[b].w, m_planes[c].w);
glm::vec3 res = glm::mat3(
crosses[ij2k<b, c>::k],
-crosses[ij2k<a, c>::k],
crosses[ij2k<a, b>::k]
) *
glm::vec3(m_planes[a].w, m_planes[b].w, m_planes[c].w);
return res * (-1.0f / D);
}
#endif // MATHS_FRUSTUM_CULLING_HPP_
#endif // MATHS_FRUSTUM_CULLING_HPP_
+20 -15
View File
@@ -3,13 +3,12 @@
#include <algorithm>
static int get_packer_score(const rectangle& rect) {
if (rect.width * rect.height > 100)
return rect.height * rect.height * 1000;
if (rect.width * rect.height > 100) return rect.height * rect.height * 1000;
return (rect.width * rect.height * rect.height);
}
LMPacker::LMPacker(const uint32_t sizes[], size_t length) {
for (unsigned int i = 0; i < length/2; i++) {
for (unsigned int i = 0; i < length / 2; i++) {
rectangle rect(i, 0, 0, (int)sizes[i * 2], (int)sizes[i * 2 + 1]);
rects.push_back(rect);
}
@@ -27,8 +26,13 @@ void LMPacker::cleanup() {
placed.clear();
}
bool LMPacker::build(uint32_t width, uint32_t height,
uint16_t extension, uint32_t mbit, uint32_t vstep) {
bool LMPacker::build(
uint32_t width,
uint32_t height,
uint16_t extension,
uint32_t mbit,
uint32_t vstep
) {
cleanup();
this->mbit = mbit;
this->width = width;
@@ -53,7 +57,7 @@ bool LMPacker::build(uint32_t width, uint32_t height,
rect.height += extension * 2;
if (mpix > 1) {
if (rect.width % mpix > 0) {
rect.extX = mpix - (rect.width % mpix);
rect.extX = mpix - (rect.width % mpix);
}
if (rect.height % mpix > 0) {
rect.extY = mpix - (rect.height % mpix);
@@ -83,8 +87,8 @@ bool LMPacker::build(uint32_t width, uint32_t height,
inline rectangle* find_collision(
const LMPacker::matrix_ptr& matrix, int x, int y, int w, int h
) {
for (int row = y; row < y+h; row++) {
for (int col = x; col < x+w; col++) {
for (int row = y; row < y + h; row++) {
for (int col = x; col < x + w; col++) {
rectangle* rect = matrix[row][col];
if (rect) {
return rect;
@@ -94,9 +98,11 @@ inline rectangle* find_collision(
return nullptr;
}
inline void fill(LMPacker::matrix_ptr& matrix, rectangle* rect, int x, int y, int w, int h) {
for (int row = y; row < y+h; row++) {
for (int col = x; col < x+w; col++) {
inline void fill(
LMPacker::matrix_ptr& matrix, rectangle* rect, int x, int y, int w, int h
) {
for (int row = y; row < y + h; row++) {
for (int col = x; col < x + w; col++) {
matrix[row][col] = rect;
}
}
@@ -122,11 +128,11 @@ bool LMPacker::place(rectangle* rectptr, uint32_t vstep) {
} else {
if (skiplines) {
unsigned int lfree = 0;
while (lfree + x < mwidth && !lower[x + lfree] && lfree < rw) {
while (lfree + x < mwidth && !lower[x + lfree] && lfree < rw
) {
lfree++;
}
if (lfree >= rw)
skiplines = false;
if (lfree >= rw) skiplines = false;
}
prect = find_collision(matrix, x, y, rw, rh);
if (prect) {
@@ -146,4 +152,3 @@ bool LMPacker::place(rectangle* rectptr, uint32_t vstep) {
}
return false;
}
+12 -5
View File
@@ -3,10 +3,11 @@
#ifndef LMPACKER_HPP_
#define LMPACKER_HPP_
#include <stdlib.h>
#include <stdint.h>
#include <vector>
#include <stdlib.h>
#include <memory>
#include <vector>
struct rectangle {
unsigned int idx;
@@ -18,7 +19,7 @@ struct rectangle {
int extY = 0;
rectangle(unsigned int idx, int x, int y, int width, int height)
: idx(idx), x(x), y(y), width(width), height(height){
: idx(idx), x(x), y(y), width(width), height(height) {
}
};
@@ -46,11 +47,17 @@ public:
bool buildFast(uint32_t width, uint32_t height, uint16_t extension) {
return build(width, height, extension, 1, 2);
}
bool build(uint32_t width, uint32_t height, uint16_t extension, uint32_t mbit, uint32_t vstep);
bool build(
uint32_t width,
uint32_t height,
uint16_t extension,
uint32_t mbit,
uint32_t vstep
);
std::vector<rectangle> getResult() {
return rects;
}
};
#endif // LMPACKER_HPP_
#endif // LMPACKER_HPP_
+7 -5
View File
@@ -10,17 +10,19 @@ struct UVRegion {
float v2;
UVRegion(float u1, float v1, float u2, float v2)
: u1(u1), v1(v1), u2(u2), v2(v2){}
: u1(u1), v1(v1), u2(u2), v2(v2) {
}
UVRegion() : u1(0.0f), v1(0.0f), u2(1.0f), v2(1.0f){}
UVRegion() : u1(0.0f), v1(0.0f), u2(1.0f), v2(1.0f) {
}
inline float getWidth() const {
return fabs(u2-u1);
return fabs(u2 - u1);
}
inline float getHeight() const {
return fabs(v2-v1);
return fabs(v2 - v1);
}
};
#endif // MATHS_UVREGION_HPP_
#endif // MATHS_UVREGION_HPP_
+11 -20
View File
@@ -28,11 +28,7 @@ struct AABB {
/// @brief Get AABB dimensions: width, height, depth
inline glm::vec3 size() const {
return glm::vec3(
fabs(b.x - a.x),
fabs(b.y - a.y),
fabs(b.z - a.z)
);
return glm::vec3(fabs(b.x - a.x), fabs(b.y - a.y), fabs(b.z - a.z));
}
inline glm::vec3 center() const {
@@ -59,8 +55,10 @@ struct AABB {
inline bool contains(const glm::vec3 pos) const {
const glm::vec3 p = min();
const glm::vec3 s = size();
return !(pos.x < p.x || pos.y < p.y || pos.z < p.z ||
pos.x >= p.x+s.x || pos.y >= p.y+s.y || pos.z >= p.z+s.z);
return !(
pos.x < p.x || pos.y < p.y || pos.z < p.z || pos.x >= p.x + s.x ||
pos.y >= p.y + s.y || pos.z >= p.z + s.z
);
}
void fix() {
@@ -92,25 +90,18 @@ struct AABB {
inline bool intersect(const AABB& aabb) {
return (
a.x <= aabb.b.x &&
b.x >= aabb.a.x &&
a.y <= aabb.b.y &&
b.y >= aabb.a.y &&
a.z <= aabb.b.z &&
b.z >= aabb.a.z
a.x <= aabb.b.x && b.x >= aabb.a.x && a.y <= aabb.b.y &&
b.y >= aabb.a.y && a.z <= aabb.b.z && b.z >= aabb.a.z
);
}
inline bool intersect(const AABB& aabb, float margin) {
return (
a.x <= aabb.b.x+margin &&
b.x >= aabb.a.x-margin &&
a.y <= aabb.b.y+margin &&
b.y >= aabb.a.y-margin &&
a.z <= aabb.b.z+margin &&
b.z >= aabb.a.z-margin
a.x <= aabb.b.x + margin && b.x >= aabb.a.x - margin &&
a.y <= aabb.b.y + margin && b.y >= aabb.a.y - margin &&
a.z <= aabb.b.z + margin && b.z >= aabb.a.z - margin
);
}
};
#endif // MATHS_AABB_HPP_
#endif // MATHS_AABB_HPP_
+2 -2
View File
@@ -12,7 +12,7 @@ public:
inline int rand() {
seed = (214013 * seed + 2531011);
return (seed>>16) & 0x7FFF;
return (seed >> 16) & 0x7FFF;
}
inline float randFloat() {
@@ -20,4 +20,4 @@ public:
}
};
#endif // MATHS_FASTMATHS_HPP_
#endif // MATHS_FASTMATHS_HPP_
+159 -129
View File
@@ -1,258 +1,288 @@
#include "rays.hpp"
#include "aabb.hpp"
#include "aabb.hpp"
#include "glm/glm.hpp"
static const rayvec3 X_AXIS = rayvec3(1,0,0), Y_AXIS = rayvec3(0,1,0), Z_AXIS = rayvec3(0,0,1);
static const rayvec3 X_AXIS = rayvec3(1, 0, 0), Y_AXIS = rayvec3(0, 1, 0),
Z_AXIS = rayvec3(0, 0, 1);
Ray::Ray(const rayvec3& origin, const rayvec3& dir) : origin(origin), dir(dir) {}
Ray::Ray(const rayvec3& origin, const rayvec3& dir) : origin(origin), dir(dir) {
}
//make faces from AABB
AABBFaces::AABBFaces(const rayvec3& parentBoxPos, const AABB& parentBox){
rayvec3 pbMin = parentBox.min(), // every face is min-point and opposite corner point
pbMax = parentBox.max(),
pbRealPos = parentBoxPos + pbMin;
rayvec2 yzMax = rayvec2(parentBoxPos.y + pbMax.y, parentBoxPos.z + pbMax.z ),
xzMax = rayvec2(parentBoxPos.x + pbMax.x, parentBoxPos.z + pbMax.z ),
xyMax = rayvec2(parentBoxPos.x + pbMax.x, parentBoxPos.y + pbMax.y );
faces[0] = { parentBoxPos + rayvec3(pbMax.x, pbMin.y, pbMin.z), yzMax };
// make faces from AABB
AABBFaces::AABBFaces(const rayvec3& parentBoxPos, const AABB& parentBox) {
rayvec3 pbMin = parentBox.min(
), // every face is min-point and opposite corner point
pbMax = parentBox.max(), pbRealPos = parentBoxPos + pbMin;
rayvec2 yzMax = rayvec2(parentBoxPos.y + pbMax.y, parentBoxPos.z + pbMax.z),
xzMax = rayvec2(parentBoxPos.x + pbMax.x, parentBoxPos.z + pbMax.z),
xyMax = rayvec2(parentBoxPos.x + pbMax.x, parentBoxPos.y + pbMax.y);
faces[0] = {parentBoxPos + rayvec3(pbMax.x, pbMin.y, pbMin.z), yzMax};
faces[1] = {pbRealPos, yzMax};
faces[2] = { parentBoxPos + rayvec3(pbMin.x, pbMax.y, pbMin.z), xzMax };
faces[2] = {parentBoxPos + rayvec3(pbMin.x, pbMax.y, pbMin.z), xzMax};
faces[3] = {pbRealPos, xzMax};
faces[4] = { parentBoxPos + rayvec3(pbMin.x, pbMin.y, pbMax.z), xyMax };
faces[4] = {parentBoxPos + rayvec3(pbMin.x, pbMin.y, pbMax.z), xyMax};
faces[5] = {pbRealPos, xyMax};
}
RayRelation Ray::intersectYZFace(
const rayvec3& faceMin,
const rayvec2& faceOppositeCorner, //y and z global coords of opposite corner
glm::ivec3& normal_ret,
scalar_t& distance_ret //sinonym of rayCoef
){
if (fabs(glm::dot(dir, X_AXIS)) < 1.0E-8){ //precision
RayRelation Ray::intersectYZFace(
const rayvec3& faceMin,
const rayvec2&
faceOppositeCorner, // y and z global coords of opposite corner
glm::ivec3& normal_ret,
scalar_t& distance_ret // sinonym of rayCoef
) {
if (fabs(glm::dot(dir, X_AXIS)) < 1.0E-8) { // precision
return RayRelation::Parallel;
}
scalar_t rayCoef = (faceMin.x - origin.x) / (dir.x);// equivalent to distance if dir normalized
scalar_t rayCoef = (faceMin.x - origin.x) /
(dir.x); // equivalent to distance if dir normalized
if (rayCoef < 0) return RayRelation::None;
rayvec3 intersectPoint = {faceMin.x,
rayCoef*dir.y + origin.y,
rayCoef*dir.z + origin.z};
rayvec3 intersectPoint = {
faceMin.x, rayCoef * dir.y + origin.y, rayCoef * dir.z + origin.z};
if (intersectPoint.y >= faceMin.y
&& intersectPoint.y <= faceOppositeCorner[0]
&& intersectPoint.z >= faceMin.z
&& intersectPoint.z <= faceOppositeCorner[1]){
distance_ret = rayCoef; // believe that dir normalized
if (dir.x > 0) normal_ret = -X_AXIS;
else normal_ret = X_AXIS;
return RayRelation::Intersect;
}
if (intersectPoint.y >= faceMin.y &&
intersectPoint.y <= faceOppositeCorner[0] &&
intersectPoint.z >= faceMin.z &&
intersectPoint.z <= faceOppositeCorner[1]) {
distance_ret = rayCoef; // believe that dir normalized
if (dir.x > 0)
normal_ret = -X_AXIS;
else
normal_ret = X_AXIS;
return RayRelation::Intersect;
}
return RayRelation::None;
}
RayRelation Ray::intersectXZFace(
const rayvec3& faceMin,
const rayvec2& faceOppositeCorner, //x and z global coords of opposite corner
glm::ivec3& normal_ret,
scalar_t& distance_ret
){
if (fabs(glm::dot(dir, Y_AXIS)) < 1.0E-8){ //precision
const rayvec3& faceMin,
const rayvec2&
faceOppositeCorner, // x and z global coords of opposite corner
glm::ivec3& normal_ret,
scalar_t& distance_ret
) {
if (fabs(glm::dot(dir, Y_AXIS)) < 1.0E-8) { // precision
return RayRelation::Parallel;
}
scalar_t rayCoef = (faceMin.y - origin.y) / (dir.y);// equivalent to distance if dir normalized
scalar_t rayCoef = (faceMin.y - origin.y) /
(dir.y); // equivalent to distance if dir normalized
if (rayCoef < 0) return RayRelation::None;
rayvec3 intersectPoint = { rayCoef *dir.x + origin.x,
faceMin.y,
rayCoef*dir.z + origin.z};
rayvec3 intersectPoint = {
rayCoef * dir.x + origin.x, faceMin.y, rayCoef * dir.z + origin.z};
if (intersectPoint.x >= faceMin.x //Face-hit check
&& intersectPoint.x <= faceOppositeCorner[0]
&& intersectPoint.z >= faceMin.z
&& intersectPoint.z <= faceOppositeCorner[1] ){
distance_ret = rayCoef; // believe that dir normalized
if (dir.y > 0) normal_ret = -Y_AXIS;
else normal_ret = Y_AXIS;
if (intersectPoint.x >= faceMin.x // Face-hit check
&& intersectPoint.x <= faceOppositeCorner[0] &&
intersectPoint.z >= faceMin.z &&
intersectPoint.z <= faceOppositeCorner[1]) {
distance_ret = rayCoef; // believe that dir normalized
if (dir.y > 0)
normal_ret = -Y_AXIS;
else
normal_ret = Y_AXIS;
return RayRelation::Intersect;
}
}
return RayRelation::None;
}
RayRelation Ray::intersectXYFace(
const rayvec3& faceMin,
const rayvec2& faceOppositeCorner, //x and y global coords of opposite corner
glm::ivec3& normal_ret,
scalar_t& distance_ret
){
if (fabs(glm::dot(dir, Z_AXIS)) < 1.0E-8){ //precision
const rayvec3& faceMin,
const rayvec2&
faceOppositeCorner, // x and y global coords of opposite corner
glm::ivec3& normal_ret,
scalar_t& distance_ret
) {
if (fabs(glm::dot(dir, Z_AXIS)) < 1.0E-8) { // precision
return RayRelation::Parallel;
}
scalar_t rayCoef = (faceMin.z - origin.z) / (dir.z); // equivalent to distance if dir normalized
scalar_t rayCoef = (faceMin.z - origin.z) /
(dir.z); // equivalent to distance if dir normalized
if (rayCoef < 0) return RayRelation::None;
rayvec3 intersectPoint = { rayCoef *dir.x + origin.x,
rayCoef*dir.y + origin.y,
faceMin.z};
if (intersectPoint.x >= faceMin.x //Face-hit check
&& intersectPoint.x <= faceOppositeCorner[0]
&& intersectPoint.y >= faceMin.y
&& intersectPoint.y <= faceOppositeCorner[1] ){
distance_ret = rayCoef; // believe that dir normalized
if (dir.z > 0) normal_ret = -Z_AXIS;
else normal_ret = Z_AXIS;
rayvec3 intersectPoint = {
rayCoef * dir.x + origin.x, rayCoef * dir.y + origin.y, faceMin.z};
if (intersectPoint.x >= faceMin.x // Face-hit check
&& intersectPoint.x <= faceOppositeCorner[0] &&
intersectPoint.y >= faceMin.y &&
intersectPoint.y <= faceOppositeCorner[1]) {
distance_ret = rayCoef; // believe that dir normalized
if (dir.z > 0)
normal_ret = -Z_AXIS;
else
normal_ret = Z_AXIS;
return RayRelation::Intersect;
}
return RayRelation::None;
}
RayRelation Ray::isIntersectsYZFace(
const rayvec3& faceMin,
const rayvec2& faceOppositeCorner
){
if (fabs(glm::dot(dir, X_AXIS)) < 1.0E-8) { //precision of "parallelity"
const rayvec3& faceMin, const rayvec2& faceOppositeCorner
) {
if (fabs(glm::dot(dir, X_AXIS)) < 1.0E-8) { // precision of "parallelity"
return RayRelation::Parallel;
}
scalar_t rayCoef = (faceMin.x - origin.x) / (dir.x);
if (rayCoef < 0) return RayRelation::None;
rayvec3 intersectPoint = { faceMin.x,
rayCoef * dir.y + origin.y,
rayCoef * dir.z + origin.z };
rayvec3 intersectPoint = {
faceMin.x, rayCoef * dir.y + origin.y, rayCoef * dir.z + origin.z};
if (intersectPoint.y >= faceMin.y
&& intersectPoint.y <= faceOppositeCorner[0]
&& intersectPoint.z >= faceMin.z
&& intersectPoint.z <= faceOppositeCorner[1]) {
if (intersectPoint.y >= faceMin.y &&
intersectPoint.y <= faceOppositeCorner[0] &&
intersectPoint.z >= faceMin.z &&
intersectPoint.z <= faceOppositeCorner[1]) {
return RayRelation::Intersect;
}
return RayRelation::None;
}
RayRelation Ray::isIntersectsXZFace(
const rayvec3& faceMin,
const rayvec2& faceOppositeCorner
) {
if (fabs(glm::dot(dir, Y_AXIS)) < 1.0E-8) { //precision of "parallelity"
const rayvec3& faceMin, const rayvec2& faceOppositeCorner
) {
if (fabs(glm::dot(dir, Y_AXIS)) < 1.0E-8) { // precision of "parallelity"
return RayRelation::Parallel;
}
scalar_t rayCoef = (faceMin.y - origin.y) / (dir.y);
if (rayCoef < 0) return RayRelation::None;
rayvec3 intersectPoint = { rayCoef * dir.x + origin.x,
faceMin.y,
rayCoef * dir.z + origin.z };
rayvec3 intersectPoint = {
rayCoef * dir.x + origin.x, faceMin.y, rayCoef * dir.z + origin.z};
if (intersectPoint.x >= faceMin.x //Face-hit check
&& intersectPoint.x <= faceOppositeCorner[0]
&& intersectPoint.z >= faceMin.z
&& intersectPoint.z <= faceOppositeCorner[1]) {
if (intersectPoint.x >= faceMin.x // Face-hit check
&& intersectPoint.x <= faceOppositeCorner[0] &&
intersectPoint.z >= faceMin.z &&
intersectPoint.z <= faceOppositeCorner[1]) {
return RayRelation::Intersect;
}
return RayRelation::None;
}
RayRelation Ray::isIntersectsXYFace(
const rayvec3& faceMin,
const rayvec2& faceOppositeCorner
) {
if (fabs(glm::dot(dir, Z_AXIS)) < 1.0E-8) { //precision of "parallelity"
const rayvec3& faceMin, const rayvec2& faceOppositeCorner
) {
if (fabs(glm::dot(dir, Z_AXIS)) < 1.0E-8) { // precision of "parallelity"
return RayRelation::Parallel;
}
scalar_t rayCoef = (faceMin.z - origin.z) / (dir.z);
if (rayCoef < 0) return RayRelation::None;
rayvec3 intersectPoint = { rayCoef * dir.x + origin.x,
rayCoef * dir.y + origin.y,
faceMin.z };
rayvec3 intersectPoint = {
rayCoef * dir.x + origin.x, rayCoef * dir.y + origin.y, faceMin.z};
if (intersectPoint.x >= faceMin.x //Face-hit check
&& intersectPoint.x <= faceOppositeCorner[0]
&& intersectPoint.y >= faceMin.y
&& intersectPoint.y <= faceOppositeCorner[1]) {
if (intersectPoint.x >= faceMin.x // Face-hit check
&& intersectPoint.x <= faceOppositeCorner[0] &&
intersectPoint.y >= faceMin.y &&
intersectPoint.y <= faceOppositeCorner[1]) {
return RayRelation::Intersect;
}
return RayRelation::None;
}
RayRelation Ray::intersectAABB(
const rayvec3& boxPos,
const AABB& box,
float maxDist,
glm::ivec3& normal_ret,
scalar_t& distance_ret){
const rayvec3& boxPos,
const AABB& box,
float maxDist,
glm::ivec3& normal_ret,
scalar_t& distance_ret
) {
const AABBFaces& boxFaces = AABBFaces(boxPos, box);
return intersectAABBFaces(boxFaces, maxDist, normal_ret, distance_ret);
}
RayRelation Ray::intersectAABBFaces(
const AABBFaces& boxFaces,
float maxDist,
glm::ivec3& normal_ret,
scalar_t& distance_ret){
const AABBFaces& boxFaces,
float maxDist,
glm::ivec3& normal_ret,
scalar_t& distance_ret
) {
scalar_t faceDist;
distance_ret = maxDist;
glm::ivec3 bufNormal;
//unsigned char intersectedCount = 0; //this code is very uncomfortable, DONT LEARN IT!
// unsigned char intersectedCount = 0; //this code is very uncomfortable,
// DONT LEARN IT!
bool isIntersect = false;
if (intersectYZFace(
boxFaces.faces[0].first, boxFaces.faces[0].second, bufNormal, faceDist
) > RayRelation::None && faceDist < distance_ret){
boxFaces.faces[0].first,
boxFaces.faces[0].second,
bufNormal,
faceDist
) > RayRelation::None &&
faceDist < distance_ret) {
isIntersect = true;
normal_ret = bufNormal;
distance_ret = faceDist;
}
if (intersectYZFace(
boxFaces.faces[1].first, boxFaces.faces[1].second, bufNormal, faceDist
) > RayRelation::None && faceDist < distance_ret) {
boxFaces.faces[1].first,
boxFaces.faces[1].second,
bufNormal,
faceDist
) > RayRelation::None &&
faceDist < distance_ret) {
isIntersect = true;
normal_ret = bufNormal;
distance_ret = faceDist;
}
if (intersectXZFace(
boxFaces.faces[2].first, boxFaces.faces[2].second, bufNormal, faceDist
) > RayRelation::None && faceDist < distance_ret) {
boxFaces.faces[2].first,
boxFaces.faces[2].second,
bufNormal,
faceDist
) > RayRelation::None &&
faceDist < distance_ret) {
isIntersect = true;
normal_ret = bufNormal;
distance_ret = faceDist;
}
if (intersectXZFace(
boxFaces.faces[3].first, boxFaces.faces[3].second, bufNormal, faceDist
) > RayRelation::None && faceDist < distance_ret) {
boxFaces.faces[3].first,
boxFaces.faces[3].second,
bufNormal,
faceDist
) > RayRelation::None &&
faceDist < distance_ret) {
isIntersect = true;
normal_ret = bufNormal;
distance_ret = faceDist;
}
if (intersectXYFace(
boxFaces.faces[4].first, boxFaces.faces[4].second, bufNormal, faceDist
) > RayRelation::None && faceDist < distance_ret) {
boxFaces.faces[4].first,
boxFaces.faces[4].second,
bufNormal,
faceDist
) > RayRelation::None &&
faceDist < distance_ret) {
isIntersect = true;
normal_ret = bufNormal;
distance_ret = faceDist;
}
if (intersectXYFace(
boxFaces.faces[5].first, boxFaces.faces[5].second, bufNormal, faceDist
) > RayRelation::None && faceDist < distance_ret) {
boxFaces.faces[5].first,
boxFaces.faces[5].second,
bufNormal,
faceDist
) > RayRelation::None &&
faceDist < distance_ret) {
isIntersect = true;
normal_ret = bufNormal;
distance_ret = faceDist;
}
if (isIntersect) return RayRelation::Intersect;
return RayRelation::None;
}
+26 -23
View File
@@ -4,22 +4,21 @@
#include "aabb.hpp"
#include "glm/glm.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/gtx/hash.hpp"
#include <array>
#include "glm/gtx/hash.hpp"
using rayvec3 = glm::highp_dvec3;
using rayvec2 = glm::highp_dvec2;
using scalar_t = double;
enum class RayRelation {
Embed=2, Intersect=1, Parallel=0, None=0
};
enum class RayRelation { Embed = 2, Intersect = 1, Parallel = 0, None = 0 };
class AABBFaces {
static const unsigned char AABBFACES_COUNT = 6;
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
AABBFaces() = default;
AABBFaces(const rayvec3& parentBoxPos, const AABB& parentBox);
@@ -30,48 +29,52 @@ public:
rayvec3 origin;
rayvec3 dir;
Ray(const rayvec3& rayOrigin,
const rayvec3& rayDir);
Ray(const rayvec3& rayOrigin, const rayvec3& rayDir);
RayRelation isIntersectsYZFace(
const rayvec3& faceMin,
const rayvec2& faceOppositeCorner);
const rayvec3& faceMin, const rayvec2& faceOppositeCorner
);
RayRelation isIntersectsXZFace(
const rayvec3& faceMin,
const rayvec2& faceOppositeCorner);
const rayvec3& faceMin, const rayvec2& faceOppositeCorner
);
RayRelation isIntersectsXYFace(
const rayvec3& faceMin,
const rayvec2& faceOppositeCorner);
const rayvec3& faceMin, const rayvec2& faceOppositeCorner
);
///returns normal and distance
RayRelation intersectYZFace(
/// returns normal and distance
RayRelation intersectYZFace(
const rayvec3& faceMin,
const rayvec2& faceOppositeCorner,
glm::ivec3& normal_ret,
scalar_t& distance_ret);
scalar_t& distance_ret
);
RayRelation intersectXZFace(
const rayvec3& faceMin,
const rayvec2& faceOppositeCorner,
glm::ivec3& normal_ret,
scalar_t& distance_ret);
scalar_t& distance_ret
);
RayRelation intersectXYFace(
const rayvec3& faceMin,
const rayvec2& faceOppositeCorner,
glm::ivec3& normal_ret,
scalar_t& distance_ret);
scalar_t& distance_ret
);
RayRelation intersectAABB(
const rayvec3& boxPos,
const AABB& box,
float maxDist,
glm::ivec3& normal_ret,
scalar_t& distance_ret);
scalar_t& distance_ret
);
RayRelation intersectAABBFaces( // calculates only normal and distance
RayRelation intersectAABBFaces( // calculates only normal and distance
const AABBFaces& boxFaces,
float maxDist,
glm::ivec3& normal_ret,
scalar_t& distance_ret);
scalar_t& distance_ret
);
};
#endif // SRC_VOXNATHS_HPP_
#endif // SRC_VOXNATHS_HPP_
+14 -8
View File
@@ -1,17 +1,18 @@
#ifndef MATHS_UTIL_HPP_
#define MATHS_UTIL_HPP_
#include <ctime>
#include <stdint.h>
#include <ctime>
class PseudoRandom {
unsigned short seed;
public:
PseudoRandom(){
PseudoRandom() {
seed = (unsigned short)time(0);
}
int rand(){
int rand() {
seed = (seed + 0x7ed5 + (seed << 6));
seed = (seed ^ 0xc23c ^ (seed >> 9));
seed = (seed + 0x1656 + (seed << 3));
@@ -36,14 +37,19 @@ public:
return (x << 32ULL) | y;
}
void setSeed(int number){
seed = ((unsigned short)(number*23729) ^ (unsigned short)(number+16786));
void setSeed(int number) {
seed =
((unsigned short)(number * 23729) ^ (unsigned short)(number + 16786)
);
rand();
}
void setSeed(int number1, int number2){
seed = (((unsigned short)(number1*23729) | (unsigned short)(number2%16786)) ^ (unsigned short)(number2*number1));
void setSeed(int number1, int number2) {
seed =
(((unsigned short)(number1 * 23729) |
(unsigned short)(number2 % 16786)) ^
(unsigned short)(number2 * number1));
rand();
}
};
#endif // MATHS_UTIL_HPP_
#endif // MATHS_UTIL_HPP_
+1 -1
View File
@@ -48,4 +48,4 @@ inline light_t light_pack(ubyte r, ubyte g, ubyte b, ubyte s) {
return r | (g << 4) | (b << 8) | (s << 12);
}
#endif // VOXNATHS_HPP_
#endif // VOXNATHS_HPP_