diff --git a/a.out b/a.out index d4a9451..f30b51f 100755 Binary files a/a.out and b/a.out differ diff --git a/display.cmi b/display.cmi index 7461930..341bd14 100644 Binary files a/display.cmi and b/display.cmi differ diff --git a/display.cmo b/display.cmo index 627d725..edcc38e 100644 Binary files a/display.cmo and b/display.cmo differ diff --git a/display.ml b/display.ml index 832f83b..65d8167 100644 --- a/display.ml +++ b/display.ml @@ -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 n = Array.length m1 and p = Array.length m1.(0) @@ -146,70 +142,82 @@ let swap arr i j = arr.(i) <- arr.(j); 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 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 - set_color (rgb 192 192 192); + 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 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 + 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 - *) + (* 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 + 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; - swap distances i !idmax; - swap order i !idmax; - done; - (*Printf.printf "| "; - for i = 0 to 5 do - Printf.printf "%f " distances.(i) - done; - Stdlib.print_endline "|";*) - set_line_width 5; - for i = 0 to 5 do - let light = max (0.) (1. -. (distances.(i)) /. 7.5) 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); - done ;; + set_line_width 5; + for i = 0 to 5 do + if draw_faces.(i) then begin + let light = max (0.) (1. -. (distances.(i)) /. 7.5) 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 sum_x (poly : pt_3d array) = let res = ref 0. in @@ -392,6 +400,8 @@ let get1char () = (* ---------------------------------------------------- *) (* ---------------------------------------------------- *) +(* ---------------------------------------------------- *) +(* ---------------------------------------------------- *) type tile = Free | Wall | Crate | Exit | Craxit | Camera ;; diff --git a/display.mli b/display.mli deleted file mode 100644 index 9b8277f..0000000 --- a/display.mli +++ /dev/null @@ -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