Advertisement
Masterchoc

Untitled

Sep 29th, 2019
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.35 KB | None | 0 0
  1.  
  2. UVSphere::UVSphere(float radius, const glm::ivec2& segments) :
  3.         StaticMesh(),
  4.         radius(radius),
  5.         segments(segments)
  6.     {
  7.         std::vector<float> vertices;
  8.         std::vector<float> normals;
  9.         std::vector<float> uvs;
  10.         std::vector<unsigned int> indices;
  11.         bool odd = false;
  12.         float inv_len = 1.0f / radius;
  13.  
  14.         for (int y = 0; y <= segments.y; ++y)
  15.         {
  16.             for (int x = 0; x <= segments.x; ++x)
  17.             {
  18.                 float seg_x = (float)x / (float)segments.x;
  19.                 float seg_y = (float)y / (float)segments.y;
  20.                 float theta = seg_x * 2.0f * PI;
  21.                 float phi = seg_y * PI;
  22.                 float pos_x = std::cos(theta) * std::sin(phi) * radius;
  23.                 float pos_y = std::sin(-PI / 2 + PI * seg_y) * radius;
  24.                 float pos_z = std::sin(theta) * std::sin(phi) * radius;
  25.  
  26.                 vertices.push_back(pos_x);
  27.                 vertices.push_back(pos_y);
  28.                 vertices.push_back(pos_z);
  29.  
  30.                 normals.push_back(pos_x);
  31.                 normals.push_back(pos_y);
  32.                 normals.push_back(pos_z);
  33.  
  34.                 uvs.push_back(seg_x);
  35.                 uvs.push_back(seg_y);
  36.             }
  37.         }
  38.  
  39.         for (int y = 0; y < segments.y; ++y)
  40.         {
  41.             if (!odd)
  42.             {
  43.                 for (int x = 0; x <= segments.x; ++x)
  44.                 {
  45.                     indices.push_back(y       * (segments.x + 1) + x);
  46.                     indices.push_back((y + 1) * (segments.x + 1) + x);
  47.                 }
  48.             }
  49.             else
  50.             {
  51.                 for (int x = segments.x; x >= 0; --x)
  52.                 {
  53.                     indices.push_back((y + 1) * (segments.x + 1) + x);
  54.                     indices.push_back(y       * (segments.x + 1) + x);
  55.                 }
  56.             }
  57.  
  58.             odd = !odd;
  59.         }
  60.  
  61.         this->setVertices(vertices);
  62.         this->setNormals(normals);
  63.         this->setUvs(uvs);
  64.         this->setIndices(indices);
  65.         this->setDrawMode(StaticMesh::DrawMode::TRIANGLE_STRIP);
  66.         calculateTangents();
  67.  
  68.         vao = new VertexArray();
  69.  
  70.         vbo = new VertexBuffer(getVertices().data(), (unsigned int)getVertices().size() * sizeof(float));
  71.         vao->addBuffer(*vbo, 0, 3);
  72.  
  73.         nbo = new VertexBuffer(getNormals().data(), (unsigned int)getNormals().size() * sizeof(float));
  74.         vao->addBuffer(*nbo, 1, 3);
  75.  
  76.         uvbo = new VertexBuffer(getUvs().data(), (unsigned int)getUvs().size() * sizeof(float));
  77.         vao->addBuffer(*uvbo, 2, 2);
  78.  
  79.         tbo = new VertexBuffer(getTangents().data(), (unsigned int)getTangents().size() * sizeof(float));
  80.         vao->addBuffer(*tbo, 3, 3);
  81.  
  82.         ibo = new IndexBuffer(getIndices().data(), (unsigned int)getIndices().size());
  83.  
  84.         setVertexCount((unsigned int)getVertices().size() / 3);
  85.         setName("SphereStaticMesh");
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement