Right in time

This commit is contained in:
ao 2018-10-10 16:20:54 +02:00
parent 4b46f00150
commit d98b2d625e
3 changed files with 79 additions and 40 deletions

View File

@ -9,7 +9,7 @@ BSpline::BSpline(
int nbPtsCtrlX,
int nbPtsCtrlZ,
float stepX,
float stepY)
float stepZ)
{
// Simple vérification
switch (typeSpline) {
@ -29,7 +29,7 @@ BSpline::BSpline(
// Initialisation des valeurs
_k = k;
_stepX = stepX;
_stepY = stepY;
_stepZ = stepZ;
srand(time(NULL));
// Génération du polygone de contrôle
@ -102,40 +102,71 @@ 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);
int decX = 0, decZ = 0, m = _k - 1, nbPtsMaillageX = 0, nbPtsMaillageZ = 0;
uint i = 0, n = 0;
double u = 0, v = 0;
//std::vector<glm::vec3> vecteursPointsControle (_k);
std::vector<glm::vec3> generatrice;
std::vector<std::vector<glm::vec3>> pointsInfluents;
std::vector<glm::vec3> vecteurHorizontal;
// Évaluation de p(u, v), revient à évaluer la génératrice sur les directrices
//Parcours de u
for(u = _vecteurNodal[m]; u <= _vecteurNodal[_nbPtsCtrlX]; u += _stepX){
// Parcours de v
for(v = _vecteurNodal[m]; v <= _vecteurNodal[_nbPtsCtrlZ]; v += _stepZ){
// Évaluation de p(u, v), revient à évaluer la génératrice sur les directrices
// Réinitialisation
vecteurHorizontal.clear();
pointsInfluents.clear();
generatrice.clear();
decX = 0;
decZ = 0;
// Vecteur de nbPtsCtrlZ directrices
dec = 0;
// Calcul du décalage
for(int i = _k; u > _vecteurNodal[i]; ++i)
++dec;
++decX;
for(int j = _k; v > _vecteurNodal[j]; ++j)
++decZ;
// 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];
// Calcul des points influents
pointsInfluents.clear();
for(int i = 0; i < _k; ++i) {
vecteurHorizontal.clear();
for(int j = 0; j < _k; ++j) {
glm::vec3 pt = _pointsDeControle[((decX + i) * _nbPtsCtrlZ) + (decZ + j)];
vecteurHorizontal.push_back(pt);
}
pointsInfluents.push_back(vecteurHorizontal);
}
// m+1 directrices floraisons
for(int i = 0; i < _k; ++i) {
generatrice.push_back(floraison(v, decZ, _k, pointsInfluents[i]));
}
// Calcul de b-spline sur la génératrice (locale)
// Appel de la floraison récursive
glm::vec3 newVertex = floraison(u, dec, _k, vecteursPointsControle);
glm::vec3 newVertex = floraison(u, decX, _k, generatrice);
// 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});
}
//_normals.insert(_normals.end(), {1, -1, -1});
//_indices.insert(_indices.end(), {i, i, ++i});
// Incrémenter taille en z
if(u == _vecteurNodal[m]) nbPtsMaillageZ++;
}
nbPtsMaillageX++;
}
std::cout << nbPtsMaillageX * nbPtsMaillageZ << " INDICES CALCULÉS" << std::endl;
// Maillage
for(unsigned int i = 0; i < _pointsDeControle.size(); ++i) {
for(unsigned int i = 0; i < _vertices.size() / 3; ++i) {
// Indices
int x = i / nbPtsMaillageZ;
int z = i % nbPtsMaillageX;
@ -152,8 +183,11 @@ void BSpline::calculerSpline2D()
}
}
std::cout << _vertices.size() / 3 * 2 << " TRIANGLES CALCULÉS" << std::endl;
// 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];
@ -171,35 +205,39 @@ void BSpline::calculerSpline2D()
_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;
for(int i = 0; i < _vertices.size(); i+=3) {
glm::vec3 normale = glm::normalize(glm::vec3(_normals[i], _normals[i+1], _normals[i+2]));
_normals.at(i) = normale.x;
_normals.at(i+1) = normale.y;
_normals.at(i+2) = normale.z;
}
std::cout << _normals.size()/3 << " NORMALES CALCULÉES" << std::endl;
// 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;
//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;
//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
*/
//_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
@ -208,6 +246,7 @@ void BSpline::calculerSpline2D()
_vertices.insert(_vertices.end(), {_pointsDeControle[i].x, _pointsDeControle[i].y, _pointsDeControle[i].z});
_normals.insert(_normals.end(), {0, 0, 0});
}
std::cout << "POLYGONE AJOUTÉ" << std::endl;
}
glm::vec3 BSpline::floraison(float u, int dec, int k, std::vector<glm::vec3> vecteursPointsControle)
@ -260,7 +299,7 @@ void BSpline::construirePolygone(EPolygone typePolygone, int nbPtsCtrlX, int nbP
}
case EPolygone::GAUSSIEN3D: {
float sigma = 0.5f;
float sigma = 0.3f;
float r, s = 2.0f * sigma * sigma;
for(int x = - (nbPtsCtrlX / 2) + 1; x < nbPtsCtrlX / 2; ++x) {
for(int z = - (nbPtsCtrlZ / 2) + 1; z < nbPtsCtrlZ / 2; ++z) {
@ -278,7 +317,7 @@ 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) / 2;
y = fmod(x + z, 3);
z = (j * zMin + (nbPtsCtrlZ - j) * zMax) / (zMin - zMax);
_pointsDeControle.push_back(glm::vec3(x, y, z));
}
@ -293,7 +332,7 @@ 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 = yMin + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(yMax-yMin))) / 3;
y = yMin + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(yMax-yMin))) / 2;
z = (j * zMin + (nbPtsCtrlZ - j) * zMax) / (zMin - zMax);
_pointsDeControle.push_back(glm::vec3(x, y, z));
}

View File

@ -20,7 +20,7 @@ public:
int nbPtsCtrlX = 10,
int nbPtsCtrlZ = 10,
float stepX = 0.1,
float stepY = 0.1);
float stepZ = 0.1);
std::vector<GLfloat> const & getVertices() const {return _vertices;}
std::vector<GLfloat> const & getNormals() const {return _normals;}
std::vector<GLuint> const & getIndices() const {return _indices;}
@ -32,7 +32,7 @@ private:
// Paramètres
int _k, _nbPtsCtrlX, _nbPtsCtrlZ;
float _stepX, _stepY;
float _stepX, _stepZ;
ESpline _typeSpline;
EPolygone _typePolygone;

View File

@ -19,11 +19,11 @@ SimpleSpline::SimpleSpline(int width, int height, MainWindow* w) : Scene(width,
* Pas pour delta u et Pas pour delta v */
BSpline bspline = BSpline(
BSpline::ESpline::SPLINE2D,
3,
4,
BSpline::EPolygone::RANDOM3D,
BSpline::EVecteurNodal::UNIFORME,
16,
16,
BSpline::EVecteurNodal::OUVERTUNIFORME,
8,
8,
0.1,
0.1
);
@ -31,7 +31,7 @@ SimpleSpline::SimpleSpline(int width, int height, MainWindow* w) : Scene(width,
_vertices = bspline.getVertices();
_indices = bspline.getIndices();
_normals = bspline.getNormals();
//std::cout << "test" << std::endl;
std::cout << "Vertices count: " << _vertices.size() / 3 << std::endl;
std::cout << "Edges count: " << _indices.size() << std::endl;
std::cout << "Triangles count: " << _indices.size() / 3 << std::endl;