Advertisement
Krythic

Wireframe Builder

Aug 3rd, 2020
2,608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.49 KB | None | 0 0
  1. using SharpDX;
  2. using SharpDX.Direct3D11;
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. namespace VoidwalkerEngine.Framework.DirectX.Rendering
  7. {
  8.     public static class Wireframe
  9.     {
  10.         public static ModelMesh GenerateWireframeSphere(Device device, Vector3 location, float radius)
  11.         {
  12.             float pi = (float)Math.PI;
  13.             List<Vertex> vertices = new List<Vertex>();
  14.             List<Vertex> segments = new List<Vertex>();
  15.             float twoPi = 2 * pi;
  16.             float angleStep = twoPi / (16 * (1 + (radius / 16f)));
  17.             int iterator = 0;
  18.             int firstIndex = 0;
  19.             for (float angle = 0f; angle <= twoPi; angle += angleStep)
  20.             {
  21.                 float x = radius * (float)Math.Sin(angle);
  22.                 float y = radius * (float)Math.Cos(angle);
  23.                 float z = 0;
  24.                 vertices.Add(new Vertex(location.X + x, location.Y + y, location.Z + z));
  25.                 if (iterator != 0 && iterator % 2 == 0)
  26.                 {
  27.                     segments.Add(new Vertex(vertices[iterator]));
  28.                     segments.Add(new Vertex(vertices[iterator - 1]));
  29.                 }
  30.                 if (angle >= twoPi - angleStep) // Wrap around and eat that tail like a good chompy boi Jörmungandr.
  31.                 {
  32.                     segments.Add(new Vertex(vertices[firstIndex]));
  33.                     segments.Add(new Vertex(vertices[iterator]));
  34.                 }
  35.                 iterator++;
  36.             }
  37.             firstIndex = vertices.Count;
  38.             for (float angle = 0f; angle <= twoPi; angle += angleStep)
  39.             {
  40.                 float x = radius * (float)Math.Sin(angle);
  41.                 float y = 0;
  42.                 float z = radius * (float)Math.Cos(angle);
  43.                 vertices.Add(new Vertex(location.X + x, location.Y + y, location.Z + z));
  44.                 if (iterator != 0 && iterator % 2 == 0)
  45.                 {
  46.                     segments.Add(new Vertex(vertices[iterator]));
  47.                     segments.Add(new Vertex(vertices[iterator - 1]));
  48.                 }
  49.                 if (angle >= twoPi - angleStep) // Wrap around and eat that tail like a good chompy boi Jörmungandr.
  50.                 {
  51.                     segments.Add(new Vertex(vertices[firstIndex]));
  52.                     segments.Add(new Vertex(vertices[iterator]));
  53.                 }
  54.                 iterator++;
  55.             }
  56.             firstIndex = vertices.Count;
  57.             for (float angle = 0f; angle <= twoPi; angle += angleStep)
  58.             {
  59.                 float x = 0;
  60.                 float y = radius * (float)Math.Sin(angle);
  61.                 float z = radius * (float)Math.Cos(angle);
  62.                 vertices.Add(new Vertex(location.X + x, location.Y + y, location.Z + z));
  63.                 if (iterator != 0 && iterator % 2 == 0)
  64.                 {
  65.                     segments.Add(new Vertex(vertices[iterator]));
  66.                     segments.Add(new Vertex(vertices[iterator - 1]));
  67.                 }
  68.                 if (angle >= twoPi - angleStep) // Wrap around and eat that tail like a good chompy boi Jörmungandr.
  69.                 {
  70.                     segments.Add(new Vertex(vertices[firstIndex]));
  71.                     segments.Add(new Vertex(vertices[iterator]));
  72.                 }
  73.                 iterator++;
  74.             }
  75.             vertices.AddRange(segments);
  76.             ModelMesh mesh = new ModelMesh(device, vertices.ToArray());
  77.             return mesh;
  78.         }
  79.     }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement