/
ArbitraryMeshVertex.h
228 lines (189 loc) · 5.3 KB
/
ArbitraryMeshVertex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#pragma once
#include <cstddef>
#include "math/Vector3.h"
#include "Vertex3f.h"
#include "TexCoord2f.h"
#include "VertexTraits.h"
/**
* Data structure representing a mesh vertex.
*/
class ArbitraryMeshVertex
{
public:
TexCoord2f texcoord;
Normal3f normal;
Vertex3f vertex;
Normal3f tangent;
Normal3f bitangent;
// Vertex colour
Vector3 colour;
/// Default constructor.
ArbitraryMeshVertex()
: tangent(0, 0, 0),
bitangent(0, 0, 0),
colour(1.0, 1.0, 1.0)
{}
/// Initialising constructor, leaves colour at 1,1,1 and tangent vectors at 0,0,0
ArbitraryMeshVertex(const Vertex3f& v, const Normal3f& n, const TexCoord2f& t) :
texcoord(t),
normal(n),
vertex(v),
tangent(0, 0, 0),
bitangent(0, 0, 0),
colour(1.0, 1.0, 1.0)
{}
/// Initialising constructor, leaves tangent vectors at 0,0,0
ArbitraryMeshVertex(const Vertex3f& v, const Normal3f& n, const TexCoord2f& t, const Vector3& c) :
texcoord(t),
normal(n),
vertex(v),
tangent(0, 0, 0),
bitangent(0, 0, 0),
colour(c)
{}
/// Cast to simple Vertex3f, throwing away other components
operator Vertex3f() const
{
return vertex;
}
};
/// Less-than comparison for ArbitraryMeshVertex
inline bool operator<(const ArbitraryMeshVertex& first,
const ArbitraryMeshVertex& other)
{
if (first.texcoord != other.texcoord)
{
return first.texcoord < other.texcoord;
}
if (first.normal != other.normal)
{
return first.normal < other.normal;
}
if (first.vertex != other.vertex)
{
return first.vertex < other.vertex;
}
return false;
}
/// Equality comparison for ArbitraryMeshVertex
inline bool operator==(const ArbitraryMeshVertex& first,
const ArbitraryMeshVertex& other)
{
return first.texcoord == other.texcoord
&& first.normal == other.normal
&& first.vertex == other.vertex;
}
/// Inequality comparison for ArbitraryMeshVertex
inline bool operator!=(const ArbitraryMeshVertex& first,
const ArbitraryMeshVertex& other)
{
return !(first == other);
}
namespace render
{
/// VertexTraits specialisation for ArbitraryMeshVertex
template<> class VertexTraits<ArbitraryMeshVertex>
{
public:
static const void* VERTEX_OFFSET()
{
return reinterpret_cast<const void*>(
offsetof(ArbitraryMeshVertex, vertex)
);
}
static bool hasNormal() { return true; }
static const void* NORMAL_OFFSET()
{
return reinterpret_cast<const void*>(
offsetof(ArbitraryMeshVertex, normal)
);
}
static bool hasTexCoord() { return true; }
static const void* TEXCOORD_OFFSET()
{
return reinterpret_cast<const void*>(
offsetof(ArbitraryMeshVertex, texcoord)
);
}
static bool hasTangents() { return true; }
static const void* TANGENT_OFFSET()
{
return reinterpret_cast<const void*>(
offsetof(ArbitraryMeshVertex, tangent)
);
}
static const void* BITANGENT_OFFSET()
{
return reinterpret_cast<const void*>(
offsetof(ArbitraryMeshVertex, bitangent)
);
}
};
}
/**
* String output for ArbitraryMeshVertex.
*/
inline std::ostream& operator<< (std::ostream& os, const ArbitraryMeshVertex& v)
{
os << "ArbitraryMeshVertex { "
<< " vertex = " << v.vertex << ", normal = " << v.normal
<< ", texcoord = " << v.texcoord
<< " }";
return os;
}
/// \brief Calculates the tangent vectors for a triangle \p a, \p b, \p c and stores the tangent in \p s and the bitangent in \p t.
inline void ArbitraryMeshTriangle_calcTangents(const ArbitraryMeshVertex& a,
const ArbitraryMeshVertex& b, const ArbitraryMeshVertex& c,
Vector3& s, Vector3& t)
{
s = Vector3(0, 0, 0);
t = Vector3(0, 0, 0);
Vector3 aVec, bVec, cVec;
{
aVec.set(a.vertex.x(), a.texcoord.s(), a.texcoord.t());
bVec.set(b.vertex.x(), b.texcoord.s(), b.texcoord.t());
cVec.set(c.vertex.x(), c.texcoord.s(), c.texcoord.t());
Vector3 cross( (bVec-aVec).crossProduct(cVec-aVec) );
if(fabs(cross.x()) > 0.000001f) {
s.x() = -cross.y() / cross.x();
}
if(fabs(cross.x()) > 0.000001f) {
t.x() = -cross.z() / cross.x();
}
}
{
aVec.set(a.vertex.y(), a.texcoord.s(), a.texcoord.t());
bVec.set(b.vertex.y(), b.texcoord.s(), b.texcoord.t());
cVec.set(c.vertex.y(), c.texcoord.s(), c.texcoord.t());
Vector3 cross( (bVec-aVec).crossProduct(cVec-aVec));
if(fabs(cross.x()) > 0.000001f) {
s.y() = -cross.y() / cross.x();
}
if(fabs(cross.x()) > 0.000001f) {
t.y() = -cross.z() / cross.x();
}
}
{
aVec.set(a.vertex.z(), a.texcoord.s(), a.texcoord.t());
bVec.set(b.vertex.z(), b.texcoord.s(), b.texcoord.t());
cVec.set(c.vertex.z(), c.texcoord.s(), c.texcoord.t());
Vector3 cross( (bVec-aVec).crossProduct(cVec-aVec));
if(fabs(cross.x()) > 0.000001f) {
s.z() = -cross.y() / cross.x();
}
if(fabs(cross.x()) > 0.000001f) {
t.z() = -cross.z() / cross.x();
}
}
}
inline void ArbitraryMeshTriangle_sumTangents(ArbitraryMeshVertex& a, ArbitraryMeshVertex& b, ArbitraryMeshVertex& c)
{
Vector3 s, t;
ArbitraryMeshTriangle_calcTangents(a, b, c, s, t);
a.tangent += s;
b.tangent += s;
c.tangent += s;
a.bitangent += t;
b.bitangent += t;
c.bitangent += t;
}