mirror of
https://github.com/simtactics/mysimulation.git
synced 2025-07-15 02:26:44 -04:00
Added FSO.Files for use with the API server
Don't ask me. FreeSO is the prime example of dependency hell.
This commit is contained in:
parent
4b5e584eeb
commit
8fec258215
104 changed files with 14653 additions and 163 deletions
70
server/tso.files/RC/OBJReader.cs
Executable file
70
server/tso.files/RC/OBJReader.cs
Executable file
|
@ -0,0 +1,70 @@
|
|||
using Microsoft.Xna.Framework;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
|
||||
namespace FSO.Files.RC
|
||||
{
|
||||
/// <summary>
|
||||
/// Simple reader for .OBJ files. Only made to import from blender.
|
||||
/// </summary>
|
||||
public class OBJ
|
||||
{
|
||||
public List<Vector3> Vertices = new List<Vector3>();
|
||||
public List<Vector2> TextureCoords = new List<Vector2>();
|
||||
public List<Vector3> Normals = new List<Vector3>();
|
||||
public Dictionary<string, List<int[]>> FacesByObjgroup = new Dictionary<string, List<int[]>>();
|
||||
|
||||
public OBJ(Stream obj)
|
||||
{
|
||||
using (var reader = new StreamReader(obj))
|
||||
Read(reader);
|
||||
}
|
||||
|
||||
public void Read(StreamReader read)
|
||||
{
|
||||
string objGroup = "_default";
|
||||
string line = "";
|
||||
List<int[]> indices = new List<int[]>();
|
||||
FacesByObjgroup[objGroup] = indices;
|
||||
while ((line = read.ReadLine()) != null)
|
||||
{
|
||||
line = line.TrimStart();
|
||||
var comInd = line.IndexOf("#");
|
||||
if (comInd != -1) line = line.Substring(0, comInd);
|
||||
var split = line.Split(' ');
|
||||
if (split.Length == 0) continue;
|
||||
switch (split[0])
|
||||
{
|
||||
case "o": //set object group
|
||||
objGroup = split[1];
|
||||
if (!FacesByObjgroup.TryGetValue(objGroup, out indices))
|
||||
{
|
||||
indices = new List<int[]>();
|
||||
FacesByObjgroup[objGroup] = indices;
|
||||
}
|
||||
break;
|
||||
case "v":
|
||||
Vertices.Add(new Vector3(float.Parse(split[1], CultureInfo.InvariantCulture), float.Parse(split[2], CultureInfo.InvariantCulture), float.Parse(split[3], CultureInfo.InvariantCulture)));
|
||||
break;
|
||||
case "vt":
|
||||
TextureCoords.Add(new Vector2(float.Parse(split[1], CultureInfo.InvariantCulture), float.Parse(split[2], CultureInfo.InvariantCulture)));
|
||||
break;
|
||||
case "vn":
|
||||
Normals.Add(new Vector3(float.Parse(split[1], CultureInfo.InvariantCulture), float.Parse(split[2], CultureInfo.InvariantCulture), float.Parse(split[3], CultureInfo.InvariantCulture)));
|
||||
break;
|
||||
case "f":
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
var split2 = split[i + 1].Split('/');
|
||||
if (split2.Length == 2)
|
||||
indices.Add(new int[] { int.Parse(split2[0]), int.Parse(split2[1]) });
|
||||
else if (split2.Length == 3)
|
||||
indices.Add(new int[] { int.Parse(split2[0]), int.Parse(split2[1]), int.Parse(split2[2]) });
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue