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,8 +142,21 @@ 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);
@ -193,14 +202,11 @@ let draw_cube_p (cube : pt_3d array) screen_wd screen_ht fov r g b =
done; done;
swap distances i !idmax; swap distances i !idmax;
swap order i !idmax; swap order i !idmax;
swap draw_faces i !idmax;
done; done;
(*Printf.printf "| ";
for i = 0 to 5 do
Printf.printf "%f " distances.(i)
done;
Stdlib.print_endline "|";*)
set_line_width 5; set_line_width 5;
for i = 0 to 5 do for i = 0 to 5 do
if draw_faces.(i) then begin
let light = max (0.) (1. -. (distances.(i)) /. 7.5) in let light = max (0.) (1. -. (distances.(i)) /. 7.5) in
let face_R = int_of_float ((float_of_int r) *. light) let face_R = int_of_float ((float_of_int r) *. light)
and face_G = int_of_float ((float_of_int g) *. light) and face_G = int_of_float ((float_of_int g) *. light)
@ -209,7 +215,9 @@ let draw_cube_p (cube : pt_3d array) screen_wd screen_ht fov r g b =
fill_poly order.(i); fill_poly order.(i);
set_color black; set_color black;
draw_poly_line order.(i); draw_poly_line order.(i);
done ;; end
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
@ -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