Skip to content

Commit

Permalink
Support obj vertex colors
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Jan 9, 2025
1 parent b8044d2 commit f16eb9c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 75 deletions.
67 changes: 23 additions & 44 deletions example/book.odin
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@ package example

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

@private
State_Book :: struct {
rotation: mat4,
objects: []Object,
}

Object :: struct {
using locations: Input_Locations_Boxes,
vao : VAO,
vao: VAO,
positions: []vec3,
colors : []u8vec4,
colors: []u8vec4,
rotation: mat4,
}

@private
Expand All @@ -33,40 +28,30 @@ setup_book :: proc(s: ^State_Book, program: gl.Program) {
obj.parse_line(&data, line)
}

objects: [dynamic]Object

for object in data.objects {
append(&objects, Object{})
o := last(&objects)

o.vao = gl.CreateVertexArray()
s.vao = gl.CreateVertexArray()

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

o.positions = lines.pos[:len(lines)]
o.colors = lines.col[:len(lines)]

for &pos in o.positions {
pos *= 1000
}

slice.fill(o.colors, rand_color())


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

input_locations_boxes(&o.locations, program)
s.positions = vertices.pos[:len(vertices)]
for &pos in s.positions {
pos *= 1000
}

attribute(o.a_position, gl.CreateBuffer(), o.positions)
attribute(o.a_color , gl.CreateBuffer(), o.colors)
s.colors = make([]rgba, len(vertices))
for &col, i in s.colors {
col = to_rgba(vertices[i].col)
}

s.objects = objects[:]

/* Init rotation */
s.rotation = 1
gl.BindVertexArray(s.vao)

input_locations_boxes(&s.locations, program)

attribute(s.a_position, gl.CreateBuffer(), s.positions)
attribute(s.a_color , gl.CreateBuffer(), s.colors)

/* Init rotation */
s.rotation = 1
}

@private
Expand All @@ -89,15 +74,9 @@ frame_book :: proc(s: ^State_Book, delta: f32) {
mat *= glm.mat4Translate({0, 0, -900 + scale * 720})
mat *= s.rotation



for &o in s.objects {

gl.BindVertexArray(o.vao)

uniform(o.u_matrix, mat)

gl.DrawArrays(gl.TRIANGLES, 0, len(o.positions))
}
gl.BindVertexArray(s.vao)

uniform(s.u_matrix, mat)

gl.DrawArrays(gl.TRIANGLES, 0, len(s.positions))
}
6 changes: 2 additions & 4 deletions example/chair.odin
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,14 @@ setup_chair :: proc(s: ^State_Chair, program: gl.Program) {
lines := obj.object_to_triangles(data, object, context.allocator)

o.positions = lines.pos[:len(lines)]
o.colors = lines.col[:len(lines)]

for &pos in o.positions {
pos *= 100
pos.y -= 300
}


o.colors = make([]rgba, len(o.positions))
slice.fill(o.colors, rand_color())


gl.BindVertexArray(o.vao)

input_locations_boxes(&o.locations, program)
Expand Down
2 changes: 1 addition & 1 deletion example/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ <h4 style="margin-bottom: 0.5rem;">Demos</h4>
<li><a href="#lathe"> Lathe </a></li>
<li><a href="#suzanne"> Suzanne </a></li>
<li><a href="#chair"> Chair </a></li>
<!-- <li><a href="#book"> Book </a></li> -->
<li><a href="#book"> Book </a></li>
</ol>
</nav>

Expand Down
3 changes: 1 addition & 2 deletions example/suzanne.odin
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ 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)]
s.colors = lines.col[:len(lines)]

for &pos in s.positions {
pos *= 100
}

s.colors = make([]rgba, len(s.positions))
slice.fill(s.colors, GREEN)

/* Init rotation */
Expand Down
7 changes: 6 additions & 1 deletion example/utils.odin
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,16 @@ u8vec4_to_vec4 :: #force_inline proc "contextless" (rgba: u8vec4) -> vec4 {
return {f32(rgba.r)/255, f32(rgba.g)/255, f32(rgba.b)/255, f32(rgba.a)/255}
}
rgba_to_vec4 :: u8vec4_to_vec4
vec4_from_rgba :: rgba_to_vec4

to_rgba_3_1 :: #force_inline proc "contextless" (color: $A/[3]u8, a: u8) -> rgba {
return {color.r, color.g, color.b, a}
}
to_rgba :: proc {to_rgba_3_1}
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}
}
to_rgba :: proc {to_rgba_3_1, vec3_to_rgba}


last_array :: #force_inline proc "contextless" (arr: $T/[$N]$E) -> E {return arr[len(arr)-1]}
last_dyn_array :: #force_inline proc "contextless" (arr: $T/[dynamic]$E) -> E {return arr[len(arr)-1]}
Expand Down
47 changes: 24 additions & 23 deletions obj/parser.odin
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ u8vec4 :: [4]u8

Index :: struct {
/*
indices start ar 1
zero means it points to nowhere
`data.positions[idx.position-1]`
indices are base-1
*/
position: int,
texcoord: int,
Expand Down Expand Up @@ -99,9 +97,12 @@ Data :: struct {
// }

init_data :: proc (data: ^Data, allocator := context.allocator) {
data.positions = make([dynamic]vec3, 0, 32, allocator)
data.texcoords = make([dynamic]vec2, 0, 32, allocator)
data.normals = make([dynamic]vec3, 0, 32, allocator)

// indices are base-1, add zero index to be able to index it normally
data.positions = make([dynamic]vec3, 1, 32, allocator)
data.texcoords = make([dynamic]vec2, 1, 32, allocator)
data.normals = make([dynamic]vec3, 1, 32, allocator)

data.colors = make([dynamic]vec3, 0, 32, allocator)
data.objects = make([dynamic]Object, 1, 4, allocator)
data.objects[0] = object_make(data)
Expand Down Expand Up @@ -445,21 +446,21 @@ parse_line :: proc (data: ^Data, str: string)
parse_usemtl(data, &ptr)
}
}

// if len(data.colors) > 0 {
// /* Fill the remaining slots in the colors array */
// for _ in 0 ..< len(data.positions) - len(data.colors) {
// append(&data.colors, 1)
// }
// }
}

Vertex :: struct {
pos: vec3,
col: u8vec4,
col: vec3,
}
Vertices :: #soa[]Vertex

vertex_get_from_index :: proc (data: Data, idx: Index) -> (v: Vertex) {
v.pos = data.positions[idx.position]
// colors array is only filled if the colors data is in the file
v.col = data.colors[idx.position] if idx.position < len(data.colors) else 1
return
}

object_to_triangles :: proc (data: Data, g: Object, allocator := context.allocator) -> Vertices {

vertices := make(#soa[dynamic]Vertex, 0, 3*len(g.indices), allocator)
Expand All @@ -468,9 +469,9 @@ object_to_triangles :: proc (data: Data, g: Object, allocator := context.allocat
for i in 2 ..< len(indices) {
a, b, c := indices[0], indices[i-1], indices[i]
append(&vertices, ..[]Vertex{
{pos = data.positions[a.position-1]},
{pos = data.positions[b.position-1]},
{pos = data.positions[c.position-1]},
vertex_get_from_index(data, a),
vertex_get_from_index(data, b),
vertex_get_from_index(data, c),
})
}
}
Expand All @@ -486,12 +487,12 @@ object_to_lines :: proc (data: Data, g: Object, allocator := context.allocator)
for i in 2 ..< len(indices) {
a, b, c := indices[0], indices[i-1], indices[i]
append(&vertices, ..[]Vertex{
{pos = data.positions[a.position-1]},
{pos = data.positions[b.position-1]},
{pos = data.positions[b.position-1]},
{pos = data.positions[c.position-1]},
{pos = data.positions[c.position-1]},
{pos = data.positions[a.position-1]},
vertex_get_from_index(data, a),
vertex_get_from_index(data, b),
vertex_get_from_index(data, b),
vertex_get_from_index(data, c),
vertex_get_from_index(data, c),
vertex_get_from_index(data, a),
})
}
}
Expand Down

0 comments on commit f16eb9c

Please sign in to comment.