Move vector types to base layer

This commit is contained in:
Riyyi
2026-10-29 21:38:59 +01:00
parent 163aa3052b
commit af063209ff
3 changed files with 33 additions and 24 deletions
+17
View File
@@ -0,0 +1,17 @@
package base
// -----------------------------------------
// Types
Vec2 :: [2]f32
Vec3 :: [3]f32
Vec4 :: [4]f32
Mat3 :: matrix[3, 3]f32
Mat4 :: matrix[4, 4]f32
Vertex :: struct #align (4) {
pos: Vec3,
col: Vec3,
uv: Vec2, // texture coordinate
}
+12 -11
View File
@@ -6,6 +6,7 @@ import "core:os"
import "core:strconv" import "core:strconv"
import "core:strings" import "core:strings"
import "sindri:base"
import "sindri:platform" import "sindri:platform"
// Minimal OBJ loader: reads v, vt, vn and triangulates convex polygon faces. // Minimal OBJ loader: reads v, vt, vn and triangulates convex polygon faces.
@@ -14,7 +15,7 @@ import "sindri:platform"
load_obj_model :: proc( load_obj_model :: proc(
path: string, path: string,
) -> ( ) -> (
vertices: []platform.Vertex, vertices: []base.Vertex,
indices: []u16, indices: []u16,
ok: bool, ok: bool,
) { ) {
@@ -25,10 +26,10 @@ load_obj_model :: proc(
} }
defer delete(data) defer delete(data)
positions: [dynamic]platform.Vec3 positions: [dynamic]base.Vec3
uvs: [dynamic]platform.Vec2 uvs: [dynamic]base.Vec2
normals: [dynamic]platform.Vec3 normals: [dynamic]base.Vec3
temp_vertices: [dynamic]platform.Vertex temp_vertices: [dynamic]base.Vertex
temp_indices: [dynamic]u16 temp_indices: [dynamic]u16
defer { defer {
delete(positions) delete(positions)
@@ -53,18 +54,18 @@ load_obj_model :: proc(
x, _ := strconv.parse_f32(fields[1]) x, _ := strconv.parse_f32(fields[1])
y, _ := strconv.parse_f32(fields[2]) y, _ := strconv.parse_f32(fields[2])
z, _ := strconv.parse_f32(fields[3]) z, _ := strconv.parse_f32(fields[3])
append(&positions, platform.Vec3{x, y, z}) append(&positions, base.Vec3{x, y, z})
case "vt": case "vt":
if len(fields) < 3 do continue if len(fields) < 3 do continue
u, _ := strconv.parse_f32(fields[1]) u, _ := strconv.parse_f32(fields[1])
v, _ := strconv.parse_f32(fields[2]) v, _ := strconv.parse_f32(fields[2])
append(&uvs, platform.Vec2{u, v}) append(&uvs, base.Vec2{u, v})
case "vn": case "vn":
if len(fields) < 4 do continue if len(fields) < 4 do continue
x, _ := strconv.parse_f32(fields[1]) x, _ := strconv.parse_f32(fields[1])
y, _ := strconv.parse_f32(fields[2]) y, _ := strconv.parse_f32(fields[2])
z, _ := strconv.parse_f32(fields[3]) z, _ := strconv.parse_f32(fields[3])
append(&normals, platform.Vec3{x, y, z}) append(&normals, base.Vec3{x, y, z})
case "f": case "f":
face_indices: [dynamic]u16 face_indices: [dynamic]u16
defer delete(face_indices) defer delete(face_indices)
@@ -89,7 +90,7 @@ load_obj_model :: proc(
vi -= 1 // OBJ indices are 1-based vi -= 1 // OBJ indices are 1-based
if vi < 0 || vi >= len(positions) do continue if vi < 0 || vi >= len(positions) do continue
uv := platform.Vec2{0, 0} uv := base.Vec2{0, 0}
if len(vti_str) > 0 { if len(vti_str) > 0 {
vti, _ := strconv.parse_int(vti_str) vti, _ := strconv.parse_int(vti_str)
vti -= 1 vti -= 1
@@ -102,7 +103,7 @@ load_obj_model :: proc(
append( append(
&temp_vertices, &temp_vertices,
platform.Vertex { base.Vertex {
pos = positions[vi], pos = positions[vi],
col = {1, 1, 1}, col = {1, 1, 1},
uv = uv, uv = uv,
@@ -140,7 +141,7 @@ load_obj_model :: proc(
v.pos = (v.pos - center) * scale v.pos = (v.pos - center) * scale
} }
vertices = make([]platform.Vertex, len(temp_vertices)) vertices = make([]base.Vertex, len(temp_vertices))
indices = make([]u16, len(temp_indices)) indices = make([]u16, len(temp_indices))
copy(vertices, temp_vertices[:]) copy(vertices, temp_vertices[:])
copy(indices, temp_indices[:]) copy(indices, temp_indices[:])
+4 -13
View File
@@ -14,15 +14,6 @@ import "sindri:base"
// ----------------------------------------- // -----------------------------------------
// Types // Types
Vec2 :: [2]f32
Vec3 :: [3]f32
Vertex :: struct #align (4) {
pos: Vec3,
col: Vec3,
uv: Vec2, // texture coordinate
}
Camera_Uniform :: struct { Camera_Uniform :: struct {
mvp: linalg.Matrix4f32, mvp: linalg.Matrix4f32,
} }
@@ -247,7 +238,7 @@ frame :: proc "c" () {
} }
// Upload arbitrary vertex/index data to GPU buffers // Upload arbitrary vertex/index data to GPU buffers
fill_buffers_with_model :: proc(vertices: []Vertex, indices: []u16) { fill_buffers_with_model :: proc(vertices: []base.Vertex, indices: []u16) {
state.vertex_buffer = wgpu.DeviceCreateBufferWithData( state.vertex_buffer = wgpu.DeviceCreateBufferWithData(
state.device, state.device,
&{label = "model vertices", usage = {.Vertex}}, &{label = "model vertices", usage = {.Vertex}},
@@ -319,7 +310,7 @@ on_device :: proc "c" (
uniforms_struct := to_wgsl_struct(Camera_Uniform, "Uniforms") uniforms_struct := to_wgsl_struct(Camera_Uniform, "Uniforms")
defer delete(uniforms_struct) defer delete(uniforms_struct)
vertex_input_struct := to_wgsl_struct( vertex_input_struct := to_wgsl_struct(
Vertex, base.Vertex,
"VertexInput", "VertexInput",
locations = true, locations = true,
) )
@@ -423,11 +414,11 @@ on_device :: proc "c" (
// ---------------------------------------- // ----------------------------------------
// Buffer shape // Buffer shape
vertex_attributes := to_vertex_attributes(Vertex) vertex_attributes := to_vertex_attributes(base.Vertex)
vertex_layout := wgpu.VertexBufferLayout { vertex_layout := wgpu.VertexBufferLayout {
stepMode = .Vertex, stepMode = .Vertex,
arrayStride = size_of(Vertex), arrayStride = size_of(base.Vertex),
attributeCount = len(vertex_attributes), attributeCount = len(vertex_attributes),
attributes = raw_data(vertex_attributes), attributes = raw_data(vertex_attributes),
} }