Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using OpenTK;
- using OpenTK.Graphics;
- using OpenTK.Graphics.OpenGL;
- using System;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace TFMV
- {
- class Program
- {
- class Mesh
- {
- public int normalIndicesCount, normalIndicesOffset;
- public int positionIndicesCount, positionIndicesOffset;
- public int tangentIndicesCount, tangentIndicesOffset;
- public int uvIndicesCount, uvIndicesOffset;
- public int normalCount, normalOffset;
- public int positionCount, positionOffset;
- public int tangentCount, tangentOffset;
- public int uvCount, uvOffset;
- }
- class MeshData
- {
- public int[] normalIndices;
- public int[] positionIndices;
- public int[] tangentIndices;
- public int[] uvIndices;
- public Vector3[] normal;
- public Vector3[] position;
- public Vector4[] tangent;
- public Vector2[] uv;
- }
- static Mesh ReadMsh(string FileName)
- {
- string data = File.ReadAllText(FileName);
- data = Regex.Replace(data, "[\\s\\n\\r\\t]+", " ");
- Mesh mesh = new Mesh();
- string[] splitedData = data.Split(' ');
- string str = Regex.Match(data, "normal\\s=\\s{\\scount\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
- MatchCollection match = Regex.Matches(str, "\\d+");
- mesh.normalIndicesCount = Convert.ToInt32(match[0].Value);
- mesh.normalIndicesOffset = Convert.ToInt32(match[1].Value);
- str = Regex.Match(data, "position\\s=\\s{\\scount\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
- match = Regex.Matches(str, "\\d+");
- mesh.positionIndicesCount = Convert.ToInt32(match[0].Value);
- mesh.positionIndicesOffset = Convert.ToInt32(match[1].Value);
- str = Regex.Match(data, "tangent\\s=\\s{\\scount\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
- match = Regex.Matches(str, "\\d+");
- mesh.tangentIndicesCount = Convert.ToInt32(match[0].Value);
- mesh.tangentIndicesOffset = Convert.ToInt32(match[1].Value);
- str = Regex.Match(data, "uv0\\s=\\s{\\scount\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
- match = Regex.Matches(str, "\\d+");
- mesh.uvIndicesCount = Convert.ToInt32(match[1].Value);
- mesh.uvIndicesOffset = Convert.ToInt32(match[2].Value);
- str = Regex.Match(data, "normal\\s=\\s{\\scount\\s=\\s\\d+,\\snumComp\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
- match = Regex.Matches(str, "\\d+");
- mesh.normalCount = Convert.ToInt32(match[0].Value);
- mesh.normalOffset = Convert.ToInt32(match[2].Value);
- str = Regex.Match(data, "position\\s=\\s{\\scount\\s=\\s\\d+,\\snumComp\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
- match = Regex.Matches(str, "\\d+");
- mesh.positionCount = Convert.ToInt32(match[0].Value);
- mesh.positionOffset = Convert.ToInt32(match[2].Value);
- str = Regex.Match(data, "tangent\\s=\\s{\\scount\\s=\\s\\d+,\\snumComp\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
- match = Regex.Matches(str, "\\d+");
- mesh.tangentCount = Convert.ToInt32(match[0].Value);
- mesh.tangentOffset = Convert.ToInt32(match[2].Value);
- str = Regex.Match(data, "uv0\\s=\\s{\\scount\\s=\\s\\d+,\\snumComp\\s=\\s\\d+,\\soffset\\s=\\s\\d+,\\s}", RegexOptions.IgnorePatternWhitespace).Value;
- match = Regex.Matches(str, "\\d+");
- mesh.uvCount = Convert.ToInt32(match[1].Value);
- mesh.uvOffset = Convert.ToInt32(match[3].Value);
- return mesh;
- }
- static byte[] ReadBlob(string FileName)
- {
- return File.ReadAllBytes(FileName);
- }
- static MeshData ParseBlob(byte[] data, Mesh mesh)
- {
- MeshData meshData = new MeshData();
- meshData.normalIndices = new int[mesh.normalIndicesCount / 4];
- meshData.positionIndices = new int[mesh.positionIndicesCount / 4];
- meshData.tangentIndices = new int[mesh.tangentIndicesCount / 4];
- meshData.uvIndices = new int[mesh.uvIndicesCount / 4];
- for (int i = mesh.normalIndicesOffset, j = 0; j < mesh.normalIndicesCount / 4; i += 4, j++)
- meshData.normalIndices[j] = BitConverter.ToInt32(data, i);
- for (int i = mesh.positionIndicesOffset, j = 0; j < mesh.positionIndicesCount / 4; i += 4, j++)
- meshData.positionIndices[j] = BitConverter.ToInt32(data, i);
- for (int i = mesh.tangentIndicesOffset, j = 0; j < mesh.tangentIndicesCount / 4; i += 4, j++)
- meshData.tangentIndices[j] = BitConverter.ToInt32(data, i);
- for (int i = mesh.uvIndicesOffset, j = 0; j < mesh.uvIndicesCount / 4; i += 4, j++)
- meshData.uvIndices[j] = BitConverter.ToInt32(data, i);
- meshData.normal = new Vector3[mesh.normalCount / 3];
- meshData.position = new Vector3[mesh.positionCount / 3];
- meshData.tangent = new Vector4[mesh.tangentCount / 4];
- meshData.uv = new Vector2[mesh.uvCount / 2];
- for (int i = mesh.normalOffset, j = 0; j < mesh.normalCount / 3 / 4; i += 12, j++)
- meshData.normal[j] = new Vector3(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + 4), BitConverter.ToSingle(data, i + 8));
- for (int i = mesh.positionOffset, j = 0; j < mesh.positionCount / 3 / 4; i += 12, j++)
- meshData.position[j] = new Vector3(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + 4), BitConverter.ToSingle(data, i + 8));
- for (int i = mesh.tangentOffset, j = 0; j < mesh.tangentCount / 4 / 4; i += 16, j++)
- meshData.tangent[j] = new Vector4(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + 4), BitConverter.ToSingle(data, i + 8), BitConverter.ToSingle(data, i + 12));
- for (int i = mesh.uvOffset, j = 0; j < mesh.uvCount / 2 / 4; i += 8, j++)
- meshData.uv[j] = new Vector2(BitConverter.ToSingle(data, i), BitConverter.ToSingle(data, i + 4));
- return meshData;
- }
- static void Main(string[] Args)
- {
- if (Args.Length < 1)
- {
- Console.WriteLine("Первым параметром ожидалось имя модели");
- Console.ReadKey(true);
- return;
- }
- string meshFileName = Args[0];
- string blobFileName = Args[0] + ".blob";
- if (!File.Exists(meshFileName))
- {
- Console.WriteLine("Файл {0} не найден", meshFileName);
- Console.ReadKey(true);
- return;
- }
- if (!File.Exists(blobFileName))
- {
- Console.WriteLine("Файл {0} не найден", blobFileName);
- Console.ReadKey(true);
- return;
- }
- Mesh mesh = ReadMsh(meshFileName);
- byte[] blob = ReadBlob(blobFileName);
- MeshData meshData = ParseBlob(blob, mesh);
- GameWindow window = new GameWindow(800, 600, GraphicsMode.Default, "Train-Fever Model Viewer 0.1 by Omich", GameWindowFlags.Default, DisplayDevice.Default, 2, 0, GraphicsContextFlags.Default);
- window.Load += delegate(object sender, EventArgs e)
- {
- GL.Enable(EnableCap.DepthTest);
- GL.Enable(EnableCap.Lighting);
- GL.Enable(EnableCap.Light0);
- };
- window.Resize += delegate(object sender, EventArgs e)
- {
- GL.Viewport(0, 0, window.Width, window.Height);
- GL.MatrixMode(MatrixMode.Projection);
- Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver2, (float)window.Width / window.Height, 0.1f, 1000.0f);
- GL.LoadMatrix(ref projection);
- GL.MatrixMode(MatrixMode.Modelview);
- };
- float angle = 0;
- window.RenderFrame += delegate(object sender, FrameEventArgs e)
- {
- angle += (float)e.Time;
- GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
- float x = 15 * (float)Math.Cos(angle);
- float y = 15 * (float)Math.Sin(angle);
- Matrix4 view = Matrix4.LookAt(new Vector3(x, y, 1), Vector3.Zero, Vector3.UnitZ);
- GL.LoadMatrix(ref view);
- GL.Begin(PrimitiveType.Triangles);
- for (int i = 0; i < meshData.positionIndices.Length; i++)
- {
- GL.Normal3(meshData.normal[meshData.normalIndices[i]]);
- GL.TexCoord2(meshData.uv[meshData.uvIndices[i]]);
- GL.Vertex3(meshData.position[meshData.positionIndices[i]]);
- }
- GL.End();
- window.SwapBuffers();
- };
- window.Run();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement