SelectTemplate(object item, DependencyObject container) { FrameworkElement element = container as FrameworkElement; if (element != null && item != null && item is RW4ModelSectionView) { RW4ModelSectionView sectionView = item as RW4ModelSectionView; switch (sectionView.Section.TypeCode) { case SectionTypeCodes.Texture: return(element.FindResource("viewTexture") as DataTemplate); case SectionTypeCodes.Mesh: return(element.FindResource("viewMesh") as DataTemplate); case SectionTypeCodes.VertexArray: return(element.FindResource("viewVertexArray") as DataTemplate); case SectionTypeCodes.VertexFormat: return(element.FindResource("viewVertexFormat") as DataTemplate); case SectionTypeCodes.Material: return(element.FindResource("viewMaterial") as DataTemplate); default: return(element.FindResource("viewData") as DataTemplate); } } return(null); }
private void mnuImportOBJ_Click(object sender, RoutedEventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.DefaultExt = ".obj"; dlg.Filter = "Wavefront .OBJ|*.obj"; if (dlg.ShowDialog().GetValueOrDefault(false)) { RW4ModelSectionView sectionView = this.DataContext as RW4ModelSectionView; VertexFormat vertexFormatSection = (VertexFormat)sectionView.Model.Sections.First(s => s.TypeCode == SectionTypeCodes.VertexFormat).obj; RW4Section section = sectionView.Section; SporeMaster.RenderWare4.RW4Mesh meshSection = section.obj as SporeMaster.RenderWare4.RW4Mesh; IConverter converter = new WaveFrontOBJConverter(); RW4Mesh newMesh = converter.Import(meshSection, dlg.FileName); meshSection.vertices.vertices.section.obj = newMesh.vertices.vertices; meshSection.triangles.triangles.section.obj = newMesh.triangles.triangles; this.DataContext = sectionView; section.Changed(); } }
private void mnuRecalculateTangent_Click(object sender, RoutedEventArgs e) { RW4ModelSectionView sectionView = this.DataContext as RW4ModelSectionView; RW4Section section = sectionView.Section; SporeMaster.RenderWare4.RW4Mesh meshSection = section.obj as SporeMaster.RenderWare4.RW4Mesh; TangentSolver(meshSection); section.Changed(); }
private void mnuExportOBJ_Click(object sender, RoutedEventArgs e) { SaveFileDialog dlg = new SaveFileDialog(); dlg.DefaultExt = ".obj"; dlg.Filter = "WaveFront .OBJ|*.obj"; if (dlg.ShowDialog().GetValueOrDefault(false)) { if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4ModelSectionView)) { matDiffuseMain.Brush = new SolidColorBrush(Colors.LightGray); RW4ModelSectionView sectionView = this.DataContext as RW4ModelSectionView; // listBoxTextures.ItemsSource = sectionView.Model.Sections.Where<RW4Section>(s => s.TypeCode == SectionTypeCodes.Texture); RW4Section section = sectionView.Section; RW4Mesh mesh = section.obj as RW4Mesh; mesh.Export(dlg.FileName); } } }
private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4ModelSectionView)) { if (dataGridVertices != null) { dataGridVertices.Columns.Clear(); RW4ModelSectionView sectionView = this.DataContext as RW4ModelSectionView; section = sectionView.Section; //section = this.DataContext as RW4Section; SporeMaster.RenderWare4.RW4VertexArray vertexArraySection = section.obj as SporeMaster.RenderWare4.RW4VertexArray; if (vertexArraySection.vertices.Length > 0) { Vertex firstVertex = vertexArraySection.vertices[0]; int i = 0; foreach (IVertexComponentValue value in firstVertex.VertexComponents) { DataGridTextColumn textColumn = new DataGridTextColumn(); textColumn.Header = value.Usage.ToString(); textColumn.Binding = new Binding(string.Format("VertexComponents[{0}].Value", i)); dataGridVertices.Columns.Add(textColumn); i++; } DataGridTextColumn textColumnElement = new DataGridTextColumn(); textColumnElement.Header = "Element"; textColumnElement.Binding = new Binding(string.Format("Element", i)); dataGridVertices.Columns.Add(textColumnElement); //load materials List <MaterialTextureReference> materials = sectionView.Model.Materials; dataGridVertices.ItemsSource = vertexArraySection.vertices.ToList <Vertex>(); VertexShort4NValue oldTexVal = new VertexShort4NValue(); // WriteableBitmap colorMap = materials[4].GetBitmap(); List <ModelElement> Elements = new List <ModelElement>(); try { IEnumerable <int> elements = vertexArraySection.vertices.Select(s => s.Element).Distinct(); foreach (int el in elements) { Vertex elementVertex = vertexArraySection.vertices.First(v => v.Element == el); IEnumerable <IVertexComponentValue> textureSizeElements = elementVertex.VertexComponents.Where(c => c.DeclarationType == D3DDECLTYPE.D3DDECLTYPE_SHORT4N); Elements.Add(new ModelElement() { ElementId = el, MaterialEntry = materials, ColorComponent = elementVertex.VertexComponents.First(c => c.DeclarationType == D3DDECLTYPE.D3DDECLTYPE_D3DCOLOR) as VertexD3DColorValue, TextureCoordinatesBaseLayer = textureSizeElements.ElementAt(0) as VertexShort4NValue, TextureCoordinatesTopLayer = textureSizeElements.ElementAt(1) as VertexShort4NValue }); } dataGridElements.ItemsSource = Elements; this.ColorBitmap = materials[4].GetBitmap(); } catch { } } } } }
private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4ModelSectionView)) { matDiffuseMain.Brush = new SolidColorBrush(Colors.LightGray); RW4ModelSectionView sectionView = this.DataContext as RW4ModelSectionView; // listBoxTextures.ItemsSource = sectionView.Model.Sections.Where<RW4Section>(s => s.TypeCode == SectionTypeCodes.Texture); RW4Section section = sectionView.Section; SporeMaster.RenderWare4.RW4Mesh mesh = section.obj as SporeMaster.RenderWare4.RW4Mesh; meshMain.TriangleIndices.Clear(); meshMain.Positions.Clear(); meshMain.Normals.Clear(); meshMain.TextureCoordinates.Clear(); if (_boundingBox != null) { viewPort.Children.Remove(_boundingBox); _boundingBox = null; } RW4Section bboxSection = sectionView.Model.Sections.FirstOrDefault <RW4Section>(s => s.TypeCode == SectionTypeCodes.BBox); try { if (mesh != null) { VertexD3DColorValue lastColorValue = null; //separate the mesh into 'chunks' that can be given different textures if necessary foreach (var v in mesh.vertices.vertices) { if (v.VertexComponents.Exists(vc => vc.DeclarationType == D3DDECLTYPE.D3DDECLTYPE_D3DCOLOR)) { VertexD3DColorValue colorVal = v.VertexComponents.First(vc => vc.DeclarationType == D3DDECLTYPE.D3DDECLTYPE_D3DCOLOR) as VertexD3DColorValue; if (colorVal != lastColorValue) { //create a new 'chunk'. ModelVisual3D newChunckContainer = new ModelVisual3D(); viewPort.Children.Add(newChunckContainer); GeometryModel3D newChunk = new GeometryModel3D(); // GeometryContainer.Content.add } } VertexFloat3Value position = v.VertexComponents.First(vc => vc.Usage == D3DDECLUSAGE.D3DDECLUSAGE_POSITION) as VertexFloat3Value; meshMain.Positions.Add(new Point3D(position.X, position.Y, position.Z)); IVertexComponentValue normal = v.VertexComponents.First(vc => vc.Usage == D3DDECLUSAGE.D3DDECLUSAGE_NORMAL); if (normal != null) { if (normal is VertexUByte4Value) { VertexUByte4Value normalValue = normal as VertexUByte4Value; meshMain.Normals.Add(new Vector3D( (((float)normalValue.X) - 127.5f) / 127.5f, (((float)normalValue.Y) - 127.5f) / 127.5f, (((float)normalValue.Z) - 127.5f) / 127.5f )); } else if (normal is VertexFloat3Value) { VertexFloat3Value normalValue = normal as VertexFloat3Value; meshMain.Normals.Add(new Vector3D(normalValue.X, normalValue.Y, normalValue.Z)); } } IVertexComponentValue textureCoordinates = v.VertexComponents.First(vc => vc.Usage == D3DDECLUSAGE.D3DDECLUSAGE_TEXCOORD); if (textureCoordinates != null) { if (textureCoordinates is VertexFloat2Value) { VertexFloat2Value textureCoordinatesValue = textureCoordinates as VertexFloat2Value; meshMain.TextureCoordinates.Add(new System.Windows.Point(textureCoordinatesValue.X, textureCoordinatesValue.Y)); } if (textureCoordinates is VertexFloat4Value) { VertexFloat4Value textureCoordinatesValue = textureCoordinates as VertexFloat4Value; meshMain.TextureCoordinates.Add(new System.Windows.Point(textureCoordinatesValue.X, textureCoordinatesValue.Y)); } } } foreach (var t in mesh.triangles.triangles) { meshMain.TriangleIndices.Add((int)t.i); meshMain.TriangleIndices.Add((int)t.j); meshMain.TriangleIndices.Add((int)t.k); } } } catch { } viewPort.ZoomExtents(); } }