/// <summary> /// Read collision file from file<para/> /// Чтение файлов коллизий из файла /// </summary> /// <param name="filename">File name<para/>Имя файла</param> public CollisionFile(string filename) { if (!File.Exists(filename)) { throw new FileNotFoundException("[CollisionFile] File not found: "+Path.GetFileName(filename), filename); } // Opening reader // Открытие ридера Collisions = new List<Group>(); BinaryReader f = new BinaryReader(new FileStream(filename, FileMode.Open, FileAccess.Read), Encoding.ASCII); // Read all the models // Чтение всех моделей while (f.BaseStream.Position<f.BaseStream.Length-1) { // Reading header // Чтение заголовка string header = new string(f.ReadChars(4)); if (header != "COLL") { throw new Exception("[CollisionFile] Unknown collision format: "+header); } f.BaseStream.Position += 4; // Creating new group // Создание новой группы Group c = new Group(); // Reading collision head // Чтение оглавления файла c.Name = f.ReadVCString(22).ToLower(); c.ID = f.ReadUInt16(); // Reading bounds // Чтение габаритов c.BoundsRadius = f.ReadSingle(); c.BoundsCenter = f.ReadVCVector(); c.BoundsMin = f.ReadVCVector(); c.BoundsMax = f.ReadVCVector(); // Reading objects // Чтение объектов int numSpheres = f.ReadInt32(); if (numSpheres>0) { // Spheres // Сферы c.Spheres = new Sphere[numSpheres]; for (int i = 0; i < numSpheres; i++) { Sphere s = new Sphere(); s.Radius = f.ReadSingle(); s.Center = f.ReadVCVector(); s.ReadParams(f); c.Spheres[i] = s; } } // Skip unknown index // Пропуск неизвестного числа f.BaseStream.Position += 4; int numBoxes = f.ReadInt32(); if (numBoxes>0) { // Boxes // Боксы c.Boxes = new Box[numBoxes]; for (int i = 0; i < numBoxes; i++) { Box b = new Box(); b.Min = f.ReadVCVector(); b.Max = f.ReadVCVector(); b.ReadParams(f); c.Boxes[i] = b; } } int numVerts = f.ReadInt32(); if (numVerts>0) { // Vertices and triangles // Вершины и треугольники c.Vertices = new Vector3[numVerts]; for (int i = 0; i < numVerts; i++) { c.Vertices[i] = f.ReadVCVector(); } // Reading trimeshes // Чтение тримешей int indexCount = f.ReadInt32(); List<int>[] indices = new List<int>[SurfaceMaterialsCount]; int meshCount = 0; for (int i = 0; i < indexCount; i++) { // Reading single tri // Чтение треугольника int v0 = f.ReadInt32(); int v1 = f.ReadInt32(); int v2 = f.ReadInt32(); int mat = f.ReadByte(); f.BaseStream.Position += 3; // Determining surf // Поиск поверхности if (indices[mat]==null) { indices[mat] = new List<int>(); meshCount++; } indices[mat].AddRange(new int[]{ v0, v1, v2 }); } // Storing trimeshes // Сохранение тримешей c.Meshes = new Trimesh[meshCount]; int p = 0; for (int i = 0; i < indices.Length; i++) { if (indices[i]!=null) { c.Meshes[p] = new Trimesh() { Flags = 0, Material = (byte)i, Indices = indices[i].ToArray() }; p++; } } }else{ // Skip indices - they are empty // Пропуск индексов - они пустые f.BaseStream.Position += 4; } // Store mesh // Сохранение меша if (c.Spheres != null || c.Boxes != null || c.Meshes !=null) { Collisions.Add(c); } // Give prior to main thread // Отдаём предпочтение основному потоку System.Threading.Thread.Sleep(0); } // Closing reader // Закрытие потока f.Close(); }
/// <summary> /// Read frames for bone from stream<para/> /// Чтение кадров анимации из файла /// </summary> Frame[] ReadFrames(BinaryReader f, int count) { // Reading header // Чтение заголовка Header h = ReadHeader(f); // Detecting single frame size // Определение размера одного кадра bool havePos = false; bool haveScale = false; if (h.Type == HeaderType.RotationTransitionKey) { havePos = true; } else if (h.Type == HeaderType.RotationTransitionScaleKey) { havePos = true; haveScale = true; } // Reading frames // Чтение кадров Frame[] frames = new Frame[count]; for (int i = 0; i < count; i++) { Frame fr = new Frame(); // Rotation // Поворот fr.Rotation = f.ReadVCQuaternion(); // Transition // Перемещение if (havePos) { fr.Transition = f.ReadVCVector(); fr.HasTransition = true; } // Scale // Изменение размера if (haveScale) { fr.Scale = f.ReadVCVector(); fr.HasScale = true; } // Frame time // Время кадра fr.Delay = f.ReadSingle(); frames[i] = fr; } return frames; }