depth detection maybe

This commit is contained in:
Alexandre 2025-01-18 22:46:21 +01:00
parent db60e9a707
commit 727d097f45
9 changed files with 143 additions and 7 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -297,6 +297,14 @@ double distance_pt_cube_3d(double x0, double y0, double z0, cube cb) {
return distance_pt_cube_0_3d(x0, y0, z0, *cb) ; return distance_pt_cube_0_3d(x0, y0, z0, *cb) ;
} }
double zdepth_of_pt(double x0, double y0, double z0) {
double pz0;
project_to_camera(x0, y0, z0, NULL, NULL, &pz0);
return pz0;
}
// ------------------------------------------------------------------------------------------------ //
void project_to_camera(double x0, double y0, double z0, double* rx, double* ry, double* rz) { void project_to_camera(double x0, double y0, double z0, double* rx, double* ry, double* rz) {
// align pt to (0, 0, 0) // align pt to (0, 0, 0)
double x = x0 - camx ; double x = x0 - camx ;
@ -314,6 +322,23 @@ void project_to_camera(double x0, double y0, double z0, double* rx, double* ry,
if(rz != NULL) {*rz = zry*cos(rot_vt) + yry*sin(rot_vt) ;} if(rz != NULL) {*rz = zry*cos(rot_vt) + yry*sin(rot_vt) ;}
} }
void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0 c) {
// align pt to (0, 0, 0)
double x = x0 - (c.x + c.w/2.0) ;
double y = y0 - (c.y + c.h/2.0) ;
double z = z0 - (c.z + c.d/2.0) ;
// rotate (y)
double xry = x*cos(c.hz_angle) + z*sin(c.hz_angle) ;
double yry = y ;
double zry = z*cos(c.hz_angle) - x*sin(c.hz_angle) ;
// rotate (x)
if(rx != NULL) {*rx = xry ;}
if(ry != NULL) {*ry = yry*cos(c.vt_angle) - zry*sin(c.vt_angle);}
if(rz != NULL) {*rz = zry*cos(c.vt_angle) + yry*sin(c.vt_angle);}
}
// ------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------ //
void remove_entity(entity** arr, int* memlen, int* len, int index) { void remove_entity(entity** arr, int* memlen, int* len, int index) {

View File

@ -43,12 +43,14 @@ double distance_pt_cube_0_3d_weighted(double x0, double y0, double z0, double mx
double distance_pt_cube_axis_max(double coord, double begin, double end); double distance_pt_cube_axis_max(double coord, double begin, double end);
double distance_pt_cube_aligned_3d_max(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd); double distance_pt_cube_aligned_3d_max(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd);
double distance_pt_cube_0_3d_max(double x0, double y0, double z0, cube_0 c); double distance_pt_cube_0_3d_max(double x0, double y0, double z0, cube_0 c);
double zdepth_of_pt(double x0, double y0, double z0);
void remove_entity(entity** arr, int* memlen, int* len, int index); void remove_entity(entity** arr, int* memlen, int* len, int index);
void add_entity(entity** arr, int* memlen, int* len, entity ent); void add_entity(entity** arr, int* memlen, int* len, entity ent);
double distance_pt_cube_3d(double x0, double y0, double z0, cube cb); double distance_pt_cube_3d(double x0, double y0, double z0, cube cb);
void project_to_camera(double x0, double y0, double z0, double* rx, double* ry, double* rz); void project_to_camera(double x0, double y0, double z0, double* rx, double* ry, double* rz);
void project_to_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0 c);
void import_digits(SDL_Renderer* renderer); void import_digits(SDL_Renderer* renderer);
void import_letters(SDL_Renderer* renderer); void import_letters(SDL_Renderer* renderer);

View File

@ -708,22 +708,23 @@ void drawCurrentRoom(SDL_Renderer* renderer) {
insertionSort_tp(current_room->tps, current_room->tps_size); insertionSort_tp(current_room->tps, current_room->tps_size);
insertionSort_ent(current_room->ents, current_room->ent_len); insertionSort_ent(current_room->ents, current_room->ent_len);
for(int k1 = 0; k1 < current_room->map_size; k1++) { /*for(int k1 = 0; k1 < current_room->map_size; k1++) {
current_room->map[k1].red = 188 ; current_room->map[k1].red = 188 ;
current_room->map[k1].green = 0 ; current_room->map[k1].green = 0 ;
current_room->map[k1].blue = 0 ; current_room->map[k1].blue = 0 ;
} }
int front ;
for(int k1 = 0; k1 < current_room->map_size; k1++) { for(int k1 = 0; k1 < current_room->map_size; k1++) {
for(int k2 = k1+1; k2 < current_room->map_size; k2++) { for(int k2 = k1+1; k2 < current_room->map_size; k2++) {
if(cubeOverlap(current_room->map[k1], current_room->map[k2])) { if(cubeOverlap(current_room->map[k1], current_room->map[k2], &front)) {
current_room->map[k1].red = 0 ; current_room->map[k1].red = 188*(front==1) ;
current_room->map[k1].green = 188 ; current_room->map[k1].green = 188 ;
current_room->map[k2].red = 0 ; current_room->map[k2].red = 188*(front==-1) ;
current_room->map[k2].green = 188 ; current_room->map[k2].green = 188 ;
} }
} }
} }*/
if(true || draw_type == 0) { if(true || draw_type == 0) {
for(int k = 0; k < current_room->map_size; k++) { for(int k = 0; k < current_room->map_size; k++) {

View File

@ -31,11 +31,21 @@ pt_2d* triangle_2 ;
pt_2d* cube_t1 ; pt_2d* cube_t1 ;
pt_2d* cube_t2 ; pt_2d* cube_t2 ;
int* dOrder1 ;
int* dOrder2 ;
double* zdepths1 ;
double* zdepths2 ;
void trInit() { void trInit() {
triangle_1 = malloc(sizeof(pt_2d)*3); triangle_1 = malloc(sizeof(pt_2d)*3);
triangle_2 = malloc(sizeof(pt_2d)*3); triangle_2 = malloc(sizeof(pt_2d)*3);
cube_t1 = malloc(sizeof(pt_2d)*6); cube_t1 = malloc(sizeof(pt_2d)*6);
cube_t2 = malloc(sizeof(pt_2d)*6); cube_t2 = malloc(sizeof(pt_2d)*6);
dOrder1 = malloc(sizeof(int)*6);
dOrder2 = malloc(sizeof(int)*6);
zdepths1 = malloc(sizeof(double)*4);
zdepths2 = malloc(sizeof(double)*4);
} }
double det2D(pt_2d* p1, pt_2d* p2, pt_2d* p3) { double det2D(pt_2d* p1, pt_2d* p2, pt_2d* p3) {
@ -328,7 +338,104 @@ int fillPolygon(int sf, cube_0 c, int trig, pt_2d* ret) {
} }
} }
bool cubeOverlap(cube_0 c1, cube_0 c2) { int visibleSurfaces(double x0, double y0, double z0, cube_0 cb, int* dOrd) {
// returns the number of surfaces that should be drawn, as well as filling dOrd for said surfaces :
// 0 = +x ; 1 = -x
// 2 = +y ; 3 = -y
// 4 = +z ; 5 = -z
// align cube center to (0, 0, 0)
double x = x0 - (cb.x + cb.w/2.0) ;
double y = y0 - (cb.y + cb.h/2.0) ;
double z = z0 - (cb.z + cb.d/2.0) ;
// rotate (y)
double xry = x*cos(cb.hz_angle) + z*sin(cb.hz_angle) ;
double yry = y ;
double zry = z*cos(cb.hz_angle) - x*sin(cb.hz_angle) ;
// rotate (x)
double xrx = xry ;
double yrx = yry*cos(cb.vt_angle) + zry*sin(cb.vt_angle) ;
double zrx = zry*cos(cb.vt_angle) - yry*sin(cb.vt_angle) ;
// cube is centered and aligned
int id = 0 ;
if(xrx > cb.w/2.0) {
dOrd[id] = 0 ;
id += 1 ;
} else if(xrx < -cb.w/2.0) {
dOrd[id] = 1 ;
id += 1 ;
}
if(yrx > cb.h/2.0) {
dOrd[id] = 2 ;
id += 1 ;
} else if(yrx < -cb.h/2.0) {
dOrd[id] = 3 ;
id += 1 ;
}
if(zrx > cb.d/2.0) {
dOrd[id] = 4 ;
id += 1 ;
} else if(zrx < -cb.d/2.0) {
dOrd[id] = 5 ;
id += 1 ;
}
if(id == 0) { // inside the cube
for(int k = 0; k < 6; k++) {
dOrd[k] = k ;
}
return 6;
} else {
return id ;
}
}
void get_zdepths(cube_0 c, int sf, double* ret) {
pt_2d campt = to_fpoint(camx, camy, camz);
if(sf == 0 || sf == 1) { // x
ret[0] = zdepth_of_pt(c.x + c.w*(sf==0), c.y, c.z );
ret[1] = zdepth_of_pt(c.x + c.w*(sf==0), c.y + c.h, c.z );
ret[2] = zdepth_of_pt(c.x + c.w*(sf==0), c.y , c.z + c.d);
ret[3] = zdepth_of_pt(c.x + c.w*(sf==0), c.y + c.h, c.z + c.d);
} else if(sf == 2 || sf == 3) { // y
ret[0] = zdepth_of_pt(c.x, c.y + c.h*(sf==2), c.z );
ret[1] = zdepth_of_pt(c.x + c.w, c.y + c.h*(sf==2), c.z );
ret[2] = zdepth_of_pt(c.x , c.y + c.h*(sf==2), c.z + c.d);
ret[3] = zdepth_of_pt(c.x + c.w, c.y + c.h*(sf==2), c.z + c.d);
} else { // z
ret[0] = zdepth_of_pt(c.x, c.y, c.z + c.d*(sf==4));
ret[1] = zdepth_of_pt(c.x + c.w, c.y, c.z + c.d*(sf==4));
ret[2] = zdepth_of_pt(c.x , c.y + c.h, c.z + c.d*(sf==4));
ret[3] = zdepth_of_pt(c.x + c.w, c.y + c.h, c.z + c.d*(sf==4));
}
}
bool is_all_behind(double* back, double* front) {
for(int k = 0; k < 3; k++) {
if(back[k] > front[k]) {
return false;
}
}
return true;
}
int which_is_in_front(cube_0 c1, int sfc1, cube_0 c2, int sfc2) {
int nVis1 = visibleSurfaces(camx, camy, camz, c1, dOrder1);
int nVis2 = visibleSurfaces(camx, camy, camz, c2, dOrder2);
for(int k1 = 0; k1 < nVis1; k1++) {
for(int k2 = 0; k2 < nVis2; k2++) {
get_zdepths(c1, dOrder1[k1], zdepths1);
get_zdepths(c2, dOrder2[k2], zdepths2);
if(is_all_behind(zdepths1, zdepths2)) {
return 1;
} else if(is_all_behind(zdepths2, zdepths1)) {
return -1;
}
}
}
return 0;
}
bool cubeOverlap(cube_0 c1, cube_0 c2, int* retval) {
int len1 = 0; int len1 = 0;
int len2 = 0; int len2 = 0;
for(int i1 = 0; i1 < 6; i1++) { for(int i1 = 0; i1 < 6; i1++) {
@ -337,6 +444,7 @@ bool cubeOverlap(cube_0 c1, cube_0 c2) {
len1 = fillPolygon(i1, c1, n%2, cube_t1); len1 = fillPolygon(i1, c1, n%2, cube_t1);
len2 = fillPolygon(i2, c2, n/2, cube_t2); len2 = fillPolygon(i2, c2, n/2, cube_t2);
if(multipleTrianglesIntersection(cube_t1, len1, cube_t2, len2)) { if(multipleTrianglesIntersection(cube_t1, len1, cube_t2, len2)) {
if(retval != NULL) {*retval = which_is_in_front(c1, i1, c2, i2);}
return true; return true;
} }
} }

View File

@ -15,6 +15,6 @@ bool triangleIntersectionDec(pt_2d t1_1, pt_2d t1_2, pt_2d t1_3, pt_2d t2_1, pt_
int returnTriangle(double x0, double y0, double z0, double x1, double y1, double z1, double x2, double y2, double z2, pt_2d* retarr); int returnTriangle(double x0, double y0, double z0, double x1, double y1, double z1, double x2, double y2, double z2, pt_2d* retarr);
int fillPolygon(int sf, cube_0 c, int trig, pt_2d* ret); int fillPolygon(int sf, cube_0 c, int trig, pt_2d* ret);
bool cubeOverlap(cube_0 c1, cube_0 c2); bool cubeOverlap(cube_0 c1, cube_0 c2, int* retval);
#endif #endif