Merge pull request #67 from A-lex-Ra/main
rays efficiency improve + refactoring
This commit is contained in:
commit
aefc9aa909
@ -17,35 +17,36 @@ using std::unique_ptr;
|
||||
#ifdef LIBPNG
|
||||
#include <png.h>
|
||||
|
||||
// returns 0 if all-right, 1 otherwise
|
||||
int _png_write(const char* filename, uint width, uint height, const ubyte* data, bool alpha) {
|
||||
png_structp png_ptr = NULL;
|
||||
png_infop info_ptr = NULL;
|
||||
png_structp png_ptr = nullptr;
|
||||
png_infop info_ptr = nullptr;
|
||||
uint pixsize = alpha ? 4 : 3;
|
||||
|
||||
// Open file for writing (binary mode)
|
||||
FILE* fp = fopen(filename, "wb");
|
||||
if (fp == NULL) {
|
||||
if (fp == nullptr) {
|
||||
fprintf(stderr, "Could not open file %s for writing\n", filename);
|
||||
fclose(fp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Initialize write structure
|
||||
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||
if (png_ptr == NULL) {
|
||||
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
||||
if (png_ptr == nullptr) {
|
||||
fprintf(stderr, "Could not allocate write struct\n");
|
||||
fclose(fp);
|
||||
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
||||
png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Initialize info structure
|
||||
info_ptr = png_create_info_struct(png_ptr);
|
||||
if (info_ptr == NULL) {
|
||||
if (info_ptr == nullptr) {
|
||||
fprintf(stderr, "Could not allocate info struct\n");
|
||||
fclose(fp);
|
||||
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
|
||||
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
||||
png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
|
||||
return 1;
|
||||
|
||||
}
|
||||
@ -55,7 +56,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
|
||||
fprintf(stderr, "Error during png creation\n");
|
||||
fclose(fp);
|
||||
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
|
||||
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
||||
png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -85,58 +86,59 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
|
||||
}
|
||||
|
||||
// End write
|
||||
png_write_end(png_ptr, NULL);
|
||||
png_write_end(png_ptr, nullptr);
|
||||
|
||||
fclose(fp);
|
||||
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
|
||||
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
||||
png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ImageData* _png_load(const char* file){
|
||||
FILE *f;
|
||||
int is_png, bit_depth, color_type, row_bytes;
|
||||
FILE *f = nullptr;
|
||||
bool is_png = false;
|
||||
int bit_depth, color_type, row_bytes;
|
||||
png_infop info_ptr, end_info;
|
||||
png_uint_32 t_width, t_height;
|
||||
png_byte header[8], *image_data;
|
||||
png_bytepp row_pointers;
|
||||
png_structp png_ptr;
|
||||
|
||||
if (!(f = fopen(file, "r"))) {
|
||||
if ((f = fopen(file, "r")) == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
if (fread(header, 1, 8, f) < 8) {
|
||||
if (fread(header, 1, 8, f) < 8) { // check of read elements count
|
||||
fclose(f);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
is_png = !png_sig_cmp(header, 0, 8);
|
||||
is_png = (png_sig_cmp(header, 0, 8) == 0);
|
||||
if (!is_png) {
|
||||
fclose(f);
|
||||
return nullptr;
|
||||
}
|
||||
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
|
||||
NULL, NULL);
|
||||
if (!png_ptr) {
|
||||
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
|
||||
nullptr, nullptr);
|
||||
if (png_ptr == nullptr) {
|
||||
fclose(f);
|
||||
return nullptr;
|
||||
}
|
||||
info_ptr = png_create_info_struct(png_ptr);
|
||||
if (!info_ptr) {
|
||||
png_destroy_read_struct( &png_ptr, (png_infopp) NULL,
|
||||
(png_infopp) NULL );
|
||||
if (info_ptr == nullptr) {
|
||||
png_destroy_read_struct( &png_ptr, (png_infopp) nullptr,
|
||||
(png_infopp) nullptr );
|
||||
fclose(f);
|
||||
return nullptr;
|
||||
}
|
||||
end_info = png_create_info_struct(png_ptr);
|
||||
if (!end_info) {
|
||||
png_destroy_read_struct(&png_ptr, (png_infopp) NULL,
|
||||
(png_infopp) NULL);
|
||||
if (end_info == nullptr) {
|
||||
png_destroy_read_struct(&png_ptr, (png_infopp) nullptr,
|
||||
(png_infopp) nullptr);
|
||||
fclose(f);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (setjmp(png_jmpbuf(png_ptr))) {
|
||||
if (setjmp(png_jmpbuf(png_ptr)) != 0) {
|
||||
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
|
||||
fclose(f);
|
||||
return nullptr;
|
||||
@ -146,7 +148,7 @@ ImageData* _png_load(const char* file){
|
||||
png_set_sig_bytes( png_ptr, 8 );
|
||||
png_read_info( png_ptr, info_ptr );
|
||||
png_get_IHDR( png_ptr, info_ptr, &t_width, &t_height, &bit_depth,
|
||||
&color_type, NULL, NULL, NULL );
|
||||
&color_type, nullptr, nullptr, nullptr );
|
||||
png_read_update_info( png_ptr, info_ptr );
|
||||
row_bytes = png_get_rowbytes( png_ptr, info_ptr );
|
||||
image_data = new png_byte[row_bytes * t_height];
|
||||
@ -156,7 +158,7 @@ ImageData* _png_load(const char* file){
|
||||
return nullptr;
|
||||
}
|
||||
row_pointers = (png_bytepp) malloc( t_height * sizeof(png_bytep) );
|
||||
if ( !row_pointers ) {
|
||||
if ( row_pointers == nullptr ) {
|
||||
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
||||
delete[] image_data;
|
||||
fclose(f);
|
||||
@ -180,6 +182,7 @@ ImageData* _png_load(const char* file){
|
||||
png_get_color_type( png_ptr, info_ptr ) );
|
||||
png_destroy_read_struct( &png_ptr, &info_ptr, &end_info );
|
||||
delete[] image_data;
|
||||
free( row_pointers );
|
||||
fclose(f);
|
||||
return nullptr;
|
||||
}
|
||||
@ -194,10 +197,12 @@ ImageData* _png_load(const char* file){
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
static const int SPNG_SUCCESS = 0;
|
||||
//returns spng result code
|
||||
int _png_write(const char* filename, uint width, uint height, const ubyte* data, bool alpha) {
|
||||
int fmt;
|
||||
int ret = 0;
|
||||
spng_ctx* ctx = NULL;
|
||||
spng_ctx* ctx = nullptr;
|
||||
spng_ihdr ihdr = { 0 };
|
||||
uint pixsize = alpha ? 4 : 3;
|
||||
|
||||
@ -212,7 +217,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
|
||||
spng_set_ihdr(ctx, &ihdr);
|
||||
fmt = SPNG_FMT_PNG;
|
||||
ret = spng_encode_image(ctx, data, (size_t)width * (size_t)height * pixsize , fmt, SPNG_ENCODE_FINALIZE);
|
||||
if (ret) {
|
||||
if (ret != SPNG_SUCCESS) {
|
||||
printf("spng_encode_image() error: %s\n", spng_strerror(ret));
|
||||
fflush(stdout);
|
||||
spng_ctx_free(ctx);
|
||||
@ -222,7 +227,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
|
||||
size_t png_size;
|
||||
void* png_buf = spng_get_png_buffer(ctx, &png_size, &ret);
|
||||
|
||||
if (png_buf == NULL) {
|
||||
if (png_buf == nullptr) {
|
||||
printf("spng_get_png_buffer() error: %s\n", spng_strerror(ret));
|
||||
}
|
||||
else {
|
||||
@ -235,7 +240,7 @@ int _png_write(const char* filename, uint width, uint height, const ubyte* data,
|
||||
|
||||
ImageData* _png_load(const char* file){
|
||||
int r = 0;
|
||||
FILE *png;
|
||||
FILE *png = nullptr;
|
||||
char *pngbuf = nullptr;
|
||||
spng_ctx *ctx = nullptr;
|
||||
unsigned char *out = nullptr;
|
||||
@ -255,7 +260,7 @@ ImageData* _png_load(const char* file){
|
||||
return nullptr;
|
||||
}
|
||||
pngbuf = new char[siz_pngbuf];
|
||||
if(fread(pngbuf, siz_pngbuf, 1, png) != 1){
|
||||
if(fread(pngbuf, siz_pngbuf, 1, png) != 1){ //check of read elements count
|
||||
fclose(png);
|
||||
delete[] pngbuf;
|
||||
std::cerr << "fread() failed" << std::endl;
|
||||
@ -269,14 +274,14 @@ ImageData* _png_load(const char* file){
|
||||
return nullptr;
|
||||
}
|
||||
r = spng_set_crc_action(ctx, SPNG_CRC_USE, SPNG_CRC_USE);
|
||||
if (r){
|
||||
if (r != SPNG_SUCCESS){
|
||||
delete[] pngbuf;
|
||||
spng_ctx_free(ctx);
|
||||
std::cerr << "spng_set_crc_action() error: " << spng_strerror(r) << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
r = spng_set_png_buffer(ctx, pngbuf, siz_pngbuf);
|
||||
if (r){
|
||||
if (r != SPNG_SUCCESS){
|
||||
delete[] pngbuf;
|
||||
spng_ctx_free(ctx);
|
||||
std::cerr << "spng_set_png_buffer() error: " << spng_strerror(r) << std::endl;
|
||||
@ -285,7 +290,7 @@ ImageData* _png_load(const char* file){
|
||||
|
||||
spng_ihdr ihdr;
|
||||
r = spng_get_ihdr(ctx, &ihdr);
|
||||
if (r){
|
||||
if (r != SPNG_SUCCESS){
|
||||
delete[] pngbuf;
|
||||
spng_ctx_free(ctx);
|
||||
std::cerr << "spng_get_ihdr() error: " << spng_strerror(r) << std::endl;
|
||||
@ -306,7 +311,7 @@ ImageData* _png_load(const char* file){
|
||||
|
||||
size_t out_size;
|
||||
r = spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &out_size);
|
||||
if (r){
|
||||
if (r != SPNG_SUCCESS){
|
||||
delete[] pngbuf;
|
||||
spng_ctx_free(ctx);
|
||||
std::cerr << "spng_decoded_image_size() error: " << spng_strerror(r) << std::endl;
|
||||
@ -314,7 +319,7 @@ ImageData* _png_load(const char* file){
|
||||
}
|
||||
out = new unsigned char[out_size];
|
||||
r = spng_decode_image(ctx, out, out_size, SPNG_FMT_RGBA8, 0);
|
||||
if (r){
|
||||
if (r != SPNG_SUCCESS){
|
||||
delete[] out;
|
||||
delete[] pngbuf;
|
||||
spng_ctx_free(ctx);
|
||||
|
||||
@ -4,9 +4,11 @@
|
||||
|
||||
#include "glm/glm.hpp"
|
||||
|
||||
std::unordered_map<rayvec3, AABBFaces> Rays::raysBoxCache_ = {};
|
||||
std::unordered_map<rayvec3, AABBFaces> Ray::raysBoxCache_ = {};
|
||||
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) {}
|
||||
|
||||
//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
|
||||
@ -29,30 +31,28 @@ AABBFaces::AABBFaces(const rayvec3& parentBoxPos, const AABB& parentBox){
|
||||
}
|
||||
|
||||
template <>
|
||||
RayRelation Rays::rayIntersectAAFace<AAFaceKind::Xperp>(
|
||||
const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir,
|
||||
RayRelation Ray::intersectAAFace<AAFaceKind::Xperp>(
|
||||
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(rayDir, X_AXIS)) < 1.0E-8){ //precision
|
||||
if (fabs(glm::dot(dir, X_AXIS)) < 1.0E-8){ //precision
|
||||
return RayRelation::Parallel;
|
||||
}
|
||||
|
||||
scalar_t rayCoef = (faceMin.x - rayOrigin.x) / (rayDir.x);// equivalent to distance if raydir 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*rayDir.y + rayOrigin.y,
|
||||
rayCoef*rayDir.z + rayOrigin.z};
|
||||
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 raydir normalized
|
||||
if (rayDir.x > 0) normal_ret = -X_AXIS;
|
||||
distance_ret = rayCoef; // believe that dir normalized
|
||||
if (dir.x > 0) normal_ret = -X_AXIS;
|
||||
else normal_ret = X_AXIS;
|
||||
return RayRelation::Intersect;
|
||||
}
|
||||
@ -60,30 +60,28 @@ RayRelation Rays::rayIntersectAAFace<AAFaceKind::Xperp>(
|
||||
}
|
||||
|
||||
template <>
|
||||
RayRelation Rays::rayIntersectAAFace<AAFaceKind::Yperp>(
|
||||
const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir,
|
||||
RayRelation Ray::intersectAAFace<AAFaceKind::Yperp>(
|
||||
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(rayDir, Y_AXIS)) < 1.0E-8){ //precision
|
||||
if (fabs(glm::dot(dir, Y_AXIS)) < 1.0E-8){ //precision
|
||||
return RayRelation::Parallel;
|
||||
}
|
||||
|
||||
scalar_t rayCoef = (faceMin.y - rayOrigin.y) / (rayDir.y);// equivalent to distance if raydir 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 *rayDir.x + rayOrigin.x,
|
||||
rayvec3 intersectPoint = { rayCoef *dir.x + origin.x,
|
||||
faceMin.y,
|
||||
rayCoef*rayDir.z + rayOrigin.z};
|
||||
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 raydir normalized
|
||||
if (rayDir.y > 0) normal_ret = -Y_AXIS;
|
||||
distance_ret = rayCoef; // believe that dir normalized
|
||||
if (dir.y > 0) normal_ret = -Y_AXIS;
|
||||
else normal_ret = Y_AXIS;
|
||||
return RayRelation::Intersect;
|
||||
}
|
||||
@ -91,30 +89,28 @@ RayRelation Rays::rayIntersectAAFace<AAFaceKind::Yperp>(
|
||||
}
|
||||
|
||||
template <>
|
||||
RayRelation Rays::rayIntersectAAFace<AAFaceKind::Zperp>(
|
||||
const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir,
|
||||
RayRelation Ray::intersectAAFace<AAFaceKind::Zperp>(
|
||||
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(rayDir, Z_AXIS)) < 1.0E-8){ //precision
|
||||
if (fabs(glm::dot(dir, Z_AXIS)) < 1.0E-8){ //precision
|
||||
return RayRelation::Parallel;
|
||||
}
|
||||
|
||||
scalar_t rayCoef = (faceMin.z - rayOrigin.z) / (rayDir.z); // equivalent to distance if raydir 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 *rayDir.x + rayOrigin.x,
|
||||
rayCoef*rayDir.y + rayOrigin.y,
|
||||
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 raydir normalized
|
||||
if (rayDir.z > 0) normal_ret = -Z_AXIS;
|
||||
distance_ret = rayCoef; // believe that dir normalized
|
||||
if (dir.z > 0) normal_ret = -Z_AXIS;
|
||||
else normal_ret = Z_AXIS;
|
||||
return RayRelation::Intersect;
|
||||
}
|
||||
@ -122,21 +118,19 @@ RayRelation Rays::rayIntersectAAFace<AAFaceKind::Zperp>(
|
||||
}
|
||||
|
||||
template <>
|
||||
RayRelation Rays::isRayIntersectsAAFace<AAFaceKind::Xperp>(
|
||||
const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir,
|
||||
RayRelation Ray::isIntersectsAAFace<AAFaceKind::Xperp>(
|
||||
const rayvec3& faceMin,
|
||||
const rayvec2& faceOppositeCorner
|
||||
){
|
||||
if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8) { //precision of "parallelity"
|
||||
if (fabs(glm::dot(dir, X_AXIS)) < 1.0E-8) { //precision of "parallelity"
|
||||
return RayRelation::Parallel;
|
||||
}
|
||||
|
||||
scalar_t rayCoef = (faceMin.x - rayOrigin.x) / (rayDir.x);
|
||||
scalar_t rayCoef = (faceMin.x - origin.x) / (dir.x);
|
||||
if (rayCoef < 0) return RayRelation::None;
|
||||
rayvec3 intersectPoint = { faceMin.x,
|
||||
rayCoef * rayDir.y + rayOrigin.y,
|
||||
rayCoef * rayDir.z + rayOrigin.z };
|
||||
rayCoef * dir.y + origin.y,
|
||||
rayCoef * dir.z + origin.z };
|
||||
|
||||
if (intersectPoint.y >= faceMin.y
|
||||
&& intersectPoint.y <= faceOppositeCorner[0]
|
||||
@ -148,21 +142,19 @@ RayRelation Rays::isRayIntersectsAAFace<AAFaceKind::Xperp>(
|
||||
}
|
||||
|
||||
template <>
|
||||
RayRelation Rays::isRayIntersectsAAFace<AAFaceKind::Yperp>(
|
||||
const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir,
|
||||
RayRelation Ray::isIntersectsAAFace<AAFaceKind::Yperp>(
|
||||
const rayvec3& faceMin,
|
||||
const rayvec2& faceOppositeCorner
|
||||
) {
|
||||
if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8) { //precision of "parallelity"
|
||||
if (fabs(glm::dot(dir, Y_AXIS)) < 1.0E-8) { //precision of "parallelity"
|
||||
return RayRelation::Parallel;
|
||||
}
|
||||
|
||||
scalar_t rayCoef = (faceMin.y - rayOrigin.y) / (rayDir.y);
|
||||
scalar_t rayCoef = (faceMin.y - origin.y) / (dir.y);
|
||||
if (rayCoef < 0) return RayRelation::None;
|
||||
rayvec3 intersectPoint = { rayCoef * rayDir.x + rayOrigin.x,
|
||||
rayvec3 intersectPoint = { rayCoef * dir.x + origin.x,
|
||||
faceMin.y,
|
||||
rayCoef * rayDir.z + rayOrigin.z };
|
||||
rayCoef * dir.z + origin.z };
|
||||
|
||||
if (intersectPoint.x >= faceMin.x //Face-hit check
|
||||
&& intersectPoint.x <= faceOppositeCorner[0]
|
||||
@ -174,20 +166,18 @@ RayRelation Rays::isRayIntersectsAAFace<AAFaceKind::Yperp>(
|
||||
}
|
||||
|
||||
template <>
|
||||
RayRelation Rays::isRayIntersectsAAFace<AAFaceKind::Zperp>(
|
||||
const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir,
|
||||
RayRelation Ray::isIntersectsAAFace<AAFaceKind::Zperp>(
|
||||
const rayvec3& faceMin,
|
||||
const rayvec2& faceOppositeCorner
|
||||
) {
|
||||
if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8) { //precision of "parallelity"
|
||||
if (fabs(glm::dot(dir, Z_AXIS)) < 1.0E-8) { //precision of "parallelity"
|
||||
return RayRelation::Parallel;
|
||||
}
|
||||
|
||||
scalar_t rayCoef = (faceMin.z - rayOrigin.z) / (rayDir.z);
|
||||
scalar_t rayCoef = (faceMin.z - origin.z) / (dir.z);
|
||||
if (rayCoef < 0) return RayRelation::None;
|
||||
rayvec3 intersectPoint = { rayCoef * rayDir.x + rayOrigin.x,
|
||||
rayCoef * rayDir.y + rayOrigin.y,
|
||||
rayvec3 intersectPoint = { rayCoef * dir.x + origin.x,
|
||||
rayCoef * dir.y + origin.y,
|
||||
faceMin.z };
|
||||
|
||||
if (intersectPoint.x >= faceMin.x //Face-hit check
|
||||
@ -199,9 +189,7 @@ RayRelation Rays::isRayIntersectsAAFace<AAFaceKind::Zperp>(
|
||||
return RayRelation::None;
|
||||
}
|
||||
|
||||
RayRelation Rays::rayIntersectAABB(
|
||||
const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir,
|
||||
RayRelation Ray::intersectAABB(
|
||||
const rayvec3& boxPos,
|
||||
const AABB& box,
|
||||
float maxDist,
|
||||
@ -211,23 +199,21 @@ RayRelation Rays::rayIntersectAABB(
|
||||
|
||||
if (raysBoxCache_.find(boxPos) != raysBoxCache_.end()){
|
||||
const AABBFaces& boxFaces = raysBoxCache_[boxPos];
|
||||
return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, normal_ret, distance_ret);
|
||||
return intersectAABBFaces(boxFaces, maxDist, normal_ret, distance_ret);
|
||||
} else {
|
||||
const AABBFaces& boxFaces = AABBFaces(boxPos, box);
|
||||
raysBoxCache_[boxPos] = boxFaces;
|
||||
return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, normal_ret, distance_ret);
|
||||
return intersectAABBFaces(boxFaces, maxDist, normal_ret, distance_ret);
|
||||
}
|
||||
|
||||
} else {
|
||||
const AABBFaces& boxFaces = AABBFaces(boxPos, box);
|
||||
return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, normal_ret, distance_ret);
|
||||
return intersectAABBFaces(boxFaces, maxDist, normal_ret, distance_ret);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
RayRelation Rays::rayIntersectAABBFaces(
|
||||
const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir,
|
||||
RayRelation Ray::intersectAABBFaces(
|
||||
const AABBFaces& boxFaces,
|
||||
float maxDist,
|
||||
glm::ivec3& normal_ret,
|
||||
@ -239,48 +225,48 @@ RayRelation Rays::rayIntersectAABBFaces(
|
||||
//unsigned char intersectedCount = 0; //this code is very uncomfortable, DONT LEARN IT!
|
||||
bool isIntersect = false;
|
||||
|
||||
if (rayIntersectAAFace<AABBFaces::KINDS_ORDER[0]>(
|
||||
rayOrigin, rayDir, boxFaces.faces[0].first, boxFaces.faces[0].second, bufNormal, faceDist
|
||||
if (intersectAAFace<AABBFaces::KINDS_ORDER[0]>(
|
||||
boxFaces.faces[0].first, boxFaces.faces[0].second, bufNormal, faceDist
|
||||
) > RayRelation::None && faceDist < distance_ret){
|
||||
isIntersect = true;
|
||||
normal_ret = bufNormal;
|
||||
distance_ret = faceDist;
|
||||
}
|
||||
|
||||
if (rayIntersectAAFace<AABBFaces::KINDS_ORDER[1]>(
|
||||
rayOrigin, rayDir, boxFaces.faces[1].first, boxFaces.faces[1].second, bufNormal, faceDist
|
||||
if (intersectAAFace<AABBFaces::KINDS_ORDER[1]>(
|
||||
boxFaces.faces[1].first, boxFaces.faces[1].second, bufNormal, faceDist
|
||||
) > RayRelation::None && faceDist < distance_ret) {
|
||||
isIntersect = true;
|
||||
normal_ret = bufNormal;
|
||||
distance_ret = faceDist;
|
||||
}
|
||||
|
||||
if (rayIntersectAAFace<AABBFaces::KINDS_ORDER[2]>(
|
||||
rayOrigin, rayDir, boxFaces.faces[2].first, boxFaces.faces[2].second, bufNormal, faceDist
|
||||
if (intersectAAFace<AABBFaces::KINDS_ORDER[2]>(
|
||||
boxFaces.faces[2].first, boxFaces.faces[2].second, bufNormal, faceDist
|
||||
) > RayRelation::None && faceDist < distance_ret) {
|
||||
isIntersect = true;
|
||||
normal_ret = bufNormal;
|
||||
distance_ret = faceDist;
|
||||
}
|
||||
|
||||
if (rayIntersectAAFace<AABBFaces::KINDS_ORDER[3]>(
|
||||
rayOrigin, rayDir, boxFaces.faces[3].first, boxFaces.faces[3].second, bufNormal, faceDist
|
||||
if (intersectAAFace<AABBFaces::KINDS_ORDER[3]>(
|
||||
boxFaces.faces[3].first, boxFaces.faces[3].second, bufNormal, faceDist
|
||||
) > RayRelation::None && faceDist < distance_ret) {
|
||||
isIntersect = true;
|
||||
normal_ret = bufNormal;
|
||||
distance_ret = faceDist;
|
||||
}
|
||||
|
||||
if (rayIntersectAAFace<AABBFaces::KINDS_ORDER[4]>(
|
||||
rayOrigin, rayDir, boxFaces.faces[4].first, boxFaces.faces[4].second, bufNormal, faceDist
|
||||
if (intersectAAFace<AABBFaces::KINDS_ORDER[4]>(
|
||||
boxFaces.faces[4].first, boxFaces.faces[4].second, bufNormal, faceDist
|
||||
) > RayRelation::None && faceDist < distance_ret) {
|
||||
isIntersect = true;
|
||||
normal_ret = bufNormal;
|
||||
distance_ret = faceDist;
|
||||
}
|
||||
|
||||
if (rayIntersectAAFace<AABBFaces::KINDS_ORDER[5]>(
|
||||
rayOrigin, rayDir, boxFaces.faces[5].first, boxFaces.faces[5].second, bufNormal, faceDist
|
||||
if (intersectAAFace<AABBFaces::KINDS_ORDER[5]>(
|
||||
boxFaces.faces[5].first, boxFaces.faces[5].second, bufNormal, faceDist
|
||||
) > RayRelation::None && faceDist < distance_ret) {
|
||||
isIntersect = true;
|
||||
normal_ret = bufNormal;
|
||||
|
||||
@ -42,67 +42,60 @@ struct std::hash<rayvec3>{
|
||||
}
|
||||
};
|
||||
|
||||
class Rays{
|
||||
class Ray{
|
||||
protected:
|
||||
static const bool IS_RAYS_BOX_CACHE_ON = false;
|
||||
static std::unordered_map<rayvec3, AABBFaces> raysBoxCache_; //[boxPos]: faces array
|
||||
|
||||
public:
|
||||
rayvec3 origin;
|
||||
rayvec3 dir;
|
||||
|
||||
Ray(const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir);
|
||||
|
||||
//optimized, NOT returns intersectPoint coordinates and normal vector
|
||||
template <AAFaceKind faceKind>
|
||||
static RayRelation isRayIntersectsAAFace(
|
||||
const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir,
|
||||
template <AAFaceKind faceKind>
|
||||
RayRelation isIntersectsAAFace(
|
||||
const rayvec3& faceMin,
|
||||
const rayvec2& faceOppositeCorner
|
||||
);
|
||||
);
|
||||
|
||||
//returns only normal
|
||||
template <AAFaceKind faceKind>
|
||||
static RayRelation rayIntersectAAFace(
|
||||
const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir,
|
||||
template <AAFaceKind faceKind>
|
||||
RayRelation intersectAAFace(
|
||||
const rayvec3& faceMin,
|
||||
const rayvec2& faceOppositeCorner,
|
||||
glm::ivec3& normal_ret
|
||||
);
|
||||
);
|
||||
|
||||
//returns normal and distance
|
||||
template <AAFaceKind faceKind>
|
||||
static RayRelation rayIntersectAAFace(
|
||||
const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir,
|
||||
template <AAFaceKind faceKind>
|
||||
RayRelation intersectAAFace(
|
||||
const rayvec3& faceMin,
|
||||
const rayvec2& faceOppositeCorner,
|
||||
glm::ivec3& normal_ret,
|
||||
scalar_t& distance_ret
|
||||
);
|
||||
);
|
||||
|
||||
// returns normal, distance and intersection point
|
||||
template <AAFaceKind faceKind>
|
||||
static RayRelation rayIntersectAAFace(
|
||||
const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir,
|
||||
template <AAFaceKind faceKind>
|
||||
RayRelation intersectAAFace(
|
||||
const rayvec3& faceMin,
|
||||
const rayvec2& faceOppositeCorner,
|
||||
glm::ivec3& normal_ret,
|
||||
scalar_t& distance_ret,
|
||||
rayvec3& intersectPoint_ret
|
||||
);
|
||||
);
|
||||
|
||||
static RayRelation rayIntersectAABB(
|
||||
const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir,
|
||||
RayRelation intersectAABB(
|
||||
const rayvec3& boxPos,
|
||||
const AABB& box,
|
||||
float maxDist,
|
||||
glm::ivec3& normal_ret,
|
||||
scalar_t& distance_ret);
|
||||
|
||||
static RayRelation rayIntersectAABBFaces( // calculates only normal and distance
|
||||
const rayvec3& rayOrigin,
|
||||
const rayvec3& rayDir,
|
||||
RayRelation intersectAABBFaces( // calculates only normal and distance
|
||||
const AABBFaces& boxFaces,
|
||||
float maxDist,
|
||||
glm::ivec3& normal_ret,
|
||||
|
||||
@ -235,7 +235,8 @@ voxel* Chunks::rayCast(vec3 start,
|
||||
? def->rt.hitboxes[voxel->rotation()]
|
||||
: def->hitbox;
|
||||
scalar_t distance;
|
||||
if (Rays::rayIntersectAABB(start, dir, iend, box, maxDist, norm, distance) > RayRelation::None){
|
||||
Ray ray(start, dir);
|
||||
if (ray.intersectAABB(iend, box, maxDist, norm, distance) > RayRelation::None){
|
||||
end = start + (dir * vec3(distance));
|
||||
return voxel;
|
||||
}
|
||||
@ -333,8 +334,9 @@ vec3 Chunks::rayCastToObstacle(vec3 start, vec3 dir, float maxDist) {
|
||||
: def->hitbox;
|
||||
scalar_t distance;
|
||||
ivec3 norm;
|
||||
Ray ray(start, dir);
|
||||
// norm is dummy now, can be inefficient
|
||||
if (Rays::rayIntersectAABB(start, dir, ivec3(ix, iy, iz), box, maxDist, norm, distance) > RayRelation::None) {
|
||||
if (ray.intersectAABB(ivec3(ix, iy, iz), box, maxDist, norm, distance) > RayRelation::None) {
|
||||
return start + (dir * vec3(distance));
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user