/// <summary> /// Store the f, v, vt and vn lines into the object /// </summary> /// <param name="value"></param> /// <param name="objectObj"></param> /// <param name="allVertices"></param> /// <param name="indexesObj"></param> private static void VerticeData(string value, ObjectObj objectObj, AllVerticesDataObj allVertices, LocalIndexesObj indexesObj) { VertIndexesObj vertIndexes = new VertIndexesObj(); foreach (string indexes in ObjUtils.SplitVertData(value)) { VertIndexObj vertIndex = new VertIndexObj(); string[] indexList = ObjUtils.SplitIndexes(indexes); int length = indexList.Length; for (int i = 0; i < length; i++) { int index = 0; if (indexList[i] != "") // the vt line can be empty { if (Int32.TryParse(indexList[i], out int tmpIndex)) // Get the index { index = tmpIndex; } } if (i == 0) // v { if (index != 0) { vertIndex.V = indexesObj.vIndex; string vLine = allVertices.GetVIndex(index - 1); // Obj index start at 1 if (vLine != null) { objectObj.VerticesList.Add(new Point(vLine)); indexesObj.vIndex++; } } } else if (i == 1) // vt { if (index != 0) { vertIndex.Vt = indexesObj.vtIndex; string vtLine = allVertices.GetVtIndex(index - 1); // Obj index start at 1 if (vtLine != null) { objectObj.UVsList.Add(new UVCoordinates(vtLine)); indexesObj.vtIndex++; } } } else if (i == 2) // vn { if (index != 0) { vertIndex.Vn = indexesObj.vnIndex; string vnLine = allVertices.GetVnIndex(index - 1); // Obj index start at 1 if (vnLine != null) { objectObj.NormalsList.Add(new Vector(vnLine)); indexesObj.vnIndex++; } } } } vertIndexes.VertIndexList.Add(vertIndex); } objectObj.VertIndexesList.Add(vertIndexes); }
/// <summary> /// Calculate the normal vector for each triangle /// </summary> /// <param name="objData">Data parsed from the obj file</param> public static void CalculateNormal(ObjData objData) { foreach (ObjectObj objectObj in objData.ObjectsList) { // A triangle is made from 3 points LocalIndexesObj localIndexes = new LocalIndexesObj(); List <Vector> normalsList = objectObj.NormalsList; localIndexes.vnIndex = normalsList.Count; // Set the current max vnIndex List <Point> verticesList = objectObj.VerticesList; int verticesLength = verticesList.Count; foreach (VertIndexesObj vertIndexes in objectObj.VertIndexesList) { if (vertIndexes.VertIndexList.Count >= 3) // Should always be the case { VertIndexObj vertIndex1 = vertIndexes.VertIndexList[0]; VertIndexObj vertIndex2 = vertIndexes.VertIndexList[1]; VertIndexObj vertIndex3 = vertIndexes.VertIndexList[2]; // If a normal needs to be calculated if (vertIndex1.Vn == null || vertIndex2.Vn == null || vertIndex3.Vn == null) { // If all points indexes are correctly defined if (vertIndex1.V != null && vertIndex2.V != null && vertIndex3.V != null) { Point p1 = (vertIndex1.V < verticesLength) ? verticesList[(int)vertIndex1.V] : null; Point p2 = (vertIndex2.V < verticesLength) ? verticesList[(int)vertIndex2.V] : null; Point p3 = (vertIndex3.V < verticesLength) ? verticesList[(int)vertIndex3.V] : null; // If all indexes lead to existing points if (p1 != null && p2 != null && p3 != null) { Vector vector1 = new Vector(p1, p2); Vector vector2 = new Vector(p2, p3); Vector vectorNormal = Vector.GetNormalVector(vector1, vector2); normalsList.Add(vectorNormal.GetUnitVector()); // Add to the list the unit vector // We assign the index of the normal vector to if (vertIndex1.Vn == null) { vertIndex1.Vn = localIndexes.vnIndex; } if (vertIndex2.Vn == null) { vertIndex2.Vn = localIndexes.vnIndex; } if (vertIndex3.Vn == null) { vertIndex3.Vn = localIndexes.vnIndex; } // Increment the index for the next normal vector localIndexes.vnIndex++; } } } } } } }