private static void GetNativeTriangleList(StreamWriter OBJWriter, Extension_0003 extension, ref List <Triangle> triangleList, ref int totalVertexIndices) { NativeDataGC n = null; foreach (RWSection rw in extension.extensionSectionList) { if (rw is BinMeshPLG_050E binmesh) { if (binmesh.numMeshes == 0) { return; } } if (rw is NativeDataPLG_0510 native) { n = native.nativeDataStruct.nativeData; } } if (n == null) { throw new Exception(); } List <Vertex3> vertexList_init = new List <Vertex3>(); List <RenderWareFile.Color> colorList_init = new List <RenderWareFile.Color>(); List <TextCoord> textCoordList_init = new List <TextCoord>(); foreach (Declaration d in n.declarations) { foreach (object o in d.entryList) { if (o is Vertex3 v) { vertexList_init.Add(v); } else if (o is RenderWareFile.Color c) { colorList_init.Add(c); } else if (o is TextCoord t) { textCoordList_init.Add(t); } else { throw new Exception(); } } } foreach (TriangleDeclaration td in n.triangleDeclarations) { foreach (TriangleList tl in td.TriangleListList) { List <Vertex3> vertexList_final = new List <Vertex3>(); List <RenderWareFile.Color> colorList_final = new List <RenderWareFile.Color>(); List <TextCoord> textCoordList_final = new List <TextCoord>(); foreach (int[] objectList in tl.entries) { for (int j = 0; j < objectList.Count(); j++) { if (n.declarations[j].declarationType == Declarations.Vertex) { vertexList_final.Add(vertexList_init[objectList[j]]); } else if (n.declarations[j].declarationType == Declarations.Color) { colorList_final.Add(colorList_init[objectList[j]]); } else if (n.declarations[j].declarationType == Declarations.TextCoord) { textCoordList_final.Add(textCoordList_init[objectList[j]]); } } } bool control = true; for (int i = 2; i < vertexList_final.Count(); i++) { if (control) { triangleList.Add(new Triangle { MaterialIndex = td.MaterialIndex, vertex1 = i - 2 + totalVertexIndices, vertex2 = i - 1 + totalVertexIndices, vertex3 = i + totalVertexIndices }); control = false; } else { triangleList.Add(new Triangle { MaterialIndex = td.MaterialIndex, vertex1 = i - 2 + totalVertexIndices, vertex2 = i + totalVertexIndices, vertex3 = i - 1 + totalVertexIndices }); control = true; } } //Write vertex list to obj foreach (Vertex3 i in vertexList_final) { OBJWriter.WriteLine("v " + i.X.ToString() + " " + i.Y.ToString() + " " + i.Z.ToString()); } OBJWriter.WriteLine(); //Write uv list to obj if (textCoordList_final.Count() > 0) { if (Program.levelEditor.checkBoxFlipUVs.Checked) { foreach (TextCoord i in textCoordList_final) { OBJWriter.WriteLine("vt " + i.X.ToString() + " " + (-i.Y).ToString()); } } else { foreach (TextCoord i in textCoordList_final) { OBJWriter.WriteLine("vt " + i.X.ToString() + " " + i.Y.ToString()); } } } OBJWriter.WriteLine(); // Write vcolors to obj if (colorList_final.Count() > 0) { foreach (RenderWareFile.Color i in colorList_final) { OBJWriter.WriteLine("vc " + i.R.ToString() + " " + i.G.ToString() + " " + i.B.ToString() + " " + i.A.ToString()); } } OBJWriter.WriteLine(); totalVertexIndices += vertexList_final.Count(); } } }
private void AddNativeData(SharpDevice device, Extension_0003 extension, List <string> MaterialStream, Matrix transformMatrix) { isNativeData = true; NativeDataGC n = null; foreach (RWSection rw in extension.extensionSectionList) { if (rw is BinMeshPLG_050E binmesh) { if (binmesh.numMeshes == 0) { return; } } if (rw is NativeDataPLG_0510 native) { n = native.nativeDataStruct.nativeData; break; } } if (n == null) { throw new Exception(); } List <Vertex3> vertexList1 = new List <Vertex3>(); List <Vertex3> normalList = new List <Vertex3>(); List <RenderWareFile.Color> colorList = new List <RenderWareFile.Color>(); List <Vertex2> textCoordList = new List <Vertex2>(); foreach (Declaration d in n.declarations) { foreach (object o in d.entryList) { if (d.declarationType == Declarations.Vertex) { vertexList1.Add((Vertex3)o); } else if (d.declarationType == Declarations.Color) { colorList.Add((RenderWareFile.Color)o); } else if (d.declarationType == Declarations.TextCoord) { textCoordList.Add((Vertex2)o); } else if (d.declarationType == Declarations.Normal) { normalList.Add((Vertex3)o); } else { throw new Exception(); } } } List <VertexColoredTextured> vertexList = new List <VertexColoredTextured>(); List <int> indexList = new List <int>(); int k = 0; int previousAmount = 0; List <SharpSubSet> subSetList = new List <SharpSubSet>(); foreach (TriangleDeclaration td in n.triangleDeclarations) { foreach (TriangleList tl in td.TriangleListList) { foreach (int[] objectList in tl.entries) { Vector3 position = new Vector3(); SharpDX.Color color = new SharpDX.Color(255, 255, 255, 255); Vector2 textureCoordinate = new Vector2(); Vector3 normal = new Vector3(); for (int j = 0; j < objectList.Count(); j++) { if (n.declarations[j].declarationType == Declarations.Vertex) { position = (Vector3)Vector3.Transform( new Vector3( vertexList1[objectList[j]].X, vertexList1[objectList[j]].Y, vertexList1[objectList[j]].Z), transformMatrix); } else if (n.declarations[j].declarationType == Declarations.Color) { color = new SharpDX.Color(colorList[objectList[j]].R, colorList[objectList[j]].G, colorList[objectList[j]].B, colorList[objectList[j]].A); if (color.A == 0) { color = new SharpDX.Color(255, 255, 255, 255); } } else if (n.declarations[j].declarationType == Declarations.TextCoord) { textureCoordinate.X = textCoordList[objectList[j]].X; textureCoordinate.Y = textCoordList[objectList[j]].Y; } else if (n.declarations[j].declarationType == Declarations.Normal) { normal = new Vector3( normalList[objectList[j]].X, normalList[objectList[j]].Y, normalList[objectList[j]].Z); } } vertexList.Add(new VertexColoredTextured(position, textureCoordinate, color)); indexList.Add(k); k++; vertexListG.Add(position); } subSetList.Add(new SharpSubSet(previousAmount, vertexList.Count() - previousAmount, TextureManager.GetTextureFromDictionary(MaterialStream[td.MaterialIndex]), MaterialStream[td.MaterialIndex])); previousAmount = vertexList.Count(); } } if (vertexList.Count > 0) { for (int i = 2; i < indexList.Count; i++) { triangleList.Add(new Triangle(0, (ushort)(i + triangleListOffset - 2), (ushort)(i + triangleListOffset - 1), (ushort)(i + triangleListOffset))); } triangleListOffset += vertexList.Count; VertexColoredTextured[] vertices = vertexList.ToArray(); AddToMeshList(SharpMesh.Create(device, vertices, indexList.ToArray(), subSetList, SharpDX.Direct3D.PrimitiveTopology.TriangleStrip)); } else { AddToMeshList(null); } }
private static void GetNativeTriangleList(StreamWriter objWriter, Extension_0003 extension, List <string> materialList, ref List <Triangle> triangleList, ref int totalVertexIndices, bool flipUVs) { NativeDataGC n = null; foreach (RWSection rw in extension.extensionSectionList) { if (rw is BinMeshPLG_050E binmesh) { if (binmesh.numMeshes == 0) { return; } } if (rw is NativeDataPLG_0510 native) { n = native.nativeDataStruct.nativeData; } } if (n == null) { throw new Exception(); } List <Vertex3> vertexList_init = new List <Vertex3>(); List <Vertex3> normalList_init = new List <Vertex3>(); List <Color> colorList_init = new List <Color>(); List <Vertex2> textCoordList_init = new List <Vertex2>(); foreach (Declaration d in n.declarations) { foreach (object o in d.entryList) { if (d.declarationType == Declarations.Vertex) { vertexList_init.Add((Vertex3)o); } else if (d.declarationType == Declarations.Normal) { normalList_init.Add((Vertex3)o); } else if (d.declarationType == Declarations.Color) { colorList_init.Add((Color)o); } else if (d.declarationType == Declarations.TextCoord) { textCoordList_init.Add((Vertex2)o); } else { throw new Exception(); } } } foreach (TriangleDeclaration td in n.triangleDeclarations) { foreach (TriangleList tl in td.TriangleListList) { List <Vertex3> vertexList_final = new List <Vertex3>(); List <Vertex3> normalList_final = new List <Vertex3>(); List <Color> colorList_final = new List <Color>(); List <Vertex2> textCoordList_final = new List <Vertex2>(); foreach (int[] objectList in tl.entries) { for (int j = 0; j < objectList.Count(); j++) { if (n.declarations[j].declarationType == Declarations.Vertex) { vertexList_final.Add(vertexList_init[objectList[j]]); } else if (n.declarations[j].declarationType == Declarations.Normal) { normalList_final.Add(normalList_init[objectList[j]]); } else if (n.declarations[j].declarationType == Declarations.Color) { colorList_final.Add(colorList_init[objectList[j]]); } else if (n.declarations[j].declarationType == Declarations.TextCoord) { textCoordList_final.Add(textCoordList_init[objectList[j]]); } } } if (materialList != null) { triangleList.Clear(); } bool control = true; for (int i = 2; i < vertexList_final.Count(); i++) { if (control) { triangleList.Add(new Triangle { materialIndex = td.MaterialIndex, vertex1 = i - 2 + totalVertexIndices, vertex2 = i - 1 + totalVertexIndices, vertex3 = i + totalVertexIndices }); } else { triangleList.Add(new Triangle { materialIndex = td.MaterialIndex, vertex1 = i - 2 + totalVertexIndices, vertex2 = i + totalVertexIndices, vertex3 = i - 1 + totalVertexIndices }); } control = !control; } //Write vertex list to obj foreach (Vertex3 i in vertexList_final) { objWriter.WriteLine("v " + i.X.ToString() + " " + i.Y.ToString() + " " + i.Z.ToString()); } objWriter.WriteLine(); //Write normal list to obj foreach (Vertex3 i in normalList_final) { objWriter.WriteLine("v " + i.X.ToString() + " " + i.Y.ToString() + " " + i.Z.ToString()); } objWriter.WriteLine(); //Write uv list to obj if (textCoordList_final.Count() > 0) { if (flipUVs) { foreach (Vertex2 i in textCoordList_final) { objWriter.WriteLine("vt " + i.X.ToString() + " " + (-i.Y).ToString()); } } else { foreach (Vertex2 i in textCoordList_final) { objWriter.WriteLine("vt " + i.X.ToString() + " " + i.Y.ToString()); } } } objWriter.WriteLine(); // Write vcolors to obj if (colorList_final.Count() > 0) { foreach (Color i in colorList_final) { objWriter.WriteLine("vc " + i.R.ToString() + " " + i.G.ToString() + " " + i.B.ToString() + " " + i.A.ToString()); } } objWriter.WriteLine(); totalVertexIndices += vertexList_final.Count(); if (materialList == null) { return; } for (int i = 0; i < materialList.Count; i++) { if (!string.IsNullOrEmpty(lastMaterial) && lastMaterial != materialList[i]) { objWriter.WriteLine("g obj_" + materialList[i]); objWriter.WriteLine("usemtl " + materialList[i] + "_m"); } foreach (Triangle j in triangleList) { if (j.materialIndex == i) { objWriter.WriteLine("f " + j.vertex1.ToString() + "/" + j.vertex1.ToString() + "/" + j.vertex1.ToString() + " " + j.vertex2.ToString() + "/" + j.vertex2.ToString() + "/" + j.vertex2.ToString() + " " + j.vertex3.ToString() + "/" + j.vertex3.ToString() + "/" + j.vertex3.ToString()); } } objWriter.WriteLine(); lastMaterial = materialList[i]; } } } }
void AddNativeData(Extension_0003 extension, List <string> MaterialStream) { NativeDataGC n = null; foreach (RWSection rw in extension.extensionSectionList) { if (rw is BinMeshPLG_050E binmesh) { if (binmesh.numMeshes == 0) { return; } } if (rw is NativeDataPLG_0510 native) { n = native.nativeDataStruct.nativeData; break; } } if (n == null) { throw new Exception(ChunkName + ChunkNumber.ToString()); } List <Vertex3> vertexList1 = new List <Vertex3>(); List <RenderWareFile.Color> colorList = new List <RenderWareFile.Color>(); List <TextCoord> textCoordList = new List <TextCoord>(); foreach (Declaration d in n.declarations) { foreach (object o in d.entryList) { if (o is Vertex3 v) { vertexList1.Add(v); } else if (o is RenderWareFile.Color c) { colorList.Add(c); } else if (o is TextCoord t) { textCoordList.Add(t); } else { throw new Exception(); } } } List <VertexColoredTextured> vertexList = new List <VertexColoredTextured>(); List <int> indexList = new List <int>(); int k = 0; int previousAmount = 0; List <SharpSubSet> subSetList = new List <SharpSubSet>(); foreach (TriangleDeclaration td in n.triangleDeclarations) { foreach (TriangleList tl in td.TriangleListList) { foreach (int[] objectList in tl.entries) { VertexColoredTextured v = new VertexColoredTextured(); for (int j = 0; j < objectList.Count(); j++) { if (n.declarations[j].declarationType == Declarations.Vertex) { v.Position.X = vertexList1[objectList[j]].X; v.Position.Y = vertexList1[objectList[j]].Y; v.Position.Z = vertexList1[objectList[j]].Z; } else if (n.declarations[j].declarationType == Declarations.Color) { v.Color = new SharpDX.Color(colorList[objectList[j]].R, colorList[objectList[j]].G, colorList[objectList[j]].B, colorList[objectList[j]].A); } else if (n.declarations[j].declarationType == Declarations.TextCoord) { v.TextureCoordinate.X = textCoordList[objectList[j]].X; v.TextureCoordinate.Y = textCoordList[objectList[j]].Y; } } vertexList.Add(v); indexList.Add(k); k++; this.vertexList.Add(new Vector3(v.Position.X, v.Position.Y, v.Position.Z)); } if (BSPRenderer.TextureStream.ContainsKey(MaterialStream[td.MaterialIndex])) { subSetList.Add(new SharpSubSet(previousAmount, vertexList.Count() - previousAmount, BSPRenderer.TextureStream[MaterialStream[td.MaterialIndex]])); } else { subSetList.Add(new SharpSubSet(previousAmount, vertexList.Count() - previousAmount, BSPRenderer.whiteDefault)); } previousAmount = vertexList.Count(); } } if (vertexList.Count > 0) { meshList.Add(SharpMesh.Create(SharpRenderer.device, vertexList.ToArray(), indexList.ToArray(), subSetList, SharpDX.Direct3D.PrimitiveTopology.TriangleStrip)); } }
private static void GetNativeTriangleList(Scene scene, Extension_0003 extension) { NativeDataGC n = null; foreach (RWSection rw in extension.extensionSectionList) { if (rw is BinMeshPLG_050E binmesh) { if (binmesh.numMeshes == 0) { return; } } if (rw is NativeDataPLG_0510 native) { n = native.nativeDataStruct.nativeData; } } if (n == null) { throw new Exception("Native data not found"); } List <Vertex3> vertexList_init = new List <Vertex3>(); List <RenderWareFile.Color> colorList_init = new List <RenderWareFile.Color>(); List <Vertex2> textCoordList_init = new List <Vertex2>(); foreach (Declaration d in n.declarations) { foreach (object o in d.entryList) { if (o is Vertex3 v) { vertexList_init.Add(v); } else if (o is RenderWareFile.Color c) { colorList_init.Add(c); } else if (o is Vertex2 t) { textCoordList_init.Add(t); } else { throw new Exception(); } } } foreach (TriangleDeclaration td in n.triangleDeclarations) { Mesh mesh = new Mesh(PrimitiveType.Triangle) { MaterialIndex = td.MaterialIndex, Name = scene.Materials[td.MaterialIndex].Name.Replace("mat_", "mesh_") + "_" + (scene.MeshCount + 1).ToString() }; foreach (TriangleList tl in td.TriangleListList) { int totalVertexIndices = mesh.VertexCount; int vcount = 0; foreach (int[] objectList in tl.entries) { for (int j = 0; j < objectList.Count(); j++) { if (n.declarations[j].declarationType == Declarations.Vertex) { var v = vertexList_init[objectList[j]]; mesh.Vertices.Add(new Vector3D(v.X, v.Y, v.Z)); vcount++; } else if (n.declarations[j].declarationType == Declarations.Color) { var c = colorList_init[objectList[j]]; mesh.VertexColorChannels[0].Add(new Color4D( c.R / 255f, c.G / 255f, c.B / 255f, c.A / 255f)); } else if (n.declarations[j].declarationType == Declarations.TextCoord) { var v = textCoordList_init[objectList[j]]; mesh.TextureCoordinateChannels[0].Add(new Vector3D(v.X, v.Y, 0f)); } } } bool control = true; for (int i = 2; i < vcount; i++) { if (control) { mesh.Faces.Add(new Face(new int[] { i - 2 + totalVertexIndices, i - 1 + totalVertexIndices, i + totalVertexIndices })); control = false; } else { mesh.Faces.Add(new Face(new int[] { i - 2 + totalVertexIndices, i + totalVertexIndices, i - 1 + totalVertexIndices })); control = true; } } } scene.Meshes.Add(mesh); } }