void compute_smooth_vertex_tangents_pose(MeshObject& object, const size_t motion_segment_index) { assert(object.get_tex_coords_count() > 0); const size_t vertex_count = object.get_vertex_count(); const size_t triangle_count = object.get_triangle_count(); vector<GVector3> tangents(vertex_count, GVector3(0.0)); for (size_t i = 0; i < triangle_count; ++i) { const Triangle& triangle = object.get_triangle(i); if (!triangle.has_vertex_attributes()) continue; const GVector2 v0_uv = object.get_tex_coords(triangle.m_a0); const GVector2 v1_uv = object.get_tex_coords(triangle.m_a1); const GVector2 v2_uv = object.get_tex_coords(triangle.m_a2); // // Reference: // // Physically Based Rendering, first edition, pp. 128-129 // const GScalar du0 = v0_uv[0] - v2_uv[0]; const GScalar dv0 = v0_uv[1] - v2_uv[1]; const GScalar du1 = v1_uv[0] - v2_uv[0]; const GScalar dv1 = v1_uv[1] - v2_uv[1]; const GScalar det = du0 * dv1 - dv0 * du1; if (det == GScalar(0.0)) continue; const GVector3& v2 = object.get_vertex_pose(triangle.m_v2, motion_segment_index); const GVector3 dp0 = object.get_vertex_pose(triangle.m_v0, motion_segment_index) - v2; const GVector3 dp1 = object.get_vertex_pose(triangle.m_v1, motion_segment_index) - v2; const GVector3 tangent = normalize(dv1 * dp0 - dv0 * dp1); tangents[triangle.m_v0] += tangent; tangents[triangle.m_v1] += tangent; tangents[triangle.m_v2] += tangent; } for (size_t i = 0; i < vertex_count; ++i) object.set_vertex_tangent_pose(i, motion_segment_index, safe_normalize(tangents[i])); }
void compute_smooth_vertex_normals_pose(MeshObject& object, const size_t motion_segment_index) { const size_t vertex_count = object.get_vertex_count(); const size_t triangle_count = object.get_triangle_count(); vector<GVector3> normals(vertex_count, GVector3(0.0)); for (size_t i = 0; i < triangle_count; ++i) { const Triangle& triangle = object.get_triangle(i); const GVector3& v0 = object.get_vertex_pose(triangle.m_v0, motion_segment_index); const GVector3& v1 = object.get_vertex_pose(triangle.m_v1, motion_segment_index); const GVector3& v2 = object.get_vertex_pose(triangle.m_v2, motion_segment_index); const GVector3 normal = normalize(cross(v1 - v0, v2 - v0)); normals[triangle.m_v0] += normal; normals[triangle.m_v1] += normal; normals[triangle.m_v2] += normal; } for (size_t i = 0; i < vertex_count; ++i) object.set_vertex_normal_pose(i, motion_segment_index, safe_normalize(normals[i])); }