public ViewFloat4(VertexFloat4Value d3dColorValue) { dataContextValue = new VertexFloat4Value() { X = d3dColorValue.X, Y = d3dColorValue.Y, Z = d3dColorValue.Z, W = d3dColorValue.W }; DataContext = dataContextValue; InitializeComponent(); }
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(); } }