Advertisement
Krythic

Voidwalker Model Loader

Mar 5th, 2023
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. using SharpDX;
  2. using SharpDX.Direct3D11;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6.  
  7. namespace VoidwalkerEngine.Framework.DirectX.Rendering
  8. {
  9.     public class WavefrontModel
  10.     {
  11.         public string Name { get; set; }
  12.         public List<Vertex> Vertices { get; set; }
  13.  
  14.         public WavefrontModel()
  15.         {
  16.             this.Vertices = new List<Vertex>();
  17.         }
  18.  
  19.         public static WavefrontModel Load(string filePath)
  20.         {
  21.             if (File.Exists(filePath))
  22.             {
  23.                 return Load(File.ReadAllBytes(filePath));
  24.             }
  25.             else
  26.             {
  27.                 throw new FileNotFoundException("Could not locate: " + filePath);
  28.             }
  29.         }
  30.  
  31.         public static WavefrontModel Load(byte[] bytes)
  32.         {
  33.             using (MemoryStream memoryStream = new MemoryStream(bytes))
  34.             {
  35.                 using (StreamReader reader = new StreamReader(memoryStream))
  36.                 {
  37.                     WavefrontModel model = new WavefrontModel();
  38.                     List<Vector3> parsedVertices = new List<Vector3>();
  39.                     List<Vector2> parsedTexCoords = new List<Vector2>();
  40.                     List<Vector3> parsedNormals = new List<Vector3>();
  41.                     string line;
  42.                     while ((line = reader.ReadLine()) != null)
  43.                     {
  44.                         if (line.Length > 0)
  45.                         {
  46.                             string[] segments = line.Split(' ');
  47.                             if (line.StartsWith("o "))
  48.                             {
  49.                                 Console.WriteLine(line);
  50.                             }
  51.                             if (line.StartsWith("v "))
  52.                             {
  53.                                 /**
  54.                                  * Fix Precision Errors
  55.                                  */
  56.                                 string xComponent = segments[1];
  57.                                 string yComponent = segments[2];
  58.                                 string zComponent = segments[3];
  59.                                 if (xComponent.StartsWith("-0.00") || xComponent.StartsWith("0.00"))
  60.                                 {
  61.                                     xComponent = "0";
  62.                                 }
  63.                                 if (yComponent.StartsWith("-0.00") || yComponent.StartsWith("0.00"))
  64.                                 {
  65.                                     yComponent = "0";
  66.                                 }
  67.                                 if (zComponent.StartsWith("-0.00") || zComponent.StartsWith("0.00"))
  68.                                 {
  69.                                     zComponent = "0";
  70.                                 }
  71.  
  72.                                 Vector3 location = new Vector3(
  73.                                         float.Parse(xComponent),
  74.                                         float.Parse(yComponent),
  75.                                         float.Parse(zComponent));
  76.                                 parsedVertices.Add(location);
  77.                             }
  78.                             else if (line.StartsWith("vt "))
  79.                             {
  80.                                 parsedTexCoords.Add(
  81.                                     new Vector2(
  82.                                         Single.Parse(segments[1]),
  83.                                        1 - Single.Parse(segments[2])));
  84.                             }
  85.                             else if (line.StartsWith("vn "))
  86.                             {
  87.                                 parsedNormals.Add(
  88.                                     new Vector3(
  89.                                         Single.Parse(segments[1]),
  90.                                         Single.Parse(segments[2]),
  91.                                         Single.Parse(segments[3])));
  92.                             }
  93.                             else if (line.StartsWith("f "))
  94.                             {
  95.                                 for (int i = 1; i <= 3; i++)
  96.                                 {
  97.                                     string[] faceSegments = segments[i].Split('/');
  98.                                     int verticeIndex = Int32.Parse(faceSegments[0]) - 1;
  99.                                     int texCoordIndex = Int32.Parse(faceSegments[1]) - 1;
  100.                                     int normalIndex = Int32.Parse(faceSegments[2]) - 1;
  101.                                     Vertex vertex = new Vertex
  102.                                     {
  103.                                         Location = parsedVertices[verticeIndex],
  104.                                         TexCoords = parsedTexCoords[texCoordIndex],
  105.                                         Normal = parsedNormals[normalIndex]
  106.                                     };
  107.                                     model.Vertices.Add(vertex);
  108.                                 }
  109.                             }
  110.                         }
  111.                     }
  112.                     reader.Close();
  113.                     return model;
  114.                 }
  115.             }
  116.         }
  117.  
  118.         public GraphicsMesh ToModelMesh(Device device)
  119.         {
  120.             return new GraphicsMesh(device, this.Vertices.ToArray());
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement