Advertisement
Guest User

Untitled

a guest
Sep 6th, 2014
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.49 KB | None | 0 0
  1. using OpenTK;
  2. using OpenTK.Graphics;
  3. using OpenTK.Graphics.OpenGL;
  4. using System;
  5. using System.IO;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace TFMV
  9. {
  10.     class Program
  11.     {
  12.         class Mesh
  13.         {
  14.             public int normalIndicesCount, normalIndicesOffset;
  15.             public int positionIndicesCount, positionIndicesOffset;
  16.             public int tangentIndicesCount, tangentIndicesOffset;
  17.             public int uvIndicesCount, uvIndicesOffset;
  18.  
  19.             public int normalCount, normalOffset;
  20.             public int positionCount, positionOffset;
  21.             public int tangentCount, tangentOffset;
  22.             public int uvCount, uvOffset;
  23.         }
  24.  
  25.         class MeshData
  26.         {
  27.             public int[] normalIndices;
  28.             public int[] positionIndices;
  29.             public int[] tangentIndices;
  30.             public int[] uvIndices;
  31.  
  32.             public Vector3[] normal;
  33.             public Vector3[] position;
  34.             public Vector4[] tangent;
  35.             public Vector2[] uv;
  36.         }
  37.  
  38.         static Mesh ReadMsh(string FileName)
  39.         {
  40.             string data = File.ReadAllText(FileName);
  41.             data = Regex.Replace(data, "[\\s\\n\\r\\t]+", " ");
  42.  
  43.             Mesh mesh = new Mesh();
  44.             string[] splitedData = data.Split(' ');
  45.  
  46.             string str = Regex.Match(data, "normal\\s=\\s{\\scount\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
  47.             MatchCollection match = Regex.Matches(str, "\\d+");
  48.             mesh.normalIndicesCount = Convert.ToInt32(match[0].Value);
  49.             mesh.normalIndicesOffset = Convert.ToInt32(match[1].Value);
  50.  
  51.             str = Regex.Match(data, "position\\s=\\s{\\scount\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
  52.             match = Regex.Matches(str, "\\d+");
  53.             mesh.positionIndicesCount = Convert.ToInt32(match[0].Value);
  54.             mesh.positionIndicesOffset = Convert.ToInt32(match[1].Value);
  55.  
  56.             str = Regex.Match(data, "tangent\\s=\\s{\\scount\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
  57.             match = Regex.Matches(str, "\\d+");
  58.             mesh.tangentIndicesCount = Convert.ToInt32(match[0].Value);
  59.             mesh.tangentIndicesOffset = Convert.ToInt32(match[1].Value);
  60.  
  61.             str = Regex.Match(data, "uv0\\s=\\s{\\scount\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
  62.             match = Regex.Matches(str, "\\d+");
  63.             mesh.uvIndicesCount = Convert.ToInt32(match[1].Value);
  64.             mesh.uvIndicesOffset = Convert.ToInt32(match[2].Value);
  65.  
  66.             str = Regex.Match(data, "normal\\s=\\s{\\scount\\s=\\s\\d+,\\snumComp\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
  67.             match = Regex.Matches(str, "\\d+");
  68.             mesh.normalCount = Convert.ToInt32(match[0].Value);
  69.             mesh.normalOffset = Convert.ToInt32(match[2].Value);
  70.  
  71.             str = Regex.Match(data, "position\\s=\\s{\\scount\\s=\\s\\d+,\\snumComp\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
  72.             match = Regex.Matches(str, "\\d+");
  73.             mesh.positionCount = Convert.ToInt32(match[0].Value);
  74.             mesh.positionOffset = Convert.ToInt32(match[2].Value);
  75.  
  76.             str = Regex.Match(data, "tangent\\s=\\s{\\scount\\s=\\s\\d+,\\snumComp\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
  77.             match = Regex.Matches(str, "\\d+");
  78.             mesh.tangentCount = Convert.ToInt32(match[0].Value);
  79.             mesh.tangentOffset = Convert.ToInt32(match[2].Value);
  80.  
  81.             str = Regex.Match(data, "uv0\\s=\\s{\\scount\\s=\\s\\d+,\\snumComp\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
  82.             match = Regex.Matches(str, "\\d+");
  83.             mesh.uvCount = Convert.ToInt32(match[1].Value);
  84.             mesh.uvOffset = Convert.ToInt32(match[3].Value);
  85.  
  86.             return mesh;
  87.         }
  88.  
  89.         static byte[] ReadBlob(string FileName)
  90.         {
  91.             return File.ReadAllBytes(FileName);
  92.         }
  93.  
  94.         static MeshData ParseBlob(byte[] data, Mesh mesh)
  95.         {
  96.             MeshData meshData = new MeshData();
  97.  
  98.             meshData.normalIndices = new int[mesh.normalIndicesCount / 4];
  99.             meshData.positionIndices = new int[mesh.positionIndicesCount / 4];
  100.             meshData.tangentIndices = new int[mesh.tangentIndicesCount / 4];
  101.             meshData.uvIndices = new int[mesh.uvIndicesCount / 4];
  102.  
  103.             for (int i = mesh.normalIndicesOffset, j = 0; j < mesh.normalIndicesCount / 4; i += 4, j++)
  104.                 meshData.normalIndices[j] = BitConverter.ToInt32(data, i);
  105.  
  106.             for (int i = mesh.positionIndicesOffset, j = 0; j < mesh.positionIndicesCount / 4; i += 4, j++)
  107.                 meshData.positionIndices[j] = BitConverter.ToInt32(data, i);
  108.  
  109.             for (int i = mesh.tangentIndicesOffset, j = 0; j < mesh.tangentIndicesCount / 4; i += 4, j++)
  110.                 meshData.tangentIndices[j] = BitConverter.ToInt32(data, i);
  111.  
  112.             for (int i = mesh.uvIndicesOffset, j = 0; j < mesh.uvIndicesCount / 4; i += 4, j++)
  113.                 meshData.uvIndices[j] = BitConverter.ToInt32(data, i);
  114.  
  115.             meshData.normal = new Vector3[mesh.normalCount / 3];
  116.             meshData.position = new Vector3[mesh.positionCount / 3];
  117.             meshData.tangent = new Vector4[mesh.tangentCount / 4];
  118.             meshData.uv = new Vector2[mesh.uvCount / 2];
  119.  
  120.             for (int i = mesh.normalOffset, j = 0; j < mesh.normalCount / 3 / 4; i += 12, j++)
  121.                 meshData.normal[j] = new Vector3(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + 4), BitConverter.ToSingle(data, i + 8));
  122.  
  123.             for (int i = mesh.positionOffset, j = 0; j < mesh.positionCount / 3 / 4; i += 12, j++)
  124.                 meshData.position[j] = new Vector3(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + 4), BitConverter.ToSingle(data, i + 8));
  125.  
  126.             for (int i = mesh.tangentOffset, j = 0; j < mesh.tangentCount / 4 / 4; i += 16, j++)
  127.                 meshData.tangent[j] = new Vector4(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + 4), BitConverter.ToSingle(data, i + 8), BitConverter.ToSingle(data, i + 12));
  128.  
  129.             for (int i = mesh.uvOffset, j = 0; j < mesh.uvCount / 2 / 4; i += 8, j++)
  130.                 meshData.uv[j] = new Vector2(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + 4));
  131.  
  132.             return meshData;
  133.         }
  134.  
  135.         static void Main(string[] Args)
  136.         {
  137.             if (Args.Length < 1)
  138.             {
  139.                 Console.WriteLine("Первым параметром ожидалось имя модели");
  140.                 Console.ReadKey(true);
  141.                 return;
  142.             }
  143.  
  144.             string meshFileName = Args[0];
  145.             string blobFileName = Args[0] + ".blob";
  146.  
  147.             if (!File.Exists(meshFileName))
  148.             {
  149.                 Console.WriteLine("Файл {0} не найден", meshFileName);
  150.                 Console.ReadKey(true);
  151.                 return;
  152.             }
  153.  
  154.             if (!File.Exists(blobFileName))
  155.             {
  156.                 Console.WriteLine("Файл {0} не найден", blobFileName);
  157.                 Console.ReadKey(true);
  158.                 return;
  159.             }
  160.  
  161.             Mesh mesh = ReadMsh(meshFileName);
  162.             byte[] blob = ReadBlob(blobFileName);
  163.             MeshData meshData = ParseBlob(blob, mesh);
  164.  
  165.             GameWindow window = new GameWindow(800, 600, GraphicsMode.Default, "Train-Fever Model Viewer 0.1 by Omich", GameWindowFlags.Default, DisplayDevice.Default, 2, 0, GraphicsContextFlags.Default);
  166.  
  167.             window.Load += delegate(object sender, EventArgs e)
  168.             {
  169.                 GL.Enable(EnableCap.DepthTest);
  170.                 GL.Enable(EnableCap.Lighting);
  171.                 GL.Enable(EnableCap.Light0);
  172.             };
  173.  
  174.             window.Resize += delegate(object sender, EventArgs e)
  175.             {
  176.                 GL.Viewport(0, 0, window.Width, window.Height);
  177.  
  178.                 GL.MatrixMode(MatrixMode.Projection);
  179.                 Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver2, (float)window.Width / window.Height, 0.1f, 1000.0f);
  180.                 GL.LoadMatrix(ref projection);
  181.  
  182.                 GL.MatrixMode(MatrixMode.Modelview);
  183.             };
  184.  
  185.             float angle = 0;
  186.  
  187.             window.RenderFrame += delegate(object sender, FrameEventArgs e)
  188.             {
  189.                 angle += (float)e.Time;
  190.  
  191.                 GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  192.  
  193.                 float x = 15 * (float)Math.Cos(angle);
  194.                 float y = 15 * (float)Math.Sin(angle);
  195.  
  196.                 Matrix4 view = Matrix4.LookAt(new Vector3(x, y, 1), Vector3.Zero, Vector3.UnitZ);
  197.                 GL.LoadMatrix(ref view);
  198.  
  199.                 GL.Begin(PrimitiveType.Triangles);
  200.  
  201.                 for (int i = 0; i < meshData.positionIndices.Length; i++)
  202.                 {
  203.                     GL.Normal3(meshData.normal[meshData.normalIndices[i]]);
  204.                     GL.TexCoord2(meshData.uv[meshData.uvIndices[i]]);
  205.                     GL.Vertex3(meshData.position[meshData.positionIndices[i]]);
  206.                 }
  207.  
  208.                 GL.End();
  209.  
  210.                 window.SwapBuffers();
  211.             };
  212.  
  213.             window.Run();
  214.         }
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement