Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sphere::Sphere(int n) : Shape() {
- //no factor greater than 5 (far too long computation time)
- if( n > 5 )
- n = 5;
- float five = 5.0;
- float a = 2.0 / ( 1.0 + sqrt(five) );
- Vector3 origin(0,0,0);
- //icosahedron vertices
- Vector3 tv0(0 , a, -1);
- Vector3 tv1(-a, 1, 0);
- Vector3 tv2(a, 1, 0);
- Vector3 tv3(0, a, 1);
- Vector3 tv4(-1, 0, a);
- Vector3 tv5(0, -a, 1);
- Vector3 tv6(1, 0, a);
- Vector3 tv7(1, 0, -a);
- Vector3 tv8(0, -a, -1);
- Vector3 tv9(-1, 0, -a);
- Vector3 tv10(-a, -1, 0);
- Vector3 tv11(a, -1, 0);
- //create the triangles in the original mesh
- vector< vector<Vector3> > triangles;
- for each triangle in the icosahedrion
- create that triangle and add it to the triangles vector
- //so we have all of the vertices in there original coordinates
- //with that, we subdivide each polygon into 4 new ones and
- //do this n number of times
- for( int i = 1; i < n; i++ ) {
- vector< vector<Vector3> > tempTriangles;
- for( int j = 0; j < triangles.size(); j++ ) {
- //now we subdivide
- vector<Vector3> triangle = triangles.at(j);
- Vector3 two = triangle.at(2); //point 2 of the triangle
- Vector3 one = triangle.at(1); //point 1 of the triangle
- Vector3 zero = triangle.at(0); //point 0 of the triangle
- Vector3 a( ((two+zero)*.5) ); //intermediate point a
- Vector3 b( ((one+zero)*.5) ); //intermediate point b
- Vector3 c( ((two+one)*.5) ); //intermediate point c
- add four the new triangles to the temp container
- }
- triangles = tempTriangles;
- }
- normalize each triangle vertex (hint: use the temp container here again)
- create triangles in the order we get them out of the triangles container
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement