Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- shape GenerateShape::makeSphere(shape sphere, GLuint recursions, float radius)
- {
- if(sphere.numVerts == 0)//AKA the shape is empty
- sphere = makeTetrahedron(radius);// Make the base; a tetrahedron.
- if(!recursions) //If there are no recursions to make left.
- return sphere;
- std::vector<GLushort> newIndices;
- int i;
- for (i = 0; i < sphere.numIndices; i+=3)
- {
- vertex oldVert1 = sphere.vertices[sphere.indices[i]];
- vertex oldVert2 = sphere.vertices[sphere.indices[i+1]];
- vertex oldVert3 = sphere.vertices[sphere.indices[i+2]];
- vertex newVert;
- glm::vec3 newPosition; //The centroid
- newPosition.x = (oldVert1.position.x + oldVert2.position.x + oldVert3.position.x)/3;
- newPosition.y = (oldVert1.position.y + oldVert2.position.y + oldVert3.position.y)/3;
- newPosition.z = (oldVert1.position.z + oldVert2.position.z + oldVert3.position.z)/3;
- newPosition = glm::normalize(newPosition) * radius;
- newVert.position = newPosition;
- newVert.colour = glm::vec3(1, 1, 0); //Test colour
- sphere.vertices.push_back(newVert); //Add the vertex to the shapes list
- //Calculate the new vertex's index and add the indices for the three new triangles.
- GLuint newVertIndex = sphere.vertices.size()-1;
- printf("%d/n", newVertIndex);
- newIndices.push_back(i);
- newIndices.push_back(i+1);
- newIndices.push_back(newVertIndex);
- newIndices.push_back(i+1);
- newIndices.push_back(i+2);
- newIndices.push_back(newVertIndex);
- newIndices.push_back(i+2);
- newIndices.push_back(i);
- newIndices.push_back(newVertIndex);
- }
- //Set important data
- sphere.indices.resize(newIndices.size());
- sphere.indices.swap(newIndices);
- sphere.numVerts = sphere.vertices.size();
- sphere.numIndices = sphere.indices.size();
- return makeSphere(sphere, recursions-1, radius);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement