sculpto
trimesh.cpp
Go to the documentation of this file.
1/*!****************************************************************//*!*
2 * \file trimesh.cpp
3 * \brief Topology object triangles mesh class declaration module.
4 *
5 * \author Sabitov Kirill
6 * \date 08 July 2022
7 *********************************************************************/
8
9#include "sclpch.h"
10#include "trimesh.h"
11
12scl::topology::trimesh::trimesh(const std::vector<vertex> &Vertices, const std::vector<u32> &Indieces) :
13 basis(mesh_type::TRIANGLES, Vertices, Indices)
14{
15}
16
17scl::topology::trimesh::trimesh(const std::vector<vertex> &&Vertices, const std::vector<u32> &&Indieces) :
18 basis(mesh_type::TRIANGLES, Vertices, Indices)
19{
20}
21
23{
24 vec3 min = Vertices[0].Position;
25 vec3 max = Vertices[0].Position;
26
27 for (INT i = 1; i < Vertices.size(); i++)
28 min = vec3::Min(min, Vertices[i].Position),
29 max = vec3::Max(max, Vertices[i].Position);
30 Min = min, Max = max;
31}
32
34{
35 for (INT i = 0; i < Indices.size(); i += 3)
36 {
37 vec3 A = Vertices[Indices[i + 1]].Position - Vertices[Indices[i]].Position;
38 vec3 B = Vertices[Indices[i + 2]].Position - Vertices[Indices[i]].Position;
39 vec3 N = A.Cross(B).Normalized();
40
41 Vertices[Indices[i + 0]].Normal = vec3(Vertices[Indices[i + 0]].Normal + N).Normalized();
42 Vertices[Indices[i + 1]].Normal = vec3(Vertices[Indices[i + 1]].Normal + N).Normalized();
43 Vertices[Indices[i + 2]].Normal = vec3(Vertices[Indices[i + 2]].Normal + N).Normalized();
44 }
45}
46
48{
49 bool is_ok = true;
50 for (INT i = 0; i < Indices.size() && is_ok; i += 3)
51 is_ok = EvaluateTriangleTangentSpace(Vertices[Indices[i + 0]],
52 Vertices[Indices[i + 1]],
53 Vertices[Indices[i + 2]]);
54 if (is_ok)
55 for (auto &V : Vertices) EvaluateVertexOrthoganalTBNBasis(V);
56 else
57 for (auto &V : Vertices)
58 {
59 vec3 N = V.Normal;
60
61 if (N.Z > N.X && N.Z > N.Y || N.Y > N.X && N.Y > N.Z)
63 V.Tangent = vec3(1, 0, 0);
64 else
66 V.Tangent = vec3(0, 1, 0);
67 V.Bitangent = N.Cross(V.Tangent).Normalized();
68 V.Tangent = V.Bitangent.Cross(N).Normalized();
69 }
70}
71
73{
74 float s1 = P1.TexCoords.X - P0.TexCoords.X;
75 float s2 = P2.TexCoords.X - P0.TexCoords.X;
76 float t1 = P1.TexCoords.Y - P0.TexCoords.Y;
77 float t2 = P2.TexCoords.Y - P0.TexCoords.Y;
78 float det = s1 * t2 - s2 * t1;
79
80 if (det != 0)
81 {
82 vec3 E1 = P1.Position - P0.Position;
83 vec3 E2 = P2.Position - P0.Position;
84 vec3 T = ((E1 * t2 - E2 * t1) / det).Normalized();
85 vec3 B = ((E2 * s1 - E1 * s2) / det).Normalized();
86
87 P0.Tangent += T; P0.Bitangent += B;
88 P1.Tangent += T; P1.Bitangent += B;
89 P2.Tangent += T; P2.Bitangent += B;
90 return true;
91 }
92 return false;
93}
94
96{
97 P.Normal.Normalize(), P.Tangent.Normalize(), P.Bitangent.Normalize();
98
99 P.Tangent = (P.Tangent - P.Normal * P.Normal.Dot(P.Tangent)).Normalized();
100 P.Bitangent = (P.Bitangent - P.Normal * P.Normal.Dot(P.Bitangent) - P.Tangent * P.Tangent.Dot(P.Bitangent)).Normalized();
101}
vec3 Normalized() const
Definition: vec3.h:190
static vec3 Max(const vec3 &A, const vec3 &B)
Definition: vec3.h:148
static vec3 Min(const vec3 &A, const vec3 &B)
Definition: vec3.h:140
virtual void EvaluateBoundBox()
Definition: trimesh.cpp:22
virtual void EvaluateNormals()
Definition: trimesh.cpp:33
virtual void EvaluateTangentSpace()
Definition: trimesh.cpp:47
static bool EvaluateTriangleTangentSpace(vertex &P0, vertex &P1, vertex &P2)
Definition: trimesh.cpp:72
static void EvaluateVertexOrthoganalTBNBasis(vertex &P)
Definition: trimesh.cpp:95
T Max(T Num1, T Num2)
Definition: math_common.h:61
T Min(T Num1, T Num2)
Definition: math_common.h:49
mesh_type
Definition: vertex_array.h:18
math::vec3< float > vec3
Definition: base.h:38
Sculpto library prehompiled header. Defines common definitions, includes commonly used modules.
vec3 Bitangent
Definition: vertex.h:85
vec3 Position
Definition: vertex.h:82
vec2 TexCoords
Definition: vertex.h:86
vec3 Tangent
Definition: vertex.h:84
Topology object triangles mesh class declaration module.