added support for custom textures ,but it takes way too long to load :(

This commit is contained in:
Alexandre 2024-07-02 16:24:45 +02:00
parent 4be82de50c
commit 6da4f7853a
9 changed files with 200 additions and 34 deletions

BIN
a.out

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -14,6 +14,108 @@ ocamlfind ocamlc -linkpkg -package unix -linkpkg -package graphics -thread -pack
(* ------------------------------------------------------------- *) (* ------------------------------------------------------------- *)
(* ------------------------------------------------------------- *) (* ------------------------------------------------------------- *)
type texture = {mutable width : int ; mutable height : int ; mutable arr_red : int array array ; mutable arr_green : int array array ; mutable arr_blue : int array array} ;;
let parse_texture filename =
let ptr = open_in filename in
let tex = {width = 0 ; height = 0; arr_red = Array.make_matrix 1 1 0; arr_green = Array.make_matrix 1 1 0; arr_blue = Array.make_matrix 1 1 0} in
try
let buffer = ref 0 in
let side = ref 0 in
(* read dimensions *)
while !side <> 2 do
let c = input_char ptr in
let code = Char.code c in
if code >= 48 && code <= 57 then begin
buffer := !buffer * 10;
buffer := !buffer + code - 48
end
else begin
if !side = 0 then
tex.width <- !buffer
else
tex.height <- !buffer;
incr side;
buffer := 0
end
done;
Printf.printf "size is (%d, %d)" tex.width tex.height;
Stdlib.print_endline " ";
tex.arr_red <- Array.make_matrix (tex.width) (tex.height) 0;
tex.arr_green <- Array.make_matrix (tex.width) (tex.height) 0;
tex.arr_blue <- Array.make_matrix (tex.width) (tex.height) 0;
(* read data*)
let cred = ref 0
and cgreen = ref 0
and cblue = ref 0 in
let which_color = ref 0 in
let cur_w = ref 0
and cur_h = ref 0 in
while true do
let c = input_char ptr in
let code = Char.code c in
if code >= 48 && code <= 57 then begin (* integer *)
buffer := !buffer * 10;
buffer := !buffer + code - 48
end
else if c = ',' then begin
if !which_color = 0 then
cred := !buffer
else
cgreen := !buffer;
(* blue is not seen here *)
incr which_color;
buffer := 0
end
else if c = ' ' then begin
cblue := !buffer;
tex.arr_red.(!cur_w).(!cur_h) <- !cred;
tex.arr_green.(!cur_w).(!cur_h) <- !cgreen;
tex.arr_blue.(!cur_w).(!cur_h) <- !cblue;
incr cur_w;
buffer := 0;
which_color := 0
end
else if c = '\n' then begin
cblue := !buffer;
tex.arr_red.(!cur_w).(!cur_h) <- !cred;
tex.arr_green.(!cur_w).(!cur_h) <- !cgreen;
tex.arr_blue.(!cur_w).(!cur_h) <- !cblue;
incr cur_h;
cur_w := 0;
buffer := 0;
which_color := 0
end
done;
failwith "Oh so while true can exit on its own..."
with
| End_of_file ->
close_in ptr ;
Printf.printf "Successfully parsed texture ";
Printf.printf "'%s'" filename;
Stdlib.print_endline " ";
tex
| exn -> close_in ptr ; raise exn ;;
let stone = parse_texture "output.txt" ;;
(* ------------------------------------------------------------- *)
(* ------------------------------------------------------------- *)
type 'a dynamic = {mutable tab : 'a array ; mutable len : int ; mutable memlen : int} ;; 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_3d = {mutable x : float ; mutable y : float ; mutable z : float} ;;
@ -42,27 +144,6 @@ let dyn_append arr elt =
(* ------------------------------------------------------------- *) (* ------------------------------------------------------------- *)
(* ------------------------------------------------------------- *) (* ------------------------------------------------------------- *)
let matrix_mult m1 m2 =
let n = Array.length m1
and p = Array.length m1.(0)
and r = Array.length m2
and s = Array.length m2.(0) in
let mres = Array.make_matrix n s 0 in
if p <> r then
failwith "ERROR : matrixes cannot be multipied, maybe try with reversed inputs ?\n"
else begin
for i = 0 to n-1 do
for j = 0 to s-1 do
for k = 0 to p do
mres.(i).(j) <- mres.(i).(j) + m1.(i).(k) + m2.(k).(j)
done
done
done
end;
mres ;;
let abs x = if x >= 0 then x else -x ;; let abs x = if x >= 0 then x else -x ;;
let absf x = if x >= 0. then x else -.(x) ;; let absf x = if x >= 0. then x else -.(x) ;;
@ -83,7 +164,7 @@ let should_be_drawn (pt : pt_3d) =
*) *)
let should_be_drawn_gr (pt : pt_3d) = let should_be_drawn_gr (pt : pt_3d) =
pt.z > 0.44 ;; pt.z > 0.4 ;;
let sign x = let sign x =
if x >= 0. then 1. else -. (1.) ;; if x >= 0. then 1. else -. (1.) ;;
@ -95,6 +176,9 @@ let is_cube_behind_camera (cube : pt_3d array) =
done; done;
!res ;; !res ;;
let adapt_to_dims x y =
(max 0 (min __width__ x), max 0 (min __height__ y)) ;;
let debug_1 (smth : pt_3d array) = let debug_1 (smth : pt_3d array) =
for i = 0 to Array.length smth -1 do for i = 0 to Array.length smth -1 do
Printf.printf "(%f, %f, %f)" smth.(i).x smth.(i).y smth.(i).z; Printf.printf "(%f, %f, %f)" smth.(i).x smth.(i).y smth.(i).z;
@ -109,6 +193,7 @@ let to_graphics (flat : pt_2d array) screen_wd screen_ht =
and proj_y = int_of_float ((float_of_int screen_ht) *. (1. +. flat.(k).y) /. 2.) in and proj_y = int_of_float ((float_of_int screen_ht) *. (1. +. flat.(k).y) /. 2.) in
(*Printf.printf "Converting to (%d %d)" proj_x proj_y; (*Printf.printf "Converting to (%d %d)" proj_x proj_y;
Stdlib.print_endline " ";*) Stdlib.print_endline " ";*)
(*res.(k) <- adapt_to_dims proj_x proj_y;*)
res.(k) <- (proj_x, proj_y); res.(k) <- (proj_x, proj_y);
done; done;
res ;; res ;;
@ -138,8 +223,8 @@ let project (shape : pt_3d array) screen_wd screen_ht fov =
res.(k).y <- shape.(k).y /. (shape.(k).z *. Float.tan (((float_of_int fov) *. 3.14159265358 /. 180.) /. 2.)) res.(k).y <- shape.(k).y /. (shape.(k).z *. Float.tan (((float_of_int fov) *. 3.14159265358 /. 180.) /. 2.))
end end
else begin else begin
res.(k).x <- (absf shape.(k).x) /. (ar *. (0.44 *. (sign shape.(k).x)) *. Float.tan (((float_of_int fov) *. 3.14159265358 /. 180.) /. 2.)); res.(k).x <- (absf shape.(k).x) /. (ar *. (0.4 *. (sign shape.(k).x)) *. Float.tan (((float_of_int fov) *. 3.14159265358 /. 180.) /. 2.));
res.(k).y <- (absf shape.(k).y) /. ((0.44 *. (sign shape.(k).y)) *. Float.tan (((float_of_int fov) *. 3.14159265358 /. 180.) /. 2.)) res.(k).y <- (absf shape.(k).y) /. ((0.4 *. (sign shape.(k).y)) *. Float.tan (((float_of_int fov) *. 3.14159265358 /. 180.) /. 2.))
end; end;
(*Printf.printf "added (%f %f)" res.(k).x res.(k).y; (*Printf.printf "added (%f %f)" res.(k).x res.(k).y;
Stdlib.print_endline " ";*) Stdlib.print_endline " ";*)
@ -185,6 +270,40 @@ 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.(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)) ;; (res, res.(0) || res.(1) || res.(2) || res.(3) || res.(4) || res.(5)) ;;
let convex_seg x1 x2 theta maxtheta =
let ratio = (float_of_int theta) /. (float_of_int maxtheta) in
int_of_float ((1. -. ratio) *. (float_of_int x1) +. ratio *. (float_of_int x2)) ;;
let convex_pt (p1 : int * int) (p2 : int * int) theta maxtheta =
let ratio = (float_of_int theta) /. (float_of_int maxtheta) in
let mid_x = int_of_float ((1. -. ratio) *. (float_of_int (fst p1)) +. ratio *. (float_of_int (fst p2)))
and mid_y = int_of_float ((1. -. ratio) *. (float_of_int (snd p1)) +. ratio *. (float_of_int (snd p2))) in
(mid_x, mid_y) ;;
let draw_texture (rect : (int * int) array) (text : texture) light =
(*set_color white;
fill_poly rect ;;*)
for i = 0 to text.width -1 do
for j = 0 to text.height -1 do
let face_R = int_of_float ((float_of_int text.arr_red.(i).(j)) *. light)
and face_G = int_of_float ((float_of_int text.arr_green.(i).(j)) *. light)
and face_B = int_of_float ((float_of_int text.arr_blue.(i).(j)) *. light) in
set_color (rgb face_R face_G face_B);
let pt_a = convex_pt rect.(0) rect.(1) i text.width
and pt_b = convex_pt rect.(0) rect.(1) (i+1) text.width
and pt_e = convex_pt rect.(3) rect.(2) (i+1) text.width
and pt_f = convex_pt rect.(3) rect.(2) i text.width in
let bot_left = convex_pt pt_a pt_f j text.height
and bot_right = convex_pt pt_b pt_e j text.height
and top_left = convex_pt pt_a pt_f (j+1) text.height
and top_right = convex_pt pt_b pt_e (j+1) text.height in
fill_poly [|bot_left; bot_right; top_right; top_left|]
done
done ;;
let draw_cube_p (cube : pt_3d array) screen_wd screen_ht fov r g b = let draw_cube_p (cube : pt_3d array) screen_wd screen_ht fov r g b =
let adjusted = adjust_to_camera cube in let adjusted = adjust_to_camera cube in
let (draw_faces, draw_cube) = are_faces_behind adjusted in let (draw_faces, draw_cube) = are_faces_behind adjusted in
@ -204,18 +323,18 @@ let draw_cube_p (cube : pt_3d array) screen_wd screen_ht fov r g b =
|] in |] in
let order = [| let order = [|
[|graphed.(0); graphed.(1); graphed.(2); graphed.(3); graphed.(0)|]; [|graphed.(0); graphed.(1); graphed.(2); graphed.(3)|];
[|graphed.(4); graphed.(5); graphed.(6); graphed.(7); graphed.(4)|]; [|graphed.(4); graphed.(5); graphed.(6); graphed.(7)|];
[|graphed.(0); graphed.(1); graphed.(5); graphed.(4); graphed.(0)|]; [|graphed.(0); graphed.(1); graphed.(5); graphed.(4)|];
[|graphed.(1); graphed.(2); graphed.(6); graphed.(5); graphed.(1)|]; [|graphed.(1); graphed.(2); graphed.(6); graphed.(5)|];
[|graphed.(2); graphed.(3); graphed.(7); graphed.(6); graphed.(2)|]; [|graphed.(2); graphed.(3); graphed.(7); graphed.(6)|];
[|graphed.(3); graphed.(0); graphed.(4); graphed.(7); graphed.(3)|]; [|graphed.(3); graphed.(0); graphed.(4); graphed.(7)|];
|] in |] in
(* Note : edge orders must be as following : (* Note : edge orders must be as following :
7--------6 7--------6
/| /| /| /|
/ | / | / | / |
4--------5 | 4--------5 |
| | | | | | | |
| 3-----|--2 | 3-----|--2
@ -246,6 +365,7 @@ let draw_cube_p (cube : pt_3d array) screen_wd screen_ht fov r g b =
and face_B = int_of_float ((float_of_int b) *. light) in and face_B = int_of_float ((float_of_int b) *. light) in
set_color (rgb face_R face_G face_B); set_color (rgb face_R face_G face_B);
fill_poly order.(i); fill_poly order.(i);
(*draw_texture order.(i) stone light ;*)
set_color black; set_color black;
draw_poly_line order.(i); draw_poly_line order.(i);
end end
@ -401,7 +521,7 @@ type tile = Free | Wall | Crate | Exit | Craxit | Camera ;;
let width = 15 let width = 15
and height = 15 and height = 15
and depth = 45 ;; and depth = 15 ;;
(* dimensions *) (* dimensions *)
let render_distance = 7 ;; let render_distance = 7 ;;
@ -665,7 +785,7 @@ let play_dos laby =
Stdlib.print_endline "Building terrain..."; Stdlib.print_endline "Building terrain...";
cheesify laby; cheesify laby;
Stdlib.print_endline "Rendering terrain..."; Stdlib.print_endline "Converting terrain...";
let hash = chunkify laby 2 in let hash = chunkify laby 2 in
camera_xyz.z <- -. (1.5) ; camera_xyz.z <- -. (1.5) ;
@ -677,11 +797,11 @@ let play_dos laby =
while true do while true do
ignore (Sys.command "clear") ; ignore (Sys.command "clear") ;
Stdlib.print_endline "Rendering terrain...";
auto_synchronize false; auto_synchronize false;
open_graph " 1500x1000"; open_graph " 1500x1000";
set_color black; set_color black;
fill_poly [|(0, 0); (__width__, 0); (__width__, __height__); (0, __height__); (0, 0)|]; fill_poly [|(0, 0); (__width__, 0); (__width__, __height__); (0, __height__); (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 let (ch_x, ch_y, ch_z) = coords_to_chunk_f (-. camera_xyz.x) (-. camera_xyz.y) camera_xyz.z in

26
image_parsing.py Normal file
View File

@ -0,0 +1,26 @@
import math
import numpy as np
import PIL.Image as Img
def import_img(filename, show=False):
img = Img.open(filename)
width, height = img.size
if(show):
Img.show(img)
ptr = open("output.txt", "w")
ptr.write(str(width) + " " + str(height))
ptr.write("\n")
for w in range(0,width,10):
for h in range(0,height,10):
(red, green, blue) = img.getpixel((w,h))
ptr.write(str(red) + "," + str(green) + "," + str(blue))
if(h != height-1):
ptr.write(" ")
ptr.write("\n")
print("Successfully parsed image '", filename, "'")
ptr.close()
import_img("stone.bmp")

17
output.txt Normal file
View File

@ -0,0 +1,17 @@
16 16
143,143,143 130,130,130 127,127,127 127,127,127 116,116,116 124,124,124 116,116,116 116,116,116 143,143,143 130,130,130 106,106,106 127,127,127 127,127,127 140,140,140 129,129,129 127,127,127
143,143,143 130,130,130 119,119,119 124,124,124 124,124,124 138,138,138 127,127,127 119,119,119 143,143,143 130,130,130 123,123,123 130,130,130 119,119,119 143,143,143 122,122,122 124,124,124
143,143,143 119,119,119 108,108,108 136,136,136 127,127,127 128,128,128 126,126,126 108,108,108 143,143,143 142,142,142 121,121,121 124,124,124 116,116,116 129,129,129 118,118,118 127,127,127
143,143,143 123,123,123 106,106,106 144,144,144 119,119,119 124,124,124 135,135,135 107,107,107 131,131,131 131,131,131 124,124,124 126,126,126 116,116,116 118,118,118 123,123,123 126,126,126
127,127,127 119,119,119 116,116,116 127,127,127 116,116,116 124,124,124 141,141,141 117,117,117 143,143,143 130,130,130 116,116,116 127,127,127 127,127,127 119,119,119 126,126,126 127,127,127
119,119,119 122,122,122 118,118,118 138,138,138 116,116,116 124,124,124 141,141,141 107,107,107 130,130,130 128,128,128 116,116,116 138,138,138 140,140,140 126,126,126 129,129,129 127,127,127
116,116,116 124,124,124 116,116,116 133,133,133 124,124,124 127,127,127 120,120,120 109,109,109 127,127,127 139,139,139 104,104,104 141,141,141 128,128,128 127,127,127 127,127,127 123,123,123
124,124,124 127,127,127 110,110,110 124,124,124 119,119,119 124,124,124 133,133,133 130,130,130 139,139,139 143,143,143 102,102,102 132,132,132 119,119,119 119,119,119 127,127,127 116,116,116
116,116,116 124,124,124 116,116,116 142,142,142 127,127,127 127,127,127 141,141,141 129,129,129 143,143,143 143,143,143 113,113,113 127,127,127 116,116,116 116,116,116 126,126,126 127,127,127
107,107,107 128,128,128 116,116,116 142,142,142 140,140,140 120,120,120 117,117,117 121,121,121 143,143,143 134,134,134 127,127,127 127,127,127 124,124,124 117,117,117 126,126,126 136,136,136
116,116,116 128,128,128 116,116,116 133,133,133 143,143,143 119,119,119 116,116,116 118,118,118 128,128,128 127,127,127 121,121,121 124,124,124 127,127,127 125,125,125 126,126,126 141,141,141
116,116,116 104,104,104 124,124,124 126,126,126 143,143,143 111,111,111 114,114,114 116,116,116 127,127,127 137,137,137 126,126,126 126,126,126 102,102,102 125,125,125 127,127,127 132,132,132
127,127,127 105,105,105 124,124,124 127,127,127 143,143,143 119,119,119 126,126,126 116,116,116 127,127,127 140,140,140 129,129,129 141,141,141 104,104,104 114,114,114 147,147,147 129,129,129
127,127,127 115,115,115 127,127,127 122,122,122 118,118,118 110,110,110 123,123,123 123,123,123 127,127,127 140,140,140 122,122,122 141,141,141 113,113,113 114,114,114 147,147,147 121,121,121
127,127,127 125,125,125 126,126,126 116,116,116 124,124,124 119,119,119 125,125,125 127,127,127 127,127,127 140,140,140 113,113,113 133,133,133 104,104,104 114,114,114 147,147,147 123,123,123
127,127,127 122,122,122 127,127,127 116,116,116 127,127,127 142,142,142 130,130,130 127,127,127 119,119,119 130,130,130 116,116,116 134,134,134 112,112,112 137,137,137 143,143,143 129,129,129

BIN
stone.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

BIN
stoned.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

3
texture404.txt Normal file
View File

@ -0,0 +1,3 @@
2 2
250,0,250 0,0,0
0,0,0 250,0,250