added procedural generation

This commit is contained in:
Alexandre 2024-07-03 19:49:51 +02:00
parent 70aa163db0
commit 6feef49545
5 changed files with 25 additions and 49 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -142,7 +142,7 @@ 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 100 i ; len = 0 ; memlen = 100} ;;
{tab = Array.make 25 i ; len = 0 ; memlen = 25} ;;
let dyn_append arr elt =
let fct x =
@ -152,8 +152,6 @@ let dyn_append arr elt =
arr.tab.(0)
in
if arr.len = arr.memlen then begin
Stdlib.print_endline "resized";
Unix.sleepf 0.5;
let newarr = Array.init (2 * arr.memlen) fct in
arr.memlen <- 2 * arr.memlen;
arr.tab <- newarr
@ -316,31 +314,6 @@ let convex_3d (p1 : pt_3d) (p2 : pt_3d) theta maxtheta =
z = (1. -. ratio) *. p1.z +. ratio *. p2.z
} ;;
(*
----\\ WARNING : THIS BIT YIELDS A SEGMENTATION FAULT //-----
exception Out_of_bounds ;;
let get_single_pt pt =
try
Graphics.point_color (fst pt) (snd pt)
with
| Graphics.Graphic_failure _ -> raise Out_of_bounds ;;
let is_hidden (rect : (int * int) array) =
try
let midpt = ((fst rect.(0) + fst rect.(1) + fst rect.(2) + fst rect.(3))/4, (snd rect.(0) + snd rect.(1) + snd rect.(2) + snd rect.(3))/4) in
let color_BL = get_single_pt rect.(0)
and color_BR = get_single_pt rect.(1)
and color_TL = get_single_pt rect.(2)
and color_TR = get_single_pt rect.(3)
and color_C = get_single_pt midpt in
(color_BL = 0) || (color_BR = 0) || (color_TL = 0) || (color_TR = 0) || (color_C = 0)
with
| Out_of_bounds -> false ;;
*)
let ctc_one x =
if x >= 0 then
x / chunk_size
@ -524,12 +497,12 @@ let draw_cube_p (cube : pt_3d array) (hash : (int * int * int, coloredCube dynam
for i = 0 to 5 do
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)
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);*)
draw_texture order.(i) stone light ;
fill_poly order.(i);
(*draw_texture order.(i) stone light ;*)
set_color black;
draw_poly_line order.(i);
end
@ -818,23 +791,6 @@ let chunkify_2 laby sz =
done
done;
(*
let s = ref 0 in
for i = 0 to 2 do
for j = 0 to 2 do
for k = 0 to 2 do
let nc = ((Hashtbl.find cubes (i, j, k)).len) in
Printf.printf "len for (%d, %d, %d) : %d" i j k nc;
s := !s + nc;
Stdlib.print_endline " "
done
done
done;
Printf.printf "s = %d" !s;
Stdlib.print_endline " ";
*)
(*Unix.sleepf 100.0;*)
cubes ;;
let chunkify laby sz =
@ -1010,10 +966,30 @@ let rec move_cam_hash_2 hash cx cy cz b c =(* Printf.printf "[%b]" b; Stdlib.pri
| 'e' -> camera_angle_y := !camera_angle_y - 30
| _ -> () ;;
let initialize_chunk hash ch_x ch_y ch_z =
let dyna = dyn_create {cube = create_cube 0 0 0 1; red = 33; green = 33; blue = 22} in
for i = 0 to 3 do
for j = 0 to 3 do
dyn_append dyna {cube = create_cube (4*ch_x +i) (4*ch_y + (i + j) mod 4) (4*ch_z+j) 1; red = 32; green = 32; blue = 200};
done
done;
Hashtbl.add hash (ch_x, ch_y, ch_z) dyna ;;
let initialize_flat hash ch_x ch_y ch_z =
let dyna = dyn_create {cube = create_cube 0 0 0 1; red = 33; green = 33; blue = 22} in
for i = 0 to 3 do
for j = 0 to 3 do
dyn_append dyna {cube = create_cube (4*ch_x +i) (4*ch_y) (4*ch_z+j) 1; red = 32; green = 222; blue = 32};
done
done;
Hashtbl.add hash (ch_x, ch_y, ch_z) dyna ;;
let manage_unexisting_chunk hash ch_x ch_y ch_z screen_wd screen_ht fov =
try
draw_multiples_cubes_colored_hash (Hashtbl.find hash (ch_x, ch_y, ch_z)) hash screen_wd screen_ht fov
with
| Not_found when ch_y = (-1)-> initialize_chunk hash ch_x ch_y ch_z
| Not_found when ch_y = (-2)-> initialize_flat hash ch_x ch_y ch_z
| Not_found -> () ;;
let render_chunks hash camx camy camz ch_distance screen_wd screen_ht fov =

BIN
display.o

Binary file not shown.