Fixed major graphics bug (farther cubes getting distorted) + added floor

This commit is contained in:
Alexandre 2024-07-01 19:10:46 +02:00
parent 745149237a
commit 95926f322b
4 changed files with 9 additions and 224 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -83,7 +83,7 @@ let should_be_drawn (pt : pt_3d) =
*) *)
let should_be_drawn_gr (pt : pt_3d) = let should_be_drawn_gr (pt : pt_3d) =
pt.z > 0.1 ;; pt.z > 0.44 ;;
let sign x = let sign x =
if x >= 0. then 1. else -. (1.) ;; if x >= 0. then 1. else -. (1.) ;;
@ -138,8 +138,8 @@ let project (shape : pt_3d array) screen_wd screen_ht fov =
res.(k).y <- shape.(k).y /. (shape.(k).z *. Float.tan (((float_of_int fov) *. 3.14159265358 /. 180.) /. 2.)) res.(k).y <- shape.(k).y /. (shape.(k).z *. Float.tan (((float_of_int fov) *. 3.14159265358 /. 180.) /. 2.))
end end
else begin else begin
res.(k).x <- (absf shape.(k).x) /. (ar *. (0.1 *. (sign shape.(k).x)) *. Float.tan (((float_of_int fov) *. 3.14159265358 /. 180.) /. 2.)); res.(k).x <- (absf shape.(k).x) /. (ar *. (0.44 *. (sign shape.(k).x)) *. Float.tan (((float_of_int fov) *. 3.14159265358 /. 180.) /. 2.));
res.(k).y <- (absf shape.(k).y) /. ((0.1 *. (sign shape.(k).y)) *. Float.tan (((float_of_int fov) *. 3.14159265358 /. 180.) /. 2.)) res.(k).y <- (absf shape.(k).y) /. ((0.44 *. (sign shape.(k).y)) *. Float.tan (((float_of_int fov) *. 3.14159265358 /. 180.) /. 2.))
end; end;
(*Printf.printf "added (%f %f)" res.(k).x res.(k).y; (*Printf.printf "added (%f %f)" res.(k).x res.(k).y;
Stdlib.print_endline " ";*) Stdlib.print_endline " ";*)
@ -185,92 +185,6 @@ let are_faces_behind (cube : pt_3d array) =
res.(5) <- (should_be_drawn_gr cube.(3)) || (should_be_drawn_gr cube.(0)) || (should_be_drawn_gr cube.(4)) || (should_be_drawn_gr cube.(7)); res.(5) <- (should_be_drawn_gr cube.(3)) || (should_be_drawn_gr cube.(0)) || (should_be_drawn_gr cube.(4)) || (should_be_drawn_gr cube.(7));
(res, res.(0) || res.(1) || res.(2) || res.(3) || res.(4) || res.(5)) ;; (res, res.(0) || res.(1) || res.(2) || res.(3) || res.(4) || res.(5)) ;;
let are_faces_behind_rev (cube : pt_3d array) =
let res = Array.make 6 false in
res.(0) <-
((should_be_drawn_gr cube.(0)) || (should_be_drawn_gr cube.(2))) &&
((should_be_drawn_gr cube.(1)) || (should_be_drawn_gr cube.(3))) &&
((should_be_drawn_gr cube.(0)) || (should_be_drawn_gr cube.(1))) &&
((should_be_drawn_gr cube.(1)) || (should_be_drawn_gr cube.(2))) &&
((should_be_drawn_gr cube.(2)) || (should_be_drawn_gr cube.(3))) &&
((should_be_drawn_gr cube.(3)) || (should_be_drawn_gr cube.(0))) ;
res.(1) <-
((should_be_drawn_gr cube.(4)) || (should_be_drawn_gr cube.(6))) &&
((should_be_drawn_gr cube.(5)) || (should_be_drawn_gr cube.(7))) &&
((should_be_drawn_gr cube.(4)) || (should_be_drawn_gr cube.(5))) &&
((should_be_drawn_gr cube.(5)) || (should_be_drawn_gr cube.(6))) &&
((should_be_drawn_gr cube.(6)) || (should_be_drawn_gr cube.(7))) &&
((should_be_drawn_gr cube.(7)) || (should_be_drawn_gr cube.(4))) ;
res.(2) <-
((should_be_drawn_gr cube.(0)) || (should_be_drawn_gr cube.(5))) &&
((should_be_drawn_gr cube.(1)) || (should_be_drawn_gr cube.(4))) &&
((should_be_drawn_gr cube.(0)) || (should_be_drawn_gr cube.(1))) &&
((should_be_drawn_gr cube.(1)) || (should_be_drawn_gr cube.(5))) &&
((should_be_drawn_gr cube.(5)) || (should_be_drawn_gr cube.(4))) &&
((should_be_drawn_gr cube.(4)) || (should_be_drawn_gr cube.(0))) ;
res.(3) <-
((should_be_drawn_gr cube.(1)) || (should_be_drawn_gr cube.(6))) &&
((should_be_drawn_gr cube.(2)) || (should_be_drawn_gr cube.(5))) &&
((should_be_drawn_gr cube.(1)) || (should_be_drawn_gr cube.(2))) &&
((should_be_drawn_gr cube.(2)) || (should_be_drawn_gr cube.(6))) &&
((should_be_drawn_gr cube.(6)) || (should_be_drawn_gr cube.(5))) &&
((should_be_drawn_gr cube.(5)) || (should_be_drawn_gr cube.(1))) ;
res.(4) <-
((should_be_drawn_gr cube.(2)) || (should_be_drawn_gr cube.(7))) &&
((should_be_drawn_gr cube.(3)) || (should_be_drawn_gr cube.(6))) &&
((should_be_drawn_gr cube.(2)) || (should_be_drawn_gr cube.(3))) &&
((should_be_drawn_gr cube.(3)) || (should_be_drawn_gr cube.(7))) &&
((should_be_drawn_gr cube.(7)) || (should_be_drawn_gr cube.(6))) &&
((should_be_drawn_gr cube.(6)) || (should_be_drawn_gr cube.(2))) ;
res.(5) <-
((should_be_drawn_gr cube.(3)) || (should_be_drawn_gr cube.(4))) &&
((should_be_drawn_gr cube.(0)) || (should_be_drawn_gr cube.(7))) &&
((should_be_drawn_gr cube.(3)) || (should_be_drawn_gr cube.(0))) &&
((should_be_drawn_gr cube.(0)) || (should_be_drawn_gr cube.(4))) &&
((should_be_drawn_gr cube.(4)) || (should_be_drawn_gr cube.(7))) &&
((should_be_drawn_gr cube.(7)) || (should_be_drawn_gr cube.(3))) ;
(res, res.(0) || res.(1) || res.(2) || res.(3) || res.(4) || res.(5)) ;;
let are_edges_behind (cube : pt_3d array) =
let res = Array.make 6 [||] in
res.(0) <- [|
(should_be_drawn_gr cube.(0)) && (should_be_drawn_gr cube.(1));
(should_be_drawn_gr cube.(1)) && (should_be_drawn_gr cube.(2));
(should_be_drawn_gr cube.(2)) && (should_be_drawn_gr cube.(3));
(should_be_drawn_gr cube.(3)) && (should_be_drawn_gr cube.(0))
|];
res.(1) <- [|
(should_be_drawn_gr cube.(4)) && (should_be_drawn_gr cube.(5));
(should_be_drawn_gr cube.(5)) && (should_be_drawn_gr cube.(6));
(should_be_drawn_gr cube.(6)) && (should_be_drawn_gr cube.(7));
(should_be_drawn_gr cube.(7)) && (should_be_drawn_gr cube.(4))
|];
res.(2) <- [|
(should_be_drawn_gr cube.(0)) && (should_be_drawn_gr cube.(1));
(should_be_drawn_gr cube.(1)) && (should_be_drawn_gr cube.(5));
(should_be_drawn_gr cube.(5)) && (should_be_drawn_gr cube.(4));
(should_be_drawn_gr cube.(4)) && (should_be_drawn_gr cube.(0))
|];
res.(3) <- [|
(should_be_drawn_gr cube.(1)) && (should_be_drawn_gr cube.(2));
(should_be_drawn_gr cube.(2)) && (should_be_drawn_gr cube.(6));
(should_be_drawn_gr cube.(6)) && (should_be_drawn_gr cube.(5));
(should_be_drawn_gr cube.(5)) && (should_be_drawn_gr cube.(1))
|];
res.(4) <- [|
(should_be_drawn_gr cube.(2)) && (should_be_drawn_gr cube.(3));
(should_be_drawn_gr cube.(3)) && (should_be_drawn_gr cube.(7));
(should_be_drawn_gr cube.(7)) && (should_be_drawn_gr cube.(6));
(should_be_drawn_gr cube.(6)) && (should_be_drawn_gr cube.(2))
|];
res.(5) <- [|
(should_be_drawn_gr cube.(3)) && (should_be_drawn_gr cube.(0));
(should_be_drawn_gr cube.(0)) && (should_be_drawn_gr cube.(4));
(should_be_drawn_gr cube.(4)) && (should_be_drawn_gr cube.(7));
(should_be_drawn_gr cube.(7)) && (should_be_drawn_gr cube.(3))
|];
res ;;
let draw_cube_p (cube : pt_3d array) screen_wd screen_ht fov r g b = let draw_cube_p (cube : pt_3d array) screen_wd screen_ht fov r g b =
let adjusted = adjust_to_camera cube in let adjusted = adjust_to_camera cube in
let (draw_faces, draw_cube) = are_faces_behind adjusted in let (draw_faces, draw_cube) = are_faces_behind adjusted in
@ -338,138 +252,6 @@ let draw_cube_p (cube : pt_3d array) screen_wd screen_ht fov r g b =
done done
end ;; end ;;
let draw_cube_p_rev (cube : pt_3d array) screen_wd screen_ht fov r g b =
let adjusted = adjust_to_camera cube in
let (draw_faces, draw_cube) = are_faces_behind_rev adjusted in
if draw_cube then begin
let proj = project adjusted screen_wd screen_ht fov in
let graphed = to_graphics proj screen_wd screen_ht in
set_color (rgb 192 192 192);
let distances = [|
max (farthest_pt cube.(0) cube.(1)) (farthest_pt cube.(2) cube.(3));
max (farthest_pt cube.(4) cube.(5)) (farthest_pt cube.(6) cube.(7));
max (farthest_pt cube.(0) cube.(1)) (farthest_pt cube.(5) cube.(4));
max (farthest_pt cube.(1) cube.(2)) (farthest_pt cube.(6) cube.(5));
max (farthest_pt cube.(2) cube.(3)) (farthest_pt cube.(7) cube.(6));
max (farthest_pt cube.(3) cube.(0)) (farthest_pt cube.(4) cube.(7));
|] in
let order = [|
[|graphed.(0); graphed.(1); graphed.(2); graphed.(3); graphed.(0)|];
[|graphed.(4); graphed.(5); graphed.(6); graphed.(7); graphed.(4)|];
[|graphed.(0); graphed.(1); graphed.(5); graphed.(4); graphed.(0)|];
[|graphed.(1); graphed.(2); graphed.(6); graphed.(5); graphed.(1)|];
[|graphed.(2); graphed.(3); graphed.(7); graphed.(6); graphed.(2)|];
[|graphed.(3); graphed.(0); graphed.(4); graphed.(7); graphed.(3)|];
|] in
(* Note : edge orders must be as following :
7--------6
/| /|
/ | / |
4--------5 |
| | | |
| 3-----|--2
| / | /
|/ |/
0--------1
*)
for i = 0 to 5 do
let cur_max = ref distances.(i) in
let idmax = ref i in
for j = i to 5 do
if distances.(j) > !cur_max then begin
cur_max := distances.(j);
idmax := j
end
done;
swap distances i !idmax;
swap order i !idmax;
swap draw_faces i !idmax;
done;
set_line_width 5;
for i = 0 to 5 do
if draw_faces.(i) then begin
let light = max (0.) (1. -. (distances.(i)) /. 10.) in
let face_R = int_of_float ((float_of_int r) *. light)
and face_G = int_of_float ((float_of_int g) *. light)
and face_B = int_of_float ((float_of_int b) *. light) in
set_color (rgb face_R face_G face_B);
fill_poly order.(i);
set_color black;
draw_poly_line order.(i);
end
done
end ;;
let draw_cube_hollow_p (cube : pt_3d array) screen_wd screen_ht fov r g b=
let adjusted = adjust_to_camera cube in
let (draw_faces, draw_cube) = are_faces_behind adjusted in
let draw_edges = are_edges_behind adjusted in
if draw_cube then begin
let proj = project adjusted screen_wd screen_ht fov in
let graphed = to_graphics proj screen_wd screen_ht in
set_color (rgb 192 192 192);
let distances = [|
max (farthest_pt cube.(0) cube.(1)) (farthest_pt cube.(2) cube.(3));
max (farthest_pt cube.(4) cube.(5)) (farthest_pt cube.(6) cube.(7));
max (farthest_pt cube.(0) cube.(1)) (farthest_pt cube.(5) cube.(4));
max (farthest_pt cube.(1) cube.(2)) (farthest_pt cube.(6) cube.(5));
max (farthest_pt cube.(2) cube.(3)) (farthest_pt cube.(7) cube.(6));
max (farthest_pt cube.(3) cube.(0)) (farthest_pt cube.(4) cube.(7));
|] in
let order = [|
[|graphed.(0); graphed.(1); graphed.(2); graphed.(3); graphed.(0)|];
[|graphed.(4); graphed.(5); graphed.(6); graphed.(7); graphed.(4)|];
[|graphed.(0); graphed.(1); graphed.(5); graphed.(4); graphed.(0)|];
[|graphed.(1); graphed.(2); graphed.(6); graphed.(5); graphed.(1)|];
[|graphed.(2); graphed.(3); graphed.(7); graphed.(6); graphed.(2)|];
[|graphed.(3); graphed.(0); graphed.(4); graphed.(7); graphed.(3)|];
|] in
(* Note : edge orders must be as following :
7--------6
/| /|
/ | / |
4--------5 |
| | | |
| 3-----|--2
| / | /
|/ |/
0--------1
*)
for i = 0 to 5 do
let cur_max = ref distances.(i) in
let idmax = ref i in
for j = i to 5 do
if distances.(j) > !cur_max then begin
cur_max := distances.(j);
idmax := j
end
done;
swap distances i !idmax;
swap order i !idmax;
swap draw_faces i !idmax;
swap draw_edges i !idmax
done;
set_line_width 5;
set_color (rgb r g b);
for i = 0 to 5 do
for j = 0 to 3 do
if draw_edges.(i).(j) then begin
draw_poly_line [|order.(i).(j) ; order.(i).((j+1) mod 4)|];
end
done
done
end ;;
let sum_x (poly : pt_3d array) = let sum_x (poly : pt_3d array) =
let res = ref 0. in let res = ref 0. in
for i = 0 to (Array.length poly -1) do for i = 0 to (Array.length poly -1) do
@ -772,7 +554,7 @@ let cheesify (laby : tile array array array) =
for h = 0 to height-1 do for h = 0 to height-1 do
for d = 0 to depth-1 do for d = 0 to depth-1 do
let rand_w = Random.int width let rand_w = Random.int width
and rand_h = Random.int height and rand_h = 1 + Random.int (height-1)
and rand_d = Random.int depth in and rand_d = Random.int depth in
(*Printf.printf "chose (%d, %d, %d)" rand_w rand_h rand_d; (*Printf.printf "chose (%d, %d, %d)" rand_w rand_h rand_d;
Stdlib.print_endline " ";*) Stdlib.print_endline " ";*)
@ -869,8 +651,11 @@ let rec move_cam_hash (cubes : coloredCube dynamic) b c =(* Printf.printf "[%b]"
let play_dos laby = let play_dos laby =
try try
Stdlib.print_endline "Building terrain...";
cheesify laby; cheesify laby;
let hash = chunkify laby 3 in
Stdlib.print_endline "Rendering terrain...";
let hash = chunkify laby 2 in
camera_xyz.z <- -. (1.5) ; camera_xyz.z <- -. (1.5) ;
camera_xyz.x <- -. (float_of_int width) /. 2. ; camera_xyz.x <- -. (float_of_int width) /. 2. ;
@ -884,7 +669,7 @@ let play_dos laby =
auto_synchronize false; auto_synchronize false;
open_graph " 1500x1000"; open_graph " 1500x1000";
set_color black; set_color black;
fill_poly [|(0, 0); (1500, 0); (1500, 1000); (0, 1000); (0, 0)|]; fill_poly [|(0, 0); (__width__, 0); (__width__, __height__); (0, __height__); (0, 0)|];
set_color white; set_color white;
let (ch_x, ch_y, ch_z) = coords_to_chunk_f (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z in let (ch_x, ch_y, ch_z) = coords_to_chunk_f (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z in