experimenting with drawOrder

This commit is contained in:
Alexandre 2025-01-13 22:35:22 +01:00
parent 15cace6833
commit 333083d164
10 changed files with 209 additions and 66 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -216,10 +216,22 @@ double distance_pt_cube_axis(double coord, double begin, double end) {
}
}
double distance_pt_cube_axis_max(double coord, double begin, double end) {
if(coord < (begin+end)/2) {
return absf(end-coord) ;
} else {
return absf(begin-coord) ;
}
}
double distance_pt_cube_aligned_3d(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd) {
return (distance_pt_cube_axis(x0, cx, cx+cw)+distance_pt_cube_axis(y0, cy, cy+ch)+distance_pt_cube_axis(z0, cz, cz+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) {
return (distance_pt_cube_axis_max(x0, cx, cx+cw)+distance_pt_cube_axis_max(y0, cy, cy+ch)+distance_pt_cube_axis_max(z0, cz, cz+cd)) ;
}
double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0 c) {
// places the origin at the center of the cube
double x = x0 - (c.x + c.w/2.0) ;
@ -239,6 +251,25 @@ double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0 c) {
return distance_pt_cube_aligned_3d(xrx, yrx, zrx, -c.w/2.0, -c.h/2.0, -c.d/2.0, c.w, c.h, c.d) ;
}
double distance_pt_cube_0_3d_max(double x0, double y0, double z0, cube_0 c) {
// places the origin at the center of the cube
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 the point : y then x
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) ;
double xrx = xry ;
double yrx = yry*cos(c.vt_angle) - zry*sin(c.vt_angle) ;
double zrx = zry*cos(c.vt_angle) + yry*sin(c.vt_angle) ;
// now the cube and pt are aligned, and (0, 0, 0) is at the cube's (bary)center
return distance_pt_cube_aligned_3d_max(xrx, yrx, zrx, -c.w/2.0, -c.h/2.0, -c.d/2.0, c.w, c.h, c.d) ;
}
double distance_pt_cube_aligned_3d_weighted(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd, double mx, double my, double mz) {
return (mx*distance_pt_cube_axis(x0, cx, cx+cw)+my*distance_pt_cube_axis(y0, cy, cy+ch)+mz*distance_pt_cube_axis(z0, cz, cz+cd)) ;
}

View File

@ -36,9 +36,14 @@ double distance_pt_seg_3d(double x, double y, double z, double sx, double sy, do
double distance_pt_cube_axis(double coord, double begin, double end);
double distance_pt_cube_aligned_3d(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd);
double distance_pt_cube_0_3d(double x0, double y0, double z0, cube_0 c);
double distance_pt_cube_aligned_3d_weighted(double x0, double y0, double z0, double cx, double cy, double cz, double cw, double ch, double cd, double mx, double my, double mz);
double distance_pt_cube_0_3d_weighted(double x0, double y0, double z0, double mx, double my, double mz, cube_0 c);
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);
void remove_entity(entity** arr, int* memlen, int* len, int index);
void add_entity(entity** arr, int* memlen, int* len, entity ent);

View File

@ -180,11 +180,74 @@ void project_to_camera(double x0, double y0, double z0, double* rx, double* ry,
double zry = z*cos(rot_hz) - x*sin(rot_hz) ;
// rotate (x)
*rx = xry ;
*ry = yry*cos(rot_vt) - zry*sin(rot_vt) ;
*rz = zry*cos(rot_vt) + yry*sin(rot_vt) ;
if(rx != NULL) {*rx = xry ;}
if(ry != NULL) {*ry = yry*cos(rot_vt) - zry*sin(rot_vt) ;}
if(rz != NULL) {*rz = zry*cos(rot_vt) + yry*sin(rot_vt) ;}
}
// ---------------------------------------------------------------------------------------------------------------------------------- //
double pt_z_distance_to_camera(double x, double y, double z) {
double ret ;
project_to_camera(x, y, z, NULL, NULL, &ret) ;
return absf(ret) ;
}
double segment_z_distance_to_camera(double x0, double y0, double z0, double x1, double y1, double z1) {
double ret0 = pt_z_distance_to_camera(x0, y0, z0);
double ret1 = pt_z_distance_to_camera(x1, y1, z1);
double theta = -(
((x1 - x0) * (x0 - camx) + (y1 - y0) * (y0 - camy) + (z1 - z0) * (z0 - camz)) /
((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0) + (z1 - z0) * (z1 - z0))
); // projection factor of camera onto the line
return absf(convex_pt(ret0, ret1, mind(maxd(theta, 0.0), 1.0)));
}
double square_z_distance_to_camera(
double x0, double y0, double z0,
double x1, double y1, double z1,
double x2, double y2, double z2
) {
return maxd(segment_z_distance_to_camera(x0, y0, z0, x1, y1, z1), segment_z_distance_to_camera(x0, y0, z0, x2, y2, z2));
}
double cube_z_distance_to_camera(cube_0 cb) {
double dist_0 = square_z_distance_to_camera(
cb.x + cb.w, cb.y, cb.z,
cb.x + cb.w, cb.y + cb.h, cb.z,
cb.x + cb.w, cb.y, cb.z + cb.d
);
double dist_1 = square_z_distance_to_camera(
cb.x, cb.y, cb.z,
cb.x, cb.y + cb.h, cb.z,
cb.x, cb.y, cb.z + cb.d
);
double dist_2 = square_z_distance_to_camera(
cb.x, cb.y + cb.h, cb.z,
cb.x + cb.w, cb.y + cb.h, cb.z,
cb.x, cb.y + cb.h, cb.z + cb.d
);
double dist_3 = square_z_distance_to_camera(
cb.x, cb.y, cb.z,
cb.x + cb.w, cb.y, cb.z,
cb.x, cb.y, cb.z + cb.d
);
double dist_4 = square_z_distance_to_camera(
cb.x, cb.y, cb.z + cb.d,
cb.x + cb.w, cb.y, cb.z + cb.d,
cb.x, cb.y + cb.h, cb.z + cb.d
);
double dist_5 = square_z_distance_to_camera(
cb.x, cb.y, cb.z,
cb.x + cb.w, cb.y, cb.z,
cb.x, cb.y + cb.h, cb.z
);
return maxd(maxd(dist_0, dist_1), maxd(maxd(dist_2, dist_3), maxd(dist_4, dist_5)));
}
// ---------------------------------------------------------------------------------------------------------------------------------- //
void rotate_cube(double x0, double y0, double z0, double* rx, double* ry, double* rz, cube_0 cb) {
// align pt to (0, 0, 0)
double x = x0 - (cb.x + cb.w/2) ;
@ -605,13 +668,20 @@ void swap_ent(entity* arr, int i, int j) {
arr[j] = temp;
}
int multhz = 10.0 ;
double mult_x = 1.0 ;
double mult_y = 1.0 ;
double mult_z = 1.0 ;
void insertionSort_cb(cube_0* arr, int len) {
for(int k = 0; k < len; k++) {
int j = k-1 ;
while(j >= 0) {
if(distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, arr[j]) < distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, arr[j+1])) {
//if(distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, arr[j]) < distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, arr[j+1])) {
//if(distance_pt_cube_0_3d_max(camx, camy, camz, arr[j]) < distance_pt_cube_0_3d_max(camx, camy, camz, arr[j+1])) {
if(
distance_pt_cube_0_3d_max(camx, camy, camz, arr[j]) + distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, arr[j]) <
distance_pt_cube_0_3d_max(camx, camy, camz, arr[j+1]) + distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, arr[j+1])) {
//if(cube_z_distance_to_camera(arr[j]) < cube_z_distance_to_camera(arr[j+1])) {
swap_cb(arr, j, j+1);
j -= 1;
} else {
@ -625,7 +695,11 @@ void insertionSort_tp(teleporter* arr, int len) {
for(int k = 0; k < len; k++) {
int j = k-1 ;
while(j >= 0) {
if(distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, arr[j].hitbox) < distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, arr[j+1].hitbox)) {
//if(distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, arr[j].hitbox) < distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, arr[j+1].hitbox)) {
//if(distance_pt_cube_0_3d_max(camx, camy, camz, arr[j].hitbox) < distance_pt_cube_0_3d_max(camx, camy, camz, arr[j+1].hitbox)) {
if(distance_pt_cube_0_3d_max(camx, camy, camz, arr[j].hitbox) + distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, arr[j].hitbox) <
distance_pt_cube_0_3d_max(camx, camy, camz, arr[j+1].hitbox) + distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, arr[j+1].hitbox)) {
//if(cube_z_distance_to_camera(arr[j].hitbox) < cube_z_distance_to_camera(arr[j+1].hitbox)) {
swap_tp(arr, j, j+1);
j -= 1;
} else {
@ -639,7 +713,11 @@ void insertionSort_ent(entity* arr, int len) {
for(int k = 0; k < len; k++) {
int j = k-1 ;
while(j >= 0) {
if(distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, *(arr[j].pos)) < distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, *(arr[j+1].pos))) {
//if(distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, *(arr[j].pos)) < distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, *(arr[j+1].pos))) {
//if(distance_pt_cube_0_3d_max(camx, camy, camz, *(arr[j].pos)) < distance_pt_cube_0_3d_max(camx, camy, camz, *(arr[j+1].pos))) {
if(distance_pt_cube_0_3d_max(camx, camy, camz, *(arr[j].pos)) + distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, *(arr[j].pos)) <
distance_pt_cube_0_3d_max(camx, camy, camz, *(arr[j+1].pos)) + distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, *(arr[j+1].pos))) {
//if(cube_z_distance_to_camera(*(arr[j].pos)) < cube_z_distance_to_camera(*(arr[j+1].pos))) {
swap_ent(arr, j, j+1);
j -= 1;
} else {
@ -653,6 +731,8 @@ void drawCurrentRoom(SDL_Renderer* renderer) {
insertionSort_cb(current_room->map, current_room->map_size);
insertionSort_tp(current_room->tps, current_room->tps_size);
insertionSort_ent(current_room->ents, current_room->ent_len);
if(true || draw_type == 0) {
for(int k = 0; k < current_room->map_size; k++) {
drawFullCube(renderer, current_room->map[k]);
}
@ -662,8 +742,8 @@ void drawCurrentRoom(SDL_Renderer* renderer) {
for(int k = 0; k < current_room->tps_size; k++) {
drawFullCube(renderer, current_room->tps[k].hitbox);
}
/*int k_cb = 0 ;
} else if(draw_type == 1) {
int k_cb = 0 ;
int k_tp = 0 ;
int k_et = 0 ;
@ -675,29 +755,29 @@ void drawCurrentRoom(SDL_Renderer* renderer) {
while(k_cb < current_room->map_size || k_tp < current_room->tps_size || k_et < current_room->ent_len) {
if(updated == 7) {
if(k_et < current_room->ent_len) {
det = distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, *(current_room->ents[k_et].pos));
det = distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, *(current_room->ents[k_et].pos));
}
if(k_tp < current_room->tps_size) {
dtp = distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, current_room->tps[k_tp].hitbox);
dtp = distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, current_room->tps[k_tp].hitbox);
}
if(k_cb < current_room->map_size) {
dcb = distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, current_room->map[k_cb]);
dcb = distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, current_room->map[k_cb]);
}
} else if((updated/4)%2 == 1) {
if(k_et < current_room->ent_len) {
det = distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, *(current_room->ents[k_et].pos));
det = distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, *(current_room->ents[k_et].pos));
} else {
det = -1.0 ;
}
} else if((updated/2)%2 == 1) {
if(k_tp < current_room->tps_size) {
dtp = distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, current_room->tps[k_tp].hitbox);
dtp = distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, current_room->tps[k_tp].hitbox);
} else {
dtp = -1.0 ;
}
} else if(updated%2 == 1) {
if(k_cb < current_room->map_size) {
dcb = distance_pt_cube_0_3d_weighted(camx, camy, camz, 1.0, multhz, 1.0, current_room->map[k_cb]);
dcb = distance_pt_cube_0_3d_weighted(camx, camy, camz, mult_x, mult_y, mult_z, current_room->map[k_cb]);
} else {
dcb = -1.0 ;
}
@ -708,16 +788,19 @@ void drawCurrentRoom(SDL_Renderer* renderer) {
drawFullCube(renderer, current_room->map[k_cb]);
updated += 1 ;
k_cb += 1;
} else if(mn == dtp) {
}
if(mn == dtp) {
drawFullCube(renderer, current_room->tps[k_tp].hitbox);
updated += 2 ;
k_tp += 1;
} else {
}
if(mn == det) {
drawFullCube(renderer, *(current_room->ents[k_et].pos));
updated += 4;
k_et += 1;
}
}*/
}
}
}
// -------------------------------------------------------------------------------------------------------------------------------- //

View File

@ -21,6 +21,15 @@ void drawCharToRenderer(SDL_Renderer* renderer, imgs data, char c, int X, int Y,
void drawNumberToRenderer(SDL_Renderer* renderer, imgs data, int n, int X, int Y, int W, int H, int Woffset);
void drawStringToRenderer(SDL_Renderer* renderer, imgs data, char* s, int X, int Y, int W, int H);
double pt_z_distance_to_camera(double x, double y, double z);
double segment_z_distance_to_camera(double x0, double y0, double z0, double x1, double y1, double z1);
double square_z_distance_to_camera(
double x0, double y0, double z0,
double x1, double y1, double z1,
double x2, double y2, double z2
);
double cube_z_distance_to_camera(cube_0 cb);
void axialRotation_X0(double* y, double* z, double theta);
void axialRotation_X(double* y, double* z, double theta, double cst_y, double cst_z);
void axialRotation_Y0(double* x, double* z, double theta);

View File

@ -18,7 +18,7 @@
#include "move.h"
// ---------------------------------------------------------------------------------------------------- //
double sensitivity = 0.22 ;
double sensitivity = 0.12 ;
double fov = 90.0 ;
double speed = 0.22 ;
double min_dist = 0.7 ;
@ -33,6 +33,8 @@ double rot_vt ;
double tan_fov ;
int draw_type ;
bool has_changed ;
void init_csts() {
@ -41,6 +43,7 @@ void init_csts() {
camz = 3.0 ;
rot_hz = 0.0 ;
rot_vt = 180.0 ;
draw_type = 0 ;
tan_fov = tan((fov * 3.14159 / 180.0) / 2.0) ;
}
@ -94,6 +97,7 @@ bool is_colliding(float dtime) {
return false ;
}
bool pass = true ;
void playerActions(float dtime) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
@ -168,6 +172,15 @@ void playerActions(float dtime) {
}
}
if(state[SDL_SCANCODE_SPACE] == 1) {
if(pass) {
pass = false ;
draw_type = (1+draw_type)%2 ;
}
} else {
pass = true ;
}
if(state[SDL_SCANCODE_T] == 1) {
fprintf(stderr, "Killed.\n") ;
exit(1) ;

View File

@ -105,4 +105,6 @@ extern int* drawOrder ;
extern int coins ;
extern int draw_type ;
#endif