| 1 | /********************************************************************** |
| 2 | * |
| 3 | * .obj loader |
| 4 | * |
| 5 | * Copyright (c) 2021 Dario Deledda. All rights reserved. |
| 6 | * Use of this source code is governed by an MIT license |
| 7 | * that can be found in the LICENSE file. |
| 8 | * |
| 9 | * TODO: |
| 10 | **********************************************************************/ |
| 11 | module obj |
| 12 | |
| 13 | import gg.m4 |
| 14 | import sokol.gfx |
| 15 | |
| 16 | // part struct maintaining the face indices |
| 17 | pub struct Part { |
| 18 | pub mut: |
| 19 | faces [][][3]int // v n t index order, if -1 not available |
| 20 | name string |
| 21 | material string |
| 22 | } |
| 23 | |
| 24 | // material struct, all Ks and Ns are stored as maps of string |
| 25 | pub struct Material { |
| 26 | pub mut: |
| 27 | name string |
| 28 | ks map[string]m4.Vec4 |
| 29 | ns map[string]f32 |
| 30 | maps map[string]string |
| 31 | } |
| 32 | |
| 33 | // render data used for the rendering |
| 34 | pub struct Render_data { |
| 35 | pub mut: |
| 36 | pipeline gfx.Pipeline |
| 37 | bind gfx.Bindings |
| 38 | n_vert u32 |
| 39 | material string |
| 40 | } |
| 41 | |
| 42 | // base object parts struct |
| 43 | pub struct ObjPart { |
| 44 | pub mut: |
| 45 | v []m4.Vec4 // position |
| 46 | vn []m4.Vec4 // normals |
| 47 | vp []m4.Vec4 // vertex params |
| 48 | vt []m4.Vec4 // textures |
| 49 | |
| 50 | name string |
| 51 | part []Part // parts of the ObjPart |
| 52 | mat []Material // list of the materials of the ObjPart |
| 53 | mat_map map[string]int // mapping material name to its material index |
| 54 | texture map[string]gfx.Image // GPU loaded texture map |
| 55 | sampler map[string]gfx.Sampler // GPU loaded sampler |
| 56 | material_file string // .mtl file name for the .obj |
| 57 | |
| 58 | rend_data []Render_data // render data used for the rendering |
| 59 | |
| 60 | t_m m4.Mat4 = m4.unit_m4() // transform matrix for this ObjPart |
| 61 | // child []ObjPart |
| 62 | // stats |
| 63 | min m4.Vec4 // min 3d position in the ObjPart |
| 64 | max m4.Vec4 // max 3d position in the ObjPart |
| 65 | radius f32 // bounding circle radius of the ObjPart |
| 66 | } |
| 67 | |
| 68 | // used in to pass the matrices to the shader |
| 69 | pub struct Mats { |
| 70 | pub mut: |
| 71 | mv m4.Mat4 |
| 72 | mvp m4.Mat4 |
| 73 | nm m4.Mat4 |
| 74 | } |
| 75 | |
| 76 | // data passed to the vertex shader |
| 77 | pub struct Tmp_vs_param { |
| 78 | pub mut: |
| 79 | mv m4.Mat4 |
| 80 | mvp m4.Mat4 |
| 81 | nm m4.Mat4 |
| 82 | } |
| 83 | |
| 84 | // data passed to the pixel shader |
| 85 | pub struct Tmp_fs_param { |
| 86 | pub mut: |
| 87 | light m4.Vec4 |
| 88 | ka m4.Vec4 = m4.Vec4{ |
| 89 | e: [f32(0.1), 0.0, 0.0, 1.0]! |
| 90 | } |
| 91 | kd m4.Vec4 = m4.Vec4{ |
| 92 | e: [f32(0.5), 0.5, 0.5, 1.0]! |
| 93 | } |
| 94 | ks m4.Vec4 = m4.Vec4{ |
| 95 | e: [f32(1.0), 1.0, 1.0, 1.0]! |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // shader data for the rendering |
| 100 | pub struct Shader_data { |
| 101 | pub mut: |
| 102 | vs_data &Tmp_vs_param = unsafe { nil } |
| 103 | vs_len int |
| 104 | fs_data &Tmp_fs_param = unsafe { nil } |
| 105 | fs_len int |
| 106 | } |
| 107 | |