124 lines
2.9 KiB
C++
124 lines
2.9 KiB
C++
#include "meshbuilder.h"
|
|
|
|
void MeshBuilder::addPosition(float x, float y, float z)
|
|
{
|
|
addPosition(glm::vec3(x, y, z));
|
|
}
|
|
|
|
void MeshBuilder::addPosition(const glm::vec3 &position)
|
|
{
|
|
positions.push_back(position);
|
|
}
|
|
|
|
void MeshBuilder::addNormal(float x, float y, float z)
|
|
{
|
|
addNormal(glm::vec3(x, y, z));
|
|
}
|
|
|
|
void MeshBuilder::addNormal(const glm::vec3 &normal)
|
|
{
|
|
normals.push_back(normal);
|
|
}
|
|
|
|
void MeshBuilder::addTexCoord(float u, float v)
|
|
{
|
|
addTexCoord(glm::vec2(u, v));
|
|
}
|
|
|
|
void MeshBuilder::addTexCoord(const glm::vec2 &texCoord)
|
|
{
|
|
texCoords.push_back(texCoord);
|
|
}
|
|
|
|
|
|
void MeshBuilder::addVertex(glm::vec3 &position, glm::vec3 &normal)
|
|
{
|
|
addPosition(position);
|
|
addNormal(normal);
|
|
}
|
|
|
|
void MeshBuilder::addVertex(glm::vec3 &position, glm::vec2 &texCoord)
|
|
{
|
|
addPosition(position);
|
|
addTexCoord(texCoord);
|
|
}
|
|
|
|
void MeshBuilder::addVertex(glm::vec3 &position, glm::vec3 &normal, glm::vec2 &texCoord)
|
|
{
|
|
addVertex(position, normal);
|
|
addTexCoord(texCoord);
|
|
}
|
|
|
|
|
|
void MeshBuilder::addTriangle(int i1, int i2, int i3)
|
|
{
|
|
if(!locked)
|
|
{
|
|
indices.push_back(i1);
|
|
indices.push_back(i2);
|
|
indices.push_back(i3);
|
|
}
|
|
}
|
|
|
|
void MeshBuilder::computeTangents()
|
|
{
|
|
if(!hasTexCoords())
|
|
return;
|
|
tangents = std::vector<Tangents>(positions.size());
|
|
|
|
for (int i=0; i < indices.size(); i += 3)
|
|
{
|
|
const glm::vec3 &v1 = positions[i];
|
|
const glm::vec3 &v2 = positions[i+1];
|
|
const glm::vec3 &v3 = positions[i+2];
|
|
|
|
const glm::vec2& w1 = texCoords[i];
|
|
const glm::vec2& w2 = texCoords[i+1];
|
|
const glm::vec2& w3 = texCoords[i+2];
|
|
|
|
float x1 = v2.x - v1.x;
|
|
float x2 = v3.x - v1.x;
|
|
float y1 = v2.y - v1.y;
|
|
float y2 = v3.y - v1.y;
|
|
float z1 = v2.z - v1.z;
|
|
float z2 = v3.z - v1.z;
|
|
|
|
float s1 = w2.x - w1.x;
|
|
float s2 = w3.x - w1.x;
|
|
float t1 = w2.y - w1.y;
|
|
float t2 = w3.y - w1.y;
|
|
|
|
float r = 1.0f / (s1 * t2 - s2 * t1);
|
|
glm::vec3 sdir((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r,
|
|
(t2 * z1 - t1 * z2) * r);
|
|
glm::vec3 tdir((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r,
|
|
(s1 * z2 - s2 * z1) * r);
|
|
|
|
Tangents& tan1 = tangents[i];
|
|
Tangents& tan2 = tangents[i+1];
|
|
Tangents& tan3 = tangents[i+2];
|
|
|
|
tan1.tangent += sdir;
|
|
tan2.tangent += sdir;
|
|
tan3.tangent += sdir;
|
|
|
|
tan1.binormal += tdir;
|
|
tan2.binormal += tdir;
|
|
tan3.binormal += tdir;
|
|
}
|
|
|
|
// building tangent matrix
|
|
/*
|
|
for (long a = 0; a < vertexCount; a++)
|
|
{
|
|
const Vector3D& n = normal[a];
|
|
const Vector3D& t = tan1[a];
|
|
|
|
// Gram-Schmidt orthogonalize
|
|
tangent[a] = (t - n * Dot(n, t)).Normalize();
|
|
|
|
// Calculate handedness
|
|
tangent[a].w = (Dot(Cross(n, t), tan2[a]) < 0.0F) ? -1.0F : 1.0F;
|
|
}*/
|
|
}
|