| 1061 | } |
| 1062 | |
| 1063 | static void GenerateNormalData(const dmRigDDF::Mesh* mesh, const Matrix4& normal_matrix, const dmArray<Matrix4>& pose_matrices, float* normals_buffer, float* tangents_buffer) |
| 1064 | { |
| 1065 | const float* normals_in = mesh->m_Normals.m_Data; |
| 1066 | bool has_tangents = mesh->m_Tangents.m_Count > 0; |
| 1067 | const float* tangents_in = has_tangents ? mesh->m_Tangents.m_Data : 0; |
| 1068 | const uint32_t vertex_count = mesh->m_Positions.m_Count / 3; |
| 1069 | Vector4 normal; |
| 1070 | Vector4 tangent; |
| 1071 | |
| 1072 | // Non skinned data |
| 1073 | if (!mesh->m_BoneIndices.m_Count || pose_matrices.Size() == 0) |
| 1074 | { |
| 1075 | for (uint32_t i = 0; i < vertex_count; ++i) |
| 1076 | { |
| 1077 | Vector3 normal_in(normals_in[i*3+0], normals_in[i*3+1], normals_in[i*3+2]); |
| 1078 | normal = normal_matrix * normal_in; |
| 1079 | if (lengthSqr(normal) > 0.0f) { |
| 1080 | normalize(normal); |
| 1081 | } |
| 1082 | |
| 1083 | *normals_buffer++ = normal[0]; |
| 1084 | *normals_buffer++ = normal[1]; |
| 1085 | *normals_buffer++ = normal[2]; |
| 1086 | |
| 1087 | if (has_tangents) |
| 1088 | { |
| 1089 | Vector3 tangent_in(tangents_in[i*4+0], tangents_in[i*4+1], tangents_in[i*4+2]); |
| 1090 | float tangent_handedness = tangents_in[i*4+3]; |
| 1091 | tangent = normal_matrix * tangent_in; |
| 1092 | if (lengthSqr(tangent) > 0.0f) { |
| 1093 | normalize(tangent); |
| 1094 | } |
| 1095 | *tangents_buffer++ = tangent[0]; |
| 1096 | *tangents_buffer++ = tangent[1]; |
| 1097 | *tangents_buffer++ = tangent[2]; |
| 1098 | *tangents_buffer++ = tangent_handedness; |
| 1099 | } |
| 1100 | } |
| 1101 | return; |
| 1102 | } |
| 1103 | |
| 1104 | // Skinned data |
| 1105 | const uint32_t* indices = mesh->m_BoneIndices.m_Data; |
| 1106 | const float* weights = mesh->m_Weights.m_Data; |
| 1107 | for (uint32_t i = 0; i < vertex_count; ++i) |
| 1108 | { |
| 1109 | const Vector3 normal_in(normals_in[i*3+0], normals_in[i*3+1], normals_in[i*3+2]); |
| 1110 | Vector4 normal_out(0.0f, 0.0f, 0.0f, 0.0f); |
| 1111 | |
| 1112 | const Vector3 tangent_in = has_tangents ? Vector3(tangents_in[i*4+0], tangents_in[i*4+1], tangents_in[i*4+2]) : Vector3(0,0,0); |
| 1113 | const float tangent_handedness = has_tangents ? tangents_in[i*4+3] : 0.0f; |
| 1114 | Vector4 tangent_out(0.0f, 0.0f, 0.0f, 0.0f); |
| 1115 | |
| 1116 | const uint32_t bi_offset = i * 4; |
| 1117 | const uint32_t* bone_indices = &indices[bi_offset]; |
| 1118 | const float* bone_weights = &weights[bi_offset]; |
| 1119 | |
| 1120 | if (bone_weights[0]) |