SparrowRenderer/meshbuilder.cpp

168 lines
4.1 KiB
C++

#include "meshbuilder.h"
#include <glm/ext.hpp>
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(const glm::vec3 &position, const glm::vec3 &normal)
{
addPosition(position);
addNormal(normal);
}
void MeshBuilder::addVertex(const glm::vec3 &position, const glm::vec2 &texCoord)
{
addPosition(position);
addTexCoord(texCoord);
}
void MeshBuilder::addVertex(const glm::vec3 &position, const glm::vec3 &normal, const glm::vec2 &texCoord)
{
addVertex(position, normal);
addTexCoord(texCoord);
}
void MeshBuilder::addTriangle(int i1, int i2, int i3)
{
indiceGroups[currentGroup].indices.push_back(i1);
indiceGroups[currentGroup].indices.push_back(i2);
indiceGroups[currentGroup].indices.push_back(i3);
}
void MeshBuilder::addGroup(Material* myMaterial)
{
Group g;
g.material = myMaterial;
setCurrentGroup(getNbGroups());
indiceGroups.push_back(g);
}
void MeshBuilder::setCurrentGroup(int groupId)
{
currentGroup = groupId;
}
void MeshBuilder::setCurrentGroupMaterial(Material* myMaterial)
{
indiceGroups[currentGroup].material = myMaterial;
}
int MeshBuilder::getNbGroups()
{
return indiceGroups.size();
}
void MeshBuilder::computeNormals()
{
normals.resize(positions.size());
for(const Group &g : indiceGroups)
for (int i=0; i < g.indices.size(); i += 3)
{
int v0 = g.indices[i];
int v1 = g.indices[i+1];
int v2 = g.indices[i+2];
glm::vec3 n = glm::cross(positions[v1] - positions[v0], positions[v2] - positions[v0]);
normals[v0] += n;
normals[v1] += n;
normals[v2] += n;
}
for(glm::vec3 &n : normals)
n = glm::normalize(n);
}
void MeshBuilder::computeTangents()
{
if(!hasTexCoords())
return;
tangents = std::vector<Tangents>(positions.size());
for(const Group &g : indiceGroups)
for (int j=0; j < g.indices.size(); j += 3)
{
int vertexId0 = g.indices[j];
int vertexId1 = g.indices[j+1];
int vertexId2 = g.indices[j+2];
const glm::vec3 &v1 = positions[vertexId0];
const glm::vec3 &v2 = positions[vertexId1];
const glm::vec3 &v3 = positions[vertexId2];
const glm::vec2& w1 = texCoords[vertexId0];
const glm::vec2& w2 = texCoords[vertexId1];
const glm::vec2& w3 = texCoords[vertexId2];
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[vertexId0];
Tangents& tan2 = tangents[vertexId1];
Tangents& tan3 = tangents[vertexId2];
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;
}*/
}