Skip to content

Commit

Permalink
fix: standardize vertex attribute ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
claby2 committed Jan 19, 2025
1 parent 7543d5d commit 836f942
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 31 deletions.
9 changes: 4 additions & 5 deletions examples/game_of_life.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ module Grid = struct
t.cells <- new_cells

let update_vm t vertex_mesh =
(* TODO: only add visible faces/vertices. *)
let open Graphics in
let data = ref [] in
for x = 0 to t.dimension - 1 do
Expand All @@ -70,12 +71,11 @@ module Grid = struct
let cube = Primitive.Cuboid.create () in
let rec shift = function
| [] -> []
| nx :: ny :: nz :: px :: py :: pz :: rest ->
nx :: ny :: nz
:: (px +. float_of_int x -. (float_of_int t.dimension /. 2.))
| px :: py :: pz :: nx :: ny :: nz :: rest ->
(px +. float_of_int x -. (float_of_int t.dimension /. 2.))
:: (py +. float_of_int y -. (float_of_int t.dimension /. 2.))
:: (pz +. float_of_int z -. (float_of_int t.dimension /. 2.))
:: shift rest
:: nx :: ny :: nz :: shift rest
| _ -> failwith "Invalid list"
in
data := shift cube @ !data
Expand Down Expand Up @@ -167,4 +167,3 @@ let () =
in

App.run app

3 changes: 1 addition & 2 deletions lib/graphics/mesh3d.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ let rec install t =
let stride = Vertex_mesh.vertex_size mesh * size_of_float in
let offset = ref 0 in
Vertex_mesh.attributes mesh
|> Hashtbl.to_seq |> List.of_seq
|> List.sort (fun (index1, _) (index2, _) -> compare index2 index1)
|> Hashtbl.to_seq |> List.of_seq |> List.sort compare
|> List.iter (fun (index, size) ->
Gl.enable_vertex_attrib_array index;
Gl.vertex_attrib_pointer index size Gl.float false stride
Expand Down
26 changes: 13 additions & 13 deletions lib/graphics/primitive/cuboid.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ open Util
type config = { param1 : int; half_size : Math.Vec3.t; data : float list ref }

let add_tile c topl topr botl botr =
let add v = add_vec3 c.data v in
add topl;
add Math.Vec3.(normalize (cross (botl - topl) (botr - topl)));
add botl;
add Math.Vec3.(normalize (cross (botr - botl) (topl - botl)));
add botr;
add Math.Vec3.(normalize (cross (topl - botr) (botl - botr)));
let prepend v = prepend_vec3 c.data v in
prepend Math.Vec3.(normalize (cross (botl - topl) (botr - topl)));
prepend topl;
prepend Math.Vec3.(normalize (cross (botr - botl) (topl - botl)));
prepend botl;
prepend Math.Vec3.(normalize (cross (topl - botr) (botl - botr)));
prepend botr;

add topl;
add Math.Vec3.(normalize (cross (botr - topl) (topr - topl)));
add botr;
add Math.Vec3.(normalize (cross (topr - botr) (topl - botr)));
add topr;
add Math.Vec3.(normalize (cross (topl - topr) (botr - topr)))
prepend Math.Vec3.(normalize (cross (botr - topl) (topr - topl)));
prepend topl;
prepend Math.Vec3.(normalize (cross (topr - botr) (topl - botr)));
prepend botr;
prepend Math.Vec3.(normalize (cross (topl - topr) (botr - topr)));
prepend topr

let add_face c botl topl topr =
let size = 1. /. float_of_int c.param1 in
Expand Down
18 changes: 9 additions & 9 deletions lib/graphics/primitive/sphere.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ type config = {
}

let add_tile c tl tr bl br =
let add v =
add_vec3 c.data v;
add_vec3 c.data (Math.Vec3.normalize v)
let prepend v =
prepend_vec3 c.data (Math.Vec3.normalize v);
prepend_vec3 c.data v
in
add tl;
add bl;
add br;
prepend tl;
prepend bl;
prepend br;

add tl;
add br;
add tr
prepend tl;
prepend br;
prepend tr

let add_wedge c theta theta' =
let step = Float.pi /. float_of_int c.param1 in
Expand Down
2 changes: 1 addition & 1 deletion lib/graphics/util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ let check_gl_error () =
done;
if !errored then failwith "shortcircuit in print_gl_error"

let add_vec3 l v =
let prepend_vec3 l v =
let x, y, z = Math.Vec3.to_tuple v in
l := x :: y :: z :: !l
2 changes: 1 addition & 1 deletion lib/graphics/util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ val load_uniform1i : int -> int -> string -> unit
val load_uniform1f : float -> int -> string -> unit
val load_uniform3fv : Math.Vec3.t -> int -> string -> unit
val check_gl_error : unit -> unit
val add_vec3 : float list ref -> Math.Vec3.t -> unit
val prepend_vec3 : float list ref -> Math.Vec3.t -> unit

0 comments on commit 836f942

Please sign in to comment.