Fixed cubes behind camera clogging up screen

This commit is contained in:
Alexandre 2024-06-30 17:34:03 +02:00
parent 480ac36687
commit d56f1cc818
5 changed files with 71 additions and 113 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -9,10 +9,6 @@ ocamlfind ocamlc -linkpkg -package unix -linkpkg -package graphics -linkpkg -pac
*) *)
ignore (Sdl.init Sdl.Init.everything) ;;
let p = Sdl.fill_rect ;;
let matrix_mult m1 m2 = let matrix_mult m1 m2 =
let n = Array.length m1 let n = Array.length m1
and p = Array.length m1.(0) and p = Array.length m1.(0)
@ -146,70 +142,82 @@ let swap arr i j =
arr.(i) <- arr.(j); arr.(i) <- arr.(j);
arr.(j) <- temp ;; arr.(j) <- temp ;;
let are_faces_behind (cube : pt_3d array) =
let res = Array.make 6 false in
res.(0) <- (should_be_drawn_gr cube.(0)) || (should_be_drawn_gr cube.(1)) || (should_be_drawn_gr cube.(2)) || (should_be_drawn_gr cube.(3));
res.(1) <- (should_be_drawn_gr cube.(4)) || (should_be_drawn_gr cube.(5)) || (should_be_drawn_gr cube.(6)) || (should_be_drawn_gr cube.(7));
res.(2) <- (should_be_drawn_gr cube.(0)) || (should_be_drawn_gr cube.(1)) || (should_be_drawn_gr cube.(5)) || (should_be_drawn_gr cube.(4));
res.(3) <- (should_be_drawn_gr cube.(1)) || (should_be_drawn_gr cube.(2)) || (should_be_drawn_gr cube.(6)) || (should_be_drawn_gr cube.(5));
res.(4) <- (should_be_drawn_gr cube.(2)) || (should_be_drawn_gr cube.(3)) || (should_be_drawn_gr cube.(7)) || (should_be_drawn_gr cube.(6));
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)) ;;
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 proj = project (adjust_to_camera cube) screen_wd screen_ht fov in let adjusted = adjust_to_camera cube in
let (draw_faces, draw_cube) = are_faces_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 let graphed = to_graphics proj screen_wd screen_ht in
set_color (rgb 192 192 192); set_color (rgb 192 192 192);
let distances = [| let distances = [|
max (farthest_pt cube.(0) cube.(1)) (farthest_pt cube.(2) cube.(3)); 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.(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.(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.(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.(2) cube.(3)) (farthest_pt cube.(7) cube.(6));
max (farthest_pt cube.(3) cube.(0)) (farthest_pt cube.(4) cube.(7)); max (farthest_pt cube.(3) cube.(0)) (farthest_pt cube.(4) cube.(7));
|] in |] in
let order = [| let order = [|
[|graphed.(0); graphed.(1); graphed.(2); graphed.(3); graphed.(0)|]; [|graphed.(0); graphed.(1); graphed.(2); graphed.(3); graphed.(0)|];
[|graphed.(4); graphed.(5); graphed.(6); graphed.(7); graphed.(4)|]; [|graphed.(4); graphed.(5); graphed.(6); graphed.(7); graphed.(4)|];
[|graphed.(0); graphed.(1); graphed.(5); graphed.(4); graphed.(0)|]; [|graphed.(0); graphed.(1); graphed.(5); graphed.(4); graphed.(0)|];
[|graphed.(1); graphed.(2); graphed.(6); graphed.(5); graphed.(1)|]; [|graphed.(1); graphed.(2); graphed.(6); graphed.(5); graphed.(1)|];
[|graphed.(2); graphed.(3); graphed.(7); graphed.(6); graphed.(2)|]; [|graphed.(2); graphed.(3); graphed.(7); graphed.(6); graphed.(2)|];
[|graphed.(3); graphed.(0); graphed.(4); graphed.(7); graphed.(3)|]; [|graphed.(3); graphed.(0); graphed.(4); graphed.(7); graphed.(3)|];
|] in |] in
(* Note : edge orders must be as following : (* Note : edge orders must be as following :
7--------6 7--------6
/| /| /| /|
/ | / | / | / |
4--------5 | 4--------5 |
| | | | | | | |
| 3-----|--2 | 3-----|--2
| / | / | / | /
|/ |/ |/ |/
0--------1 0--------1
*) *)
for i = 0 to 5 do for i = 0 to 5 do
let cur_max = ref distances.(i) in let cur_max = ref distances.(i) in
let idmax = ref i in let idmax = ref i in
for j = i to 5 do for j = i to 5 do
if distances.(j) > !cur_max then begin if distances.(j) > !cur_max then begin
cur_max := distances.(j); cur_max := distances.(j);
idmax := j idmax := j
end end
done;
swap distances i !idmax;
swap order i !idmax;
swap draw_faces i !idmax;
done; done;
swap distances i !idmax; set_line_width 5;
swap order i !idmax; for i = 0 to 5 do
done; if draw_faces.(i) then begin
(*Printf.printf "| "; let light = max (0.) (1. -. (distances.(i)) /. 7.5) in
for i = 0 to 5 do let face_R = int_of_float ((float_of_int r) *. light)
Printf.printf "%f " distances.(i) and face_G = int_of_float ((float_of_int g) *. light)
done; and face_B = int_of_float ((float_of_int b) *. light) in
Stdlib.print_endline "|";*) set_color (rgb face_R face_G face_B);
set_line_width 5; fill_poly order.(i);
for i = 0 to 5 do set_color black;
let light = max (0.) (1. -. (distances.(i)) /. 7.5) in draw_poly_line order.(i);
let face_R = int_of_float ((float_of_int r) *. light) end
and face_G = int_of_float ((float_of_int g) *. light) done
and face_B = int_of_float ((float_of_int b) *. light) in end ;;
set_color (rgb face_R face_G face_B);
fill_poly order.(i);
set_color black;
draw_poly_line order.(i);
done ;;
let sum_x (poly : pt_3d array) = let sum_x (poly : pt_3d array) =
let res = ref 0. in let res = ref 0. in
@ -392,6 +400,8 @@ let get1char () =
(* ---------------------------------------------------- *) (* ---------------------------------------------------- *)
(* ---------------------------------------------------- *) (* ---------------------------------------------------- *)
(* ---------------------------------------------------- *)
(* ---------------------------------------------------- *)
type tile = Free | Wall | Crate | Exit | Craxit | Camera ;; type tile = Free | Wall | Crate | Exit | Craxit | Camera ;;

View File

@ -1,52 +0,0 @@
val p : 'a
val matrix_mult : int array array -> int array array -> int array array
val abs : int -> int
val absf : float -> float
type pt_3d = { mutable x : float; mutable y : float; mutable z : float; }
type pt_2d = { mutable x : float; mutable y : float; }
val camera_xyz : pt_3d
val camera_angle : int ref
val should_be_drawn_gr : pt_3d -> bool
val sign : float -> float
val is_cube_behind_camera : pt_3d array -> bool
val debug_1 : pt_3d array -> unit
val to_graphics : pt_2d array -> int -> int -> (int * int) array
val draw_pts_2d : pt_2d array -> int -> int -> unit
val project : pt_3d array -> int -> int -> int -> pt_2d array
val adjust_to_camera : pt_3d array -> pt_3d array
val sq : float -> float
val dist_from_camera : pt_3d -> float
val farthest_pt : pt_3d -> pt_3d -> float
val swap : 'a array -> int -> int -> unit
val draw_cube_p :
pt_3d array -> int -> int -> int -> int -> int -> int -> unit
val sum_x : pt_3d array -> float
val sum_y : pt_3d array -> float
val sum_z : pt_3d array -> float
val cube_dist : pt_3d array -> float
val draw_multiples_cubes : pt_3d array array -> int -> int -> int -> unit
val draw_multiples_cubes_colored :
pt_3d array array ->
int -> int array -> int array -> int array -> int -> int -> int -> unit
val create_cube : int -> int -> int -> int -> pt_3d array
val cube : pt_3d array
val cube2 : pt_3d array
val cube3 : pt_3d array
val cube4 : pt_3d array
val fov : int
val hehe : unit -> 'a
val get1char : unit -> char
type tile = Free | Wall | Crate | Exit | Craxit | Camera
val width : int
val height : int
val depth : int
val laby : tile array array array
val n_walls : int ref
val convert_laby :
tile array array array ->
pt_3d array array * int array * int array * int array
val cheesify : tile array array array -> unit
val cs : pt_3d array array
val rs : int array
val gs : int array
val bs : int array