refined ray casting to edges instead of whole cubes

This commit is contained in:
Alexandre 2024-07-03 19:34:09 +02:00
parent c70cd64e29
commit 70aa163db0
5 changed files with 29 additions and 59 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -408,7 +408,7 @@ let is_visible (pt0 : pt_3d) (hash : (int * int * int, coloredCube dynamic) Hash
| Not_found -> (*Stdlib.print_endline " EMPTY"; *)true
| ReturnBool b -> (*Printf.printf " Aborted to %b" b ; Stdlib.print_endline " "; *)b ;;
let is_visible_cube (cube : pt_3d array) (hash : (int * int * int, coloredCube dynamic) Hashtbl.t) =
let is_visible_poly (cube : pt_3d array) (hash : (int * int * int, coloredCube dynamic) Hashtbl.t) =
(*Printf.printf "cube (%f, %f, %f)" cube.(0).x cube.(0).y cube.(0).z;
Printf.printf "[of length %d]" (Array.length cube);
Stdlib.print_endline " ";*)
@ -420,6 +420,24 @@ let is_visible_cube (cube : pt_3d array) (hash : (int * int * int, coloredCube d
Stdlib.print_endline " "; *)
!res ;;
let is_visible_cube (cube : pt_3d array) (hash : (int * int * int, coloredCube dynamic) Hashtbl.t) =
let r0 = is_visible cube.(0) hash
and r1 = is_visible cube.(1) hash
and r2 = is_visible cube.(2) hash
and r3 = is_visible cube.(3) hash
and r4 = is_visible cube.(4) hash
and r5 = is_visible cube.(5) hash
and r6 = is_visible cube.(6) hash
and r7 = is_visible cube.(7) hash in
(r1 || r2 || r3 || r4 || r5 || r6 || r7 || r0, [|
r0 || r1 || r2 || r3 ;
r4 || r5 || r6 || r7 ;
r0 || r1 || r5 || r4 ;
r1 || r2 || r6 || r5 ;
r2 || r3 || r7 || r6 ;
r3 || r0 || r4 || r7
|]) ;;
let draw_texture (rect : (int * int) array) (text : texture) light =
(*set_color white;
fill_poly rect ;;*)
@ -449,7 +467,8 @@ let draw_texture (rect : (int * int) array) (text : texture) light =
let draw_cube_p (cube : pt_3d array) (hash : (int * int * int, coloredCube dynamic) Hashtbl.t) 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
if draw_cube && (is_visible_cube cube hash) then begin
let (draw_cube_2, draw_faces_2) = is_visible_cube cube hash in
if draw_cube && draw_cube_2 then begin
(*Printf.printf "drawing cube (%f, %f, %f)" cube.(0).x cube.(0).y cube.(0).z ;
Stdlib.print_endline " ";*)
let proj = project adjusted screen_wd screen_ht fov in
@ -499,10 +518,11 @@ let draw_cube_p (cube : pt_3d array) (hash : (int * int * int, coloredCube dynam
swap distances i !idmax;
swap order i !idmax;
swap draw_faces i !idmax;
swap draw_faces_2 i !idmax;
done;
set_line_width 2;
for i = 0 to 5 do
if draw_faces.(i) then begin
if draw_faces.(i) && draw_faces_2.(i) then begin
let light = max (0.) (1. -. (distances.(i)) /. 12.5) in
(*let face_R = int_of_float ((float_of_int r) *. light)
and face_G = int_of_float ((float_of_int g) *. light)
@ -516,6 +536,12 @@ let draw_cube_p (cube : pt_3d array) (hash : (int * int * int, coloredCube dynam
done
end ;;
(* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *)
(* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *)
(* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *)
(* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *)
(* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ *)
let sum_x (poly : pt_3d array) =
let res = ref 0. in
for i = 0 to (Array.length poly -1) do
@ -881,62 +907,6 @@ let cheesify (laby : tile array array array) =
done
done
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' ->
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
| _ -> () ;;
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";
set_color black;
fill_poly [|(0, 0); (1500, 0); (1500, 1000); (0, 1000); (0, 0)|];
set_color white;
draw_multiples_cubes_colored cs rs gs bs hash __width__ __height__ 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.tab true usr_input
done ;;*)
let rec move_cam_hash (cubes : coloredCube dynamic) b c =(* Printf.printf "[%b]" b; Stdlib.print_endline " " ; *)match c with
| 'z' ->

BIN
display.o

Binary file not shown.