This commit is contained in:
Alexandre 2025-01-26 19:26:24 +01:00
parent 5e3ff72046
commit 335a00a909
20 changed files with 127 additions and 48 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.

Binary file not shown.

View File

@ -105,17 +105,17 @@ double convex_tri(double a, double tha, double b, double thb, double c, double t
pt_2d convex_pt2d(pt_2d A, pt_2d B, double theta) {
pt_2d res;
res.x = convex_pt(A.x, B.x, theta);
res.y = convex_pt(A.y, B.y, theta);
res.z = convex_pt(A.z, B.z, theta);
res.x = A.x+(B.x-A.x)*theta;
res.y = A.y+(B.y-A.y)*theta;
res.z = A.z+(B.z-A.z)*theta;
return res;
}
pt_2d convex_pt2d_tri(pt_2d A, double tha, pt_2d B, double thb, pt_2d C, double thc) {
pt_2d res;
res.x = convex_tri(A.x, tha, B.x, thb, C.x, thc);
res.y = convex_tri(A.y, tha, B.y, thb, C.y, thc);
res.z = convex_tri(A.z, tha, B.z, thb, C.z, thc);
res.x = A.x*tha+B.x*thb+C.x*thc;
res.y = A.y*tha+B.y*thb+C.y*thc;
res.z = A.z*tha+B.z*thb+C.z*thc;
return res;
}
@ -225,7 +225,15 @@ teleporter create_teleporter(
// ------------------------------------------------------------------------------------------------ //
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));
}
double distance_pt_pt_2d_sq(double x0, double y0, double x1, double y1) {
return (x1 - x0)*(x1 - x0)+(y1 - y0)*(y1 - y0);
}
double distance_pt_pt_3d_sq(double x0, double y0, double z0, double x1, double y1, double z1) {
return (x1 - x0)*(x1 - x0)+(y1 - y0)*(y1 - y0)+(z1 - z0)*(z1 - z0);
}
double proj_distance_to_camera(double x, double y, double z) {
@ -236,6 +244,10 @@ double proj_pt_distance_to_camera(pt_2d p) {
return proj_distance_to_camera(p.x, p.y, p.z);
}
double proj_pt_distance_to_camera_sq(pt_2d p) {
return (p.x*p.x + p.y*p.y + p.z*p.z);
}
double distance_pt_seg_3d(double x, double y, double z, double sx, double sy, double sz, double ex, double ey, double ez) {
double theta = -(
((ex - sx) * (sx - x) + (ey - sy) * (sy - y) + (ez - sz) * (sz - z)) /

View File

@ -41,10 +41,13 @@ 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 proj_distance_to_camera(double x, double y, double z);
double proj_pt_distance_to_camera(pt_2d p);
double proj_pt_distance_to_camera_sq(pt_2d p);
double distance_pt_seg_3d(double x, double y, double z, double sx, double sy, double sz, double ex, double ey, double ez);
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_pt_2d_sq(double x0, double y0, double x1, double y1);
double distance_pt_pt_3d_sq(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_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);

View File

@ -21,7 +21,7 @@
#include "generation.h"
#include "display.h"
int flashlight = 0 ;
int flashlight = 10 ;
int MAX_SIZE = 8192 ;
@ -40,8 +40,11 @@ int* blues ;
int* triangles_order ;
bool* visited_tri ;
bool* triangles_shr ;
int visited_i ;
double shrink_distance;
void init_draworder() {
drawOrder = malloc(sizeof(int)*6) ;
draw_constant = 0.4 ;
@ -53,13 +56,17 @@ void init_draworder() {
triangles_order = malloc(sizeof(int)*MAX_SIZE) ;
//triangles_areas = malloc(sizeof(double)*MAX_SIZE) ;
visited_tri = malloc(sizeof(bool)*MAX_SIZE) ;
triangles_shr = malloc(sizeof(bool)*MAX_SIZE) ;
for(int k = 0; k < MAX_SIZE; k++) {
triangles_to_render[k] = malloc(sizeof(pt_2d)*3);
triangles_og_coords[k] = malloc(sizeof(pt_2d)*3);
triangles_order[k] = k;
visited_tri[k] = false;
triangles_shr[k] = false;
}
triangles_i = 0;
visited_i = 0;
shrink_distance = 800.0;
}
void updateRenderer(SDL_Renderer* renderer) {
@ -437,7 +444,6 @@ void renderTriangleNoProject(
double px2, double py2, double pz2,
int red, int green, int blue, bool debug
) {
const SDL_Vertex vtxs[3] = {
construct_vertex(px0, py0, max(red - (int)(flashlight*pz0), 0), max(green - (int)(flashlight*pz0), 0), max(blue - (int)(flashlight*pz0), 0), 255),
construct_vertex(px1, py1, max(red - (int)(flashlight*pz1), 0), max(green - (int)(flashlight*pz1), 0), max(blue - (int)(flashlight*pz1), 0), 255),
@ -490,6 +496,14 @@ bool bbox_inside_cam(int k) {
return !(tpr.x <= 0.0 || btl.x >= 1500.0 || tpr.y <= 0.0 || btl.y >= 1000.0);
}
bool triangle_reduce(pt_2d* tri) {
return (
distance_pt_pt_2d_sq(tri[0].x, tri[0].y, tri[1].x, tri[1].y) +
distance_pt_pt_2d_sq(tri[1].x, tri[1].y, tri[2].x, tri[2].y) +
distance_pt_pt_2d_sq(tri[2].x, tri[2].y, tri[0].x, tri[0].y) <= shrink_distance
);
}
void addTriangle(
double x0, double y0, double z0,
double x1, double y1, double z1,
@ -516,6 +530,7 @@ void addTriangle(
triangles_og_coords[triangles_i][0] = to_pt_2d(px0, py0, pz0);
triangles_og_coords[triangles_i][1] = to_pt_2d(px1, py1, pz1);
triangles_og_coords[triangles_i][2] = to_pt_2d(px2, py2, pz2);
triangles_shr[triangles_i] = triangle_reduce(triangles_to_render[triangles_i]);
//triangles_areas[triangles_i] = area_of_triangle(triangles_to_render[triangles_i]);
reds[triangles_i] = red ;
greens[triangles_i] = green ;
@ -577,6 +592,7 @@ void addTriangle(
triangles_og_coords[triangles_i][0] = to_pt_2d(fpx0, fpy0, fpz0);
triangles_og_coords[triangles_i][1] = to_pt_2d(mpx0, mpy0, mpz0);
triangles_og_coords[triangles_i][2] = to_pt_2d(fpx1, fpy1, fpz1);
triangles_shr[triangles_i] = triangle_reduce(triangles_to_render[triangles_i]);
reds[triangles_i] = red ;
greens[triangles_i] = green ;
blues[triangles_i] = blue ;
@ -590,6 +606,7 @@ void addTriangle(
triangles_og_coords[triangles_i][0] = to_pt_2d(mpx0, mpy0, mpz0);
triangles_og_coords[triangles_i][1] = to_pt_2d(mpx1, mpy1, mpz1);
triangles_og_coords[triangles_i][2] = to_pt_2d(fpx1, fpy1, fpz1);
triangles_shr[triangles_i] = triangle_reduce(triangles_to_render[triangles_i]);
reds[triangles_i] = red ;
greens[triangles_i] = green ;
blues[triangles_i] = blue ;
@ -639,6 +656,7 @@ void addTriangle(
triangles_og_coords[triangles_i][0] = to_pt_2d(px0, py0, pz0);
triangles_og_coords[triangles_i][1] = to_pt_2d(px1, py1, pz1);
triangles_og_coords[triangles_i][2] = to_pt_2d(px2, py2, pz2);
triangles_shr[triangles_i] = triangle_reduce(triangles_to_render[triangles_i]);
//triangles_areas[triangles_i] = area_of_triangle(triangles_to_render[triangles_i]);
reds[triangles_i] = red ;
greens[triangles_i] = green ;
@ -658,14 +676,16 @@ void addTriangleRotated(
int red, int green, int blue,
cube_0* cb
) {
double px0; double py0; double pz0;
double px1; double py1; double pz1;
double px2; double py2; double pz2;
rotate_cube(x0, y0, z0, &px0, &py0, &pz0, cb);
rotate_cube(x1, y1, z1, &px1, &py1, &pz1, cb);
rotate_cube(x2, y2, z2, &px2, &py2, &pz2, cb);
if(triangles_i < MAX_SIZE -1) {
addTriangle(px0, py0, pz0, px1, py1, pz1, px2, py2, pz2, red, green, blue);
if(true) {
double px0; double py0; double pz0;
double px1; double py1; double pz1;
double px2; double py2; double pz2;
rotate_cube(x0, y0, z0, &px0, &py0, &pz0, cb);
rotate_cube(x1, y1, z1, &px1, &py1, &pz1, cb);
rotate_cube(x2, y2, z2, &px2, &py2, &pz2, cb);
if(triangles_i < MAX_SIZE -1) {
addTriangle(px0, py0, pz0, px1, py1, pz1, px2, py2, pz2, red, green, blue);
}
}
}
@ -781,8 +801,22 @@ void visit(int k, bool vflag) {
visited_tri[k] = vflag ;
for(int l = 0; l < triangles_i; l++) {
if(l != k && visited_tri[l] != vflag) {
if(is_in_front(triangles_to_render[k], triangles_og_coords[k], triangles_to_render[l], triangles_og_coords[l])) {
visit(l, vflag);
if(triangles_shr[k] == false && triangles_shr[l] == false) {
if(is_in_front(triangles_to_render[k], triangles_og_coords[k], triangles_to_render[l], triangles_og_coords[l])) {
visit(l, vflag);
}
} else if(triangles_shr[k] == false) {
if(!is_in_front_lite(triangles_to_render[l], triangles_og_coords[l], triangles_to_render[k], triangles_og_coords[k])) {
visit(l, vflag);
}
} else if(triangles_shr[l] == false) {
if(is_in_front_lite(triangles_to_render[k], triangles_og_coords[k], triangles_to_render[l], triangles_og_coords[l])) {
visit(l, vflag);
}
} else {
if(is_in_front_pt_pt(triangles_to_render[k], triangles_og_coords[k], triangles_to_render[l], triangles_og_coords[l])) {
visit(l, vflag);
}
}
}
}
@ -805,9 +839,9 @@ void drawCurrentRoom(SDL_Renderer* renderer) {
add_triangles_tp(current_room->tps, current_room->tps_size);
add_triangles_ent(current_room->ents, current_room->ent_len);
drawProj();
topological_sort();
//remove_hidden(renderer);
//topological_sort();
//remove_hidden(renderer);
topological_sort();
for(int k = 0; k < triangles_i; k++) {
renderTriangleFull(renderer, triangles_order[k]);
}

View File

@ -445,6 +445,7 @@ void free_pool() {
free(pool[k0].area->ents);
free(pool[k0].area->tps);
free(pool[k0].area->map);
free(pool[k0].area);
}
free(pool);
}

View File

@ -146,6 +146,7 @@ int main(int argc, char** argv) {
free(greens);
free(blues);
free(triangles_order);
free(triangles_shr);
free(visited_tri);
free_proj();
//printf("10\n");

View File

@ -88,6 +88,7 @@ void addProjectileToDraw(projectile* proj) {
triangles_og_coords[triangles_i][0] = build_pt(ppx, ppy, ppz);
triangles_og_coords[triangles_i][1] = build_pt(ppx, ppy, ppz);
triangles_og_coords[triangles_i][2] = build_pt(ppx, ppy, ppz);
triangles_shr[triangles_i] = true;
reds[triangles_i] = proj->red;
greens[triangles_i] = proj->green;
blues[triangles_i] = proj->blue;
@ -99,6 +100,7 @@ void addProjectileToDraw(projectile* proj) {
triangles_og_coords[triangles_i][0] = build_pt(ppx, ppy, ppz);
triangles_og_coords[triangles_i][1] = build_pt(ppx, ppy, ppz);
triangles_og_coords[triangles_i][2] = build_pt(ppx, ppy, ppz);
triangles_shr[triangles_i] = true;
reds[triangles_i] = proj->red;
greens[triangles_i] = proj->green;
blues[triangles_i] = proj->blue;

View File

@ -131,8 +131,11 @@ extern int* greens ;
extern int* blues ;
extern int* triangles_order ;
extern bool* triangles_shr ;
extern bool* visited_tri ;
extern int MAX_SIZE ;
extern double shrink_distance ;
#endif

View File

@ -259,18 +259,18 @@ bool point_in_triangle(pt_2d pt, pt_2d v1, pt_2d v2, pt_2d v3, double* thetaA, d
return false;
}
bool seg_seg_inter(pt_2d p1, pt_2d p2, pt_2d p3, pt_2d p4, double* ret0, double* ret1) {
double deno = (p4.x - p3.x)*(p2.y - p1.y) - (p4.y - p3.y)*(p2.x - p1.x);
if(absf(deno) <= 0.0001) {
bool seg_seg_inter(pt_2d* p1, pt_2d* p2, pt_2d* p3, pt_2d* p4, double* ret0, double* ret1) {
double deno = ((*p4).x - (*p3).x)*((*p2).y - (*p1).y) - ((*p4).y - (*p3).y)*((*p2).x - (*p1).x);
if(absf(deno) <= 0.001) {
return false;
}
//printf("%lf\n", deno);
double alpha =
((p4.x - p3.x)*(p3.y - p1.y) - (p4.y - p3.y)*(p3.x - p1.x))/
(((*p4).x - (*p3).x)*((*p3).y - (*p1).y) - ((*p4).y - (*p3).y)*((*p3).x - (*p1).x))/
(deno) ;
double beta =
((p2.x - p1.x)*(p3.y - p1.y) - (p2.y - p1.y)*(p3.x - p1.x))/
(((*p2).x - (*p1).x)*((*p3).y - (*p1).y) - ((*p2).y - (*p1).y)*((*p3).x - (*p1).x))/
(deno) ;
if(ret0 != NULL) {*ret0 = alpha;}
@ -314,15 +314,15 @@ double w = 0.0 ;
double th1, th2;
double dist ;
bool is_hidden(SDL_Renderer* renderer, pt_2d p, pt_2d ogp, pt_2d* tri, pt_2d* og) {
if(pt_equal(p, tri[0], 0.0001) || pt_equal(p, tri[1], 0.0001) || pt_equal(p, tri[2], 0.0001)) {
if(pt_equal(p, tri[0], 0.001) || pt_equal(p, tri[1], 0.001) || pt_equal(p, tri[2], 0.001)) {
return false;
}
get_barycentric(p, tri, &u, &v, &w);
if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.0001)) {
if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.001)) {
pt_2d mid = convex_pt2d_tri(og[0], u, og[1], v, og[2], w);
dist = proj_pt_distance_to_camera(mid) - proj_pt_distance_to_camera(ogp);
if(absf(dist) >= 0.0001) {
//return (proj_pt_distance_to_camera(mid) <= proj_pt_distance_to_camera(ogp));
dist = proj_pt_distance_to_camera_sq(mid) - proj_pt_distance_to_camera_sq(ogp);
if(absf(dist) >= 0.001) {
//return (proj_pt_distance_to_camera_sq(mid) <= proj_pt_distance_to_camera_sq(ogp));
return (dist <= 0.0);
}
}
@ -332,46 +332,67 @@ bool is_hidden(SDL_Renderer* renderer, pt_2d p, pt_2d ogp, pt_2d* tri, pt_2d* og
bool is_in_front(pt_2d* tri1, pt_2d* og1, pt_2d* tri2, pt_2d* og2) {
for(int k1 = 0; k1 < 3; k1++) {
for(int k2 = 0; k2 < 3; k2++) {
if(seg_seg_inter(tri1[k1], tri1[(k1+1)%3], tri2[k2], tri2[(k2+1)%3], &th1, &th2)) {
if(seg_seg_inter(&(tri1[k1]), &(tri1[(k1+1)%3]), &(tri2[k2]), &(tri2[(k2+1)%3]), &th1, &th2)) {
pt_2d mid1 = convex_pt2d(tri1[k1], tri1[(k1+1)%3], th1);
pt_2d mid2 = convex_pt2d(tri2[k2], tri2[(k2+1)%3], th2);
dist = proj_pt_distance_to_camera(mid1) - proj_pt_distance_to_camera(mid2);
if(absf(dist) >= 0.0001) {
//return (proj_pt_distance_to_camera(mid1) <= proj_pt_distance_to_camera(mid2));
dist = proj_pt_distance_to_camera_sq(mid1) - proj_pt_distance_to_camera_sq(mid2);
if(absf(dist) >= 0.001) {
//return (proj_pt_distance_to_camera_sq(mid1) <= proj_pt_distance_to_camera_sq(mid2));
return (dist <= 0.0);
}
}
}
}
return false;
for(int k = 0; k < 3; k++) {
pt_2d p = tri1[k];
if(pt_equal(p, tri2[0], 0.0001) || pt_equal(p, tri2[1], 0.0001) || pt_equal(p, tri2[2], 0.0001)) {
if(pt_equal(p, tri2[0], 0.001) || pt_equal(p, tri2[1], 0.001) || pt_equal(p, tri2[2], 0.001)) {
return false;
}
get_barycentric(p, tri2, &u, &v, &w);
pt_2d mid = convex_pt2d_tri(og2[0], u, og2[1], v, og2[2], w);
if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.0001)) {
dist = proj_pt_distance_to_camera(mid) - proj_pt_distance_to_camera(og1[k]);
if(absf(dist) >= 0.0001) {
//return !(proj_pt_distance_to_camera(mid) <= proj_pt_distance_to_camera(og1[k]));
if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.001)) {
dist = proj_pt_distance_to_camera_sq(mid) - proj_pt_distance_to_camera_sq(og1[k]);
if(absf(dist) >= 0.001) {
//return !(proj_pt_distance_to_camera_sq(mid) <= proj_pt_distance_to_camera_sq(og1[k]));
return !(dist <= 0.0);
}
}
}
for(int k = 0; k < 3; k++) {
pt_2d p = tri2[k];
if(pt_equal(p, tri1[0], 0.0001) || pt_equal(p, tri1[1], 0.0001) || pt_equal(p, tri1[2], 0.0001)) {
if(pt_equal(p, tri1[0], 0.001) || pt_equal(p, tri1[1], 0.001) || pt_equal(p, tri1[2], 0.001)) {
return false;
}
get_barycentric(p, tri1, &u, &v, &w);
pt_2d mid = convex_pt2d_tri(og1[0], u, og1[1], v, og1[2], w);
if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.0001)) {
dist = proj_pt_distance_to_camera(mid) - proj_pt_distance_to_camera(og2[k]);
if(absf(dist) >= 0.0001) {
//return !(proj_pt_distance_to_camera(mid) >= proj_pt_distance_to_camera(og2[k]));
if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.001)) {
dist = proj_pt_distance_to_camera_sq(mid) - proj_pt_distance_to_camera_sq(og2[k]);
if(absf(dist) >= 0.001) {
//return !(proj_pt_distance_to_camera_sq(mid) >= proj_pt_distance_to_camera_sq(og2[k]));
return !(dist >= 0.0) ;
}
}
}
return false;
}
bool is_in_front_lite(pt_2d* p, pt_2d* ogp, pt_2d* tri2, pt_2d* og2) {
if(pt_equal(p[0], tri2[0], 0.001) || pt_equal(p[0], tri2[1], 0.001) || pt_equal(p[0], tri2[2], 0.001)) {
return false;
}
get_barycentric(p[0], tri2, &u, &v, &w);
if(((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) && nonzero(u, v, w, 0.001)) {
pt_2d mid = convex_pt2d_tri(og2[0], u, og2[1], v, og2[2], w);
dist = proj_pt_distance_to_camera_sq(ogp[0]) - proj_pt_distance_to_camera_sq(mid);
if(absf(dist) >= 0.001) {
//return !(proj_pt_distance_to_camera_sq(mid) >= proj_pt_distance_to_camera_sq(og2[k]));
return (dist <= 0.0) ;
}
}
return false;
}
bool is_in_front_pt_pt(pt_2d* p1, pt_2d* ogp1, pt_2d* p2, pt_2d* ogp2) {
return ((pt_equal(*p1, *p2, 0.001)) && proj_pt_distance_to_camera_sq(*ogp1) - proj_pt_distance_to_camera_sq(*ogp2) <= 0.0);
}

View File

@ -14,6 +14,8 @@ bool triangleIntersectionDec(pt_2d t1_1, pt_2d t1_2, pt_2d t1_3, pt_2d t2_1, pt_
bool multipleTrianglesIntersection(pt_2d* tri1, int len1, pt_2d* tri2, int len2, int* ret1, int* ret2);
bool is_in_front(pt_2d* tri1, pt_2d* og1, pt_2d* tri2, pt_2d* og2);
bool is_in_front_lite(pt_2d* tri1, pt_2d* og1, pt_2d* tri2, pt_2d* og2);
bool is_in_front_pt_pt(pt_2d* p1, pt_2d* ogp1, pt_2d* p2, pt_2d* ogp2);
bool is_hidden(SDL_Renderer* renderer, pt_2d p, pt_2d ogp, pt_2d* tri, pt_2d* og);

View File

@ -11,6 +11,6 @@ Teleporters :
[-5.0, 1.0, 9.0, 10.0, 2.0, 1.0, 0.0, 0.0, 0, 0, 255; 1, 0]
Weight :
20
200
$

View File

@ -13,6 +13,6 @@ Entities :
[0.0, 40.0, 0.0, 0.5, 0.5, 0.5, 0.0, 0.0, 255, 64, 0, 5, 100, 2]
Weight :
500
50
$