diff --git a/bin/back b/bin/back index 42bed0e..715d448 100755 Binary files a/bin/back and b/bin/back differ diff --git a/obj/base.o b/obj/base.o index fd27eb0..2e5cccc 100644 Binary files a/obj/base.o and b/obj/base.o differ diff --git a/obj/display.o b/obj/display.o index 1f39bd5..fc4f717 100644 Binary files a/obj/display.o and b/obj/display.o differ diff --git a/obj/generation.o b/obj/generation.o index 8c3c53d..56672c0 100644 Binary files a/obj/generation.o and b/obj/generation.o differ diff --git a/obj/triangles.o b/obj/triangles.o index c016b0f..10e84aa 100644 Binary files a/obj/triangles.o and b/obj/triangles.o differ diff --git a/src/base.c b/src/base.c index e6f2b3e..ca46b0b 100644 --- a/src/base.c +++ b/src/base.c @@ -123,6 +123,22 @@ bool is_an_integer(char c) { return ((int)c >= 48 && (int)c <= 57); } +pt_2d vect_diff(pt_2d p1, pt_2d p2) { + pt_2d res; + res.x = p2.x - p1.x ; + res.y = p2.y - p1.y ; + res.z = p2.z - p1.z ; + return res; +} + +double dot2D(pt_2d p1, pt_2d p2) { + return p1.x * p2.x + p1.y * p2.y ; +} + +double dot3D(pt_2d p1, pt_2d p2) { + return p1.x * p2.x + p1.y * p2.y + p1.z * p2.z ; +} + double to_double(int n) { return (double)n ; } diff --git a/src/base.h b/src/base.h index 3c82d91..d7a00d5 100644 --- a/src/base.h +++ b/src/base.h @@ -25,6 +25,10 @@ bool pt_equal_2D(pt_2d p1, pt_2d p2); bool pt_equal_3D_eps(pt_2d p1, pt_2d p2, double epsilon); bool pt_equal_2D_eps(pt_2d p1, pt_2d p2, double epsilon, bool debug0); +pt_2d vect_diff(pt_2d p1, pt_2d p2); +double dot2D(pt_2d p1, pt_2d p2); +double dot3D(pt_2d p1, pt_2d p2); + cube_0 create_cube_0(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b); cube create_cube(double x, double y, double z, double w, double h, double d, double hz_a, double vt_a, int r, int g, int b); void free_cube(cube c); diff --git a/src/display.c b/src/display.c index ad6ecab..4ea7dbf 100644 --- a/src/display.c +++ b/src/display.c @@ -22,12 +22,15 @@ int flashlight = 0 ; +int MAX_SIZE = 8192 ; + int* drawOrder; double draw_constant ; pt_2d** triangles_to_render ; pt_2d** triangles_og_coords ; +double* triangles_areas ; int triangles_i ; int* reds ; @@ -41,14 +44,15 @@ int visited_i ; void init_draworder() { drawOrder = malloc(sizeof(int)*6) ; draw_constant = 0.4 ; - triangles_to_render = malloc(sizeof(pt_2d*)*8192) ; - triangles_og_coords = malloc(sizeof(pt_2d*)*8192) ; - reds = malloc(sizeof(int)*8192) ; - greens = malloc(sizeof(int)*8192) ; - blues = malloc(sizeof(int)*8192) ; - triangles_order = malloc(sizeof(int)*8192) ; - visited_tri = malloc(sizeof(bool)*8192) ; - for(int k = 0; k < 8192; k++) { + triangles_to_render = malloc(sizeof(pt_2d*)*MAX_SIZE) ; + triangles_og_coords = malloc(sizeof(pt_2d*)*MAX_SIZE) ; + reds = malloc(sizeof(int)*MAX_SIZE) ; + greens = malloc(sizeof(int)*MAX_SIZE) ; + blues = malloc(sizeof(int)*MAX_SIZE) ; + triangles_order = malloc(sizeof(int)*MAX_SIZE) ; + triangles_areas = malloc(sizeof(double)*MAX_SIZE) ; + visited_tri = 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; @@ -497,6 +501,10 @@ pt_2d to_pt_2d(double x0, double y0, double z0) { return res; } +bool inside_fov(double px, double py) { + return true ; +} + void addTriangle( double x0, double y0, double z0, double x1, double y1, double z1, @@ -515,138 +523,144 @@ void addTriangle( 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) { - triangles_to_render[triangles_i][0] = to_pt_2d(1500.0 * (1.0 + (px0 / (1.5 * pz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py0 / (pz0 * tan_fov))) / 2.0, getZbuffer(px0, py0, pz0)); - triangles_to_render[triangles_i][1] = to_pt_2d(1500.0 * (1.0 + (px1 / (1.5 * pz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py1 / (pz1 * tan_fov))) / 2.0, getZbuffer(px1, py1, pz1)); - triangles_to_render[triangles_i][2] = to_pt_2d(1500.0 * (1.0 + (px2 / (1.5 * pz2 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py2 / (pz2 * tan_fov))) / 2.0, getZbuffer(px2, py2, pz2)); - 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); - reds[triangles_i] = red ; - greens[triangles_i] = green ; - blues[triangles_i] = blue ; - triangles_i += 1; - } else if((pz0 >= draw_constant) + (pz1 >= draw_constant) + (pz2 >= draw_constant) == 2) { - if(pz0 < draw_constant) { - // pz1 >= draw_constant and pz2 >+ draw_constant - fx0 = x1 ; fy0 = y1 ; fz0 = z1 ; - fpx0 = px1 ; fpy0 = py1 ; fpz0 = pz1 ; - fx1 = x2 ; fy1 = y2 ; fz1 = z2 ; - fpx1 = px2 ; fpy1 = py2 ; fpz1 = pz2 ; - mx0 = convex_pt(x1, x0, (pz1 - draw_constant)/(pz1 - pz0)); - my0 = convex_pt(y1, y0, (pz1 - draw_constant)/(pz1 - pz0)); - mz0 = convex_pt(z1, z0, (pz1 - draw_constant)/(pz1 - pz0)); - mx1 = convex_pt(x2, x0, (pz2 - draw_constant)/(pz2 - pz0)); - my1 = convex_pt(y2, y0, (pz2 - draw_constant)/(pz2 - pz0)); - mz1 = convex_pt(z2, z0, (pz2 - draw_constant)/(pz2 - pz0)); - // 1-0 segment - project_to_camera(mx0, my0, mz0, &mpx0, &mpy0, &mpz0) ; - - // 0-2 segment - project_to_camera(mx1, my1, mz1, &mpx1, &mpy1, &mpz1) ; - } else if(pz1 < draw_constant) { - // pz0 >= draw_constant and pz2 >+ draw_constant - fx0 = x0 ; fy0 = y0 ; fz0 = z0 ; - fpx0 = px0 ; fpy0 = py0 ; fpz0 = pz0 ; - fx1 = x2 ; fy1 = y2 ; fz1 = z2 ; - fpx1 = px2 ; fpy1 = py2 ; fpz1 = pz2 ; - mx0 = convex_pt(x0, x1, (pz0 - draw_constant)/(pz0 - pz1)); - my0 = convex_pt(y0, y1, (pz0 - draw_constant)/(pz0 - pz1)); - mz0 = convex_pt(z0, z1, (pz0 - draw_constant)/(pz0 - pz1)); - mx1 = convex_pt(x2, x1, (pz2 - draw_constant)/(pz2 - pz1)); - my1 = convex_pt(y2, y1, (pz2 - draw_constant)/(pz2 - pz1)); - mz1 = convex_pt(z2, z1, (pz2 - draw_constant)/(pz2 - pz1)); - // 0-1 segment - project_to_camera(mx0, my0, mz0, &mpx0, &mpy0, &mpz0) ; - - // 1-2 segment - project_to_camera(mx1, my1, mz1, &mpx1, &mpy1, &mpz1) ; - } else /*if(pz2 < draw_constant)*/ { - // pz1 >= draw_constant and pz0 >+ draw_constant - fx0 = x0 ; fy0 = y0 ; fz0 = z0 ; - fpx0 = px0 ; fpy0 = py0 ; fpz0 = pz0 ; - fx1 = x1 ; fy1 = y1 ; fz1 = z1 ; - fpx1 = px1 ; fpy1 = py1 ; fpz1 = pz1 ; - mx0 = convex_pt(x0, x2, (pz0 - draw_constant)/(pz0 - pz2)); - my0 = convex_pt(y0, y2, (pz0 - draw_constant)/(pz0 - pz2)); - mz0 = convex_pt(z0, z2, (pz0 - draw_constant)/(pz0 - pz2)); - mx1 = convex_pt(x1, x2, (pz1 - draw_constant)/(pz1 - pz2)); - my1 = convex_pt(y1, y2, (pz1 - draw_constant)/(pz1 - pz2)); - mz1 = convex_pt(z1, z2, (pz1 - draw_constant)/(pz1 - pz2)); - // 0-2 segment - project_to_camera(mx0, my0, mz0, &mpx0, &mpy0, &mpz0) ; - - // 1-2 segment - project_to_camera(mx1, my1, mz1, &mpx1, &mpy1, &mpz1) ; - } + if(inside_fov(px0, py0) || inside_fov(px1, py1) || inside_fov(px2, py2)) { + if(pz0 >= draw_constant && pz1 >= draw_constant && pz2 >= draw_constant) { + triangles_to_render[triangles_i][0] = to_pt_2d(1500.0 * (1.0 + (px0 / (1.5 * pz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py0 / (pz0 * tan_fov))) / 2.0, getZbuffer(px0, py0, pz0)); + triangles_to_render[triangles_i][1] = to_pt_2d(1500.0 * (1.0 + (px1 / (1.5 * pz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py1 / (pz1 * tan_fov))) / 2.0, getZbuffer(px1, py1, pz1)); + triangles_to_render[triangles_i][2] = to_pt_2d(1500.0 * (1.0 + (px2 / (1.5 * pz2 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py2 / (pz2 * tan_fov))) / 2.0, getZbuffer(px2, py2, pz2)); + 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_areas[triangles_i] = area_of_triangle(triangles_to_render[triangles_i]); + reds[triangles_i] = red ; + greens[triangles_i] = green ; + blues[triangles_i] = blue ; + triangles_i += 1; + } else if((pz0 >= draw_constant) + (pz1 >= draw_constant) + (pz2 >= draw_constant) == 2) { + if(pz0 < draw_constant) { + // pz1 >= draw_constant and pz2 >+ draw_constant + fx0 = x1 ; fy0 = y1 ; fz0 = z1 ; + fpx0 = px1 ; fpy0 = py1 ; fpz0 = pz1 ; + fx1 = x2 ; fy1 = y2 ; fz1 = z2 ; + fpx1 = px2 ; fpy1 = py2 ; fpz1 = pz2 ; + mx0 = convex_pt(x1, x0, (pz1 - draw_constant)/(pz1 - pz0)); + my0 = convex_pt(y1, y0, (pz1 - draw_constant)/(pz1 - pz0)); + mz0 = convex_pt(z1, z0, (pz1 - draw_constant)/(pz1 - pz0)); + mx1 = convex_pt(x2, x0, (pz2 - draw_constant)/(pz2 - pz0)); + my1 = convex_pt(y2, y0, (pz2 - draw_constant)/(pz2 - pz0)); + mz1 = convex_pt(z2, z0, (pz2 - draw_constant)/(pz2 - pz0)); + // 1-0 segment + project_to_camera(mx0, my0, mz0, &mpx0, &mpy0, &mpz0) ; + + // 0-2 segment + project_to_camera(mx1, my1, mz1, &mpx1, &mpy1, &mpz1) ; + } else if(pz1 < draw_constant) { + // pz0 >= draw_constant and pz2 >+ draw_constant + fx0 = x0 ; fy0 = y0 ; fz0 = z0 ; + fpx0 = px0 ; fpy0 = py0 ; fpz0 = pz0 ; + fx1 = x2 ; fy1 = y2 ; fz1 = z2 ; + fpx1 = px2 ; fpy1 = py2 ; fpz1 = pz2 ; + mx0 = convex_pt(x0, x1, (pz0 - draw_constant)/(pz0 - pz1)); + my0 = convex_pt(y0, y1, (pz0 - draw_constant)/(pz0 - pz1)); + mz0 = convex_pt(z0, z1, (pz0 - draw_constant)/(pz0 - pz1)); + mx1 = convex_pt(x2, x1, (pz2 - draw_constant)/(pz2 - pz1)); + my1 = convex_pt(y2, y1, (pz2 - draw_constant)/(pz2 - pz1)); + mz1 = convex_pt(z2, z1, (pz2 - draw_constant)/(pz2 - pz1)); + // 0-1 segment + project_to_camera(mx0, my0, mz0, &mpx0, &mpy0, &mpz0) ; + + // 1-2 segment + project_to_camera(mx1, my1, mz1, &mpx1, &mpy1, &mpz1) ; + } else /*if(pz2 < draw_constant)*/ { + // pz1 >= draw_constant and pz0 >+ draw_constant + fx0 = x0 ; fy0 = y0 ; fz0 = z0 ; + fpx0 = px0 ; fpy0 = py0 ; fpz0 = pz0 ; + fx1 = x1 ; fy1 = y1 ; fz1 = z1 ; + fpx1 = px1 ; fpy1 = py1 ; fpz1 = pz1 ; + mx0 = convex_pt(x0, x2, (pz0 - draw_constant)/(pz0 - pz2)); + my0 = convex_pt(y0, y2, (pz0 - draw_constant)/(pz0 - pz2)); + mz0 = convex_pt(z0, z2, (pz0 - draw_constant)/(pz0 - pz2)); + mx1 = convex_pt(x1, x2, (pz1 - draw_constant)/(pz1 - pz2)); + my1 = convex_pt(y1, y2, (pz1 - draw_constant)/(pz1 - pz2)); + mz1 = convex_pt(z1, z2, (pz1 - draw_constant)/(pz1 - pz2)); + // 0-2 segment + project_to_camera(mx0, my0, mz0, &mpx0, &mpy0, &mpz0) ; + + // 1-2 segment + project_to_camera(mx1, my1, mz1, &mpx1, &mpy1, &mpz1) ; + } - triangles_to_render[triangles_i][0] = to_pt_2d(1500.0 * (1.0 + (fpx0 / (1.5 * fpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy0 / (fpz0 * tan_fov))) / 2.0, getZbuffer(fpx0, fpy0, fpz0)); - triangles_to_render[triangles_i][1] = to_pt_2d(1500.0 * (1.0 + (mpx0 / (1.5 * mpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy0 / (mpz0 * tan_fov))) / 2.0, getZbuffer(mpx0, mpy0, mpz0)); - triangles_to_render[triangles_i][2] = to_pt_2d(1500.0 * (1.0 + (fpx1 / (1.5 * fpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy1 / (fpz1 * tan_fov))) / 2.0, getZbuffer(fpx1, fpy1, fpz1)); - 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_to_render[triangles_i+1][0] = to_pt_2d(1500.0 * (1.0 + (mpx0 / (1.5 * mpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy0 / (mpz0 * tan_fov))) / 2.0, getZbuffer(mpx0, mpy0, mpz0)); - triangles_to_render[triangles_i+1][1] = to_pt_2d(1500.0 * (1.0 + (mpx1 / (1.5 * mpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy1 / (mpz1 * tan_fov))) / 2.0, getZbuffer(mpx1, mpy1, mpz1)); - triangles_to_render[triangles_i+1][2] = to_pt_2d(1500.0 * (1.0 + (fpx1 / (1.5 * fpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy1 / (fpz1 * tan_fov))) / 2.0, getZbuffer(fpx1, fpy1, fpz1)); - triangles_og_coords[triangles_i+1][0] = to_pt_2d(mpx0, mpy0, mpz0); - triangles_og_coords[triangles_i+1][1] = to_pt_2d(mpx1, mpy1, mpz1); - triangles_og_coords[triangles_i+1][2] = to_pt_2d(fpx1, fpy1, fpz1); - reds[triangles_i] = red ; - greens[triangles_i] = green ; - blues[triangles_i] = blue ; - reds[triangles_i+1] = red ; - greens[triangles_i+1] = green ; - blues[triangles_i+1] = blue ; - triangles_i += 2 ; - } 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); - } + triangles_to_render[triangles_i][0] = to_pt_2d(1500.0 * (1.0 + (fpx0 / (1.5 * fpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy0 / (fpz0 * tan_fov))) / 2.0, getZbuffer(fpx0, fpy0, fpz0)); + triangles_to_render[triangles_i][1] = to_pt_2d(1500.0 * (1.0 + (mpx0 / (1.5 * mpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy0 / (mpz0 * tan_fov))) / 2.0, getZbuffer(mpx0, mpy0, mpz0)); + triangles_to_render[triangles_i][2] = to_pt_2d(1500.0 * (1.0 + (fpx1 / (1.5 * fpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy1 / (fpz1 * tan_fov))) / 2.0, getZbuffer(fpx1, fpy1, fpz1)); + 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_areas[triangles_i] = area_of_triangle(triangles_to_render[triangles_i]); + triangles_to_render[triangles_i+1][0] = to_pt_2d(1500.0 * (1.0 + (mpx0 / (1.5 * mpz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy0 / (mpz0 * tan_fov))) / 2.0, getZbuffer(mpx0, mpy0, mpz0)); + triangles_to_render[triangles_i+1][1] = to_pt_2d(1500.0 * (1.0 + (mpx1 / (1.5 * mpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (mpy1 / (mpz1 * tan_fov))) / 2.0, getZbuffer(mpx1, mpy1, mpz1)); + triangles_to_render[triangles_i+1][2] = to_pt_2d(1500.0 * (1.0 + (fpx1 / (1.5 * fpz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (fpy1 / (fpz1 * tan_fov))) / 2.0, getZbuffer(fpx1, fpy1, fpz1)); + triangles_og_coords[triangles_i+1][0] = to_pt_2d(mpx0, mpy0, mpz0); + triangles_og_coords[triangles_i+1][1] = to_pt_2d(mpx1, mpy1, mpz1); + triangles_og_coords[triangles_i+1][2] = to_pt_2d(fpx1, fpy1, fpz1); + triangles_areas[triangles_i+1] = area_of_triangle(triangles_to_render[triangles_i+1]); + reds[triangles_i] = red ; + greens[triangles_i] = green ; + blues[triangles_i] = blue ; + reds[triangles_i+1] = red ; + greens[triangles_i+1] = green ; + blues[triangles_i+1] = blue ; + triangles_i += 2 ; + } 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); + } - triangles_to_render[triangles_i][0] = to_pt_2d(1500.0 * (1.0 + (px0 / (1.5 * pz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py0 / (pz0 * tan_fov))) / 2.0, getZbuffer(px0, py0, pz0)); - triangles_to_render[triangles_i][1] = to_pt_2d(1500.0 * (1.0 + (px1 / (1.5 * pz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py1 / (pz1 * tan_fov))) / 2.0, getZbuffer(px1, py1, pz1)); - triangles_to_render[triangles_i][2] = to_pt_2d(1500.0 * (1.0 + (px2 / (1.5 * pz2 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py2 / (pz2 * tan_fov))) / 2.0, getZbuffer(px2, py2, pz2)); - 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); - reds[triangles_i] = red ; - greens[triangles_i] = green ; - blues[triangles_i] = blue ; - triangles_i += 1; - } else { - + triangles_to_render[triangles_i][0] = to_pt_2d(1500.0 * (1.0 + (px0 / (1.5 * pz0 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py0 / (pz0 * tan_fov))) / 2.0, getZbuffer(px0, py0, pz0)); + triangles_to_render[triangles_i][1] = to_pt_2d(1500.0 * (1.0 + (px1 / (1.5 * pz1 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py1 / (pz1 * tan_fov))) / 2.0, getZbuffer(px1, py1, pz1)); + triangles_to_render[triangles_i][2] = to_pt_2d(1500.0 * (1.0 + (px2 / (1.5 * pz2 * tan_fov))) / 2.0, 1000.0 * (1.0 + (py2 / (pz2 * tan_fov))) / 2.0, getZbuffer(px2, py2, pz2)); + 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_areas[triangles_i] = area_of_triangle(triangles_to_render[triangles_i]); + reds[triangles_i] = red ; + greens[triangles_i] = green ; + blues[triangles_i] = blue ; + triangles_i += 1; + } else { + + } } } @@ -765,12 +779,43 @@ void topological_sort() { } } +void remove_hidden(SDL_Renderer* renderer) { + printf("%d --> ", triangles_i); + for(int k = 0; k < triangles_i; k++) { + int halt = 1 ; + bool fst = false ; + bool snd = false ; + bool thd = false ; + for(int l = 0; l < halt*triangles_i; l++) { + if(l != k) { + fst = fst || (is_hidden(renderer, triangles_to_render[k][0], triangles_og_coords[k][0], triangles_to_render[l], triangles_og_coords[l], triangles_areas[l])); + snd = snd || (is_hidden(renderer, triangles_to_render[k][1], triangles_og_coords[k][1], triangles_to_render[l], triangles_og_coords[l], triangles_areas[l])); + thd = thd || (is_hidden(renderer, triangles_to_render[k][2], triangles_og_coords[k][2], triangles_to_render[l], triangles_og_coords[l], triangles_areas[l])); + if(fst && snd && thd) { + triangles_to_render[k][0] = triangles_to_render[triangles_i-1][0] ; + triangles_to_render[k][1] = triangles_to_render[triangles_i-1][1] ; + triangles_to_render[k][2] = triangles_to_render[triangles_i-1][2] ; + reds[k] = reds[triangles_i-1]; + greens[k] = greens[triangles_i-1]; + blues[k] = blues[triangles_i-1]; + triangles_areas[k] = triangles_areas[triangles_i-1]; + triangles_i -= 1; + k -= 1; + halt = 0; + } + } + } + } + printf("%d\n", triangles_i); +} + void drawCurrentRoom(SDL_Renderer* renderer) { triangles_i = 0; visited_i = 0; add_triangles_cb(current_room->map, current_room->map_size); add_triangles_tp(current_room->tps, current_room->tps_size); add_triangles_ent(current_room->ents, current_room->ent_len); + remove_hidden(renderer); //topological_sort(); for(int k = 0; k < triangles_i; k++) { renderTriangleFull(renderer, triangles_order[k]); @@ -783,7 +828,7 @@ void drawCurrentRoom(SDL_Renderer* renderer) { 75/4, 105/4, 0 ); }*/ - for(int k = 0; k < triangles_i; k++) { + for(int k = 0; k < 0*triangles_i; k++) { for(int l = 0; l < 3; l++) { drawNumberToRenderer(renderer, digits, (int)(proj_pt_distance_to_camera(triangles_og_coords[k][l])), diff --git a/src/generation.c b/src/generation.c index 5b5c245..c94db5d 100644 --- a/src/generation.c +++ b/src/generation.c @@ -80,7 +80,7 @@ void build_starting_chunk(int chx, int chy) { new->chunk_x = chx ; new->chunk_y = chy ; new->map = malloc(sizeof(cube_0)*8); - new->map_size = 8 ; + new->map_size = 9 ; new->tps = malloc(sizeof(teleporter)*4); new->tps_size = 4 ; @@ -92,6 +92,7 @@ void build_starting_chunk(int chx, int chy) { new->map[5] = create_cube_0(4.0, 1.0, 0.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128); new->map[6] = create_cube_0(0.0, 1.0, 4.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128); new->map[7] = create_cube_0(4.0, 1.0, 4.0, 1.0, 5.0, 1.0, 0.0, 0.0, 255, 255, 128); + new->map[8] = create_cube_0(10.0, 10.0, 10.0, 2.0, 2.0, 2.0, 0.0, 0.0, 255, 128, 255); new->tps[0] = create_teleporter(-1.0, 1.0, 2.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 3.14159/4.0, 0.0, 255, 0, 0 , chx-1, chy , 2.5, 2.5, 2.5); new->tps[1] = create_teleporter(2.0, 1.0, -1.0, 1.0, 2.0 + (double)(rand()%2), 1.0, 3.14159/4.0, 0.0, 255, 255, 0, chx , chy-1, 2.5, 2.5, 2.5); diff --git a/src/structure.h b/src/structure.h index 162425f..b058277 100644 --- a/src/structure.h +++ b/src/structure.h @@ -117,6 +117,7 @@ extern double draw_constant ; extern pt_2d** triangles_to_render ; extern pt_2d** triangles_og_coords ; +extern double* triangles_areas; extern int triangles_i ; #endif \ No newline at end of file diff --git a/src/triangles.c b/src/triangles.c index ee64d0e..7615a69 100644 --- a/src/triangles.c +++ b/src/triangles.c @@ -348,4 +348,64 @@ bool delta_get(pt_2d* tri1, pt_2d* og1, pt_2d* tri2, pt_2d* og2, double* ret) { } } return false; +} + +double subarea(pt_2d A, pt_2d B, pt_2d C) { + return (0.005*absf(A.x*B.y - A.x*C.y + B.x*C.y - B.x*A.y + C.x*A.y - C.x*B.y)); +} + +double area_of_triangle(pt_2d* t) { + return subarea(t[0], t[1], t[2]); +} + +void get_barycentric(pt_2d p, pt_2d* tri, double area, double* retu, double* retv, double* retw) { + pt_2d a = tri[0] ; pt_2d b = tri[1] ; pt_2d c = tri[2] ; + pt_2d v0 = vect_diff(b, a); + pt_2d v1 = vect_diff(c, a); + pt_2d v2 = vect_diff(p, a); + double d00 = dot2D(v0, v0); + double d01 = dot2D(v0, v1); + double d11 = dot2D(v1, v1); + double d20 = dot2D(v2, v0); + double d21 = dot2D(v2, v1); + double denom = d00 * d11 - d01 * d01; + if(retv != NULL) { + *retv = (d11 * d20 - d01 * d21) / denom; + } + if(retw != NULL) { + *retw = (d00 * d21 - d01 * d20) / denom; + } + if(retu != NULL) { + *retu = 1.0 - *retv - *retw ; + } +} + +bool pt_equal(pt_2d p1, pt_2d p2, double epsilon) { + return (absf(p2.x - p1.x) <= epsilon && absf(p2.y - p1.y) <= epsilon && absf(p2.z - p1.z) <= epsilon) ; +} + +bool is_hidden(SDL_Renderer* renderer, pt_2d p, pt_2d ogp, pt_2d* tri, pt_2d* og, double area) { + if(pt_equal(p, tri[0], 0.001) || pt_equal(p, tri[1], 0.001) || pt_equal(p, tri[2], 0.001)) { + return false; + } + double u = 0.0 ; + double v = 0.0 ; + double w = 0.0 ; + get_barycentric(p, tri, area, &u, &v, &w); + pt_2d mid = convex_pt2d_tri(og[0], u, og[1], v, og[2], w); + if(renderer != NULL && (u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) { + if(mid.z >= 0.4) { + SDL_Rect r; + r.x = (int)(1500.0 * (1.0 + (mid.x / (1.5 * mid.z * tan_fov))) / 2.0) -2; + r.y = (int)(1000.0 * (1.0 + (mid.y / (mid.z * tan_fov))) / 2.0) -2; + r.w = 4 ; + r.h = 4 ; + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderFillRect(renderer, &r); + } + } + if((u >= 0.0) && (v >= 0.0) && (w >= 0.0) && (u+v+w <= 1.0)) { + return (proj_pt_distance_to_camera(mid) <= proj_pt_distance_to_camera(ogp)); + } + return false; } \ No newline at end of file diff --git a/src/triangles.h b/src/triangles.h index 11b71a8..4af6c69 100644 --- a/src/triangles.h +++ b/src/triangles.h @@ -15,4 +15,7 @@ bool multipleTrianglesIntersection(pt_2d* tri1, int len1, pt_2d* tri2, int len2, bool delta_get(pt_2d* tri1, pt_2d* og1, pt_2d* tri2, pt_2d* og2, double* ret) ; +double area_of_triangle(pt_2d* t); +bool is_hidden(SDL_Renderer* renderer, pt_2d p, pt_2d ogp, pt_2d* tri, pt_2d* og, double area); + #endif \ No newline at end of file diff --git a/templates/room_3 b/templates/room_3 index 0248989..48479cc 100644 --- a/templates/room_3 +++ b/templates/room_3 @@ -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 : -50 +20 $ \ No newline at end of file diff --git a/templates/room_4 b/templates/room_4 index 21350ec..d2d8fae 100644 --- a/templates/room_4 +++ b/templates/room_4 @@ -9,6 +9,6 @@ Teleporters : [-5.0, 1.0, 9.0, 10.0, 2.0, 1.0, 0.0, 0.0, 0, 0, 255; 1, 0] Weight : -100 +10 $ \ No newline at end of file