public void RunMainLoop(Panel Panel, MainForm.MainForm mainForm) { RenderLoop.Run(Panel, () => { if (dontRender) { return; } //Resizing if (Device.MustResize) { Device.Resize(); Camera.ProjectionMatrix.AspectRatio = (float)Panel.Width / Panel.Height; } mainForm.KeyboardController(); mainForm.SetToolStripStatusLabel(Camera + " FPS: " + $"{SharpFps.StatFPS:0.0000}"); //clear color Device.Clear(backgroundColor); //Set matrices viewProjection = Camera.ViewMatrix.GetViewMatrix() * Camera.ProjectionMatrix.GetProjectionMatrix(); frustum = new BoundingFrustum(viewProjection); mainForm.TexturePatternEditor.Animate(); if (ShowCollision) { foreach (var c in mainForm.CollisionEditorDict.Values) { c.RenderCollisionModel(this); } mainForm.LevelEditor.RenderShadowCollisionModel(this); } else { mainForm.LevelEditor.RenderLevelModel(this); } if (ShowChunkBoxes) { mainForm.LevelEditor.visibilityFunctions.RenderChunkModels(this); } if (ShowObjects == CheckState.Checked) { foreach (var l in mainForm.LayoutEditorDict.Values) { l.RenderSetObjects(this, true); } } else if (ShowObjects == CheckState.Indeterminate) { foreach (var l in mainForm.LayoutEditorDict.Values) { l.RenderSetObjects(this, false); } } if (ShowCameras) { mainForm.CameraEditor.RenderCameras(this); } if (ShowStartPositions) { mainForm.ConfigEditor.RenderStartPositions(this); } if (ShowSplines) { mainForm.ConfigEditor.SplineEditor.RenderSplines(this); mainForm.LevelEditor.shadowSplineEditor.RenderSplines(this); } if (ShowQuadtree) { foreach (var c in mainForm.CollisionEditorDict.Values) { c.RenderQuadtree(this); } } //present Device.Present(); SharpFps.EndFrame(); }); //release resources whiteDefault.Dispose(); mainForm.LevelEditor.bspRenderer.Dispose(); TextureManager.DisposeTextures(); dffRenderer.Dispose(); foreach (var c in mainForm.CollisionEditors) { c.DisposeRenderStuff(); } mainForm.ConfigEditor.SplineEditor.DisposeSplines(); Cube.Dispose(); Pyramid.Dispose(); Cylinder.Dispose(); Sphere.Dispose(); basicBuffer.Dispose(); basicShader.Dispose(); defaultBuffer.Dispose(); defaultShader.Dispose(); collisionBuffer.Dispose(); collisionShader.Dispose(); tintedBuffer.Dispose(); tintedShader.Dispose(); Device.Dispose(); }
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)); } }
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)); } }
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)); } }