diff --git a/a.out b/a.out index 866e6c1..4b2a829 100755 Binary files a/a.out and b/a.out differ diff --git a/display.cmi b/display.cmi index bd005a8..2ac8bc4 100644 Binary files a/display.cmi and b/display.cmi differ diff --git a/display.cmo b/display.cmo index 92afe27..d3deac8 100644 Binary files a/display.cmo and b/display.cmo differ diff --git a/display.ml b/display.ml index 853a419..61a4b72 100644 --- a/display.ml +++ b/display.ml @@ -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 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 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) = - pt.z > 0.44 ;; + pt.z > 0.4 ;; let sign x = if x >= 0. then 1. else -. (1.) ;; @@ -95,6 +176,9 @@ let is_cube_behind_camera (cube : pt_3d array) = done; !res ;; +let adapt_to_dims x y = + (max 0 (min __width__ x), max 0 (min __height__ y)) ;; + let debug_1 (smth : pt_3d array) = for i = 0 to Array.length smth -1 do 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 (*Printf.printf "Converting to (%d %d)" proj_x proj_y; Stdlib.print_endline " ";*) + (*res.(k) <- adapt_to_dims proj_x proj_y;*) res.(k) <- (proj_x, proj_y); done; 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.)) end 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).y <- (absf shape.(k).y) /. ((0.44 *. (sign shape.(k).y)) *. 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.4 *. (sign shape.(k).y)) *. Float.tan (((float_of_int fov) *. 3.14159265358 /. 180.) /. 2.)) end; (*Printf.printf "added (%f %f)" res.(k).x res.(k).y; 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, 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 adjusted = adjust_to_camera cube 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 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)|]; + [|graphed.(0); graphed.(1); graphed.(2); graphed.(3)|]; + [|graphed.(4); graphed.(5); graphed.(6); graphed.(7)|]; + [|graphed.(0); graphed.(1); graphed.(5); graphed.(4)|]; + [|graphed.(1); graphed.(2); graphed.(6); graphed.(5)|]; + [|graphed.(2); graphed.(3); graphed.(7); graphed.(6)|]; + [|graphed.(3); graphed.(0); graphed.(4); graphed.(7)|]; |] in (* Note : edge orders must be as following : 7--------6 /| /| - / | / | + / | / | 4--------5 | | | | | | 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 set_color (rgb face_R face_G face_B); fill_poly order.(i); + (*draw_texture order.(i) stone light ;*) set_color black; draw_poly_line order.(i); end @@ -401,7 +521,7 @@ type tile = Free | Wall | Crate | Exit | Craxit | Camera ;; let width = 15 and height = 15 -and depth = 45 ;; +and depth = 15 ;; (* dimensions *) let render_distance = 7 ;; @@ -665,7 +785,7 @@ let play_dos laby = Stdlib.print_endline "Building terrain..."; cheesify laby; - Stdlib.print_endline "Rendering terrain..."; + Stdlib.print_endline "Converting terrain..."; let hash = chunkify laby 2 in camera_xyz.z <- -. (1.5) ; @@ -677,11 +797,11 @@ let play_dos laby = while true do ignore (Sys.command "clear") ; + Stdlib.print_endline "Rendering terrain..."; auto_synchronize false; open_graph " 1500x1000"; set_color black; 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 diff --git a/image_parsing.py b/image_parsing.py new file mode 100644 index 0000000..e29b587 --- /dev/null +++ b/image_parsing.py @@ -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") \ No newline at end of file diff --git a/output.txt b/output.txt new file mode 100644 index 0000000..ad4195a --- /dev/null +++ b/output.txt @@ -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 diff --git a/stone.bmp b/stone.bmp new file mode 100644 index 0000000..a852ac3 Binary files /dev/null and b/stone.bmp differ diff --git a/stoned.bmp b/stoned.bmp new file mode 100644 index 0000000..a852ac3 Binary files /dev/null and b/stoned.bmp differ diff --git a/texture404.txt b/texture404.txt new file mode 100644 index 0000000..ec9e1df --- /dev/null +++ b/texture404.txt @@ -0,0 +1,3 @@ +2 2 +250,0,250 0,0,0 +0,0,0 250,0,250