Screen no longer flickers when moving ; added dark background ; added player control keys

This commit is contained in:
Alexandre 2024-06-30 19:45:52 +02:00
parent d56f1cc818
commit d24f925b0a
4 changed files with 143 additions and 20 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -40,13 +40,16 @@ type pt_2d = {mutable x : float ; mutable y : float} ;;
(* ------------------------------------------------------------- *)
let camera_xyz = {x = 0.0 ; y = 0.0 ; z = 0.0} ;;
let camera_angle = ref 0 ;; (* in degrees *)
let camera_angle_x = ref 0 ;;
let camera_angle_y = ref 0 ;;
let camera_angle_z = ref 0 ;;
(* in degrees *)
(* ------------------------------------------------------------- *)
(*
let should_be_drawn (pt : pt_3d) =
let translated = {x = pt.x -. camera_xyz.x; y = pt.y -. camera_xyz.y; z = pt.z +. camera_xyz.z} in
(translated.z *. Float.cos ((float_of_int !camera_angle) *. 3.14159255358 /. 180.) -. translated.x *. Float.sin ((float_of_int !camera_angle) *. 3.14159255358 /. 180.)) > 0. ;;
(translated.z *. Float.cos ((float_of_int !camera_angle_y) *. 3.14159255358 /. 180.) -. translated.x *. Float.sin ((float_of_int !camera_angle_y) *. 3.14159255358 /. 180.)) > 0. ;;
*)
let should_be_drawn_gr (pt : pt_3d) =
@ -121,9 +124,9 @@ let adjust_to_camera (shape : pt_3d array) =
let res2 = Array.make (Array.length shape) {z =0.0 ; x =0.0 ; y =0.0} in
for i = 0 to Array.length shape -1 do
res2.(i) <- {
x = res.(i).x *. Float.cos ((float_of_int !camera_angle) *. 3.14159255358 /. 180.) +. res.(i).z *. Float.sin ((float_of_int !camera_angle) *. 3.14159255358 /. 180.);
x = res.(i).x *. Float.cos ((float_of_int !camera_angle_y) *. 3.14159255358 /. 180.) +. res.(i).z *. Float.sin ((float_of_int !camera_angle_y) *. 3.14159255358 /. 180.);
y = res.(i).y;
z = res.(i).z *. Float.cos ((float_of_int !camera_angle) *. 3.14159255358 /. 180.) -. res.(i).x *. Float.sin ((float_of_int !camera_angle) *. 3.14159255358 /. 180.)
z = res.(i).z *. Float.cos ((float_of_int !camera_angle_y) *. 3.14159255358 /. 180.) -. res.(i).x *. Float.sin ((float_of_int !camera_angle_y) *. 3.14159255358 /. 180.)
}
done;
(*debug_1 res2 ;*)
@ -272,7 +275,7 @@ let draw_multiples_cubes (cubes : pt_3d array array) screen_wd screen_ht fov =
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 =
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)
@ -306,6 +309,7 @@ let draw_multiples_cubes_colored (cubes : pt_3d array array) maxlen rs gs bs scr
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)
done ;;
@ -389,6 +393,32 @@ let hehe () =
Unix.sleepf 1.0;
close_graph () ;;
(*
7--------6
/| /|
/ | / |
4--------5 |
| | | |
| 3-----|--2
| / | /
|/ |/
0--------1
[|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)|];
*)
let print_cube (cube : pt_3d array) =
for j = 0 to 7 do
Printf.printf " {%f, %f, %f}\n" cube.(j).x cube.(j).y cube.(j).z
done;
Stdlib.print_endline " " ;;
let get1char () =
let termio = Unix.tcgetattr Unix.stdin in
let () =
@ -405,11 +435,13 @@ let get1char () =
type tile = Free | Wall | Crate | Exit | Craxit | Camera ;;
let width = 5
and height = 5
and depth = 5 ;;
let width = 15
and height = 15
and depth = 15 ;;
(* dimensions *)
let render_distance = 7 ;;
let laby = Array.make width [|[||]|] ;;
for i = 0 to width -1 do
laby.(i) <- Array.make_matrix height depth Wall
@ -417,6 +449,32 @@ done ;;
let n_walls = ref (width*height*depth) ;;
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 &&
cube.(6).x >= (-. cam_coords.x) &&
cube.(6).y >= (-. cam_coords.y) &&
cube.(6).z >= cam_coords.z ;;
let is_collision (cam_coords : pt_3d) (cubes : pt_3d array array) =
let res = ref false in
let n = !n_walls in
for i = 0 to n-1 do
if not !res then
res := is_collision_cube cam_coords cubes.(i)
done;
!res ;;
let convert_laby laby =
let width = Array.length laby
and height = Array.length laby.(0)
@ -466,16 +524,81 @@ let cheesify laby =
done
done;;
cheesify laby ;;
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 ;;
let (cs, rs, gs, bs) = convert_laby laby ;;
(* 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' ->
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 camera_xyz cubes) then move_cam 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 camera_xyz cubes) then move_cam 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 camera_xyz cubes) then move_cam 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 camera_xyz cubes) then move_cam cubes false 'q'
| 'p' ->
camera_xyz.y <- camera_xyz.y -. 1.;
if b && (is_collision camera_xyz cubes) then move_cam cubes false 'm'
| 'm' ->
camera_xyz.y <- camera_xyz.y +. 1.;
if b && (is_collision camera_xyz cubes) then move_cam cubes false 'p'
| 'a' -> camera_angle_y := !camera_angle_y + 15
| 'e' -> camera_angle_y := !camera_angle_y - 15
| _ -> () ;;
camera_xyz.z <- -. (2.) ;;
camera_xyz.x <- -. (float_of_int width) /. 2. ;;
camera_xyz.y <- -. (float_of_int height) /. 2. ;;
while true do
let play laby =
cheesify laby;
let (cs, rs, gs, bs) = convert_laby laby in
camera_xyz.z <- -. (1.5) ;
camera_xyz.x <- -. (float_of_int width) /. 2. ;
camera_xyz.y <- -. (float_of_int height) /. 2. ;
(*print_cubes cs ;*)
while true do
auto_synchronize false;
open_graph " 1500x1000";
draw_multiples_cubes_colored cs !n_walls rs gs bs 1500 1000 fov ;
set_color black;
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 ;
auto_synchronize true;
Printf.printf "current pos : (%f, %f, %f)" (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z;
Stdlib.print_endline " ";
let usr_input = get1char () in
move_cam cs true usr_input
done ;;
let test1 laby =
cheesify laby ;
let (cs, rs, gs, bs) = convert_laby laby 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 ;;
done ;;
play laby ;;