Obj importer turorial
Two days ago, I had the brilliant idea of making my own obj importer. It dint go so well as I thought.For this project I used Odin lang my favorite programming language, you might be asking yourself "Why make an obj importer if hundreds of it exists?". Well I'm kind of stupid Xd, Also I have never seen an easy obj importer for Odin lang so I saw this like an opportunity to make my own.Before making The Obj importer, I had to find the resources to learn how a Obj file works. I found some useful resources that you can find below.1) https://www.opengl-tutorial.org/beginners-tutorials/tutorial-7-model-loading/
2) https://paulbourke.net/dataformats/obj/
3) https://www.youtube.com/watch?v=EuqUnCcJ0tg
4) Odin lang - Discord channelAfter taking a look at these links, I had enough knowledge to make it. I got this as a result after 3 days.go fold title:"Obj importer"import "core:os"import "core:strings"import "core:strconv"import "core:fmt"load_obj::proc(file:string) -> ([dynamic]f32,[dynamic]f32,[dynamic]f32, [dynamic]int){data, err := os.read_entire_file_from_filename_or_err(file)if err != nil {fmt.println("Error reading file:", err)return nil, nil, nil, nil}vertices :[dynamic]f32tex_coord:[dynamic]f32normal:[dynamic]f32indices:[dynamic]intlines := strings.split(string(data), "\n") // Use "\n" for new linesfor line in lines {words := strings.split(line, " ")if len(words) > 0 {first_word := words[0]//(X, Y, Z)if first_word == "v" && len(words) >= 4 {x, _ := strconv.parse_f32(words[1])y, _ := strconv.parse_f32(words[2])z, _ := strconv.parse_f32(words[3])append(&vertices, x)append(&vertices, y)append(&vertices, z)}if first_word == "vt" && len(words) >= 3{tx, _ := strconv.parse_f32(words[1])ty, _ := strconv.parse_f32(words[2])append(&tex_coord, tx)append(&tex_coord, ty)}if first_word == "vn" && len(words) >= 4{nx, _ := strconv.parse_f32(words[1])ny, _ := strconv.parse_f32(words[2])nz, _ := strconv.parse_f32(words[3])append(&normal, nx)append(&normal, ny)append(&normal, nz)}if first_word == "f" && len(words) >= 4{for i := 1; i <= 3; i += 1 {f_parts:= strings.split(words[i], "/")vi, _ := strconv.parse_int(f_parts[0])append(&indices, vi - 1)}}if first_word == "g" && len(words) >= 2{//Todo}}}fmt.printfln("Done")return vertices, tex_coord, normal ,indices}main :: proc() {vertices, tex_coord, normal , indices := load_obj("model.obj")fmt.println(vertices)fmt.println(tex_coord)fmt.println(normal)fmt.println(indices)}
package main
For now this code is only capable of Returning:
1) Vertices.
2) Indices.
3) Normal.
4) Texture Coord.NOTE: This is only Capable of using triangulated vertices.
Only triangles no quads.Even dough this Obj importer is pretty advanced. is not good enough of my Games.