Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Sphere(int s_steps, int t_steps) {
- t_steps = Math.Max(3, t_steps);
- s_steps = Math.Max(3, s_steps);
- int count = (s_steps * t_steps) - (2 * s_steps) + 2;
- this.Vertices = new Vector3[count];
- this.Normals = new Vector3[count];
- // Top and bottom requires only triangles (2 * 3)
- count = s_steps * (t_steps - 2) * 6;
- this.Elements = new int[count];
- float t_increm = MathHelper.DegreesToRadians(180 / (float)t_steps);
- float s_increm = MathHelper.DegreesToRadians(360 / (float)s_steps);
- float tr = 0;
- float sr = 0;
- count = 0;
- // Used for keeping track of element indexes
- int start = 0;
- int i;
- int e_index; // Element index
- // First vertex is at very top and conntects to all steps
- Vertices[count].X = 0;
- Vertices[count].Y = 0;
- Vertices[count].Z = 1;
- Normals[count] = Vector3.Normalize(Vertices[count]);
- ++count;
- int t;
- for (t = 0; t < t_steps - 2; ++t) {
- tr += t_increm;
- for (int s = 0; s < s_steps; ++s) {
- sr += s_increm;
- Vertices[count].X = (float)(Math.Cos(sr) * Math.Sin(tr));
- Vertices[count].Y = (float)(Math.Sin(sr) * Math.Sin(tr));
- Vertices[count].Z = (float)Math.Cos(tr);
- Normals[count] = Vector3.Normalize(Vertices[count]);
- ++count;
- }
- if (t == 0) {
- e_index = 1;
- for (i = 0; i < s_steps * 3 - 3; i += 3) {
- this.Elements[i] = 0;
- this.Elements[i + 1] = e_index;
- this.Elements[i + 2] = e_index + 1;
- ++e_index;
- }
- i = s_steps * 3 - 3;
- this.Elements[i] = 0;
- this.Elements[i + 1] = e_index;
- this.Elements[i + 2] = 1;
- start += s_steps * 3;
- }
- else if (t > 0) {
- int pe_index = ((t - 1) * s_steps) + 1;
- e_index = (t * s_steps) + 1;
- for (i = start; i < start + (s_steps * 6) - 6; i += 6) {
- this.Elements[i] = pe_index;
- this.Elements[i + 1] = pe_index + 1;
- this.Elements[i + 2] = e_index;
- this.Elements[i + 3] = e_index;
- this.Elements[i + 4] = e_index + 1;
- this.Elements[i + 5] = pe_index + 1;
- ++pe_index;
- ++e_index;
- }
- i = start + s_steps * 6 - 6;
- this.Elements[i] = pe_index;
- this.Elements[i + 1] = ((t - 1) * s_steps) + 1;
- this.Elements[i + 2] = e_index;
- this.Elements[i + 3] = e_index;
- this.Elements[i + 4] = (t * s_steps) + 1;
- this.Elements[i + 5] = ((t - 1) * s_steps) + 1;
- start += s_steps * 6;
- }
- else {
- start += s_steps * 6;
- }
- }
- Vertices[count].X = 0;
- Vertices[count].Y = 0;
- Vertices[count].Z = -1;
- Normals[count] = Vector3.Normalize(Vertices[count]);
- e_index = ((t - 1) * s_steps) + 1;
- for (i = start; i < start + (s_steps * 3) - 3; i += 3) {
- this.Elements[i] = count;
- this.Elements[i + 1] = e_index;
- this.Elements[i + 2] = e_index + 1;
- ++e_index;
- }
- i = start + s_steps * 3 - 3;
- this.Elements[i] = count;
- this.Elements[i + 1] = e_index;
- this.Elements[i + 2] = ((t - 1) * s_steps) + 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement