Maillage ok, normales ok ?, camera sped up, finir 2D

This commit is contained in:
ao 2018-10-10 02:36:45 +02:00
parent 6c39ee710a
commit 4b46f00150
3 changed files with 131 additions and 17 deletions

View File

@ -15,9 +15,14 @@ BSpline::BSpline(
switch (typeSpline) {
case ESpline::SPLINE1D:
assert(k <= nbPtsCtrlX);
assert(typePolygone != EPolygone::GAUSSIEN3D);
assert(typePolygone != EPolygone::FIXE3D);
assert(typePolygone != EPolygone::FIXE3D);
break;
case ESpline::SPLINE2D:
assert(k <= nbPtsCtrlX && k <= nbPtsCtrlZ);
assert(typePolygone != EPolygone::FIXE2D);
assert(typePolygone != EPolygone::RANDOM2D);
break;
}
@ -32,6 +37,8 @@ BSpline::BSpline(
// Calcul du vecteur nodal de nbPtsCtrl valeurs
construireVecteurNodal(typeVecteur);
// Appel de la construction de la B-Spline
switch (typeSpline) {
case ESpline::SPLINE1D:
calculerSpline1D();
@ -94,6 +101,113 @@ void BSpline::calculerSpline1D()
void BSpline::calculerSpline2D()
{
// Init
int dec = 0, m = _k - 1;
uint i = 0;
double u = 0;
std::vector<glm::vec3> vecteursPointsControle (_nbPtsCtrlX);
//Parcours de u
for(u = _vecteurNodal[m]; u <= _vecteurNodal[_nbPtsCtrlX]; u += _stepX){
for(v = _vecteurNodal[m]; v <= _vecteurNodal[_nbPtsCtrlZ]; v += _stepZ){
// Évaluation de p(u, v), revient à évaluer la génératrice sur les directrices
// Vecteur de nbPtsCtrlZ directrices
dec = 0;
// Calcul du décalage
for(int i = _k; u > _vecteurNodal[i]; ++i)
++dec;
// Récupération des k points de contrôles nécessaires à la floraison
for(int i = 0; i < _k; i++){
vecteursPointsControle[i] = _pointsDeControle[dec + i];
}
// Appel de la floraison récursive
glm::vec3 newVertex = floraison(u, dec, _k, vecteursPointsControle);
// Sauvegardes des paramètres de la B-spline
_vertices.insert(_vertices.end(), {newVertex.x, newVertex.y, newVertex.z});
_normals.insert(_normals.end(), {1, -1, -1});
_indices.insert(_indices.end(), {i, i, ++i});
}
}
// Maillage
for(unsigned int i = 0; i < _pointsDeControle.size(); ++i) {
// Indices
int x = i / nbPtsMaillageZ;
int z = i % nbPtsMaillageX;
// Sommets
uint sommetA = x * nbPtsMaillageX + z;
uint sommetB = x * nbPtsMaillageX + z+1;
uint sommetC = (x+1) * nbPtsMaillageX + z;
uint sommetD = (x+1) * nbPtsMaillageX + z+1;
// Triangles
if (x != nbPtsMaillageX - 1 && z != nbPtsMaillageZ - 1) {
// Faces
_indices.insert(_indices.end(), {sommetA, sommetD, sommetB}); // Triangle 1
_indices.insert(_indices.end(), {sommetA, sommetC, sommetD}); // Triangle 2
}
}
// Normales
_normals.assign(_vertices.size(), 0.0f);
for(int i = 0; i < _indices.size(); i+=3) {
// Récupération des sommets
uint indiceSommetA = _indices[i];
uint indiceSommetB = _indices[i+1];
uint indiceSommetC = _indices[i+2];
glm::vec3 sommetA = glm::vec3(_vertices[indiceSommetA*3], _vertices[indiceSommetA*3+1], _vertices[indiceSommetA*3+2]);
glm::vec3 sommetB = glm::vec3(_vertices[indiceSommetB*3], _vertices[indiceSommetB*3+1], _vertices[indiceSommetB*3+2]);
glm::vec3 sommetC = glm::vec3(_vertices[indiceSommetC*3], _vertices[indiceSommetC*3+1], _vertices[indiceSommetC*3+2]);
// Calcul de la normale d'un triangle TODO: vérifier orientation normale
glm::vec3 normale = glm::cross(sommetB - sommetA, sommetC - sommetA);
// Ajout de la normale de la face aux normales des sommets participants
_normals[indiceSommetA*3] += normale.x; _normals[indiceSommetA*3+1] += normale.y; _normals[indiceSommetA*3+2] += normale.z;
_normals[indiceSommetB*3] += normale.x; _normals[indiceSommetB*3+1] += normale.y; _normals[indiceSommetB*3+2] += normale.z;
_normals[indiceSommetC*3] += normale.x; _normals[indiceSommetC*3+1] += normale.y; _normals[indiceSommetC*3+2] += normale.z;
}
// Normalisation des normales en chaque sommet
for(int i = 0; i < _normals.size(); i+=3) {
glm::vec3 normale = glm::normalize(glm::vec3(_normals[i*3], _normals[i*3+1], _normals[i*3+2]));
_normals[i*3] = normale.x;
_normals[i*3+1] = normale.y;
_normals[i*3+2] = normale.z;
}
// Ajouter le polygone de controle aux vecteurs
int offset = static_cast<unsigned int>(_vertices.size())/3;
std::cout << _pointsDeControle.size() << std::endl;
for(unsigned int i = 0; i < _pointsDeControle.size(); ++i) {
// Indices
int x = i / _nbPtsCtrlZ;
int z = i % _nbPtsCtrlX;
std::cout << "z: " << z << "; x: " << x << std::endl;
// Sommets
uint sommetA = x * _nbPtsCtrlX + z + offset;
uint sommetB = x * _nbPtsCtrlX + z+1 + offset;
uint sommetC = (x+1) * _nbPtsCtrlX + z + offset;
uint sommetD = (x+1) * _nbPtsCtrlX + z+1 + offset;
// Triangles
if (x != _nbPtsCtrlX - 1 && z != _nbPtsCtrlZ - 1) {
// Faces
/*
_indices.insert(_indices.end(), {sommetA, sommetD, sommetB}); // Triangle 1
_indices.insert(_indices.end(), {sommetA, sommetC, sommetD}); // Triangle 2
*/
}
// Arêtes
if (x != _nbPtsCtrlX - 1)
_indices.insert(_indices.end(), {sommetA, sommetC, sommetA}); // Arête Horizontale
if (z != _nbPtsCtrlZ - 1)
_indices.insert(_indices.end(), {sommetA, sommetB, sommetA}); // Arête Verticale
_vertices.insert(_vertices.end(), {_pointsDeControle[i].x, _pointsDeControle[i].y, _pointsDeControle[i].z});
_normals.insert(_normals.end(), {0, 0, 0});
}
}
glm::vec3 BSpline::floraison(float u, int dec, int k, std::vector<glm::vec3> vecteursPointsControle)
@ -146,10 +260,10 @@ void BSpline::construirePolygone(EPolygone typePolygone, int nbPtsCtrlX, int nbP
}
case EPolygone::GAUSSIEN3D: {
// Sigma = 1.0f
float r, s = 2.0f;
float sigma = 0.5f;
float r, s = 2.0f * sigma * sigma;
for(int x = - (nbPtsCtrlX / 2) + 1; x < nbPtsCtrlX / 2; ++x) {
for(int z = - (nbPtsCtrlZ / 2) + 1; x < nbPtsCtrlZ / 2; ++x) {
for(int z = - (nbPtsCtrlZ / 2) + 1; z < nbPtsCtrlZ / 2; ++z) {
r = sqrt(x * x + z * z);
_pointsDeControle.push_back(glm::vec3( x, (exp(-(r * r) / s)) / (M_PI * s), z));
}
@ -164,8 +278,8 @@ void BSpline::construirePolygone(EPolygone typePolygone, int nbPtsCtrlX, int nbP
for(int i = 0; i < nbPtsCtrlX; ++i) {
for(int j = 0; j < nbPtsCtrlZ; ++j) {
x = (i * xMin + (nbPtsCtrlX - i) * xMax) / (xMax - xMin);
y = fmod(x + z, 3);
z = (j * zMin + (nbPtsCtrlZ - j) * zMax) / (zMax - zMin);
y = fmod(x + z, 3) / 2;
z = (j * zMin + (nbPtsCtrlZ - j) * zMax) / (zMin - zMax);
_pointsDeControle.push_back(glm::vec3(x, y, z));
}
}
@ -177,10 +291,10 @@ void BSpline::construirePolygone(EPolygone typePolygone, int nbPtsCtrlX, int nbP
case EPolygone::RANDOM3D: {
float x, y, z;
for(int i = 0; i < nbPtsCtrlX; ++i) {
for(int j = 0; j < nbPtsCtrlZ; ++i) {
for(int j = 0; j < nbPtsCtrlZ; ++j) {
x = (i * xMin + (nbPtsCtrlX - i) * xMax) / (xMax - xMin);
y = yMin + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(yMax-yMin)));
z = (j * zMin + (nbPtsCtrlZ - j) * zMax) / (zMax - zMin);
y = yMin + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(yMax-yMin))) / 3;
z = (j * zMin + (nbPtsCtrlZ - j) * zMax) / (zMin - zMax);
_pointsDeControle.push_back(glm::vec3(x, y, z));
}
}

View File

@ -62,7 +62,7 @@ protected:
// Default camera values
constexpr GLfloat YAW = -90.0f;
constexpr GLfloat PITCH = 0.0f;
constexpr GLfloat SPEED = 3.0f;
constexpr GLfloat SPEED = 30.0f;
constexpr GLfloat SENSITIVTY = 0.25f;
constexpr GLfloat ZOOM = 45.0f;

View File

@ -17,20 +17,20 @@ SimpleSpline::SimpleSpline(int width, int height, MainWindow* w) : Scene(width,
* Type de vecteur nodal (Uniforme / Ouvert uniforme / Quelconque)
*
* Pas pour delta u et Pas pour delta v */
BSpline bspline1D = BSpline(
BSpline::ESpline::SPLINE1D,
BSpline bspline = BSpline(
BSpline::ESpline::SPLINE2D,
3,
BSpline::EPolygone::RANDOM2D,
BSpline::EPolygone::RANDOM3D,
BSpline::EVecteurNodal::UNIFORME,
5,
5,
16,
16,
0.1,
0.1
);
_vertices = bspline1D.getVertices();
_indices = bspline1D.getIndices();
_normals = bspline1D.getNormals();
_vertices = bspline.getVertices();
_indices = bspline.getIndices();
_normals = bspline.getNormals();
std::cout << "Vertices count: " << _vertices.size() / 3 << std::endl;
std::cout << "Edges count: " << _indices.size() << std::endl;