diff --git a/bin/back b/bin/back index 8907ee6..b2da8bc 100755 Binary files a/bin/back and b/bin/back differ diff --git a/obj/base.o b/obj/base.o index ef60ff1..a955de7 100644 Binary files a/obj/base.o and b/obj/base.o differ diff --git a/obj/display.o b/obj/display.o index 5fe5fd5..145d470 100644 Binary files a/obj/display.o and b/obj/display.o differ diff --git a/obj/triangles.o b/obj/triangles.o index c96df28..e578db8 100644 Binary files a/obj/triangles.o and b/obj/triangles.o differ diff --git a/src/base.c b/src/base.c index 1eceaf3..82d9ba9 100644 --- a/src/base.c +++ b/src/base.c @@ -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) ; } +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) { // align pt to (0, 0, 0) 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) ;} } +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) { diff --git a/src/base.h b/src/base.h index 56d5023..a00fa31 100644 --- a/src/base.h +++ b/src/base.h @@ -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_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 zdepth_of_pt(double x0, double y0, double z0); void remove_entity(entity** arr, int* memlen, int* len, int index); void add_entity(entity** arr, int* memlen, int* len, entity ent); 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_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0 c); void import_digits(SDL_Renderer* renderer); void import_letters(SDL_Renderer* renderer); diff --git a/src/display.c b/src/display.c index 41b24fa..876d58a 100644 --- a/src/display.c +++ b/src/display.c @@ -708,22 +708,23 @@ void drawCurrentRoom(SDL_Renderer* renderer) { insertionSort_tp(current_room->tps, current_room->tps_size); 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].green = 0 ; current_room->map[k1].blue = 0 ; } + int front ; for(int k1 = 0; k1 < current_room->map_size; k1++) { for(int k2 = k1+1; k2 < current_room->map_size; k2++) { - if(cubeOverlap(current_room->map[k1], current_room->map[k2])) { - current_room->map[k1].red = 0 ; + if(cubeOverlap(current_room->map[k1], current_room->map[k2], &front)) { + current_room->map[k1].red = 188*(front==1) ; 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 ; } } - } + }*/ if(true || draw_type == 0) { for(int k = 0; k < current_room->map_size; k++) { diff --git a/src/triangles.c b/src/triangles.c index d8cf741..24e2a7f 100644 --- a/src/triangles.c +++ b/src/triangles.c @@ -31,11 +31,21 @@ pt_2d* triangle_2 ; pt_2d* cube_t1 ; pt_2d* cube_t2 ; +int* dOrder1 ; +int* dOrder2 ; + +double* zdepths1 ; +double* zdepths2 ; + void trInit() { triangle_1 = malloc(sizeof(pt_2d)*3); triangle_2 = malloc(sizeof(pt_2d)*3); cube_t1 = 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) { @@ -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 len2 = 0; 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); len2 = fillPolygon(i2, c2, n/2, cube_t2); if(multipleTrianglesIntersection(cube_t1, len1, cube_t2, len2)) { + if(retval != NULL) {*retval = which_is_in_front(c1, i1, c2, i2);} return true; } } diff --git a/src/triangles.h b/src/triangles.h index ebb7092..631d81f 100644 --- a/src/triangles.h +++ b/src/triangles.h @@ -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 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 \ No newline at end of file