diff --git a/src/bspline.cpp b/src/bspline.cpp index 2537d1b..061b037 100644 --- a/src/bspline.cpp +++ b/src/bspline.cpp @@ -11,12 +11,12 @@ BSpline::BSpline(int k, double step, int np1) for(int i = 0; i < k+np1; ++i) _vecteurNodal.push_back(i); // Points de contrôle - _pointsDeControle.push_back(glm::vec3(-3, 0, 0)); - _pointsDeControle.push_back(glm::vec3(-1, 0, 0)); - _pointsDeControle.push_back(glm::vec3(-1, 0, 2)); - _pointsDeControle.push_back(glm::vec3( 1, 0, 2)); - _pointsDeControle.push_back(glm::vec3( 1, 0, 0)); - _pointsDeControle.push_back(glm::vec3( 3, 0, 0)); + _pointsDeControle.push_back(glm::vec3(-2, 0, -5)); + _pointsDeControle.push_back(glm::vec3(-1, 0, -5)); + _pointsDeControle.push_back(glm::vec3(-1, 1, -5)); + _pointsDeControle.push_back(glm::vec3( 1, 1, -5)); + _pointsDeControle.push_back(glm::vec3( 1, 0, -5)); + _pointsDeControle.push_back(glm::vec3( 2, 0, -5)); computeSpline(); } @@ -30,21 +30,68 @@ void BSpline::computeSpline() std::vector vecteursPointsControle (_np1); for(u = _k - 1; u < _np1; u += _step){ + // Réinitialiser dec et i ? while(u > _vecteurNodal[i]){ ++dec; ++i; } + // int i à virer ? for(int i = 0; i < _k; i++){ vecteursPointsControle[i] = _pointsDeControle[dec + i]; } - std::vector newVertices = floraison(u, dec, _k); - _vertices.insert(_vertices.end(), newVertices.begin(), newVertices.end()); + glm::vec3 newVertex = floraison(u, dec, _k, vecteursPointsControle); + //std::cout << newVertex.x << " " << newVertex.y << " " << newVertex.z << std::endl; + _vertices.push_back(newVertex); } } -std::vector BSpline::floraison(double u, int dec, int k) +glm::vec3 BSpline::floraison(float u, int dec, int k, std::vector vecteursPointsControle) { - return std::vector({0.0f, 0.0f, 0.0f}); + if(k == 0) + return vecteursPointsControle.front(); + for(int i = 0; i < k - 1; ++i) + vecteursPointsControle[i] = + (((_vecteurNodal[dec + k + i] - u) / (_vecteurNodal[dec + k + i] - _vecteurNodal[dec + 1 + i])) * vecteursPointsControle[i]) * + (((u - _vecteurNodal[dec + 1 + i]) / (_vecteurNodal[dec + k + i] - _vecteurNodal[dec + 1 + i])) * vecteursPointsControle[i+1]); + floraison(u, dec+1, k-1, vecteursPointsControle); +} + +std::vector const BSpline::getVertices() +{ + std::vector vertices; + // Ajout des points de contrôle + for(glm::vec3 point: _pointsDeControle) + vertices.insert(vertices.end(), {point.x, point.y, point.z}); + + // Ajout des points issus de la floraison + for(glm::vec3 point: _vertices) + vertices.insert(vertices.end(), {point.x, point.y, point.z}); + + return vertices; +} + +std::vector const BSpline::getNormals() +{ + std::vector normals; + for(glm::vec3 point: _pointsDeControle) + normals.insert(normals.end(), {point.x, point.y, point.z}); + // Ajout des normales des points issus de la floraison + for(glm::vec3 point: _vertices) + normals.insert(normals.end(), {point.x, point.y, point.z}); + + return normals; +} + +std::vector const BSpline::getIndices() +{ + std::vector indices; + for(uint i = 0; i < _pointsDeControle.size() - 1; ++i) + indices.insert(indices.end(), {i, i, i+1}); + // Ajout des indices pour les points issus de la floraison + for(uint i = 0; i < _vertices.size() - 1; ++i) + indices.insert(indices.end(), {i, i, i+1}); + + return indices; } diff --git a/src/bspline.h b/src/bspline.h index 0ec4f78..5f1d392 100644 --- a/src/bspline.h +++ b/src/bspline.h @@ -9,18 +9,20 @@ class BSpline { public: BSpline(int k = 2, double step = 0.1, int np1 = 6); - + std::vector const getVertices(); + std::vector const getNormals(); + std::vector const getIndices(); private: - std::vector _vecteurNodal; + std::vector _vecteurNodal; std::vector _pointsDeControle; int _k; int _np1; double _step; - std::vector _vertices; + std::vector _vertices; void computeSpline(); - std::vector floraison(double u, int dec, int k); + glm::vec3 floraison(float u, int dec, int k, std::vector vecteursPointsControle); }; #endif // BSPLINE_H diff --git a/src/hello_spheres/hellosphere.cpp b/src/hello_spheres/hellosphere.cpp index 322fb9d..541701f 100644 --- a/src/hello_spheres/hellosphere.cpp +++ b/src/hello_spheres/hellosphere.cpp @@ -13,8 +13,8 @@ SimpleSphere::SimpleSphere(int width, int height, MainWindow* w) : Scene(width, // Compute the shape to draw -> Menu ? //drawCube(0.1f); - drawUVSphere(50, _radius); - //drawIcoSphere(5, _radius); + //drawUVSphere(50, _radius); + drawIcoSphere(6, _radius); std::cout << "Vertices count: " << _vertices.size() / 3 << std::endl; std::cout << "Edges count: " << _indices.size() << std::endl; diff --git a/src/hello_spline/hellospline.cpp b/src/hello_spline/hellospline.cpp index 5ec464c..a43403f 100644 --- a/src/hello_spline/hellospline.cpp +++ b/src/hello_spline/hellospline.cpp @@ -11,6 +11,10 @@ SimpleSpline::SimpleSpline(int width, int height, MainWindow* w) : Scene(width, _lastFragment = _w->fragmentShader; // LOAD B-SPLINE + BSpline bspline1D = BSpline(); + _vertices = bspline1D.getVertices(); + _indices = bspline1D.getIndices(); + _normals = bspline1D.getNormals(); std::cout << "Vertices count: " << _vertices.size() / 3 << std::endl; std::cout << "Edges count: " << _indices.size() << std::endl;