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