Guest User

Untitled

a guest
Jul 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4.  
  5. namespace XXX
  6. {
  7. public sealed class Cylinder : MeshPrimitiveBase
  8. {
  9. public Cylinder(GraphicsDevice graphicsDevice, Color color)
  10. : this(graphicsDevice, 1, 1, 32, color)
  11. {
  12. }
  13.  
  14. public Cylinder(GraphicsDevice graphicsDevice, float height, float diameter, int tessellation, Color color)
  15. : this(graphicsDevice, height, diameter, diameter, tessellation, color)
  16. {
  17. }
  18.  
  19. public Cylinder(GraphicsDevice graphicsDevice, float height, float diameterTop, float diameterBottom, int tessellation, Color color)
  20. : base(graphicsDevice, PrimitiveType.TriangleList)
  21. {
  22. Guard.GreaterThan(height, 0, nameof(height));
  23. Guard.GreaterThan(diameterTop, 0, nameof(diameterTop));
  24. Guard.GreaterThan(diameterBottom, 0, nameof(diameterBottom));
  25. Guard.GreaterThan(tessellation, 2, nameof(tessellation));
  26.  
  27. CreateMesh(height, diameterTop, diameterBottom, tessellation, color);
  28. }
  29.  
  30. private void CreateMesh(float height, float diameterTop, float diameterBottom, int tessellation, Color color)
  31. {
  32. height /= 2;
  33.  
  34. var radius1 = diameterTop / 2;
  35. var radius2 = diameterBottom / 2;
  36.  
  37. for (var i = 0; i < tessellation; i++)
  38. {
  39. Vector3 normal = GetCircleVector(i, tessellation);
  40.  
  41. AddVertex(new VertexPositionColorNormalTexture((normal * radius1) + (Vector3.UnitY * height), color, normal, Vector2.Zero));
  42. AddVertex(new VertexPositionColorNormalTexture((normal * radius2) - (Vector3.UnitY * height), color, normal, Vector2.Zero));
  43.  
  44. AddIndex((i * 2));
  45. AddIndex(((i * 2) + 1));
  46. AddIndex(((i * 2) + 2) % (tessellation * 2));
  47.  
  48. AddIndex(((i * 2) + 1));
  49. AddIndex(((i * 2) + 3) % (tessellation * 2));
  50. AddIndex(((i * 2) + 2) % (tessellation * 2));
  51. }
  52.  
  53. CreateCap(tessellation, height, radius1, color, Vector3.UnitY);
  54. CreateCap(tessellation, height, radius2, color, -Vector3.UnitY);
  55.  
  56. PrepareBuffers();
  57. }
  58.  
  59. private void CreateCap(int tessellation, float height, float radius, Color color, Vector3 normal)
  60. {
  61. for (var i = 0; i < tessellation - 2; i++)
  62. {
  63. if (normal.Y > 0)
  64. {
  65. AddIndex(VerticesCount);
  66. AddIndex(VerticesCount + ((i + 1) % tessellation));
  67. AddIndex(VerticesCount + ((i + 2) % tessellation));
  68. }
  69. else
  70. {
  71. AddIndex(VerticesCount);
  72. AddIndex(VerticesCount + ((i + 2) % tessellation));
  73. AddIndex(VerticesCount + ((i + 1) % tessellation));
  74. }
  75. }
  76.  
  77. for (var i = 0; i < tessellation; i++)
  78. {
  79. var position = (GetCircleVector(i, tessellation) * radius) + (normal * height);
  80.  
  81. AddVertex(new VertexPositionColorNormalTexture(position, color, normal, Vector2.Zero));
  82. }
  83. }
  84.  
  85. private static Vector3 GetCircleVector(int i, int tessellation)
  86. {
  87. var angle = i * MathHelper.TwoPi / tessellation;
  88.  
  89. var dx = (float)Math.Cos(angle);
  90. var dz = (float)Math.Sin(angle);
  91.  
  92. return new Vector3(dx, 0, dz);
  93. }
  94. }
  95. }
Add Comment
Please, Sign In to add comment