Przeglądaj źródła

resource: add bones and weights data

Part-of: #276
Daniele Bartolini 1 rok temu
rodzic
commit
27339d63c5
2 zmienionych plików z 42 dodań i 0 usunięć
  1. 33 0
      src/resource/mesh.cpp
  2. 9 0
      src/resource/mesh.h

+ 33 - 0
src/resource/mesh.cpp

@@ -62,6 +62,8 @@ namespace mesh
 		array::clear(g._uvs);
 		array::clear(g._uvs);
 		array::clear(g._tangents);
 		array::clear(g._tangents);
 		array::clear(g._bitangents);
 		array::clear(g._bitangents);
+		array::clear(g._bones);
+		array::clear(g._weights);
 
 
 		array::clear(g._position_indices);
 		array::clear(g._position_indices);
 		array::clear(g._normal_indices);
 		array::clear(g._normal_indices);
@@ -93,12 +95,18 @@ namespace mesh
 		return array::size(g._bitangents) != 0;
 		return array::size(g._bitangents) != 0;
 	}
 	}
 
 
+	bool has_bones(Geometry &g)
+	{
+		return array::size(g._bones) != 0;
+	}
+
 	static u32 vertex_stride(Geometry &g)
 	static u32 vertex_stride(Geometry &g)
 	{
 	{
 		u32 stride = 0;
 		u32 stride = 0;
 		stride += 3 * sizeof(f32);
 		stride += 3 * sizeof(f32);
 		stride += (has_normals(g) ? 3 * sizeof(f32) : 0);
 		stride += (has_normals(g) ? 3 * sizeof(f32) : 0);
 		stride += (has_uvs(g)     ? 2 * sizeof(f32) : 0);
 		stride += (has_uvs(g)     ? 2 * sizeof(f32) : 0);
+		stride += (has_bones(g)   ? 8 * sizeof(f32) : 0);
 		return stride;
 		return stride;
 	}
 	}
 
 
@@ -116,6 +124,10 @@ namespace mesh
 		if (has_uvs(g)) {
 		if (has_uvs(g)) {
 			layout.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
 			layout.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
 		}
 		}
+		if (has_bones(g)) {
+			layout.add(bgfx::Attrib::Indices, 4, bgfx::AttribType::Float);
+			layout.add(bgfx::Attrib::Weight, 4, bgfx::AttribType::Float);
+		}
 
 
 		layout.end();
 		layout.end();
 		return layout;
 		return layout;
@@ -151,6 +163,23 @@ namespace mesh
 				uv.y = g._uvs[t_idx + 1];
 				uv.y = g._uvs[t_idx + 1];
 				array::push(g._vertex_buffer, (char *)&uv, sizeof(uv));
 				array::push(g._vertex_buffer, (char *)&uv, sizeof(uv));
 			}
 			}
+			if (has_bones(g)) {
+				const u16 b_idx = g._bone_indices[i] * 4;
+				Vector4 b;
+				b.x = g._bones[b_idx + 0];
+				b.y = g._bones[b_idx + 1];
+				b.z = g._bones[b_idx + 2];
+				b.w = g._bones[b_idx + 3];
+				array::push(g._vertex_buffer, (char *)&b, sizeof(b));
+
+				Vector4 w;
+				const u16 w_idx = g._weight_indices[i] * 4;
+				w.x = g._weights[w_idx + 0];
+				w.y = g._weights[w_idx + 1];
+				w.z = g._weights[w_idx + 2];
+				w.w = g._weights[w_idx + 3];
+				array::push(g._vertex_buffer, (char *)&w, sizeof(w));
+			}
 		}
 		}
 	}
 	}
 
 
@@ -271,11 +300,15 @@ Geometry::Geometry(Allocator &a)
 	, _uvs(a)
 	, _uvs(a)
 	, _tangents(a)
 	, _tangents(a)
 	, _bitangents(a)
 	, _bitangents(a)
+	, _bones(a)
+	, _weights(a)
 	, _position_indices(a)
 	, _position_indices(a)
 	, _normal_indices(a)
 	, _normal_indices(a)
 	, _uv_indices(a)
 	, _uv_indices(a)
 	, _tangent_indices(a)
 	, _tangent_indices(a)
 	, _bitangent_indices(a)
 	, _bitangent_indices(a)
+	, _bone_indices(a)
+	, _weight_indices(a)
 	, _vertex_buffer(a)
 	, _vertex_buffer(a)
 	, _index_buffer(a)
 	, _index_buffer(a)
 {
 {

+ 9 - 0
src/resource/mesh.h

@@ -31,17 +31,23 @@ struct Geometry
 {
 {
 	ALLOCATOR_AWARE;
 	ALLOCATOR_AWARE;
 
 
+	enum { MAX_BONE_WEIGHTS = 4 };
+
 	Array<f32> _positions;
 	Array<f32> _positions;
 	Array<f32> _normals;
 	Array<f32> _normals;
 	Array<f32> _uvs;
 	Array<f32> _uvs;
 	Array<f32> _tangents;
 	Array<f32> _tangents;
 	Array<f32> _bitangents;
 	Array<f32> _bitangents;
+	Array<f32> _bones;
+	Array<f32> _weights;
 
 
 	Array<u32> _position_indices;
 	Array<u32> _position_indices;
 	Array<u32> _normal_indices;
 	Array<u32> _normal_indices;
 	Array<u32> _uv_indices;
 	Array<u32> _uv_indices;
 	Array<u32> _tangent_indices;
 	Array<u32> _tangent_indices;
 	Array<u32> _bitangent_indices;
 	Array<u32> _bitangent_indices;
+	Array<u32> _bone_indices;
+	Array<u32> _weight_indices;
 
 
 	Array<char> _vertex_buffer;
 	Array<char> _vertex_buffer;
 	Array<u16> _index_buffer;
 	Array<u16> _index_buffer;
@@ -73,6 +79,9 @@ namespace mesh
 	///
 	///
 	bool has_bitangents(Geometry &g);
 	bool has_bitangents(Geometry &g);
 
 
+	///
+	bool has_bones(Geometry &g);
+
 	///
 	///
 	s32 parse(Mesh &m, const char *path, CompileOptions &opts);
 	s32 parse(Mesh &m, const char *path, CompileOptions &opts);