Compare commits
2 Commits
d24f925b0a
...
df045e2dd6
Author | SHA1 | Date |
---|---|---|
|
df045e2dd6 | |
|
7c90754be9 |
BIN
display.cmi
BIN
display.cmi
Binary file not shown.
BIN
display.cmo
BIN
display.cmo
Binary file not shown.
602
display.ml
602
display.ml
|
@ -1,14 +1,47 @@
|
|||
open Graphics ;;
|
||||
open Tsdl ;;
|
||||
|
||||
Random.self_init () ;;
|
||||
|
||||
let __width__ = 1500
|
||||
and __height__ = 1000 ;;
|
||||
|
||||
(*
|
||||
|
||||
ocamlfind ocamlc -linkpkg -package unix -linkpkg -package graphics -linkpkg -package tsdl -thread -package threads -linkpkg display.ml
|
||||
ocamlfind ocamlc -linkpkg -package unix -linkpkg -package graphics -thread -package threads -linkpkg display.ml
|
||||
|
||||
*)
|
||||
|
||||
(* ------------------------------------------------------------- *)
|
||||
(* ------------------------------------------------------------- *)
|
||||
|
||||
type 'a dynamic = {mutable tab : 'a array ; mutable len : int ; mutable memlen : int} ;;
|
||||
|
||||
type pt_3d = {mutable x : float ; mutable y : float ; mutable z : float} ;;
|
||||
type pt_2d = {mutable x : float ; mutable y : float} ;;
|
||||
|
||||
type coloredCube = {cube : pt_3d array ; red : int ; green : int ; blue : int} ;;
|
||||
|
||||
let dyn_create i =
|
||||
{tab = Array.make 10 i ; len = 0 ; memlen = 10} ;;
|
||||
|
||||
let dyn_append arr elt =
|
||||
let fct x =
|
||||
if x < arr.len then
|
||||
arr.tab.(x)
|
||||
else
|
||||
arr.tab.(0)
|
||||
in
|
||||
if arr.len = arr.memlen then begin
|
||||
let newarr = Array.init (2 * arr.memlen) fct in
|
||||
arr.memlen <- 2 * arr.memlen;
|
||||
arr.tab <- newarr
|
||||
end;
|
||||
arr.tab.(arr.len) <- elt;
|
||||
arr.len <- arr.len + 1 ;;
|
||||
|
||||
(* ------------------------------------------------------------- *)
|
||||
(* ------------------------------------------------------------- *)
|
||||
|
||||
let matrix_mult m1 m2 =
|
||||
let n = Array.length m1
|
||||
and p = Array.length m1.(0)
|
||||
|
@ -35,9 +68,6 @@ let absf x = if x >= 0. then x else -.(x) ;;
|
|||
|
||||
(* ------------------------------------------------------------- *)
|
||||
|
||||
type pt_3d = {mutable x : float ; mutable y : float ; mutable z : float} ;;
|
||||
type pt_2d = {mutable x : float ; mutable y : float} ;;
|
||||
|
||||
(* ------------------------------------------------------------- *)
|
||||
let camera_xyz = {x = 0.0 ; y = 0.0 ; z = 0.0} ;;
|
||||
let camera_angle_x = ref 0 ;;
|
||||
|
@ -155,6 +185,92 @@ 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, 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 adjusted = adjust_to_camera cube in
|
||||
let (draw_faces, draw_cube) = are_faces_behind adjusted in
|
||||
|
@ -222,6 +338,138 @@ let draw_cube_p (cube : pt_3d array) screen_wd screen_ht fov r g b =
|
|||
done
|
||||
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)) /. 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 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 res = ref 0. in
|
||||
for i = 0 to (Array.length poly -1) do
|
||||
|
@ -251,46 +499,15 @@ let cube_dist (c : pt_3d array) =
|
|||
}
|
||||
in dist_from_camera mid_pt ;;
|
||||
|
||||
let draw_multiples_cubes (cubes : pt_3d array array) screen_wd screen_ht fov =
|
||||
let n = Array.length cubes in
|
||||
let new_arr = Array.make n cubes.(0) in
|
||||
let draw_multiples_cubes_colored (cubes : pt_3d array dynamic) rs gs bs screen_wd screen_ht fov render_dist =
|
||||
let n = cubes.len in
|
||||
|
||||
let distances = Array.make n 0. in
|
||||
|
||||
for i = 0 to n-1 do
|
||||
new_arr.(i) <- cubes.(i);
|
||||
distances.(i) <- cube_dist cubes.(i)
|
||||
distances.(i) <- cube_dist cubes.tab.(i);
|
||||
done ;
|
||||
for i = 0 to n-1 do
|
||||
let cur_max = ref distances.(i) in
|
||||
let idmax = ref i in
|
||||
for j = i to n-1 do
|
||||
if distances.(j) > !cur_max then begin
|
||||
cur_max := distances.(j);
|
||||
idmax := j
|
||||
end
|
||||
done;
|
||||
swap distances i !idmax;
|
||||
swap new_arr i !idmax;
|
||||
done;
|
||||
for i = 0 to n-1 do
|
||||
draw_cube_p new_arr.(i) screen_wd screen_ht fov 192 192 192
|
||||
done ;;
|
||||
|
||||
let draw_multiples_cubes_colored (cubes : pt_3d array array) maxlen rs gs bs screen_wd screen_ht fov render_dist =
|
||||
let n = maxlen in
|
||||
|
||||
let new_arr = Array.make n cubes.(0)
|
||||
and distances = Array.make n 0.
|
||||
and reds = Array.make n 0
|
||||
and greens = Array.make n 0
|
||||
and blues = Array.make n 0 in
|
||||
|
||||
for i = 0 to n-1 do
|
||||
new_arr.(i) <- cubes.(i);
|
||||
distances.(i) <- cube_dist cubes.(i);
|
||||
reds.(i) <- rs.(i);
|
||||
greens.(i) <- gs.(i);
|
||||
blues.(i) <- bs.(i)
|
||||
done ;
|
||||
for i = 0 to n-1 do
|
||||
let cur_max = ref distances.(i) in
|
||||
let idmax = ref i in
|
||||
|
@ -301,16 +518,42 @@ let draw_multiples_cubes_colored (cubes : pt_3d array array) maxlen rs gs bs scr
|
|||
end
|
||||
done;
|
||||
swap distances i !idmax;
|
||||
swap new_arr i !idmax;
|
||||
swap reds i !idmax;
|
||||
swap greens i !idmax;
|
||||
swap blues i !idmax;
|
||||
swap cubes.tab i !idmax;
|
||||
done;
|
||||
for i = 0 to n-1 do
|
||||
(*Printf.printf "drawing cube (%f, %f, %f) with distance %f" new_arr.(i).(0).x new_arr.(i).(0).y new_arr.(i).(0).z distances.(i);
|
||||
Stdlib.print_endline "...";*)
|
||||
if distances.(i) <= (float_of_int render_dist) then
|
||||
draw_cube_p new_arr.(i) screen_wd screen_ht fov reds.(i) greens.(i) blues.(i)
|
||||
if distances.(i) <= (float_of_int render_dist) then begin
|
||||
draw_cube_p cubes.tab.(i) screen_wd screen_ht fov rs.tab.(i) gs.tab.(i) bs.tab.(i)
|
||||
end
|
||||
done ;;
|
||||
|
||||
let draw_multiples_cubes_colored_hash (dyna : coloredCube dynamic) screen_wd screen_ht fov =
|
||||
let n = dyna.len in
|
||||
|
||||
(*Printf.printf ">> %d <<" n;
|
||||
Stdlib.print_endline " ";*)
|
||||
|
||||
let distances = Array.make n 0. in
|
||||
|
||||
for i = 0 to n-1 do
|
||||
distances.(i) <- cube_dist dyna.tab.(i).cube;
|
||||
done ;
|
||||
|
||||
for i = 0 to n-1 do
|
||||
let cur_max = ref distances.(i) in
|
||||
let idmax = ref i in
|
||||
for j = i to n-1 do
|
||||
if distances.(j) > !cur_max then begin
|
||||
cur_max := distances.(j);
|
||||
idmax := j;
|
||||
end
|
||||
done;
|
||||
swap distances i !idmax;
|
||||
swap dyna.tab i !idmax;
|
||||
done;
|
||||
for i = 0 to n-1 do
|
||||
(*Printf.printf "(%d, %d, %d)" (dyna.tab.(i).red) (dyna.tab.(i).green) (dyna.tab.(i).blue);
|
||||
Stdlib.print_endline " ";*)
|
||||
draw_cube_p (dyna.tab.(i).cube) screen_wd screen_ht fov (dyna.tab.(i).red) (dyna.tab.(i).green) (dyna.tab.(i).blue)
|
||||
done ;;
|
||||
|
||||
let create_cube x0' y0' z0' sz' =
|
||||
|
@ -330,69 +573,8 @@ let create_cube x0' y0' z0' sz' =
|
|||
|]
|
||||
in res ;;
|
||||
|
||||
let cube = [|
|
||||
{x = -1.; y = 1.; z = 1.};
|
||||
{x = -2.; y = 1.; z = 1.};
|
||||
{x = -2.; y = 2.; z = 1.};
|
||||
{x = -1.; y = 2.; z = 1.};
|
||||
{x = -1.; y = 1.; z = 2.};
|
||||
{x = -2.; y = 1.; z = 2.};
|
||||
{x = -2.; y = 2.; z = 2.};
|
||||
{x = -1.; y = 2.; z = 2.}
|
||||
|] ;;
|
||||
|
||||
let cube2 = [|
|
||||
{x = 1.; y = 1.; z = 1.};
|
||||
{x = 2.; y = 1.; z = 1.};
|
||||
{x = 2.; y = 2.; z = 1.};
|
||||
{x = 1.; y = 2.; z = 1.};
|
||||
{x = 1.; y = 1.; z = 2.};
|
||||
{x = 2.; y = 1.; z = 2.};
|
||||
{x = 2.; y = 2.; z = 2.};
|
||||
{x = 1.; y = 2.; z = 2.}
|
||||
|] ;;
|
||||
|
||||
let cube3 = [|
|
||||
{x = 1.; y = 1.; z = 2.};
|
||||
{x = 2.; y = 1.; z = 2.};
|
||||
{x = 2.; y = 2.; z = 2.};
|
||||
{x = 1.; y = 2.; z = 2.};
|
||||
{x = 1.; y = 1.; z = 3.};
|
||||
{x = 2.; y = 1.; z = 3.};
|
||||
{x = 2.; y = 2.; z = 3.};
|
||||
{x = 1.; y = 2.; z = 3.}
|
||||
|] ;;
|
||||
|
||||
let cube4 = [|
|
||||
{x = 1.; y = 0.; z = 2.};
|
||||
{x = 2.; y = 0.; z = 2.};
|
||||
{x = 2.; y = -1.; z = 2.};
|
||||
{x = 1.; y = -1. ; z = 2.};
|
||||
{x = 1.; y = 0.; z = 3.};
|
||||
{x = 2.; y = 0.; z = 3.};
|
||||
{x = 2.; y = -1.; z = 3.};
|
||||
{x = 1.; y = -1.; z = 3.}
|
||||
|] ;;
|
||||
|
||||
let fov = 90 ;;
|
||||
|
||||
let hehe () =
|
||||
open_graph " 1500x1000" ;
|
||||
set_window_title "3D" ;
|
||||
|
||||
camera_xyz.z <- 0.8;
|
||||
for i = 0 to 488 do
|
||||
open_graph " 1500x1000" ;
|
||||
draw_multiples_cubes [|cube ; cube4 ; cube2 ; cube3|] 1500 1000 fov ;
|
||||
camera_xyz.z <- camera_xyz.z -. 0.1;
|
||||
Stdlib.print_endline "-";
|
||||
Stdlib.print_endline "-";
|
||||
Unix.sleepf 0.15
|
||||
done;
|
||||
|
||||
Unix.sleepf 1.0;
|
||||
close_graph () ;;
|
||||
|
||||
(*
|
||||
7--------6
|
||||
/| /|
|
||||
|
@ -441,6 +623,10 @@ and depth = 15 ;;
|
|||
(* dimensions *)
|
||||
|
||||
let render_distance = 7 ;;
|
||||
let chunk_dist = 1 ;;
|
||||
|
||||
let chunk_size = 4 ;;
|
||||
let chunk_size_f = float_of_int chunk_size ;;
|
||||
|
||||
let laby = Array.make width [|[||]|] ;;
|
||||
for i = 0 to width -1 do
|
||||
|
@ -449,17 +635,25 @@ done ;;
|
|||
|
||||
let n_walls = ref (width*height*depth) ;;
|
||||
|
||||
let ctc_one x =
|
||||
if x > 0 then
|
||||
x / chunk_size
|
||||
else
|
||||
x / chunk_size -1 ;;
|
||||
|
||||
let ctcf_one x =
|
||||
if x > 0. then
|
||||
int_of_float (x /. chunk_size_f)
|
||||
else
|
||||
int_of_float (x /. chunk_size_f) -1 ;;
|
||||
|
||||
let coords_to_chunk x y z =
|
||||
(ctc_one x, ctc_one y, ctc_one z) ;;
|
||||
|
||||
let coords_to_chunk_f x y z =
|
||||
(ctcf_one x, ctcf_one y, ctcf_one z) ;;
|
||||
|
||||
let is_collision_cube (cam_coords : pt_3d) (cube : pt_3d array) =
|
||||
(*Printf.printf "testing cube [%b, %b, %b, %b, %b, %b] :"
|
||||
(cube.(0).x >= (-. cam_coords.x))
|
||||
(cube.(0).y >= (-. cam_coords.x))
|
||||
(cube.(0).z >= cam_coords.z)
|
||||
(cube.(6).x <= (-. cam_coords.x))
|
||||
(cube.(6).y <= (-. cam_coords.y))
|
||||
(cube.(6).z <= cam_coords.z) ;
|
||||
Printf.printf "\n||{%f, %f, %f}, {%f, %f, %f}||\n" cube.(0).x cube.(0).y cube.(0).z cube.(6).x cube.(6).y cube.(6).z;
|
||||
Printf.printf "with coords (%f, %f, %f)\n" (-. cam_coords.x) (-. cam_coords.y) cam_coords.z;
|
||||
Stdlib.print_endline " ";*)
|
||||
cube.(0).x <= (-. cam_coords.x) &&
|
||||
cube.(0).y <= (-. cam_coords.y) &&
|
||||
cube.(0).z <= cam_coords.z &&
|
||||
|
@ -470,40 +664,101 @@ let is_collision_cube (cam_coords : pt_3d) (cube : pt_3d array) =
|
|||
let is_collision (cam_coords : pt_3d) (cubes : pt_3d array array) =
|
||||
let res = ref false in
|
||||
let n = !n_walls in
|
||||
|
||||
let distances = Array.make n 0. in
|
||||
|
||||
for i = 0 to n-1 do
|
||||
if not !res then
|
||||
distances.(i) <- cube_dist cubes.(i);
|
||||
done ;
|
||||
|
||||
for i = 0 to n-1 do
|
||||
if not !res && distances.(i) < 2. then
|
||||
res := is_collision_cube cam_coords cubes.(i)
|
||||
done;
|
||||
!res ;;
|
||||
|
||||
let is_collision_hash (cam_coords : pt_3d) (cubes : coloredCube dynamic) =
|
||||
let res = ref false in
|
||||
let n = cubes.len in
|
||||
|
||||
let distances = Array.make n 0. in
|
||||
|
||||
for i = 0 to n-1 do
|
||||
distances.(i) <- cube_dist cubes.tab.(i).cube;
|
||||
done ;
|
||||
|
||||
for i = 0 to n-1 do
|
||||
if not !res && distances.(i) < 2. then
|
||||
res := is_collision_cube cam_coords cubes.tab.(i).cube
|
||||
done;
|
||||
!res ;;
|
||||
|
||||
let convert_laby laby =
|
||||
let width = Array.length laby
|
||||
and height = Array.length laby.(0)
|
||||
and depth = Array.length laby.(0).(0) in
|
||||
|
||||
let cubes = Array.make (width*height*depth) [||]
|
||||
and reds = Array.make (width*height*depth) 0
|
||||
and greens = Array.make (width*height*depth) 0
|
||||
and blues = Array.make (width*height*depth) 0 in
|
||||
let cubes = dyn_create (create_cube 0 0 0 0)
|
||||
and reds = dyn_create 0
|
||||
and greens = dyn_create 0
|
||||
and blues = dyn_create 0 in
|
||||
|
||||
let index = ref 0 in
|
||||
for w = 0 to width-1 do
|
||||
for h = 0 to height-1 do
|
||||
for d = 0 to depth-1 do
|
||||
if laby.(w).(h).(d) <> Free then begin
|
||||
(*Printf.printf "added (%d, %d, %d)" w h d;
|
||||
Stdlib.print_endline " ";*)
|
||||
cubes.(!index) <- create_cube w h d 1;
|
||||
reds.(!index) <- 212;
|
||||
greens.(!index) <- 212;
|
||||
blues.(!index) <- 212;
|
||||
incr index
|
||||
dyn_append cubes (create_cube w h d 1);
|
||||
dyn_append reds 212;
|
||||
dyn_append greens 212;
|
||||
dyn_append blues 212;
|
||||
end
|
||||
done
|
||||
done
|
||||
done;
|
||||
(cubes, reds, greens, blues) ;;
|
||||
|
||||
let cheesify laby =
|
||||
let chunkify laby sz =
|
||||
let width = Array.length laby
|
||||
and height = Array.length laby.(0)
|
||||
and depth = Array.length laby.(0).(0) in
|
||||
|
||||
let cubes = Hashtbl.create 300 in
|
||||
|
||||
let add_to_table w h d cw ch cd =
|
||||
(*Printf.printf "(%d, %d, %d) (%d, %d, %d)\n" w h d cw ch cd;*)
|
||||
match Hashtbl.find_opt cubes (cw, ch, cd) with
|
||||
| None -> begin
|
||||
let dyna = dyn_create {cube = create_cube (w*sz) (h*sz) (d*sz) sz; red = 220; green = 220; blue = 220} in
|
||||
Hashtbl.add cubes (cw, ch, cd) dyna
|
||||
end
|
||||
| Some dyna -> begin
|
||||
Hashtbl.remove cubes (cw, ch, cd);
|
||||
dyn_append dyna {cube = create_cube (w*sz) (h*sz) (d*sz) sz; red = 220; green = 220; blue = 220};
|
||||
Hashtbl.add cubes (cw, ch, cd) dyna
|
||||
end
|
||||
in
|
||||
|
||||
for w = 0 to width-1 do
|
||||
for h = 0 to height-1 do
|
||||
for d = 0 to depth-1 do
|
||||
if laby.(w).(h).(d) <> Free then begin
|
||||
for i = -chunk_dist to chunk_dist do
|
||||
for j = -chunk_dist to chunk_dist do
|
||||
for k = -chunk_dist to chunk_dist do
|
||||
add_to_table w h d (w/chunk_size + i) (h/chunk_size + j) (d/chunk_size + k)
|
||||
done
|
||||
done
|
||||
done
|
||||
end(*;
|
||||
Stdlib.print_endline " ";*)
|
||||
done
|
||||
done
|
||||
done;
|
||||
cubes ;;
|
||||
|
||||
let cheesify (laby : tile array array array) =
|
||||
let width = Array.length laby
|
||||
and height = Array.length laby.(0)
|
||||
and depth = Array.length laby.(0).(0) in
|
||||
|
@ -524,14 +779,6 @@ let cheesify laby =
|
|||
done
|
||||
done;;
|
||||
|
||||
let print_cubes (cubes : pt_3d array array) =
|
||||
for i = 0 to !n_walls -1 do
|
||||
for j = 0 to 7 do
|
||||
Printf.printf " {%f, %f, %f}\n" cubes.(i).(j).x cubes.(i).(j).y cubes.(i).(j).z
|
||||
done;
|
||||
Stdlib.print_endline " "
|
||||
done ;;
|
||||
|
||||
(* z q s d for movement, p to go up, m to go down, a to rotate left, e to rotate right *)
|
||||
let rec move_cam (cubes : pt_3d array array) b c =(* Printf.printf "[%b]" b; Stdlib.print_endline " " ; *)match c with
|
||||
| 'z' ->
|
||||
|
@ -577,7 +824,7 @@ let play laby =
|
|||
fill_poly [|(0, 0); (1500, 0); (1500, 1000); (0, 1000); (0, 0)|];
|
||||
set_color white;
|
||||
|
||||
draw_multiples_cubes_colored cs !n_walls rs gs bs 1500 1000 fov render_distance ;
|
||||
draw_multiples_cubes_colored cs rs gs bs __width__ __height__ fov render_distance ;
|
||||
|
||||
auto_synchronize true;
|
||||
|
||||
|
@ -585,20 +832,71 @@ let play laby =
|
|||
Stdlib.print_endline " ";
|
||||
|
||||
let usr_input = get1char () in
|
||||
move_cam cs true usr_input
|
||||
move_cam cs.tab true usr_input
|
||||
done ;;
|
||||
|
||||
let test1 laby =
|
||||
cheesify laby ;
|
||||
let (cs, rs, gs, bs) = convert_laby laby in
|
||||
let rec move_cam_hash (cubes : coloredCube dynamic) b c =(* Printf.printf "[%b]" b; Stdlib.print_endline " " ; *)match c with
|
||||
| 'z' ->
|
||||
camera_xyz.z <- camera_xyz.z +. Float.cos ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
|
||||
camera_xyz.x <- camera_xyz.x +. Float.sin ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
|
||||
if b && (is_collision_hash camera_xyz cubes) then move_cam_hash cubes false 's'
|
||||
| 'q' ->
|
||||
camera_xyz.z <- camera_xyz.z +. Float.cos (((float_of_int !camera_angle_y) +. 90.) *. 3.1415926535 /. 180.);
|
||||
camera_xyz.x <- camera_xyz.x +. Float.sin (((float_of_int !camera_angle_y) +. 90.) *. 3.1415926535 /. 180.);
|
||||
if b && (is_collision_hash camera_xyz cubes) then move_cam_hash cubes false 'q'
|
||||
| 's' ->
|
||||
camera_xyz.z <- camera_xyz.z -. Float.cos ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
|
||||
camera_xyz.x <- camera_xyz.x -. Float.sin ((float_of_int !camera_angle_y) *. 3.1415926535 /. 180.);
|
||||
if b && (is_collision_hash camera_xyz cubes) then move_cam_hash cubes false 'z'
|
||||
| 'd' ->
|
||||
camera_xyz.z <- camera_xyz.z +. Float.cos (((float_of_int !camera_angle_y) -. 90.) *. 3.1415926535 /. 180.);
|
||||
camera_xyz.x <- camera_xyz.x +. Float.sin (((float_of_int !camera_angle_y) -. 90.) *. 3.1415926535 /. 180.);
|
||||
if b && (is_collision_hash camera_xyz cubes) then move_cam_hash cubes false 'q'
|
||||
| 'p' ->
|
||||
camera_xyz.y <- camera_xyz.y -. 1.;
|
||||
if b && (is_collision_hash camera_xyz cubes) then move_cam_hash cubes false 'm'
|
||||
| 'm' ->
|
||||
camera_xyz.y <- camera_xyz.y +. 1.;
|
||||
if b && (is_collision_hash camera_xyz cubes) then move_cam_hash cubes false 'p'
|
||||
| 'a' -> camera_angle_y := !camera_angle_y + 30
|
||||
| 'e' -> camera_angle_y := !camera_angle_y - 30
|
||||
| _ -> () ;;
|
||||
|
||||
let play_dos laby =
|
||||
try
|
||||
cheesify laby;
|
||||
let hash = chunkify laby 1 in
|
||||
|
||||
camera_xyz.z <- -. (1.5) ;
|
||||
camera_xyz.x <- -. (float_of_int width) /. 2. ;
|
||||
camera_xyz.y <- -. (float_of_int height) /. 2. ;
|
||||
while true do
|
||||
open_graph " 1500x1000";
|
||||
draw_multiples_cubes_colored cs !n_walls rs gs bs 1500 1000 fov render_distance ;
|
||||
camera_xyz.z <- camera_xyz.z +. 1.;
|
||||
Unix.sleepf 1.25
|
||||
done ;;
|
||||
|
||||
play laby ;;
|
||||
(*print_cubes cs ;*)
|
||||
|
||||
while true do
|
||||
ignore (Sys.command "clear") ;
|
||||
|
||||
auto_synchronize false;
|
||||
open_graph " 1500x1000";
|
||||
set_color black;
|
||||
fill_poly [|(0, 0); (1500, 0); (1500, 1000); (0, 1000); (0, 0)|];
|
||||
set_color white;
|
||||
|
||||
let (ch_x, ch_y, ch_z) = coords_to_chunk_f (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z in
|
||||
|
||||
draw_multiples_cubes_colored_hash (Hashtbl.find hash (ch_x, ch_y, ch_z)) __width__ __height__ fov ;
|
||||
|
||||
auto_synchronize true;
|
||||
|
||||
Printf.printf "current pos : (%f, %f, %f)\n" (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z;
|
||||
Printf.printf "current chunk : (%d, %d, %d)" ch_x ch_y ch_z;
|
||||
Stdlib.print_endline " ";
|
||||
|
||||
let usr_input = get1char () in
|
||||
move_cam_hash (Hashtbl.find hash (ch_x, ch_y, ch_z)) true usr_input
|
||||
done ;
|
||||
()
|
||||
with
|
||||
| Not_found -> Stdlib.print_endline "Looks like you tried to load an uninitialized chunk..." ;;
|
||||
|
||||
play_dos laby ;;
|
Loading…
Reference in New Issue