Skip to content

Commit

Permalink
Abstract correcting object extents
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Jan 9, 2025
1 parent 9416fa1 commit 0804af5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
4 changes: 1 addition & 3 deletions example/book.odin
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ setup_book :: proc(s: ^State_Book, program: gl.Program) {
vertices := obj.object_to_triangles(data, data.objects[0], context.allocator)

s.positions = vertices.pos[:len(vertices)]
for &pos in s.positions {
pos *= 1000
}
correct_extents(s.positions, ..get_extents(s.positions), -200, 200)

s.colors = make([]rgba, len(vertices))
for &col, i in s.colors {
Expand Down
15 changes: 3 additions & 12 deletions example/chair.odin
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ setup_chair :: proc(s: ^State_Chair, program: gl.Program) {
}

extent_min, extent_max := get_extents(data.positions[:])
extent_span := hypot(extent_max-extent_min)

goal_min, goal_max: vec3 = -200, 200
goal_span := hypot(goal_max-goal_min)

objects: [dynamic]Object

Expand All @@ -48,13 +44,10 @@ setup_chair :: proc(s: ^State_Chair, program: gl.Program) {

o.vao = gl.CreateVertexArray()

lines := obj.object_to_triangles(data, object, context.allocator)
vertices := obj.object_to_triangles(data, object, context.allocator)

o.positions = lines.pos[:len(lines)]
for &pos in o.positions {
pos -= (extent_max-extent_min)/2 + extent_min
pos *= goal_span/extent_span
}
o.positions = vertices.pos[:len(vertices)]
correct_extents(o.positions, extent_min, extent_max, -200, 200)

o.colors = make([]rgba, len(o.positions))
slice.fill(o.colors, rand_color())
Expand All @@ -71,8 +64,6 @@ setup_chair :: proc(s: ^State_Chair, program: gl.Program) {

/* Init rotation */
s.rotation = 1


}

@private
Expand Down
6 changes: 2 additions & 4 deletions example/suzanne.odin
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ setup_suzanne :: proc(s: ^State_Suzanne, program: gl.Program) {
}

lines := obj.object_to_lines(data, data.objects[0], context.allocator)

s.positions = lines.pos[:len(lines)]
for &pos in s.positions {
pos *= 100
}
correct_extents(s.positions, ..get_extents(s.positions), -200, 200)

s.colors = make([]rgba, len(s.positions))
slice.fill(s.colors, GREEN)
Expand Down
14 changes: 14 additions & 0 deletions example/utils.odin
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,20 @@ get_extents :: proc (positions: []$T) -> (v_min, v_max: T) {
return
}

correct_extents :: proc (
positions: []vec3,
in_min: vec3, in_max: vec3,
out_min: vec3, out_max: vec3,
) {
in_span := hypot(in_max-in_min)
out_span := hypot(out_max-out_min)

for &pos in positions {
pos -= (in_max-in_min)/2 + in_min
pos *= out_span/in_span
}
}

vec3_on_radius :: proc (r, a, y: f32) -> vec3 {
return {r * cos(a), y, r * sin(a)}
}
Expand Down

0 comments on commit 0804af5

Please sign in to comment.