private void AddPlane(SharpDevice device, PlaneSector_000A planeSection) { if (planeSection.leftSection is AtomicSector_0009 al) { AddAtomic(device, al); } else if (planeSection.leftSection is PlaneSector_000A pl) { AddPlane(device, pl); } else { throw new Exception(); } if (planeSection.rightSection is AtomicSector_0009 ar) { AddAtomic(device, ar); } else if (planeSection.rightSection is PlaneSector_000A pr) { AddPlane(device, pr); } else { throw new Exception(); } }
/// <summary> /// Draw Mesh /// </summary> public void Draw(SharpDevice Device) { Device.DeviceContext.InputAssembler.PrimitiveTopology = primitiveTopology; Device.DeviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(VertexBuffer, VertexSize, 0)); Device.DeviceContext.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0); Device.DeviceContext.DrawIndexed(SubSets[0].IndexCount, 0, 0); }
/// <summary> /// Draw all vertices as points /// </summary> /// <param name="count"></param> public void DrawPoints(SharpDevice Device, int count = int.MaxValue) { Device.DeviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.PointList; Device.DeviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(VertexBuffer, VertexSize, 0)); Device.DeviceContext.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0); Device.DeviceContext.DrawIndexed(Math.Min(count, SubSets[0].IndexCount), 0, 0); }
static void Main() { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-us"); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); if (!SharpDevice.IsDirectX11Supported()) { MessageBox.Show("DirectX11 feature level 11.0 is required to run Heroes Power Plant. Maximum supported feature level is " + SharpDevice.GetSupportedFeatureLevel().ToString() + ". Please update your DirectX."); return; } MainForm = new MainForm.MainForm(); Application.Run(MainForm); try { } catch (Exception ex) { MessageBox.Show($"Unhandled Exception in Power Plant: {ex.Message} {ex.InnerException} {ex.StackTrace}"); } }
/// <summary> /// Create From Vertices and Indices array /// </summary> /// <typeparam name="VType">Vertex Type</typeparam> /// <param name="device">Device</param> /// <param name="vertices">Vertices</param> /// <param name="indices">Indices</param> /// <returns>Mesh</returns> public static SharpMesh Create <VType>(SharpDevice device, VType[] vertices, int[] indices, List <SharpSubSet> SubSets, PrimitiveTopology topology = PrimitiveTopology.TriangleList) where VType : struct { return(new SharpMesh(device) { VertexBuffer = Buffer11.Create <VType>(device.Device, BindFlags.VertexBuffer, vertices), IndexBuffer = Buffer11.Create(device.Device, BindFlags.IndexBuffer, indices), VertexSize = Utilities.SizeOf <VType>(), SubSets = SubSets, primitiveTopology = topology, }); }
public static void SetSharpShader(SharpDevice device) { basicShader = new SharpShader(device, "Resources/SharpDX/Shader_Basic.hlsl", new SharpShaderDescription() { VertexShaderFunction = "VS", PixelShaderFunction = "PS" }, new InputElement[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0) }); basicBuffer = basicShader.CreateBuffer <DefaultRenderData>(); defaultShader = new SharpShader(device, "Resources/SharpDX/Shader_Default.hlsl", new SharpShaderDescription() { VertexShaderFunction = "VS", PixelShaderFunction = "PS" }, new InputElement[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0), new InputElement("COLOR", 0, Format.R8G8B8A8_UNorm, 12, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16, 0) }); defaultBuffer = defaultShader.CreateBuffer <Matrix>(); tintedShader = new SharpShader(device, "Resources/SharpDX/Shader_Tinted.hlsl", new SharpShaderDescription() { VertexShaderFunction = "VS", PixelShaderFunction = "PS" }, new InputElement[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0), new InputElement("COLOR", 0, Format.R8G8B8A8_UNorm, 12, 0), new InputElement("TEXCOORD", 0, Format.R32G32_Float, 16, 0) }); tintedBuffer = defaultShader.CreateBuffer <DefaultRenderData>(); collisionShader = new SharpShader(device, "Resources/SharpDX/Shader_Collision.hlsl", new SharpShaderDescription() { VertexShaderFunction = "VS", PixelShaderFunction = "PS" }, new InputElement[] { new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("NORMAL", 0, Format.R32G32B32_Float, 16, 0), new InputElement("COLOR", 0, Format.R8G8B8A8_UNorm, 28, 0) }); collisionBuffer = collisionShader.CreateBuffer <CollisionRenderData>(); }
/// <summary> /// Constructor /// </summary> /// <param name="device">Device</param> /// <param name="filename">Path of shader file</param> /// <param name="description">Description structure</param> /// <param name="elements">Input Layout Elements</param> public SharpShader(SharpDevice device, string filename, SharpShaderDescription description, InputElement[] elements) { Device = device; // Compile Vertex and Pixel shaders var vertexShaderByteCode = ShaderBytecode.CompileFromFile(filename, description.VertexShaderFunction, "vs_5_0"); VertexShader = new VertexShader(Device.Device, vertexShaderByteCode); //create pixel shader if (!string.IsNullOrEmpty(description.PixelShaderFunction)) { var pixelShaderByteCode = ShaderBytecode.CompileFromFile(filename, description.PixelShaderFunction, "ps_5_0"); PixelShader = new PixelShader(Device.Device, pixelShaderByteCode); } if (!string.IsNullOrEmpty(description.GeometryShaderFunction)) { var geometryShaderByteCode = ShaderBytecode.CompileFromFile(filename, description.GeometryShaderFunction, "gs_5_0"); if (description.GeometrySO == null) { GeometryShader = new GeometryShader(Device.Device, geometryShaderByteCode); } else { int[] size = new int[] { description.GeometrySO.Select(e => e.ComponentCount * 4).Sum() }; GeometryShader = new GeometryShader(Device.Device, geometryShaderByteCode, description.GeometrySO, size, -1); } } if (!string.IsNullOrEmpty(description.DomainShaderFunction)) { var domainShaderByteCode = ShaderBytecode.CompileFromFile(filename, description.DomainShaderFunction, "ds_5_0"); DomainShader = new DomainShader(Device.Device, domainShaderByteCode); } if (!string.IsNullOrEmpty(description.HullShaderFunction)) { var hullShaderByteCode = ShaderBytecode.CompileFromFile(filename, description.HullShaderFunction, "hs_5_0"); HullShader = new HullShader(Device.Device, hullShaderByteCode); } var signature = ShaderSignature.GetInputSignature(vertexShaderByteCode); // Layout from VertexShader input signature Layout = new InputLayout(Device.Device, signature, elements); }
/// <summary> /// Load texture from file /// </summary> /// <param name="device">Device</param> /// <param name="filename">Filename</param> /// <returns>Shader Resource View</returns> public static ShaderResourceView LoadTextureFromFile(this SharpDevice device, string filename) { string ext = System.IO.Path.GetExtension(filename); if (ext.ToLower() == ".dds") { return(CreateTextureFromDDS(device.Device, device.DeviceContext, System.IO.File.ReadAllBytes(filename), out bool isCube)); } else if (ext.ToLower() == ".png") { return(CreateTextureFromBitmap(device.Device, device.DeviceContext, filename)); } else { throw new Exception("Unsupported image format: " + filename); } }
public void Render(SharpDevice device) { foreach (SharpMesh mesh in meshList) { if (mesh == null) { continue; } mesh.Begin(device); for (int i = 0; i < mesh.SubSets.Count(); i++) { device.DeviceContext.PixelShader.SetShaderResource(0, mesh.SubSets[i].DiffuseMap); mesh.Draw(device, i); } } }
public SharpRenderer(Control control) { ResetColors(); Device = new SharpDevice(control, false); LoadModels(); SharpFps = new SharpFPS { FPSLimit = float.MaxValue }; Camera = new SharpCamera(SharpFps); Camera.ProjectionMatrix.AspectRatio = (float)control.ClientSize.Width / control.ClientSize.Height; SetSharpShader(); LoadTexture(); }
public SharpRenderer(Control control) { if (!SharpDevice.IsDirectX11Supported()) { MessageBox.Show("DirectX11 Not Supported"); return; } device = new SharpDevice(control); LoadModels(); aspectRatio = (float)control.ClientSize.Width / control.ClientSize.Height; sharpFPS = new SharpFPS(); sharpFPS.Reset(); SetSharpShader(device); }
public void SetHeroesBSPList(SharpDevice device, Archive heroesONEfile) { Dispose(); ReadFileMethods.isShadow = false; BSPList = new List <RenderWareModelFile>(heroesONEfile.Files.Count); ShadowColBSPList = new List <RenderWareModelFile>(); foreach (ArchiveFile file in heroesONEfile.Files) { if (!(new string[] { ".bsp", ".rg1", ".rx1" }.Contains(Path.GetExtension(file.Name).ToLower()))) { continue; } RenderWareModelFile TempBSPFile = new RenderWareModelFile(file.Name); TempBSPFile.SetChunkNumberAndName(); byte[] uncompressedData = file.DecompressThis(); TempBSPFile.SetForRendering(device, ReadFileMethods.ReadRenderWareFile(uncompressedData), uncompressedData); BSPList.Add(TempBSPFile); } }
void AddAtomic(SharpDevice device, AtomicSector_0009 AtomicSector) { if (AtomicSector.atomicSectorStruct.isNativeData) { AddNativeData(device, AtomicSector.atomicSectorExtension, MaterialList, Matrix.Identity); return; } List <VertexColoredTextured> vertexList = new List <VertexColoredTextured>(); foreach (Vertex3 v in AtomicSector.atomicSectorStruct.vertexArray) { vertexList.Add(new VertexColoredTextured(new Vector3(v.X, v.Y, v.Z), new Vector2(), new SharpDX.Color())); vertexListG.Add(new Vector3(v.X, v.Y, v.Z)); } if (!isShadowCollision) { for (int i = 0; i < vertexList.Count; i++) { RenderWareFile.Color c = AtomicSector.atomicSectorStruct.colorArray[i]; VertexColoredTextured v = vertexList[i]; v.Color = new SharpDX.Color(c.R, c.G, c.B, c.A); vertexList[i] = v; } for (int i = 0; i < vertexList.Count; i++) { Vertex2 tc = AtomicSector.atomicSectorStruct.uvArray[i]; VertexColoredTextured v = vertexList[i]; v.TextureCoordinate = new Vector2(tc.X, tc.Y); vertexList[i] = v; } } List <SharpSubSet> SubsetList = new List <SharpSubSet>(); List <int> indexList = new List <int>(); int previousIndexCount = 0; for (int i = 0; i < MaterialList.Count; i++) { for (int j = 0; j < AtomicSector.atomicSectorStruct.triangleArray.Length; j++) // each (Triangle t in AtomicSector.atomicStruct.triangleArray) { Triangle t = AtomicSector.atomicSectorStruct.triangleArray[j]; if (t.materialIndex == i) { indexList.Add(t.vertex1); indexList.Add(t.vertex2); indexList.Add(t.vertex3); if (isShadowCollision) { RenderWareFile.Color c = RenderWareFile.Color.FromString(MaterialList[i]); SharpDX.Color color = new SharpDX.Color(c.R, c.G, c.B, c.A); VertexColoredTextured v1 = vertexList[t.vertex1]; v1.Color = color; vertexList[t.vertex1] = v1; VertexColoredTextured v2 = vertexList[t.vertex2]; v2.Color = color; vertexList[t.vertex2] = v2; VertexColoredTextured v3 = vertexList[t.vertex3]; v3.Color = color; vertexList[t.vertex3] = v3; } triangleList.Add(new Triangle(t.materialIndex, (ushort)(t.vertex1 + triangleListOffset), (ushort)(t.vertex2 + triangleListOffset), (ushort)(t.vertex3 + triangleListOffset))); } } if (indexList.Count - previousIndexCount > 0) { SubsetList.Add(new SharpSubSet(previousIndexCount, indexList.Count - previousIndexCount, TextureManager.GetTextureFromDictionary(MaterialList[i]), MaterialList[i])); } previousIndexCount = indexList.Count(); } triangleListOffset += AtomicSector.atomicSectorStruct.vertexArray.Length; if (SubsetList.Count > 0) { meshList.Add(SharpMesh.Create(device, vertexList.ToArray(), indexList.ToArray(), SubsetList)); } }
/// <summary> /// Set all buffer and topology property to speed up rendering /// </summary> public void Begin(SharpDevice Device) { Device.DeviceContext.InputAssembler.PrimitiveTopology = primitiveTopology; Device.DeviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(VertexBuffer, VertexSize, 0)); Device.DeviceContext.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0); }
private SharpMesh(SharpDevice device) { Device = device; }
public void SetForRendering(SharpDevice device, RWSection[] rwChunkList, byte[] rwByteArray) { rwSectionArray = rwChunkList; if (rwByteArray == null) { rwSectionByteArray = ReadFileMethods.ExportRenderWareFile(rwSectionArray, isShadowCollision ? LevelEditor.BSP_IO_ShadowCollision.shadowRenderWareVersion : LevelEditor.BSP_IO_Heroes.heroesRenderWareVersion); #if RELEASE if (fileSizeCheck && rwSectionByteArray.Length > 450 * 1024) { System.Windows.Forms.MessageBox.Show(fileName + " is a very large file. It might crash the game if you don't use TONER mod to enable the game to load bigger files than normally."); } #endif } else { rwSectionByteArray = rwByteArray; } meshList = new List <SharpMesh>(); vertexListG = new List <Vector3>(); triangleList = new List <Triangle>(); triangleListOffset = 0; foreach (RWSection rwSection in rwSectionArray) { if (rwSection is World_000B w) { vertexAmount = w.worldStruct.numVertices; triangleAmount = w.worldStruct.numTriangles; foreach (Material_0007 m in w.materialList.materialList) { if (isShadowCollision) { MaterialList.Add(m.materialStruct.color.ToString()); } else if (m.texture != null) { MaterialList.Add(m.texture.diffuseTextureName.stringString); } else { MaterialList.Add(DefaultTexture); } } if (w.firstWorldChunk is AtomicSector_0009 a) { AddAtomic(device, a); } else if (w.firstWorldChunk is PlaneSector_000A p) { AddPlane(device, p); } } else if (rwSection is Clump_0010 c) { for (int g = 0; g < c.geometryList.geometryList.Count; g++) { AddGeometry(device, c.geometryList.geometryList[g], CreateMatrix(c.frameList, c.atomicList[g].atomicStruct.frameIndex)); } } } }
void AddNativeData(SharpDevice device, Extension_0003 extension, List <string> MaterialStream, Matrix transformMatrix) { 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 <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.R == 0 && color.G == 0 && color.B == 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; meshList.Add(SharpMesh.Create(device, vertexList.ToArray(), indexList.ToArray(), subSetList, SharpDX.Direct3D.PrimitiveTopology.TriangleStrip)); } }
/// <summary> /// Draw subset /// </summary> /// <param name="subset">Subsets</param> public void Draw(SharpDevice Device, int subset) { Device.DeviceContext.DrawIndexed(SubSets[subset].IndexCount, SubSets[subset].StartIndex, 0); }
/// <summary> /// Draw subset /// </summary> /// <param name="subset">Subsets</param> public void Draw(SharpDevice Device, int subset) { Device.DeviceContext.PixelShader.SetShaderResource(0, SubSets[subset].DiffuseMap); Device.DeviceContext.DrawIndexed(SubSets[subset].IndexCount, SubSets[subset].StartIndex, 0); }
public static ShaderResourceView LoadTextureFromRenderWareNative(this SharpDevice device, TextureNativeStruct_0001 tnStruct) { Format format = Format.Unknown; if (tnStruct.compression == 0) { //if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_C1555) != 0) // format = Format.B5G5R5A1_UNorm; //if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_C4444) != 0) // format = Format.B4G4R4A4_UNorm; //else if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_C555) != 0) // format = Format.B5G5R5A1_UNorm; if ((tnStruct.rasterFormatFlags & (TextureRasterFormat)0x0F00) == TextureRasterFormat.RASTER_C565) { format = Format.B5G6R5_UNorm; } if ((tnStruct.rasterFormatFlags & (TextureRasterFormat)0x0F00) == TextureRasterFormat.RASTER_C8888) { format = Format.B8G8R8A8_UNorm; } if ((tnStruct.rasterFormatFlags & (TextureRasterFormat)0x0F00) == TextureRasterFormat.RASTER_C888) { format = Format.B8G8R8A8_UNorm; } //else if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_D16) != 0) // format = Format.D16_UNorm; //else if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_D24) != 0) // format = Format.D24_UNorm_S8_UInt; //else if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_D32) != 0) // format = Format.D32_Float; //else if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_PAL4) != 0) // format = Format.P8; //else if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_PAL8) != 0) // format = Format.P8; } else if (tnStruct.compression == 1) { format = Format.BC1_UNorm; } else { return(null); } if (format == Format.Unknown) { throw new Exception(tnStruct.textureName); } MipMapEntry[] mipMaps; if ((tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_PAL4) != 0 | (tnStruct.rasterFormatFlags & TextureRasterFormat.RASTER_PAL8) != 0) { mipMaps = ConvertFromPalette(tnStruct.mipMaps, tnStruct.mipMapCount, tnStruct.rasterFormatFlags, tnStruct.palette); if (tnStruct.platformType == 5) { format = Format.R8G8B8A8_UNorm; } } else { mipMaps = tnStruct.mipMaps; } DataRectangle[] dataRectangles = FillInitData(mipMaps, tnStruct.width, tnStruct.height, tnStruct.bitDepth, tnStruct.mipMapCount, format); Texture2D buffer = null; while (buffer == null) { try { buffer = new Texture2D(device.Device, new Texture2DDescription() { MipLevels = tnStruct.mipMapCount, Format = format, Width = tnStruct.width, Height = tnStruct.height, ArraySize = 1, BindFlags = BindFlags.ShaderResource, Usage = ResourceUsage.Default, SampleDescription = new SampleDescription(1, 0), OptionFlags = ResourceOptionFlags.None, }, dataRectangles); } catch { } } ShaderResourceView resourceView = new ShaderResourceView(device.Device, buffer); buffer.Dispose(); return(resourceView); }
private void AddGeometry(SharpDevice device, Geometry_000F g, Matrix transformMatrix) { List <string> MaterialList = new List <string>(); foreach (Material_0007 m in g.materialList.materialList) { if (m.texture != null) { string textureName = m.texture.diffuseTextureName.stringString; if (!MaterialList.Contains(textureName)) { MaterialList.Add(textureName); } } else { MaterialList.Add(DefaultTexture); } } if ((g.geometryStruct.geometryFlags2 & GeometryFlags2.isNativeGeometry) != 0) { AddNativeData(device, g.geometryExtension, MaterialList, transformMatrix); return; } List <VertexColoredTextured> vertexList = new List <VertexColoredTextured>(); if ((g.geometryStruct.geometryFlags & GeometryFlags.hasVertexPositions) != 0) { foreach (Vertex3 v in g.geometryStruct.morphTargets[0].vertices) { Vector3 Position = (Vector3)Vector3.Transform(new Vector3(v.X, v.Y, v.Z), transformMatrix); vertexList.Add(new VertexColoredTextured(Position, new Vector2(), SharpDX.Color.White)); vertexListG.Add(Position); } } if ((g.geometryStruct.geometryFlags & GeometryFlags.hasVertexColors) != 0) { for (int i = 0; i < vertexList.Count; i++) { RenderWareFile.Color c = g.geometryStruct.vertexColors[i]; VertexColoredTextured v = vertexList[i]; v.Color = new SharpDX.Color(c.R, c.G, c.B, c.A); vertexList[i] = v; } } else { for (int i = 0; i < vertexList.Count; i++) { VertexColoredTextured v = vertexList[i]; v.Color = SharpDX.Color.White; vertexList[i] = v; } } if ((g.geometryStruct.geometryFlags & GeometryFlags.hasTextCoords) != 0) { for (int i = 0; i < vertexList.Count; i++) { Vertex2 tc = g.geometryStruct.textCoords[i]; VertexColoredTextured v = vertexList[i]; v.TextureCoordinate = new Vector2(tc.X, tc.Y); vertexList[i] = v; } } List <SharpSubSet> SubsetList = new List <SharpSubSet>(); List <int> indexList = new List <int>(); int previousIndexCount = 0; for (int i = 0; i < MaterialList.Count; i++) { foreach (Triangle t in g.geometryStruct.triangles) { if (t.materialIndex == i) { indexList.Add(t.vertex1); indexList.Add(t.vertex2); indexList.Add(t.vertex3); triangleList.Add(new Triangle(t.materialIndex, (ushort)(t.vertex1 + triangleListOffset), (ushort)(t.vertex2 + triangleListOffset), (ushort)(t.vertex3 + triangleListOffset))); } } if (indexList.Count - previousIndexCount > 0) { SubsetList.Add(new SharpSubSet(previousIndexCount, indexList.Count - previousIndexCount, TextureManager.GetTextureFromDictionary(MaterialList[i]), MaterialList[i])); } previousIndexCount = indexList.Count(); } triangleListOffset += vertexList.Count; if (SubsetList.Count > 0) { meshList.Add(SharpMesh.Create(device, vertexList.ToArray(), indexList.ToArray(), SubsetList)); } }
private void AddGeometry(SharpDevice device, Geometry_000F g, Matrix transformMatrix) { List <string> materialList = new List <string>(); foreach (Material_0007 m in g.materialList.materialList) { if (m.texture != null) { string textureName = m.texture.diffuseTextureName.stringString; materialList.Add(textureName); } else { materialList.Add(DefaultTexture); } } if ((g.geometryStruct.geometryFlags2 & GeometryFlags2.isNativeGeometry) != 0) { AddNativeData(device, g.geometryExtension, materialList, transformMatrix); return; } List <Vector3> vertexList1 = new List <Vector3>(); List <Vector3> normalList = new List <Vector3>(); List <Vector2> textCoordList = new List <Vector2>(); List <SharpDX.Color> colorList = new List <SharpDX.Color>(); if ((g.geometryStruct.geometryFlags & GeometryFlags.hasVertexPositions) != 0) { MorphTarget m = g.geometryStruct.morphTargets[0]; foreach (Vertex3 v in m.vertices) { Vector3 pos = (Vector3)Vector3.Transform(new Vector3(v.X, v.Y, v.Z), transformMatrix); vertexList1.Add(pos); vertexListG.Add(pos); } } if ((g.geometryStruct.geometryFlags & GeometryFlags.hasNormals) != 0) { for (int i = 0; i < vertexList1.Count; i++) { normalList.Add(new Vector3(g.geometryStruct.morphTargets[0].normals[i].X, g.geometryStruct.morphTargets[0].normals[i].Y, g.geometryStruct.morphTargets[0].normals[i].Z)); } } if ((g.geometryStruct.geometryFlags & GeometryFlags.hasVertexColors) != 0) { for (int i = 0; i < vertexList1.Count; i++) { RenderWareFile.Color c = g.geometryStruct.vertexColors[i]; colorList.Add(new SharpDX.Color(c.R, c.G, c.B, c.A)); } } else { for (int i = 0; i < vertexList1.Count; i++) { colorList.Add(new SharpDX.Color(1f, 1f, 1f, 1f)); } } if ((g.geometryStruct.geometryFlags & GeometryFlags.hasTextCoords) != 0) { for (int i = 0; i < vertexList1.Count; i++) { Vertex2 tc = g.geometryStruct.textCoords[i]; textCoordList.Add(new Vector2(tc.X, tc.Y)); } } else { for (int i = 0; i < vertexList1.Count; i++) { textCoordList.Add(new Vector2()); } } List <SharpSubSet> SubsetList = new List <SharpSubSet>(); List <int> indexList = new List <int>(); int previousIndexCount = 0; for (int i = 0; i < materialList.Count; i++) { foreach (Triangle t in g.geometryStruct.triangles) { if (t.materialIndex == i) { indexList.Add(t.vertex1); indexList.Add(t.vertex2); indexList.Add(t.vertex3); triangleList.Add(new Triangle(t.materialIndex, (ushort)(t.vertex1 + triangleListOffset), (ushort)(t.vertex2 + triangleListOffset), (ushort)(t.vertex3 + triangleListOffset))); } } if (indexList.Count - previousIndexCount > 0) { SubsetList.Add(new SharpSubSet(previousIndexCount, indexList.Count - previousIndexCount, TextureManager.GetTextureFromDictionary(materialList[i]), materialList[i])); } previousIndexCount = indexList.Count(); } triangleListOffset += vertexList1.Count; if (SubsetList.Count > 0) { VertexColoredTextured[] vertices = new VertexColoredTextured[vertexList1.Count]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = new VertexColoredTextured(vertexList1[i], textCoordList[i], colorList[i]); } meshList.Add(SharpMesh.Create(device, vertices, indexList.ToArray(), SubsetList)); } }