Skip to content

Commit

Permalink
Add face support, add logo
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesAverill committed Oct 12, 2023
1 parent 36cdd18 commit 1a4994b
Show file tree
Hide file tree
Showing 84 changed files with 389 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
*.cmxa
_build
.vscode/*
*.mtl
.ocamlformat
2 changes: 1 addition & 1 deletion .ocamlformat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.25.1
version=0.26.0
46 changes: 35 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
# ZENITH
Zen Engine for Navigating wIreframes In Three-dimensional Holographic space
![ZENITH Logo](media/logo.png)

![demo](media/demo.gif)
"Zen Engine for Navigating wIreframes In Three-dimensional Holographic space"

## Usage

ZENITH is built with [dune](https://dune.build), and depends on the OCaml `Graphics` library. To run:

```
make run # Will render the Utah teapot .obj file
OBJ=path_to_obj make run # Will override teapot path
make run # Will render the Utah teapot .obj file
OBJS="path_to_obj path_to_another_obj ..." make run # Will render each object around a circle
# Renders the demo shown below
OBJS="objs/uv_sphere.obj objs/torus.obj objs/star_destroyer.obj objs/pyramid.obj objs/cube.obj objs/blender_monkey.obj objs/arwing.obj" make run
```

![demo](media/demo.gif)

## Writeups

| Checkpoint | Summary |
Expand All @@ -21,14 +24,35 @@ OBJ=path_to_obj make run # Will override teapot path
| [Success 2](media/success2/Success2.md) | I can now load meshes from `.obj` files |

## Notes

### Blender

If you're using Blender to create 3D models for ZENITH, ensure that, in the export window under `Geometry`, the options "Write Normals," "Include UVs," and "Write Materials" are deactivated. These options generate vernacular in the output `.obj` file that my loader does not support.

### Supported .OBJ Vernacular

```obj
# Comments
# Vertices
v 0.0 1.9 -5.8
v 1.1 0.4 -0.7
v 38.4 0.2 7.1
# Lines
l 1 2
l 2 3
# Faces
f 1 2 3
```

### Axis configuration
```
Y
|
|
|
--------- X
Y
|
|
|
--------- X
/
/
/
/
Z
```
8 changes: 4 additions & 4 deletions bin/driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ let clear_window color =

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 largest_dist = largest_distance meshes in
_log Log_Debug (string_of_float largest_dist);
let radius = if n > 1 then 1. +. largest_dist 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)
generate_positions (theta +. delta_theta) (count - 1) (acc @ [ v3 x y 0. ])
in

generate_positions 0.0 n []
Expand Down Expand Up @@ -89,7 +91,6 @@ 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)
Expand All @@ -105,7 +106,6 @@ let rec 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;
Expand Down
2 changes: 1 addition & 1 deletion lib/config.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
open Math.Vector
open Graphics

let viewportw, viewporth = (1000, 1000)
let viewportw, viewporth = (1080, 1080)
let viewpoint = v3 0. 0. 30.
let fov = 45.0
let aspect_ratio = 1.
Expand Down
32 changes: 26 additions & 6 deletions lib/objloader.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,46 @@ open Math.Vector

type vernacular = Vertex of vec | Edge of (int * int)

let parse_obj_line line : vernacular option =
let pairs_of_consecutive_elements lst =
let rec aux first = function
| [] -> []
| [ x ] ->
[ (x, first) ]
(* Empty list or a list with only one element cannot form pairs. *)
| x :: y :: rest -> (x, y) :: aux first (y :: rest)
in
match lst with [] | [ _ ] -> [] | h :: _ :: _ -> aux h lst

let parse_obj_line line : vernacular list option =
match String.split_on_char ' ' line with
| [] -> None
| [ "#" ] -> None (* Comment line *)
| "v" :: xs ->
let v = List.map float_of_string xs in
Some (Vertex (v3 (List.nth v 0) (List.nth v 1) (List.nth v 2)))
Some [ Vertex (v3 (List.nth v 0) (List.nth v 1) (List.nth v 2)) ]
| "l" :: xs ->
let edge = List.map (fun s -> int_of_string s - 1) xs in
Some (Edge (List.nth edge 0, List.nth edge 1))
Some [ Edge (List.nth edge 0, List.nth edge 1) ]
| "f" :: xs ->
let vertices = List.map (fun s -> int_of_string s - 1) xs in
let edges = pairs_of_consecutive_elements vertices in
Some (List.map (fun x -> Edge x) edges)
| _ -> None

let rec parse_obj_lines lines vertices edges =
match lines with
| [] -> (vertices, edges)
| line :: rest -> (
match parse_obj_line line with
| Some (Vertex v) -> parse_obj_lines rest (v :: vertices) edges
| Some (Edge e) -> parse_obj_lines rest vertices (e :: edges)
| None -> parse_obj_lines rest vertices edges)
| Some [ Vertex v ] -> parse_obj_lines rest (v :: vertices) edges
| Some [ Edge e ] -> parse_obj_lines rest vertices (e :: edges)
| Some (Edge e :: t) ->
parse_obj_lines rest vertices
(List.map
(fun x -> match x with Edge _e -> _e | _ -> (0, 0))
(Edge e :: t)
@ edges)
| _ -> parse_obj_lines rest vertices edges)

let parse_obj_file filename =
let lines = ref [] in
Expand Down
Binary file added media/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/tt_octosquares/Bonuses and services.pdf
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added media/tt_octosquares/TypeType Trial License.pdf
Binary file not shown.
2 changes: 0 additions & 2 deletions objs/star_destroyer.mtl

This file was deleted.

Loading

0 comments on commit 1a4994b

Please sign in to comment.