Reversing 3 commits

This commit is contained in:
Alexandre 2025-01-18 17:15:00 +01:00
parent 0595f9fb52
commit 16cf96d8ac
13 changed files with 10 additions and 609 deletions

BIN
bin/back

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -188,10 +188,6 @@ double convex_pt(double a, double b, double theta) {
return (a+(b-a)*theta) ; return (a+(b-a)*theta) ;
} }
double convex_tri(double a, double b, double c, double tha, double thb, double thc) {
return (a*tha + b*thb + c*thc) ;
}
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1) { double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1) {
return sqrt((x1 - x0)*(x1 - x0)+(y1 - y0)*(y1 - y0)+(z1 - z0)*(z1 - z0)) ; return sqrt((x1 - x0)*(x1 - x0)+(y1 - y0)*(y1 - y0)+(z1 - z0)*(z1 - z0)) ;
} }
@ -301,204 +297,6 @@ 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) ;
} }
// ---------------- //
// intersects = !((a.max < b.min) || (b.max < a.min))
double distance_seg_seg_1d(double s0, double e0, double s1, double e1) {
double theta_s0 = -(((e1 - s1) * (s1 - s0)) / ((e1 - s1) * (e1 - s1)));
double theta_e0 = -(((e1 - s1) * (s1 - e0)) / ((e1 - s1) * (e1 - s1)));
if(
(theta_s0 >= 0.0 && theta_s0 <= 1.0) || // s0 is in [s1, e1]
(theta_e0 >= 0 && theta_e0 <= 1.0) || // s1 is in [s1, e1]
(theta_s0 < 0.0 && theta_e0 > 1.0) || // inclusion
(theta_e0 < 0.0 && theta_s0 > 1.0)) // inclusion
{
return 0.0;
} else {
double dist0 = mind(absf(theta_s0), absf(1.0 - theta_s0)) ;
double dist1 = mind(absf(theta_e0), absf(1.0 - theta_e0)) ; // get the closest theta to [0, 1]
return mind(dist1, dist0)*absf(e1 - s1);
}
}
bool intersects_seg_seg_1d(double s0, double e0, double s1, double e1) {
int amin = mind(s0, e0);
int amax = maxd(s0, e0);
int bmin = mind(s1, e1);
int bmax = maxd(s1, e1);
return !((amax < bmin) || (bmax < amin));
}
double distance_seg_seg_2d(
double s0x, double s0y,
double e0x, double e0y,
double s1x, double s1y,
double e1x, double e1y
) {
// basically ||.||_1
return (
distance_seg_seg_1d(s0x, e0x, s1x, e1x) +
distance_seg_seg_1d(s0y, e0y, s1y, e1y)
);
}
bool intersects_seg_seg_2d(
double x1, double y1,
double x2, double y2,
double x3, double y3,
double x4, double y4,
double* rett, double* retu
) {
if(absf((x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4)) < 0.01) {
return false;
}
double t =
((x1 - x3)*(y3 - y4) - (y1 - y3)*(x3 - x4))/
((x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4));
double u = -(
((x1 - x2)*(y1 - y3) - (y1 - y2)*(x1 - x3))/
((x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4)));
/*if(absf((y2 - y1)*(x4 - x3) - (y4 - y3)*(x2 - x1)) < 0.01) {
return false;
}
double t =
((y4 - y3)*(x1 - x3) - (y1 - y3)*(x4 - x3))/
((y2 - y1)*(x4 - x3) - (y4 - y3)*(x2 - x1));
double u =
((y2 - y1)*(x3 - x1) - (x2 - x1)*(y3 - y1))/
((x2 - x1)*(y4 - y3) - (y2 - y1)*(x4 - x3));*/
if(rett != NULL) {*rett = t;}
if(retu != NULL) {*retu = u;}
return ((0.0 <= t && t <= 1.0) && (0.0 <= u && u <= 1.0));
}
double distance_seg_seg_3d(
double s0x, double s0y, double s0z,
double e0x, double e0y, double e0z,
double s1x, double s1y, double s1z,
double e1x, double e1y, double e1z
) {
// same
return (
distance_seg_seg_1d(s0x, e0x, s1x, e1x) +
distance_seg_seg_1d(s0y, e0y, s1y, e1y) +
distance_seg_seg_1d(s0z, e0z, s1z, e1z)
);
}
double distance_poly_poly_2d(pt_2d* t1, int len_1, pt_2d* t2, int len_2) {
double res = 10000.0 ;
if(len_1 == 0 || len_2 == 0) {
return 727.27 ; // arbitrary
}
for(int k1 = 0; k1 < len_1; k1++) {
for(int k2 = 0; k2 < len_2; k2++) {
res = mind(res, distance_seg_seg_2d(
t1[k1].x, t1[k1].y, t1[(k1+1)%len_1].x, t1[(k1+1)%len_1].y,
t2[k2].x, t2[k2].y, t2[(k2+1)%len_2].x, t2[(k2+1)%len_2].y
));
}
}
return res;
}
double sign_triangle(pt_2d p1, pt_2d p2, pt_2d p3) {
return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
}
pt_2d to_pt2d(double x0, double y0, double z0) {
pt_2d res;
res.x = x0;
res.y = y0;
res.z = z0;
return res;
}
double dot_product_3D(pt_2d p1, pt_2d p2) {
return p1.x*p2.x + p1.y*p2.y + p1.z*p2.z ;
}
double dot_product_2D(pt_2d p1, pt_2d p2) {
return p1.x*p2.x + p1.y*p2.y ;
}
void return_barycentric(pt_2d p, pt_2d a, pt_2d b, pt_2d c, double* u, double* v, double* w) {
// get barycentric coords of p inside ABC triangle
pt_2d v0 = to_pt2d(b.x - a.x, b.y - a.y, b.z - a.z);
pt_2d v1 = to_pt2d(c.x - a.x, c.y - a.y, c.z - a.z);
pt_2d v2 = to_pt2d(p.x - a.x, p.y - a.y, p.z - a.z);
double d00 = dot_product_2D(v0, v0);
double d01 = dot_product_2D(v0, v1);
double d11 = dot_product_2D(v1, v1);
double d20 = dot_product_2D(v2, v0);
double d21 = dot_product_2D(v2, v1);
double denom = d00 * d11 - d01 * d01;
*v = (d11 * d20 - d01 * d21) / denom;
*w = (d00 * d21 - d01 * d20) / denom;
*u = 1.0 - *v - *w;
}
bool pointInTriangle (pt_2d pt, pt_2d v1, pt_2d v2, pt_2d v3, double* thetaA, double* thetaB, double* thetaC) {
double d1, d2, d3;
bool has_neg, has_pos;
d1 = sign_triangle(pt, v1, v2);
d2 = sign_triangle(pt, v2, v3);
d3 = sign_triangle(pt, v3, v1);
has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0);
has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0);
if(!(has_neg && has_pos)) {
return_barycentric(pt, v1, v2, v3, thetaA, thetaB, thetaC);
return true;
}
return false;
}
double thA, thB, thC;
int intersects_poly_poly_2d(pt_2d* t1, int len_1, pt_2d* t2, int len_2, int* retk1, int* retk2, double* rettheta1, double* rettheta2) {
if(len_1 == 0 || len_2 == 0) {
return 0 ; // arbitrary
}
for(int k1 = 0; k1 < len_1; k1++) { // Segment intersection [FAULTY]
for(int k2 = 0; k2 < len_2; k2++) {
if(intersects_seg_seg_2d(
t1[k1].x, t1[k1].y, t1[(k1+1)%len_1].x, t1[(k1+1)%len_1].y,
t2[k2].x, t2[k2].y, t2[(k2+1)%len_2].x, t2[(k2+1)%len_2].y,
rettheta1, rettheta2
)) {
if(retk1 != NULL) {*retk1 = k1;}
if(retk2 != NULL) {*retk2 = k2;}
return 1;
}
}
}
for(int k1 = 0; k1 < len_1; k1++) { // T1 is inside T2
for(int k2 = 0; k2 < len_2/3; k2++) {
if(pointInTriangle(t1[k1], t2[3*k2], t2[3*k2+1], t2[3*k2+2], &thA, &thB, &thC)) {
if(rettheta1 != NULL) {*rettheta1 = t1[k1].z;}
if(rettheta2 != NULL) {*rettheta2 = convex_tri(t2[3*k2].z, t2[3*k2+1].z, t2[3*k2+2].z, thA, thB, thC);}
return 2;
}
}
}
for(int k2 = 0; k2 < len_2; k2++) { // T2 is inside T1
for(int k1 = 0; k1 < len_1/3; k1++) {
if(pointInTriangle(t2[k2], t1[3*k1], t1[3*k1+1], t1[3*k1+2], &thA, &thB, &thC)) {
if(rettheta1 != NULL) {*rettheta1 = convex_tri(t1[3*k1].z, t1[3*k1+1].z, t1[3*3*k1+2].z, thA, thB, thC);}
if(rettheta2 != NULL) {*rettheta2 = t2[k2].z;}
return 2;
}
}
}
return 0;
}
// ------------------------------------------------------------------------------------------------ // // ------------------------------------------------------------------------------------------------ //
void remove_entity(entity** arr, int* memlen, int* len, int index) { void remove_entity(entity** arr, int* memlen, int* len, int index) {

View File

@ -11,7 +11,6 @@ double mind(double a, double b);
double maxd(double a, double b); double maxd(double a, double b);
double absf(double n); double absf(double n);
int convex_seg(int x1, int x2, double theta); int convex_seg(int x1, int x2, double theta);
double convex_tri(double a, double b, double c, double tha, double thb, double thc);
bool is_an_integer(char c); bool is_an_integer(char c);
double to_double(int n); double to_double(int n);
int to_int(double n); int to_int(double n);
@ -30,7 +29,6 @@ teleporter create_teleporter(
double convex_pt(double a, double b, double theta); double convex_pt(double a, double b, double theta);
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1); double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1);
double distance_pt_seg_3d(double x, double y, double z, double sx, double sy, double sz, double ex, double ey, double ez); double distance_pt_seg_3d(double x, double y, double z, double sx, double sy, double sz, double ex, double ey, double ez);
pt_2d to_pt2d(double x0, double y0, double z0);
double convex_pt(double a, double b, double theta); double convex_pt(double a, double b, double theta);
double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1) ; double distance_pt_pt_3d(double x0, double y0, double z0, double x1, double y1, double z1) ;
@ -46,32 +44,6 @@ 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 distance_seg_seg_1d(double s0, double e0, double s1, double e1);
double distance_seg_seg_2d(
double s0x, double s0y,
double e0x, double e0y,
double s1x, double s1y,
double e1x, double e1y
);
double distance_seg_seg_3d(
double s0x, double s0y, double s0z,
double e0x, double e0y, double e0z,
double s1x, double s1y, double s1z,
double e1x, double e1y, double e1z
);
double distance_poly_poly_2d(pt_2d* t1, int len_1, pt_2d* t2, int len_2);
bool intersects_seg_seg_1d(double s0, double e0, double s1, double e1);
bool intersects_seg_seg_2d(
double x1, double y1,
double x2, double y2,
double x3, double y3,
double x4, double y4,
double* rett, double* retu
);
bool pointInTriangle (pt_2d pt, pt_2d v1, pt_2d v2, pt_2d v3, double* thetaA, double* thetaB, double* thetaC);
int intersects_poly_poly_2d(pt_2d* t1, int len_1, pt_2d* t2, int len_2, int* retk1, int* retk2, double* rettheta1, double* rettheta2);
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);

View File

@ -20,26 +20,11 @@
#include "display.h" #include "display.h"
int* drawOrder; int* drawOrder;
int* drawOrder2;
pt_2d* tri1 ;
pt_2d* tri2 ;
cube_0* toDraw ;
int* toDrawVisited ;
int toDrawLen ;
double draw_constant = 0.4 ; double draw_constant = 0.4 ;
void init_draworder() { void init_draworder() {
drawOrder = malloc(sizeof(int)*6) ; drawOrder = malloc(sizeof(int)*6) ;
drawOrder2 = malloc(sizeof(int)*6) ;
tri1 = malloc(sizeof(pt_2d)*6);
tri2 = malloc(sizeof(pt_2d)*6);
toDraw = malloc(sizeof(cube_0)*2048);
toDrawVisited = malloc(sizeof(int)*2048);
for(int k = 0; k < 2048; k++) {
toDrawVisited[k] = 0;
}
toDrawLen = 0;
} }
void updateRenderer(SDL_Renderer* renderer) { void updateRenderer(SDL_Renderer* renderer) {
@ -452,57 +437,6 @@ int surfaceDrawOrder(double x0, double y0, double z0, cube_0 cb) {
} }
} }
int surfaceDrawOrder2(double x0, double y0, double z0, cube_0 cb) {
// returns the number of surfaces that should be drawn, as well as filling drawOrder2 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) {
drawOrder2[id] = 0 ;
id += 1 ;
} else if(xrx < -cb.w/2.0) {
drawOrder2[id] = 1 ;
id += 1 ;
}
if(yrx > cb.h/2.0) {
drawOrder2[id] = 2 ;
id += 1 ;
} else if(yrx < -cb.h/2.0) {
drawOrder2[id] = 3 ;
id += 1 ;
}
if(zrx > cb.d/2.0) {
drawOrder2[id] = 4 ;
id += 1 ;
} else if(zrx < -cb.d/2.0) {
drawOrder2[id] = 5 ;
id += 1 ;
}
if(id == 0) { // inside the cube
for(int k = 0; k < 6; k++) {
drawOrder2[k] = k ;
}
return 6;
} else {
return id ;
}
}
SDL_Vertex construct_vertex(double px, double py, int r, int g, int b) { SDL_Vertex construct_vertex(double px, double py, int r, int g, int b) {
SDL_Vertex vtx ; SDL_Vertex vtx ;
vtx.color.r = r ; vtx.color.r = r ;
@ -523,7 +457,6 @@ double fpx0; double fpy0; double fpz0;
double fpx1; double fpy1; double fpz1; double fpx1; double fpy1; double fpz1;
double mpx0; double mpy0; double mpz0; double mpx0; double mpy0; double mpz0;
double mpx1; double mpy1; double mpz1; double mpx1; double mpy1; double mpz1;
void renderTriangle( void renderTriangle(
SDL_Renderer* renderer, SDL_Renderer* renderer,
double x0, double y0, double z0, double x0, double y0, double z0,
@ -648,263 +581,6 @@ void renderTriangle(
} }
} }
pt_2d to_fpoint(double x0, double y0, double z0) {
pt_2d res;
res.x = x0;
res.y = y0;
res.z = z0;
return res;
}
int returnTriangle(
double x0, double y0, double z0,
double x1, double y1, double z1,
double x2, double y2, double z2,
pt_2d* retarr
) {
project_to_camera(x0, y0, z0, &px0, &py0, &pz0);
project_to_camera(x1, y1, z1, &px1, &py1, &pz1);
project_to_camera(x2, y2, z2, &px2, &py2, &pz2);
if(pz0 >= draw_constant && pz1 >= draw_constant && pz2 >= draw_constant) {
retarr[0] = to_fpoint(1500.0 * (1.0 + (px0 / (1.5 * pz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py0 / (pz0 * tan_fov))) / 2.0, pz0);
retarr[1] = to_fpoint(1500.0 * (1.0 + (px1 / (1.5 * pz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py1 / (pz1 * tan_fov))) / 2.0, pz1);
retarr[2] = to_fpoint(1500.0 * (1.0 + (px2 / (1.5 * pz2 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py2 / (pz2 * tan_fov))) / 2.0, pz2);
return 3;
} else if((pz0 >= draw_constant) + (pz1 >= draw_constant) + (pz2 >= draw_constant) == 2) {
if(pz0 < draw_constant) {
// pz1 >= draw_constant and pz2 >+ draw_constant
fpx0 = px1 ; fpy0 = py1 ; fpz0 = pz1 ;
fpx1 = px2 ; fpy1 = py2 ; fpz1 = pz2 ;
// 1-0 segment
project_to_camera(
convex_pt(x1, x0, (pz1 - draw_constant)/(pz1 - pz0)),
convex_pt(y1, y0, (pz1 - draw_constant)/(pz1 - pz0)),
convex_pt(z1, z0, (pz1 - draw_constant)/(pz1 - pz0)),
&mpx0, &mpy0, &mpz0) ;
// 0-2 segment
project_to_camera(
convex_pt(x2, x0, (pz2 - draw_constant)/(pz2 - pz0)),
convex_pt(y2, y0, (pz2 - draw_constant)/(pz2 - pz0)),
convex_pt(z2, z0, (pz2 - draw_constant)/(pz2 - pz0)),
&mpx1, &mpy1, &mpz1) ;
} else if(pz1 < draw_constant) {
// pz0 >= draw_constant and pz2 >+ draw_constant
fpx0 = px0 ; fpy0 = py0 ; fpz0 = pz0 ;
fpx1 = px2 ; fpy1 = py2 ; fpz1 = pz2 ;
// 0-1 segment
project_to_camera(
convex_pt(x0, x1, (pz0 - draw_constant)/(pz0 - pz1)),
convex_pt(y0, y1, (pz0 - draw_constant)/(pz0 - pz1)),
convex_pt(z0, z1, (pz0 - draw_constant)/(pz0 - pz1)),
&mpx0, &mpy0, &mpz0) ;
// 1-2 segment
project_to_camera(
convex_pt(x2, x1, (pz2 - draw_constant)/(pz2 - pz1)),
convex_pt(y2, y1, (pz2 - draw_constant)/(pz2 - pz1)),
convex_pt(z2, z1, (pz2 - draw_constant)/(pz2 - pz1)),
&mpx1, &mpy1, &mpz1) ;
} else /*if(pz2 < draw_constant)*/ {
// pz1 >= draw_constant and pz0 >+ draw_constant
fpx0 = px0 ; fpy0 = py0 ; fpz0 = pz0 ;
fpx1 = px1 ; fpy1 = py1 ; fpz1 = pz1 ;
// 0-2 segment
project_to_camera(
convex_pt(x0, x2, (pz0 - draw_constant)/(pz0 - pz2)),
convex_pt(y0, y2, (pz0 - draw_constant)/(pz0 - pz2)),
convex_pt(z0, z2, (pz0 - draw_constant)/(pz0 - pz2)),
&mpx0, &mpy0, &mpz0) ;
// 1-2 segment
project_to_camera(
convex_pt(x1, x2, (pz1 - draw_constant)/(pz1 - pz2)),
convex_pt(y1, y2, (pz1 - draw_constant)/(pz1 - pz2)),
convex_pt(z1, z2, (pz1 - draw_constant)/(pz1 - pz2)),
&mpx1, &mpy1, &mpz1) ;
}
retarr[0] = to_fpoint(1500.0 * (1.0 + (fpx0 / (1.5 * fpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy0 / (fpz0 * tan_fov))) / 2.0, fpz0);
retarr[1] = to_fpoint(1500.0 * (1.0 + (mpx0 / (1.5 * mpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy0 / (mpz0 * tan_fov))) / 2.0, mpz0);
retarr[2] = to_fpoint(1500.0 * (1.0 + (fpx1 / (1.5 * fpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy1 / (fpz1 * tan_fov))) / 2.0, fpz1);
retarr[3] = to_fpoint(1500.0 * (1.0 + (mpx0 / (1.5 * mpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy0 / (mpz0 * tan_fov))) / 2.0, mpz0);
retarr[4] = to_fpoint(1500.0 * (1.0 + (mpx1 / (1.5 * mpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy1 / (mpz1 * tan_fov))) / 2.0, mpz1);
retarr[5] = to_fpoint(1500.0 * (1.0 + (fpx1 / (1.5 * fpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy1 / (fpz1 * tan_fov))) / 2.0, fpz1);
return 6;
} else if((pz0 >= draw_constant) + (pz1 >= draw_constant) + (pz2 >= draw_constant) == 1) {
if(pz0 >= draw_constant) {
project_to_camera(
convex_pt(x0, x1, (pz0 - draw_constant)/(pz0 - pz1)),
convex_pt(y0, y1, (pz0 - draw_constant)/(pz0 - pz1)),
convex_pt(z0, z1, (pz0 - draw_constant)/(pz0 - pz1)),
&px1, &py1, &pz1);
project_to_camera(
convex_pt(x0, x2, (pz0 - draw_constant)/(pz0 - pz2)),
convex_pt(y0, y2, (pz0 - draw_constant)/(pz0 - pz2)),
convex_pt(z0, z2, (pz0 - draw_constant)/(pz0 - pz2)),
&px2, &py2, &pz2);
} else if(pz1 >= draw_constant) {
project_to_camera(
convex_pt(x1, x0, (pz1 - draw_constant)/(pz1 - pz0)),
convex_pt(y1, y0, (pz1 - draw_constant)/(pz1 - pz0)),
convex_pt(z1, z0, (pz1 - draw_constant)/(pz1 - pz0)),
&px0, &py0, &pz0);
project_to_camera(
convex_pt(x1, x2, (pz1 - draw_constant)/(pz1 - pz2)),
convex_pt(y1, y2, (pz1 - draw_constant)/(pz1 - pz2)),
convex_pt(z1, z2, (pz1 - draw_constant)/(pz1 - pz2)),
&px2, &py2, &pz2);
} else if(pz2 >= draw_constant) {
project_to_camera(
convex_pt(x2, x0, (pz2 - draw_constant)/(pz2 - pz0)),
convex_pt(y2, y0, (pz2 - draw_constant)/(pz2 - pz0)),
convex_pt(z2, z0, (pz2 - draw_constant)/(pz2 - pz0)),
&px0, &py0, &pz0);
project_to_camera(
convex_pt(x2, x1, (pz2 - draw_constant)/(pz2 - pz1)),
convex_pt(y2, y1, (pz2 - draw_constant)/(pz2 - pz1)),
convex_pt(z2, z1, (pz2 - draw_constant)/(pz2 - pz1)),
&px1, &py1, &pz1);
}
retarr[0] = to_fpoint(1500.0 * (1.0 + (px0 / (1.5 * pz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py0 / (pz0 * tan_fov))) / 2.0, pz0);
retarr[1] = to_fpoint(1500.0 * (1.0 + (px1 / (1.5 * pz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py1 / (pz1 * tan_fov))) / 2.0, pz1);
retarr[2] = to_fpoint(1500.0 * (1.0 + (px2 / (1.5 * pz2 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py2 / (pz2 * tan_fov))) / 2.0, pz2);
return 3;
} else {
return 0;
}
}
int fillPolygon(int sf, cube_0 c, int trig, pt_2d* ret) {
// trig is either 0 or 1
// returns the length of the result
if(sf == 0 || sf == 1) { // x
if(trig == 0) {
return returnTriangle(
c.x + c.w*(sf==0), c.y, c.z,
c.x + c.w*(sf==0), c.y + c.h, c.z,
c.x + c.w*(sf==0), c.y + c.h, c.z + c.d,
ret
);
} else {
return returnTriangle(
c.x + c.w*(sf==0), c.y, c.z,
c.x + c.w*(sf==0), c.y, c.z + c.d,
c.x + c.w*(sf==0), c.y + c.h, c.z + c.d,
ret
);
}
} else if(sf == 2 || sf == 3) { // y
if(trig == 0) {
return returnTriangle(
c.x, c.y + c.h*(sf==2), c.z,
c.x + c.w, c.y + c.h*(sf==2), c.z,
c.x + c.w, c.y + c.h*(sf==2), c.z + c.d,
ret
);
} else {
return returnTriangle(
c.x, c.y + c.h*(sf==2), c.z,
c.x, c.y + c.h*(sf==2), c.z + c.d,
c.x + c.w, c.y + c.h*(sf==2), c.z + c.d,
ret
);
}
} else { // z
if(trig == 0) {
return returnTriangle(
c.x, c.y, c.z + c.d*(sf==4),
c.x + c.w, c.y, c.z + c.d*(sf==4),
c.x + c.w, c.y + c.h, c.z + c.d*(sf==4),
ret
);
} else {
return returnTriangle(
c.x, c.y, c.z + c.d*(sf==4),
c.x, c.y + c.h, c.z + c.d*(sf==4),
c.x + c.w, c.y + c.h, c.z + c.d*(sf==4),
ret
);
}
}
}
bool isCollidingSfOfCube(int sf1, cube_0 c1, int sf2, cube_0 c2, double* dz) {
int k1, k2;
double th1, th2;
for(int n = 0; n < 4; n++) {
int len1 = fillPolygon(sf1, c1, n%2, tri1);
int len2 = fillPolygon(sf2, c2, n/2, tri2);
int itgt = intersects_poly_poly_2d(tri1, len1, tri2, len2, &k1, &k2, &th1, &th2);
if(itgt == 1) {
if(dz != NULL) {
double depth_1 = convex_pt(tri1[k1].z, tri1[(k1+1)%len1].z, th1);
double depth_2 = convex_pt(tri2[k2].z, tri2[(k2+1)%len2].z, th2);
if(absf(depth_2 - depth_1) > 0.01 && depth_1 > 0.01 && depth_2 > 0.01) {
*dz = depth_2 - depth_1;
if(*dz > 0.0001) {
printf("-1-%lf-\n", *dz);
}
return true;
}
} else {
return true;
}
} else if(itgt == 2) {
if(dz != NULL) {
if(absf(th2 - th1) >= 0.01 && th2 > 0.01 && th1 > 0.01) {
*dz = th2 - th1 ;
if(*dz > 0.0001) {
printf("-2-%lf-\n", *dz);
}
return true;
}
} else {
return true;
}
}
}
return false ;
}
bool compat(int k1, int k2) {
return
(k1 == 0 && k2 == 1) ||
(k1 == 1 && k2 == 0) ||
(k1 == 2 && k2 == 3) ||
(k1 == 3 && k2 == 2) ||
(k1 == 4 && k2 == 5) ||
(k1 == 5 && k2 == 4) ;
}
double is_overlapping_all(cube_0 c1, cube_0 c2) {
// 0 if no overlap >0 if c1 is in front of c2, <0 if c2 is in front of c1
double dz = 1.0 ;
for(int k1 = 0; k1 < 6; k1++) {
for(int k2 = 0; k2 < 6; k2++) {
if(compat(k1, k2) && isCollidingSfOfCube(k1, c1, k2, c2, &dz)) {
return dz;
}
}
}
return 0.0;
}
double is_overlapping(cube_0 c1, cube_0 c2) {
// 0 if no overlap >0 if c1 is in front of c2, <0 if c2 is in front of c1
int sfToDraw1 = surfaceDrawOrder(camx, camy, camz, c1);
int sfToDraw2 = surfaceDrawOrder2(camx, camy, camz, c2);
for(int k1 = 0; k1 < sfToDraw1; k1 ++) {
for(int k2 = 0; k2 < sfToDraw2; k2 ++) {
if(isCollidingSfOfCube(drawOrder[k1], c1, drawOrder2[k2], c2, NULL)) {
return 1.0 ;
}
}
}
return 0.0;
}
void renderTriangleRotated( void renderTriangleRotated(
SDL_Renderer* renderer, SDL_Renderer* renderer,
double x0, double y0, double z0, double x0, double y0, double z0,
@ -1051,50 +727,14 @@ void insertionSort_ent(entity* arr, int len) {
} }
} }
void slide_cb(cube_0* arr, int left, int right) {
for(int k = right-1; k >= left && k >= 0; k--) {
swap_cb(arr, k, k+1);
}
}
void visit(int k, cube_0* arr, int len, int vflag, int* cur) {
if(toDrawVisited[k] != vflag) {
printf("--> %d\n", k);
toDrawVisited[k] = vflag;
for(int k2 = 0; k2 < len; k2++) {
if(toDrawVisited[k2] != vflag) {
if(is_overlapping_all(arr[k], arr[k2]) >= 0.00001) {
visit(k2, arr, len, vflag, cur);
}
}
}
printf("[%d]\n", k);
toDraw[*cur] = arr[k];
*cur += 1;
}
}
void insertionDepthSort_cb(cube_0* arr, int len) {
toDrawLen = 0;
int vflag = (toDrawVisited[0]+1)%4096;
int current = 0;
for(int k = 0; k < len; k++) {
visit(k, arr, len, vflag, &current);
}
printf("\n");
}
void drawCurrentRoom(SDL_Renderer* renderer) { void drawCurrentRoom(SDL_Renderer* renderer) {
//insertionSort_cb(current_room->map, current_room->map_size); insertionSort_cb(current_room->map, current_room->map_size);
insertionDepthSort_cb(current_room->map, current_room->map_size);
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);
printf("\n\n\n");
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++) {
//drawFullCube(renderer, current_room->map[k]); drawFullCube(renderer, current_room->map[k]);
drawFullCube(renderer, toDraw[k]);
} }
for(int k = 0; k < current_room->ent_len; k++) { for(int k = 0; k < current_room->ent_len; k++) {
drawFullCube(renderer, *(current_room->ents[k].pos)); drawFullCube(renderer, *(current_room->ents[k].pos));

View File

@ -6,12 +6,6 @@ typedef struct imgs {
SDL_Texture** arr; SDL_Texture** arr;
} imgs ; } imgs ;
typedef struct pt_2d {
double x;
double y;
double z;
} pt_2d ;
struct cube_0 { struct cube_0 {
int red; int green; int blue ; int red; int green; int blue ;
double x; double x;
@ -113,7 +107,4 @@ extern int coins ;
extern int draw_type ; extern int draw_type ;
extern pt_2d* tri1 ;
extern pt_2d* tri2 ;
#endif #endif

View File

@ -22,6 +22,6 @@ Entities :
[0.0, 10.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0] [0.0, 10.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 193, 192, 0, 1, 0]
Weight : Weight :
15 10
$ $