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 229 230 231
|
#pragma once
#include <cstddef>
#include "math/Vector3.h"
#include "math/Vector4.h"
#include "Vertex3f.h"
#include "TexCoord2f.h"
#include "VertexTraits.h"
/**
* Data structure representing a mesh vertex.
*/
class MeshVertex
{
public:
TexCoord2f texcoord;
Normal3 normal;
Vertex3 vertex;
Normal3 tangent;
Normal3 bitangent;
// Vertex colour
Vector4 colour;
/// Default constructor.
MeshVertex()
: tangent(0, 0, 0),
bitangent(0, 0, 0),
colour(1.0, 1.0, 1.0, 1.0)
{}
/// Initialising constructor, leaves colour at 1,1,1,1 and tangent vectors at 0,0,0
MeshVertex(const Vertex3& v, const Normal3& n, const TexCoord2f& t) :
MeshVertex(v, n, t, { 1.0, 1.0, 1.0, 1.0 })
{}
/// Initialising constructor, leaves tangent vectors at 0,0,0
MeshVertex(const Vertex3& v, const Normal3& n, const TexCoord2f& t, const Vector4& c) :
MeshVertex(v, n, t, c, { 0, 0, 0 }, { 0, 0, 0 })
{}
// Initialises all attributes of this vertex
MeshVertex(const Vertex3& vertex_, const Normal3& normal_,
const TexCoord2f& texcoord_, const Vector4& colour_,
const Normal3& tangent_, const Normal3& bitangent_) :
texcoord(texcoord_),
normal(normal_),
vertex(vertex_),
tangent(tangent_),
bitangent(bitangent_),
colour(colour_)
{}
/// Cast to simple Vertex3, throwing away other components
operator Vertex3() const
{
return vertex;
}
};
/// Less-than comparison for MeshVertex
inline bool operator<(const MeshVertex& first,
const MeshVertex& 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 MeshVertex
inline bool operator==(const MeshVertex& first,
const MeshVertex& other)
{
return first.texcoord == other.texcoord
&& first.normal == other.normal
&& first.vertex == other.vertex;
}
/// Inequality comparison for MeshVertex
inline bool operator!=(const MeshVertex& first,
const MeshVertex& other)
{
return !(first == other);
}
namespace render
{
/// VertexTraits specialisation for MeshVertex
template<> class VertexTraits<MeshVertex>
{
public:
static const void* VERTEX_OFFSET()
{
return reinterpret_cast<const void*>(
offsetof(MeshVertex, vertex)
);
}
static bool hasNormal() { return true; }
static const void* NORMAL_OFFSET()
{
return reinterpret_cast<const void*>(
offsetof(MeshVertex, normal)
);
}
static bool hasTexCoord() { return true; }
static const void* TEXCOORD_OFFSET()
{
return reinterpret_cast<const void*>(
offsetof(MeshVertex, texcoord)
);
}
static bool hasTangents() { return true; }
static const void* TANGENT_OFFSET()
{
return reinterpret_cast<const void*>(
offsetof(MeshVertex, tangent)
);
}
static const void* BITANGENT_OFFSET()
{
return reinterpret_cast<const void*>(
offsetof(MeshVertex, bitangent)
);
}
};
}
/**
* String output for MeshVertex.
*/
inline std::ostream& operator<< (std::ostream& os, const MeshVertex& v)
{
os << "MeshVertex { "
<< " vertex = " << v.vertex << ", normal = " << v.normal
<< ", texcoord = " << v.texcoord << ", colour = " << v.colour
<< " }";
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 MeshTriangle_calcTangents(const MeshVertex& a,
const MeshVertex& b, const MeshVertex& 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).cross(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).cross(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).cross(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 MeshTriangle_sumTangents(MeshVertex& a, MeshVertex& b, MeshVertex& c)
{
Vector3 s, t;
MeshTriangle_calcTangents(a, b, c, s, t);
a.tangent += s;
b.tangent += s;
c.tangent += s;
a.bitangent += t;
b.bitangent += t;
c.bitangent += t;
}
|