2021-03-25 18:53:13 +03:00
|
|
|
/**********************************************************************
|
|
|
|
*
|
|
|
|
* .obj loader
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 Dario Deledda. All rights reserved.
|
|
|
|
* Use of this source code is governed by an MIT license
|
|
|
|
* that can be found in the LICENSE file.
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
**********************************************************************/
|
|
|
|
module obj
|
2021-04-20 17:16:35 +03:00
|
|
|
|
2021-03-25 18:53:13 +03:00
|
|
|
import gg.m4
|
2022-01-02 21:36:01 +03:00
|
|
|
import sokol.gfx
|
2021-03-25 18:53:13 +03:00
|
|
|
|
|
|
|
// part struct mantain the fae indexes list
|
|
|
|
pub struct Part {
|
|
|
|
pub mut:
|
2021-04-20 17:16:35 +03:00
|
|
|
faces [][][3]int // v n t index order, if -1 not available
|
|
|
|
name string
|
|
|
|
material string
|
2021-03-25 18:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// materias struct, all Ks and Ns are stored as maps of string
|
|
|
|
pub struct Material {
|
|
|
|
pub mut:
|
2021-04-20 17:16:35 +03:00
|
|
|
name string
|
|
|
|
ks map[string]m4.Vec4
|
|
|
|
ns map[string]f32
|
|
|
|
maps map[string]string
|
2021-03-25 18:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// render data used for the rendering
|
2021-04-20 17:16:35 +03:00
|
|
|
pub struct Render_data {
|
2021-03-25 18:53:13 +03:00
|
|
|
pub mut:
|
2022-01-02 21:36:01 +03:00
|
|
|
pipeline gfx.Pipeline
|
|
|
|
bind gfx.Bindings
|
2021-03-25 18:53:13 +03:00
|
|
|
n_vert u32
|
|
|
|
material string
|
|
|
|
}
|
|
|
|
|
|
|
|
// base object parts struct
|
|
|
|
pub struct ObjPart {
|
|
|
|
pub mut:
|
2021-04-20 17:16:35 +03:00
|
|
|
v []m4.Vec4 // position
|
|
|
|
vn []m4.Vec4 // normals
|
|
|
|
vp []m4.Vec4 // vertex params
|
|
|
|
vt []m4.Vec4 // textures
|
|
|
|
|
|
|
|
name string
|
2022-01-02 21:36:01 +03:00
|
|
|
part []Part // parts of the ObjPart
|
|
|
|
mat []Material // list of the materials of the ObjPart
|
|
|
|
mat_map map[string]int // maping material name to its material index
|
|
|
|
texture map[string]gfx.Image // GPU loaded texture map
|
2021-04-20 17:16:35 +03:00
|
|
|
material_file string // .mtl file name for the .obj
|
|
|
|
|
|
|
|
rend_data []Render_data // render data used for the rendering
|
|
|
|
|
|
|
|
t_m m4.Mat4 = m4.unit_m4() // transform matrix for this ObjPart
|
|
|
|
// child []ObjPart // childs
|
2021-03-25 18:53:13 +03:00
|
|
|
// stats
|
2021-04-20 17:16:35 +03:00
|
|
|
min m4.Vec4 // min 3d position in the ObjPart
|
|
|
|
max m4.Vec4 // max 3d position in the ObjPart
|
|
|
|
radius f32 // bounding circle radius of the ObjPart
|
2021-03-25 18:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// used in to pass the matrices to the shader
|
|
|
|
pub struct Mats {
|
|
|
|
pub mut:
|
2021-04-20 17:16:35 +03:00
|
|
|
mv m4.Mat4
|
|
|
|
mvp m4.Mat4
|
|
|
|
nm m4.Mat4
|
2021-03-25 18:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// data passed to the vertex shader
|
|
|
|
pub struct Tmp_vs_param {
|
|
|
|
pub mut:
|
2021-04-20 17:16:35 +03:00
|
|
|
mv m4.Mat4
|
2021-03-25 18:53:13 +03:00
|
|
|
mvp m4.Mat4
|
|
|
|
nm m4.Mat4
|
|
|
|
}
|
|
|
|
|
|
|
|
// data passed to the pixel shader
|
|
|
|
pub struct Tmp_fs_param {
|
|
|
|
pub mut:
|
|
|
|
ligth m4.Vec4
|
2021-04-20 17:16:35 +03:00
|
|
|
ka m4.Vec4 = m4.Vec4{
|
|
|
|
e: [f32(0.1), 0.0, 0.0, 1.0]!
|
|
|
|
}
|
2021-04-27 08:18:48 +03:00
|
|
|
kd m4.Vec4 = m4.Vec4{
|
2021-04-20 17:16:35 +03:00
|
|
|
e: [f32(0.5), 0.5, 0.5, 1.0]!
|
|
|
|
}
|
2021-04-27 08:18:48 +03:00
|
|
|
ks m4.Vec4 = m4.Vec4{
|
2021-04-20 17:16:35 +03:00
|
|
|
e: [f32(1.0), 1.0, 1.0, 1.0]!
|
|
|
|
}
|
2021-03-25 18:53:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// shader data for the rendering
|
|
|
|
pub struct Shader_data {
|
|
|
|
pub mut:
|
2022-09-15 07:59:31 +03:00
|
|
|
vs_data &Tmp_vs_param = unsafe { nil }
|
2021-03-25 18:53:13 +03:00
|
|
|
vs_len int
|
2022-09-15 07:59:31 +03:00
|
|
|
fs_data &Tmp_fs_param = unsafe { nil }
|
2021-03-25 18:53:13 +03:00
|
|
|
fs_len int
|
2021-04-20 17:16:35 +03:00
|
|
|
}
|