Advertisement
NoobsDeSroobs

Make a Sphere

Aug 27th, 2014
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. shape GenerateShape::makeSphere(shape sphere, GLuint recursions, float radius)
  2. {
  3.  
  4.     if(sphere.numVerts == 0)//AKA the shape is empty
  5.         sphere = makeTetrahedron(radius);// Make the base; a tetrahedron.
  6.  
  7.     if(!recursions) //If there are no recursions to make left.
  8.         return sphere;
  9.  
  10.     std::vector<GLushort> newIndices;
  11.     int i;
  12.     for (i = 0; i < sphere.numIndices; i+=3)
  13.     {
  14.         vertex oldVert1 = sphere.vertices[sphere.indices[i]];
  15.         vertex oldVert2 = sphere.vertices[sphere.indices[i+1]];
  16.         vertex oldVert3 = sphere.vertices[sphere.indices[i+2]];
  17.         vertex newVert;
  18.  
  19.         glm::vec3 newPosition; //The centroid
  20.         newPosition.x = (oldVert1.position.x + oldVert2.position.x + oldVert3.position.x)/3;
  21.         newPosition.y = (oldVert1.position.y + oldVert2.position.y + oldVert3.position.y)/3;
  22.         newPosition.z = (oldVert1.position.z + oldVert2.position.z + oldVert3.position.z)/3;
  23.         newPosition = glm::normalize(newPosition) * radius;
  24.         newVert.position = newPosition;
  25.         newVert.colour = glm::vec3(1, 1, 0); //Test colour
  26.  
  27.         sphere.vertices.push_back(newVert); //Add the vertex to the shapes list
  28.  
  29.         //Calculate the new vertex's index and add the indices for the three new triangles.
  30.         GLuint newVertIndex = sphere.vertices.size()-1;
  31.         printf("%d/n", newVertIndex);
  32.         newIndices.push_back(i);
  33.         newIndices.push_back(i+1);
  34.         newIndices.push_back(newVertIndex);
  35.  
  36.         newIndices.push_back(i+1);
  37.         newIndices.push_back(i+2);
  38.         newIndices.push_back(newVertIndex);
  39.  
  40.         newIndices.push_back(i+2);
  41.         newIndices.push_back(i);
  42.         newIndices.push_back(newVertIndex);
  43.     }
  44.  
  45.     //Set important data
  46.     sphere.indices.resize(newIndices.size());
  47.     sphere.indices.swap(newIndices);
  48.  
  49.     sphere.numVerts = sphere.vertices.size();
  50.     sphere.numIndices = sphere.indices.size();
  51.  
  52.     return makeSphere(sphere, recursions-1, radius);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement