Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.48 KB | None | 0 0
  1. public Sphere(int s_steps, int t_steps) {
  2.     t_steps = Math.Max(3, t_steps);
  3.     s_steps = Math.Max(3, s_steps);
  4.  
  5.  
  6.     int count = (s_steps * t_steps) - (2 * s_steps) + 2;            
  7.     this.Vertices = new Vector3[count];
  8.     this.Normals = new Vector3[count];
  9.  
  10.     // Top and bottom requires only triangles (2 * 3)
  11.     count = s_steps * (t_steps - 2) * 6;            
  12.     this.Elements = new int[count];
  13.  
  14.     float t_increm = MathHelper.DegreesToRadians(180 / (float)t_steps);
  15.     float s_increm = MathHelper.DegreesToRadians(360 / (float)s_steps);
  16.    
  17.     float tr = 0;
  18.     float sr = 0;
  19.  
  20.     count = 0;
  21.  
  22.     // Used for keeping track of element indexes
  23.     int start = 0;
  24.     int i;
  25.     int e_index; // Element index
  26.  
  27.     // First vertex is at very top and conntects to all steps
  28.     Vertices[count].X = 0;
  29.     Vertices[count].Y = 0;
  30.     Vertices[count].Z = 1;
  31.     Normals[count] = Vector3.Normalize(Vertices[count]);
  32.     ++count;
  33.  
  34.     int t;
  35.     for (t = 0; t < t_steps - 2; ++t) {
  36.         tr += t_increm;
  37.         for (int s = 0; s < s_steps; ++s) {
  38.             sr += s_increm;
  39.             Vertices[count].X = (float)(Math.Cos(sr) * Math.Sin(tr));
  40.             Vertices[count].Y = (float)(Math.Sin(sr) * Math.Sin(tr));
  41.             Vertices[count].Z = (float)Math.Cos(tr);
  42.             Normals[count] = Vector3.Normalize(Vertices[count]);
  43.             ++count;
  44.         }
  45.         if (t == 0) {
  46.             e_index = 1;
  47.             for (i = 0; i < s_steps * 3 - 3; i += 3) {
  48.                 this.Elements[i] = 0;
  49.                 this.Elements[i + 1] = e_index;
  50.                 this.Elements[i + 2] = e_index + 1;
  51.                 ++e_index;
  52.             }
  53.             i = s_steps * 3 - 3;
  54.             this.Elements[i] = 0;
  55.             this.Elements[i + 1] = e_index;
  56.             this.Elements[i + 2] = 1;
  57.             start += s_steps * 3;
  58.         }
  59.         else if (t > 0) {
  60.             int pe_index = ((t - 1) * s_steps) + 1;
  61.             e_index = (t * s_steps) + 1;                                      
  62.             for (i = start; i < start + (s_steps * 6) - 6; i += 6) {
  63.                 this.Elements[i] = pe_index;
  64.                 this.Elements[i + 1] = pe_index + 1;
  65.                 this.Elements[i + 2] = e_index;
  66.                 this.Elements[i + 3] = e_index;
  67.                 this.Elements[i + 4] = e_index + 1;
  68.                 this.Elements[i + 5] = pe_index + 1;
  69.  
  70.                 ++pe_index;
  71.                 ++e_index;
  72.             }
  73.             i = start + s_steps * 6 - 6;
  74.             this.Elements[i] = pe_index;
  75.             this.Elements[i + 1] = ((t - 1) * s_steps) + 1;
  76.             this.Elements[i + 2] = e_index;
  77.             this.Elements[i + 3] = e_index;
  78.             this.Elements[i + 4] = (t * s_steps) + 1;
  79.             this.Elements[i + 5] = ((t - 1) * s_steps) + 1;
  80.             start += s_steps * 6;
  81.         }
  82.         else {
  83.             start += s_steps * 6;
  84.         }
  85.     }
  86.  
  87.     Vertices[count].X = 0;
  88.     Vertices[count].Y = 0;
  89.     Vertices[count].Z = -1;
  90.     Normals[count] = Vector3.Normalize(Vertices[count]);
  91.    
  92.     e_index = ((t - 1) * s_steps) + 1;                        
  93.     for (i = start; i < start + (s_steps * 3) - 3; i += 3) {
  94.         this.Elements[i] = count;
  95.         this.Elements[i + 1] = e_index;
  96.         this.Elements[i + 2] = e_index + 1;
  97.         ++e_index;
  98.     }
  99.     i = start + s_steps * 3 - 3;
  100.     this.Elements[i] = count;
  101.     this.Elements[i + 1] = e_index;
  102.     this.Elements[i + 2] = ((t - 1) * s_steps) + 1;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement