Skip to content

Commit

Permalink
Change the api for parser, use default wasm allocators
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Jan 10, 2025
1 parent 8e89620 commit c9f10e2
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 102 deletions.
10 changes: 2 additions & 8 deletions example/book.odin
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package example

import glm "core:math/linalg/glsl"
import "core:strings"
import "core:slice"
import gl "../wasm/webgl"
import "../obj"
Expand All @@ -22,17 +21,12 @@ setup_book :: proc(s: ^State_Book, program: gl.Program) {
gl.Enable(gl.CULL_FACE) // don't draw back faces
gl.Enable(gl.DEPTH_TEST) // draw only closest faces

data := obj.data_make(context.temp_allocator)
obj_file := #load("./public/book.obj", string)

for line in strings.split_lines_iterator(&obj_file) {
obj.parse_line(&data, line)
}
objects := obj.parse_file(#load("./public/book.obj", string), context.temp_allocator)

s.vao = gl.CreateVertexArray()


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

extent_min, extent_max := get_extents(s.positions)
Expand Down
45 changes: 20 additions & 25 deletions example/chair.odin
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
package example

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

@private
State_Chair :: struct {
rotation: mat4,
objects: []Object,
shapes: []Shape,
}

Object :: struct {
Shape :: struct {
using locations: Input_Locations_Boxes,
vao : VAO,
positions: []vec3,
Expand All @@ -28,38 +27,34 @@ setup_chair :: proc(s: ^State_Chair, program: gl.Program) {
gl.Enable(gl.CULL_FACE) // don't draw back faces
gl.Enable(gl.DEPTH_TEST) // draw only closest faces

data := obj.data_make(context.temp_allocator)
it := chair_obj_bytes
for line in strings.split_lines_iterator(&it) {
obj.parse_line(&data, line)
}
objects := obj.parse_file(#load("./public/chair.obj", string), context.temp_allocator)

extent_min, extent_max := get_extents(data.positions[:])
extent_min, extent_max := get_extents(objects[0].vertices.position[:len(objects[0].vertices)])
for o in objects[1:] {
extend_extents(&extent_min, &extent_max, o.vertices.position[:len(o.vertices)])
}

objects: [dynamic]Object
s.shapes = make([]Shape, len(objects))

for object in data.objects {
append(&objects, Object{})
o := last(&objects)
for &shape, i in s.shapes {
o := objects[i]

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

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

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

gl.BindVertexArray(o.vao)
gl.BindVertexArray(shape.vao)

input_locations_boxes(&o.locations, program)
input_locations_boxes(&shape.locations, program)

attribute(o.a_position, gl.CreateBuffer(), o.positions)
attribute(o.a_color , gl.CreateBuffer(), o.colors)
attribute(shape.a_position, gl.CreateBuffer(), shape.positions)
attribute(shape.a_color, gl.CreateBuffer(), shape.colors)
}

s.objects = objects[:]

/* Init rotation */
s.rotation = 1
}
Expand All @@ -86,7 +81,7 @@ frame_chair :: proc(s: ^State_Chair, delta: f32) {



for &o in s.objects {
for &o in s.shapes {

gl.BindVertexArray(o.vao)

Expand Down
9 changes: 1 addition & 8 deletions example/setup.odin
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package example

import "core:mem"
import "core:math/rand"
import "core:crypto"

Expand Down Expand Up @@ -167,17 +166,11 @@ demo_state: struct #raw_union {
book: State_Book,
}


temp_arena_buffer: [mem.Megabyte]byte
temp_arena: mem.Arena = {data = temp_arena_buffer[:]}
temp_arena_allocator := mem.arena_allocator(&temp_arena)

@export
start :: proc (example_kind: Example_Kind) -> (ok: bool) {
example = example_kind
demo := demos[example]

context.temp_allocator = temp_arena_allocator
defer free_all(context.temp_allocator)

program: gl.Program
Expand Down Expand Up @@ -231,7 +224,7 @@ start :: proc (example_kind: Example_Kind) -> (ok: bool) {

@export
frame :: proc (delta: f32) {
context.temp_allocator = temp_arena_allocator

defer free_all(context.temp_allocator)

defer mouse_down_frame = false
Expand Down
14 changes: 5 additions & 9 deletions example/suzanne.odin
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
package example

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

Expand All @@ -15,18 +15,14 @@ State_Suzanne :: struct {
vertices: Vertices,
}

suzanne_obj_bytes := #load("./public/suzanne.obj", string)

@private
setup_suzanne :: proc(s: ^State_Suzanne, program: gl.Program) {

data := obj.data_make(context.temp_allocator)
it := suzanne_obj_bytes
for line in strings.split_lines_iterator(&it) {
obj.parse_line(&data, line)
objects, parse_err := obj.parse_file(#load("./public/suzanne.obj", string), context.temp_allocator)
if parse_err != nil {
fmt.eprintf("Parse error: %v", parse_err)
}

object := &data.objects[0]
object := &objects[0]

vertices := make(Vertices, len(object.vertices), context.temp_allocator)

Expand Down
9 changes: 8 additions & 1 deletion example/utils.odin
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ normals_from_positions :: proc (dst, src: []vec3) {
}
}

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

if len(positions) == 0 {
return
Expand All @@ -413,6 +413,13 @@ get_extents :: proc (positions: []$T) -> (v_min, v_max: T) {
return
}

extend_extents :: proc (v_min, v_max: ^vec3, positions: []vec3) {
a_min, a_max := get_extents(positions)
b_min, b_max := get_extents({v_min^, v_max^, a_min, a_max})
v_min ^= b_min
v_max ^= b_max
}

correct_extents :: proc (
positions: []vec3,
in_min: vec3, in_max: vec3,
Expand Down
Loading

0 comments on commit c9f10e2

Please sign in to comment.