Skip to content

Commit

Permalink
Parse directly to vertices instead of indices
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Jan 10, 2025
1 parent f74851c commit 8e89620
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 120 deletions.
12 changes: 5 additions & 7 deletions example/book.odin
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package example

import glm "core:math/linalg/glsl"
import "core:strings"
import "core:slice"
import gl "../wasm/webgl"
import "../obj"

Expand Down Expand Up @@ -30,17 +31,14 @@ setup_book :: proc(s: ^State_Book, program: gl.Program) {

s.vao = gl.CreateVertexArray()

vertices := obj.object_to_triangles(data, data.objects[0], context.allocator)

s.positions = vertices.pos[:len(vertices)]

object := &data.objects[0]
s.positions = slice.clone(object.vertices.position[:len(object.vertices)])

extent_min, extent_max := get_extents(s.positions)
correct_extents(s.positions, extent_min, extent_max, -140, 140)

s.colors = make([]rgba, len(vertices))
for &col, i in s.colors {
col = to_rgba(vertices[i].col)
}
s.colors = vec3_slice_to_rgba(object.vertices.color[:len(object.vertices)])


gl.BindVertexArray(s.vao)
Expand Down
4 changes: 1 addition & 3 deletions example/chair.odin
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ setup_chair :: proc(s: ^State_Chair, program: gl.Program) {
o := last(&objects)

o.vao = gl.CreateVertexArray()

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

o.positions = vertices.pos[:len(vertices)]
o.positions = slice.clone(object.vertices.position[:len(object.vertices)])
correct_extents(o.positions, extent_min, extent_max, -200, 200)

o.colors = make([]rgba, len(o.positions))
Expand Down
27 changes: 15 additions & 12 deletions example/suzanne.odin
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ State_Suzanne :: struct {
using locations: Input_Locations_Boxes,
vao: VAO,
rotation: mat4,
positions: []vec3,
colors: []u8vec4,
vertices: Vertices,
}

suzanne_obj_bytes := #load("./public/suzanne.obj", string)
Expand All @@ -27,15 +26,19 @@ setup_suzanne :: proc(s: ^State_Suzanne, program: gl.Program) {
obj.parse_line(&data, line)
}

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

s.positions = lines.pos[:len(lines)]
object := &data.objects[0]

extent_min, extent_max := get_extents(s.positions)
correct_extents(s.positions, extent_min, extent_max, -200, 200)
vertices := make(Vertices, len(object.vertices), context.temp_allocator)

s.colors = make([]rgba, len(s.positions))
slice.fill(s.colors, GREEN)
copy(vertices.position[:len(vertices)],
object.vertices.position[:len(object.vertices)])

extent_min, extent_max := get_extents(vertices.position[:len(vertices)])
correct_extents(vertices.position[:len(vertices)], extent_min, extent_max, -200, 200)

slice.fill(vertices.color[:len(vertices)], GREEN)

s.vertices = vertices_to_lines(vertices)

/* Init rotation */
s.rotation = 1
Expand All @@ -46,8 +49,8 @@ setup_suzanne :: proc(s: ^State_Suzanne, program: gl.Program) {

input_locations_boxes(s, program)

attribute(s.a_position, gl.CreateBuffer(), s.positions)
attribute(s.a_color , gl.CreateBuffer(), s.colors)
attribute(s.a_position, gl.CreateBuffer(), s.vertices.position[:len(s.vertices)])
attribute(s.a_color , gl.CreateBuffer(), s.vertices.color[:len(s.vertices)])
}

@private
Expand All @@ -74,5 +77,5 @@ frame_suzanne :: proc(s: ^State_Suzanne, delta: f32) {

uniform(s.u_matrix, mat)

gl.DrawArrays(gl.LINES, 0, len(s.positions))
gl.DrawArrays(gl.LINES, 0, len(s.vertices))
}
33 changes: 33 additions & 0 deletions example/utils.odin
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ to_rgba_3_1 :: #force_inline proc "contextless" (color: $A/[3]u8, a: u8) -> rgba
vec3_to_rgba :: #force_inline proc "contextless" (v: vec3, a: u8 = 255) -> rgba {
return {u8(v.r*255), u8(v.g*255), u8(v.b*255), a}
}
vec3_slice_to_rgba :: #force_inline proc (vecs: []vec3, a: u8 = 255, allocator := context.allocator) -> []rgba {
r := make([]rgba, len(vecs), allocator)
for &col, i in r {
col = to_rgba(vecs[i], a)
}
return r
}
to_rgba :: proc {to_rgba_3_1, vec3_to_rgba}


Expand Down Expand Up @@ -387,6 +394,10 @@ normals_from_positions :: proc (dst, src: []vec3) {

get_extents :: proc (positions: []$T) -> (v_min, v_max: T) {

if len(positions) == 0 {
return
}

v_min = positions[0]
v_max = positions[0]

Expand Down Expand Up @@ -420,6 +431,28 @@ vec3_on_radius :: proc (r, a, y: f32) -> vec3 {
return {r * cos(a), y, r * sin(a)}
}

Vertex :: struct {
position: vec3,
color: rgba,
}
Vertices :: #soa[]Vertex

vertices_to_lines :: proc (vecs: Vertices, allocator := context.allocator) -> Vertices #no_bounds_check {

r := make(Vertices, 2*len(vecs), allocator)

for i := 0; i < len(vecs); i += 3 {
r[i*2 + 0] = vecs[i + 0]
r[i*2 + 1] = vecs[i + 1]
r[i*2 + 2] = vecs[i + 1]
r[i*2 + 3] = vecs[i + 2]
r[i*2 + 4] = vecs[i + 2]
r[i*2 + 5] = vecs[i + 0]
}

return r
}


Attribute_int :: distinct i32
Attribute_ivec2 :: distinct i32
Expand Down
Loading

0 comments on commit 8e89620

Please sign in to comment.