generated from CharlesAverill/OCaml_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a3637d1
commit 36cdd18
Showing
12 changed files
with
873 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,30 @@ | ||
open Zenith.Mesh | ||
open Zenith.Objloader | ||
|
||
type arguments = { obj_mesh : mesh } | ||
type arguments = { obj_meshes : mesh list } | ||
|
||
let copies l n = | ||
if n <= 0 then [] | ||
else List.concat (List.map (fun x -> List.init n (fun _ -> x)) l) | ||
|
||
let parse_arguments () = | ||
let obj_file = ref "" in | ||
let obj = ref cube in | ||
let obj_files = ref [] in | ||
let objs = ref [ cube ] in | ||
let num_each = ref 1 in | ||
|
||
let speclist = | ||
[ ("-obj", Arg.String (fun s -> obj_file := s), "An .OBJ file to render") ] | ||
[ | ||
( "-n", | ||
Arg.Int (fun n -> num_each := n), | ||
"How many of each object to render" ); | ||
] | ||
in | ||
let usage_msg = "Usage: zenith [OBJ]+" in | ||
|
||
let usage_msg = "Usage: zenith [OBJ]" in | ||
Arg.parse speclist (fun n -> obj_files := n :: !obj_files) usage_msg; | ||
|
||
Arg.parse speclist (fun _ -> ()) usage_msg; | ||
if !obj_files != [] then objs := List.map load_obj !obj_files; | ||
|
||
if !obj_file != "" then obj := load_obj !obj_file; | ||
if !num_each != 1 then objs := copies !objs !num_each; | ||
|
||
{ obj_mesh = !obj } | ||
{ obj_meshes = !objs } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
open Graphics | ||
open Logging | ||
open Zenith.Config | ||
open Math.Vector | ||
open Zenith.Mesh | ||
open Zenith.Renderer | ||
|
||
let clear_window color = | ||
let fg = foreground in | ||
set_color color; | ||
fill_rect 0 0 (size_x ()) (size_y ()); | ||
set_color fg | ||
|
||
let evenly_spaced_positions meshes = | ||
let n = List.length meshes in | ||
let radius = if n > 1 then 1. +. largest_distance meshes else 0. in | ||
let delta_theta = 2.0 *. Float.pi /. float_of_int n in | ||
|
||
let rec generate_positions theta count acc = | ||
if count <= 0 then acc | ||
else | ||
let x = radius *. Float.cos theta in | ||
let y = radius *. Float.sin theta in | ||
generate_positions (theta +. delta_theta) (count - 1) (v3 x y 0. :: acc) | ||
in | ||
|
||
generate_positions 0.0 n [] | ||
|
||
let to_draw = ref [] | ||
let break_mainloop = ref false | ||
let euler = ref zvec | ||
let translation = ref zvec | ||
|
||
let update_euler n d = | ||
let eulerx, eulery, eulerz = | ||
match !euler with { x; y; z; w = __ } -> (x, y, z) | ||
in | ||
match n with | ||
| 0 -> euler := { x = eulerx +. d; y = eulery; z = eulerz; w = 0. } | ||
| 1 -> euler := { x = eulerx; y = eulery +. d; z = eulerz; w = 0. } | ||
| 2 -> euler := { x = eulerx; y = eulery; z = eulerz +. d; w = 0. } | ||
| _ -> | ||
fatal rc_MatchError | ||
("Tried to update euler angle " ^ string_of_int n | ||
^ ", but expected [0:2]") | ||
|
||
let update_translation n d = | ||
let tx, ty, tz = match !translation with { x; y; z; w = __ } -> (x, y, z) in | ||
match n with | ||
| 0 -> translation := { x = tx +. d; y = ty; z = tz; w = 0. } | ||
| 1 -> translation := { x = tx; y = ty +. d; z = tz; w = 0. } | ||
| 2 -> translation := { x = tx; y = ty; z = tz +. d; w = 0. } | ||
| _ -> | ||
fatal rc_MatchError | ||
("Tried to update translation " ^ string_of_int n | ||
^ ", but expected [0:2]") | ||
|
||
let input_dispatch key = | ||
match key with | ||
(* X Translation *) | ||
| 'a' -> update_translation 0 (-.translation_incr) | ||
| 'd' -> update_translation 0 translation_incr | ||
(* Y Translation *) | ||
| 'w' -> update_translation 1 translation_incr | ||
| 's' -> update_translation 1 (-.translation_incr) | ||
(*Z Translation *) | ||
| 'q' -> update_translation 2 (-.translation_incr) | ||
| 'e' -> update_translation 2 translation_incr | ||
(* Pitch *) | ||
| 'i' -> update_euler 0 (-.rotation_incr) | ||
| 'k' -> update_euler 0 rotation_incr | ||
(* Yaw *) | ||
| 'j' -> update_euler 1 (-.rotation_incr) | ||
| 'l' -> update_euler 1 rotation_incr | ||
(* Roll *) | ||
| 'u' -> update_euler 2 (-.rotation_incr) | ||
| 'o' -> update_euler 2 rotation_incr | ||
(* Reset scene *) | ||
| 'r' -> | ||
euler := zvec; | ||
translation := zvec | ||
(* Escape *) | ||
| x when x = char_of_int 27 -> break_mainloop := true | ||
| _ -> | ||
print_int (int_of_char key); | ||
print_endline "" | ||
|
||
let draw_scene () = | ||
display_mode false; | ||
clear_window black; | ||
(* Draw each mesh at their offset *) | ||
(* print_endline (string_of_int (List.length !to_draw - 1)); *) | ||
for i = 0 to List.length !to_draw - 1 do | ||
let mesh, offset = List.nth !to_draw i in | ||
draw_mesh mesh !euler (v3_add !translation offset) | ||
done; | ||
let input = wait_next_event [ Poll ] in | ||
synchronize (); | ||
display_mode true; | ||
if input.keypressed then input_dispatch (read_key ()) | ||
|
||
let rec main_loop () = | ||
draw_scene (); | ||
if !break_mainloop then () else main_loop () | ||
|
||
let start meshes = | ||
(* Evenly-space meshes *) | ||
_log Log_Debug (string_of_float (largest_distance meshes)); | ||
let mesh_positions = evenly_spaced_positions meshes in | ||
to_draw := List.combine meshes mesh_positions; | ||
auto_synchronize false; | ||
open_graph (" " ^ string_of_int viewportw ^ "x" ^ string_of_int viewporth); | ||
main_loop () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,6 @@ | ||
open Graphics | ||
open Logging | ||
open Zenith.Config | ||
open Zenith.Mesh | ||
open Zenith.Renderer | ||
|
||
let clear_window color = | ||
let fg = foreground in | ||
set_color color; | ||
fill_rect 0 0 (size_x ()) (size_y ()); | ||
set_color fg | ||
|
||
let to_draw = ref cube | ||
let break_mainloop = ref false | ||
let euler = ref (0., 0., 0.) | ||
|
||
let update_euler n d = | ||
let eulerx, eulery, eulerz = !euler in | ||
match n with | ||
| 0 -> euler := (eulerx +. d, eulery, eulerz) | ||
| 1 -> euler := (eulerx, eulery +. d, eulerz) | ||
| 2 -> euler := (eulerx, eulery, eulerz +. d) | ||
| _ -> | ||
fatal rc_MatchError | ||
("Tried to update euler angle " ^ string_of_int n | ||
^ ", but expected [0:2]") | ||
|
||
let input_dispatch key = | ||
match key with | ||
(* Pitch *) | ||
| 'w' -> update_euler 0 (-.rotation_incr) | ||
| 's' -> update_euler 0 rotation_incr | ||
(* Yaw *) | ||
| 'a' -> update_euler 1 (-.rotation_incr) | ||
| 'd' -> update_euler 1 rotation_incr | ||
(* Roll *) | ||
| 'q' -> update_euler 2 (-.rotation_incr) | ||
| 'e' -> update_euler 2 rotation_incr | ||
(* Reset scene *) | ||
| 'r' -> euler := (0., 0., 0.) | ||
(* Escape *) | ||
| x when x = char_of_int 27 -> break_mainloop := true | ||
| _ -> | ||
print_int (int_of_char key); | ||
print_endline "" | ||
|
||
let draw_scene () = | ||
clear_window black; | ||
draw_mesh !to_draw !euler; | ||
let input = wait_next_event [ Poll ] in | ||
synchronize (); | ||
if input.keypressed then input_dispatch (read_key ()) | ||
|
||
let rec main_loop () = | ||
draw_scene (); | ||
if !break_mainloop then () else main_loop () | ||
open Argparse | ||
open Driver | ||
|
||
let () = | ||
let args = Argparse.parse_arguments () in | ||
to_draw := args.obj_mesh; | ||
open_graph (" " ^ string_of_int viewportw ^ "x" ^ string_of_int viewporth); | ||
auto_synchronize false; | ||
main_loop () | ||
start args.obj_meshes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
open Math.Vector | ||
open Graphics | ||
|
||
let viewportw, viewporth = (700, 700) | ||
let viewpoint = v3 0. 0. 10. | ||
let viewportw, viewporth = (1000, 1000) | ||
let viewpoint = v3 0. 0. 30. | ||
let fov = 45.0 | ||
let aspect_ratio = 1. | ||
let z_near = 0.1 | ||
let z_far = 50. | ||
let rotation_incr = 7.5 | ||
let translation_incr = 0.5 | ||
let draw_verts = false | ||
let vert_radius = 5 | ||
let vert_radius = 3 | ||
let vert_color = red | ||
let edge_color = green |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Blender MTL File: 'None' | ||
# Material Count: 0 |
Oops, something went wrong.