Просмотр исходного кода

resource: read tangent/bitangent vertex attributes

Daniele Bartolini 11 месяцев назад
Родитель
Сommit
92a0e03a72
3 измененных файлов с 50 добавлено и 2 удалено
  1. 1 0
      docs/changelog.rst
  2. 28 0
      src/resource/mesh.cpp
  3. 21 2
      src/resource/mesh_resource.cpp

+ 1 - 0
docs/changelog.rst

@@ -20,6 +20,7 @@ Changelog
 * Added support to shaders and materials hot-reloading.
 * Increased the maximum number of lines that can be drawn with DebugLine.
 * Increased the maximum number of texture samplers per shader to 16.
+* Added tangent/bitangent vertex attributes support in .mesh resources.
 * Fixed a crash when moving many objects simultaneusly.
 * Fixed a crash when reloading unloaded or unsupported resources.
 * Fixed setting kinematic actor's position and rotation.

+ 28 - 0
src/resource/mesh.cpp

@@ -107,6 +107,8 @@ namespace mesh
 		u32 stride = 0;
 		stride += 3 * sizeof(f32);
 		stride += has_normals(g) ? 3*sizeof(f32) : 0;
+		stride += has_tangents(g) ? 3*sizeof(f32) : 0;
+		stride += has_bitangents(g) ? 3*sizeof(f32) : 0;
 		stride += has_bones(g) ? 8*sizeof(f32) : 0;
 		stride += has_uvs(g) ? 2*sizeof(f32) : 0;
 		return stride;
@@ -123,6 +125,12 @@ namespace mesh
 		if (has_normals(g))
 			layout.add(bgfx::Attrib::Normal, 3, bgfx::AttribType::Float, true);
 
+		if (has_tangents(g))
+			layout.add(bgfx::Attrib::Tangent, 3, bgfx::AttribType::Float, true);
+
+		if (has_bitangents(g))
+			layout.add(bgfx::Attrib::Bitangent, 3, bgfx::AttribType::Float, true);
+
 		if (has_bones(g)) {
 			layout.add(bgfx::Attrib::Indices, 4, bgfx::AttribType::Float);
 			layout.add(bgfx::Attrib::Weight, 4, bgfx::AttribType::Float);
@@ -159,6 +167,26 @@ namespace mesh
 				array::push(g._vertex_buffer, (char *)&v, sizeof(v));
 			}
 
+			if (has_tangents(g)) {
+				const u16 idx = g._tangent_indices[i] * 3;
+				Vector3 v;
+				CE_ENSURE(idx < array::size(g._tangents));
+				v.x = g._tangents[idx + 0];
+				v.y = g._tangents[idx + 1];
+				v.z = g._tangents[idx + 2];
+				array::push(g._vertex_buffer, (char *)&v, sizeof(v));
+			}
+
+			if (has_bitangents(g)) {
+				const u16 idx = g._bitangent_indices[i] * 3;
+				CE_ENSURE(idx < array::size(g._bitangents));
+				Vector3 v;
+				v.x = g._bitangents[idx + 0];
+				v.y = g._bitangents[idx + 1];
+				v.z = g._bitangents[idx + 2];
+				array::push(g._vertex_buffer, (char *)&v, sizeof(v));
+			}
+
 			if (has_bones(g)) {
 				const u16 bidx = g._bone_indices[i] * 4;
 				Vector4 b;

+ 21 - 2
src/resource/mesh_resource.cpp

@@ -246,11 +246,19 @@ namespace mesh
 
 		parse_index_array(g._position_indices, data_json[0], opts);
 
+		u32 idx = 1;
+
 		if (has_normals(g))
-			parse_index_array(g._normal_indices, data_json[1], opts);
+			parse_index_array(g._normal_indices, data_json[idx++], opts);
 
 		if (has_uvs(g))
-			parse_index_array(g._uv_indices, data_json[2], opts);
+			parse_index_array(g._uv_indices, data_json[idx++], opts);
+
+		if (has_tangents(g))
+			parse_index_array(g._tangent_indices, data_json[idx++], opts);
+
+		if (has_bitangents(g))
+			parse_index_array(g._bitangent_indices, data_json[idx++], opts);
 
 		return 0;
 	}
@@ -268,6 +276,17 @@ namespace mesh
 			err = parse_float_array(g._normals, obj["normal"], opts);
 			ENSURE_OR_RETURN(err == 0, opts);
 		}
+
+		if (json_object::has(obj, "tangent")) {
+			err = parse_float_array(g._tangents, obj["tangent"], opts);
+			ENSURE_OR_RETURN(err == 0, opts);
+		}
+
+		if (json_object::has(obj, "bitangent")) {
+			err = parse_float_array(g._bitangents, obj["bitangent"], opts);
+			ENSURE_OR_RETURN(err == 0, opts);
+		}
+
 		if (json_object::has(obj, "texcoord")) {
 			err = parse_float_array(g._uvs, obj["texcoord"], opts);
 			ENSURE_OR_RETURN(err == 0, opts);