protected override void CleanUp() { base.isLoad = false; _modelFilePath = String.Empty; _objectParameterQuantity = null; limb = null; }
/// <summary> /// Загрузчик файлов OBJ. Загружает все вершины, нормали и текстурные координаты в Limbs, где каждый limb - 1 под-объект. /// </summary> /// <param name="modelFilePath">Путь к файлу модели.</param> public override Int32 LoadModel(string modelFilePath) { _modelFilePath = modelFilePath; objectCounter(); limbs = new OBJ_Limb[_objectQuantity]; objectParameterCounter(); string modelFile = ""; bool objectCounterTrigger = false; StreamReader sr_modelFile = new StreamReader(_modelFilePath, Encoding.Default); for (Int32 i = 0; i < _objectQuantity; ++i) { limbs[i] = new OBJ_Limb(_objectParameterQuantity[i, 0], _objectParameterQuantity[i, 3], _objectParameterQuantity[i, 2], _objectParameterQuantity[i, 1]); Int32 j = 0; //iterator //trigger for correct work of file reader bool fileTrigger = false; //triggers for correct work of iterator j bool vCounterTrigger = false; bool vtCounterTrigger = false; bool vnCounterTrigger = false; bool fCounterTrigger = false; while (!sr_modelFile.EndOfStream) { #region Parsing file if (objectCounterTrigger == false) { modelFile = sr_modelFile.ReadLine(); } else { objectCounterTrigger = false; } if (modelFile.StartsWith("#")) { continue; } if (modelFile.StartsWith("o ")) // means object name { if (fileTrigger == false) { limbs[i].objectName = modelFile.Remove(0, 2); fileTrigger = true; continue; } else { break; } } if (modelFile.StartsWith("v ")) // means vertexes of object { if (vCounterTrigger == false) { j = 0; vCounterTrigger = true; } string[] splittedString = modelFile.Split(' '); limbs[i].vert[j, 0] = Convert.ToSingle(splittedString[1].Replace('.', ',')); limbs[i].vert[j, 1] = Convert.ToSingle(splittedString[2].Replace('.', ',')); limbs[i].vert[j, 2] = Convert.ToSingle(splittedString[3].Replace('.', ',')); if (j == _objectParameterQuantity[i, 0] - 1) { vnCounterTrigger = false; vtCounterTrigger = false; fCounterTrigger = false; } } else if (modelFile.StartsWith("vt ")) // means texture vertexes of object { if (vtCounterTrigger == false) { j = 0; vtCounterTrigger = true; } string[] splittedString = modelFile.Split(' '); limbs[i].t_vert[j, 0] = Convert.ToSingle(splittedString[1].Replace('.', ',')); limbs[i].t_vert[j, 1] = Convert.ToSingle(splittedString[2].Replace('.', ',')); if (j == _objectParameterQuantity[i, 1] - 1) { vnCounterTrigger = false; vCounterTrigger = false; fCounterTrigger = false; } } else if (modelFile.StartsWith("vn ")) // means normal vertexex of object { if (vnCounterTrigger == false) { j = 0; vnCounterTrigger = true; } string[] splittedString = modelFile.Split(' '); limbs[i].n_vert[j, 0] = Convert.ToSingle(splittedString[1].Replace('.', ',')); limbs[i].n_vert[j, 1] = Convert.ToSingle(splittedString[2].Replace('.', ',')); limbs[i].n_vert[j, 2] = Convert.ToSingle(splittedString[3].Replace('.', ',')); if (j == _objectParameterQuantity[i, 2] - 1) { vtCounterTrigger = false; vCounterTrigger = false; fCounterTrigger = false; } } else if (modelFile.StartsWith("f ")) // means faces of object (face includes 3 vertexes not 4) { if (fCounterTrigger == false) { j = 0; fCounterTrigger = true; } string face = modelFile.Remove(0, 2); string[] splittedString = face.Split(' '); Int32 sumOfPrevValsOfVertexes = 0; //for de-globalization of vertex counter in faces Int32 sumOfPrevValsOfTexVertexes = 0; //for de-globalization of texture vertex counter in faces Int32 sumOfPrevValsOfNormalVertexes = 0; //for de-globalization of normal vertex counter in faces if (i > 0) { for (Int32 l = 0; l < i; l++) { sumOfPrevValsOfVertexes += _objectParameterQuantity[l, 0]; sumOfPrevValsOfTexVertexes += _objectParameterQuantity[l, 1]; sumOfPrevValsOfNormalVertexes += _objectParameterQuantity[l, 2]; } } foreach (string item in splittedString) { string[] splittedItem = item.Split('/'); limbs[i].face[j, 0] = (splittedItem[0] == "") ? 0 : Convert.ToInt32(splittedItem[0]) - sumOfPrevValsOfVertexes; limbs[i].face[j, 1] = (splittedItem[1] == "") ? 0 : Convert.ToInt32(splittedItem[1]) - sumOfPrevValsOfTexVertexes; limbs[i].face[j, 2] = (splittedItem[2] == "") ? 0 : Convert.ToInt32(splittedItem[2]) - sumOfPrevValsOfNormalVertexes; j++; } if (j == _objectParameterQuantity[i, 3] - 1) { vnCounterTrigger = false; vtCounterTrigger = false; vCounterTrigger = false; } continue; } j++; #endregion } objectCounterTrigger = true; } sr_modelFile.Close(); isLoad = true; // загрузка завершена return(0); }
/// <summary> /// Загрузчик файлов OBJ. Загружает все вершины, нормали и текстурные координаты в общий Limb. /// </summary> /// <param name="modelFilePath">Путь к файлу модели.</param> public override Int32 LoadModel(string modelFilePath) { _modelFilePath = modelFilePath; objectParameterCounter(); string modelFile = ""; string[] splittedString = null; // iterators uint vCounter = 0, vtCounter = 0, vnCounter = 0, fCounter = 0; StreamReader sr_modelFile = new StreamReader(_modelFilePath, Encoding.Default); limb = new OBJ_Limb(_objectParameterQuantity[0], _objectParameterQuantity[3], _objectParameterQuantity[2], _objectParameterQuantity[1]); while (!sr_modelFile.EndOfStream) { modelFile = sr_modelFile.ReadLine(); #region Parsing file if (modelFile.StartsWith("#")) { continue; } if (modelFile.StartsWith("o ")) { continue; } if (modelFile.StartsWith("v ")) // means vertexes of object { splittedString = modelFile.Split(' '); limb.vert[vCounter, 0] = Convert.ToSingle(splittedString[1].Replace('.', ',')); limb.vert[vCounter, 1] = Convert.ToSingle(splittedString[2].Replace('.', ',')); limb.vert[vCounter, 2] = Convert.ToSingle(splittedString[3].Replace('.', ',')); splittedString = null; vCounter++; continue; } else if (modelFile.StartsWith("vt ")) // means texture vertexes of object { splittedString = modelFile.Split(' '); limb.t_vert[vtCounter, 0] = Convert.ToSingle(splittedString[1].Replace('.', ',')); limb.t_vert[vtCounter, 1] = Convert.ToSingle(splittedString[2].Replace('.', ',')); splittedString = null; vtCounter++; continue; } else if (modelFile.StartsWith("vn ")) // means normal vertexex of object { splittedString = modelFile.Split(' '); limb.n_vert[vnCounter, 0] = Convert.ToSingle(splittedString[1].Replace('.', ',')); limb.n_vert[vnCounter, 1] = Convert.ToSingle(splittedString[2].Replace('.', ',')); limb.n_vert[vnCounter, 2] = Convert.ToSingle(splittedString[3].Replace('.', ',')); splittedString = null; vnCounter++; continue; } else if (modelFile.StartsWith("f ")) // means faces of object (face includes 3 vertexes not 4) { string[] splittedItem = null; splittedString = modelFile.Remove(0, 2).Split(' '); foreach (string item in splittedString) { splittedItem = item.Split('/'); limb.face[fCounter, 0] = (splittedItem[0] == "") ? 0 : Convert.ToInt32(splittedItem[0]); limb.face[fCounter, 1] = (splittedItem[1] == "") ? 0 : Convert.ToInt32(splittedItem[1]); limb.face[fCounter, 2] = (splittedItem[2] == "") ? 0 : Convert.ToInt32(splittedItem[2]); splittedItem = null; fCounter++; } continue; } #endregion } sr_modelFile.Close(); #region Disposing of local vars vCounter = 0; vtCounter = 0; vnCounter = 0; fCounter = 0; modelFile = String.Empty; #endregion isLoad = true; // загрузка завершена return(0); }