Advertisement
Guest User

Fourth

a guest
Dec 19th, 2009
3,724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. Sphere::Sphere(int n) : Shape() {
  2.  
  3.     //no factor greater than 5 (far too long computation time)
  4.     if( n > 5 )
  5.             n = 5;
  6.  
  7.     float five = 5.0;
  8.     float a = 2.0 / ( 1.0 + sqrt(five) );
  9.  
  10.     Vector3 origin(0,0,0);
  11.  
  12.     //icosahedron vertices
  13.     Vector3 tv0(0 , a, -1);
  14.     Vector3 tv1(-a, 1, 0);
  15.     Vector3 tv2(a, 1, 0);
  16.     Vector3 tv3(0, a, 1);
  17.     Vector3 tv4(-1, 0, a);
  18.     Vector3 tv5(0, -a, 1);
  19.     Vector3 tv6(1, 0, a);
  20.     Vector3 tv7(1, 0, -a);
  21.     Vector3 tv8(0, -a, -1);
  22.     Vector3 tv9(-1, 0, -a);
  23.     Vector3 tv10(-a, -1, 0);
  24.     Vector3 tv11(a, -1, 0);
  25.  
  26.     //create the triangles in the original mesh
  27.     vector< vector<Vector3> > triangles;
  28.  
  29.     for each triangle in the icosahedrion
  30.         create that triangle and add it to the triangles vector
  31.  
  32.     //so we have all of the vertices in there original coordinates
  33.     //with that, we subdivide each polygon into 4 new ones and
  34.     //do this n number of times
  35.     for( int i = 1; i < n; i++ ) {
  36.             vector< vector<Vector3> > tempTriangles;
  37.             for( int j = 0; j < triangles.size(); j++ ) {
  38.                 //now we subdivide
  39.                 vector<Vector3> triangle = triangles.at(j);
  40.                 Vector3 two = triangle.at(2); //point 2 of the triangle
  41.                 Vector3 one = triangle.at(1); //point 1 of the triangle
  42.                 Vector3 zero = triangle.at(0); //point 0 of the triangle
  43.                 Vector3 a( ((two+zero)*.5) ); //intermediate point a
  44.                 Vector3 b( ((one+zero)*.5) ); //intermediate point b
  45.                 Vector3 c( ((two+one)*.5) ); //intermediate point c
  46.  
  47.                 add four the new triangles to the temp container
  48.  
  49.             }
  50.             triangles = tempTriangles;
  51.     }
  52.  
  53.     normalize each triangle vertex (hint: use the temp container here again)
  54.  
  55.     create triangles in the order we get them out of the triangles container
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement