Example #1
0
        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section))
            {
                section = this.DataContext as RW4Section;
                RW4Material vertexArraySection = section.obj as RW4Material;
                if (vertexArraySection != null)
                {
                    dataGridVertices.ItemsSource = vertexArraySection.Materials;

                    foreach (MaterialTextureReference mat in vertexArraySection.Materials)
                    {
                        DatabaseIndex imageIndex = DatabaseManager.Instance.Indices.Find(idx => idx.InstanceId == mat.TextureInstanceId && idx.TypeId == PropertyConstants.RasterImageType);
                        if (imageIndex != null)
                        {
                            using (MemoryStream imageByteStream = new MemoryStream(imageIndex.GetIndexData(true)))
                            {
                                RasterImage img = RasterImage.CreateFromStream(imageByteStream, RasterChannel.All);
                                panelTextures.Children.Add(new Image()
                                {
                                    Source = img.MipMaps[0], Stretch = Stretch.None
                                });
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        private void mnuExportDDS8_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section))
                {
                    RW4Section section = this.DataContext as RW4Section;
                    SporeMaster.RenderWare4.Texture textureSection = section.obj as SporeMaster.RenderWare4.Texture;

                    SaveFileDialog dlg = new SaveFileDialog();
                    dlg.Filter = "DirectDraw Surface .DDS|*.dds";
                    if (dlg.ShowDialog().GetValueOrDefault(false))
                    {
                        using (Stream stream = File.Create(dlg.FileName))
                        {
                            DXT8Image img = new DXT8Image();
                            img.Height      = textureSection.height;
                            img.Width       = textureSection.width;
                            img.TextureType = textureSection.textureType;
                            img.MipMaps     = (int)textureSection.mipmapInfo;

                            img.SetData(textureSection.texData.blob);

                            img.Write(stream);
                        }
                    }
                }
            }
            catch
            {
            }
        }
Example #3
0
        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();
            }
        }
Example #4
0
        private void LoadSection(RW4Section section)
        {
            if (section != null)
            {
                if (section.GetType() == typeof(RW4Section))
                {
                    try
                    {
                        viewContainer.Content = new RW4ModelSectionView()
                        {
                            Section = section, Model = _rw4model
                        };
                    }
                    catch { }
                    try
                    {
                        DatabaseIndexData index = (DatabaseIndexData)this.DataContext;

                        byte[] buffer = new byte[section.Size];
                        using (Stream stream = new MemoryStream(index.Data))
                        {
                            stream.Seek((int)section.Pos, SeekOrigin.Begin);
                            stream.Read(buffer, 0, (int)section.Size);
                        }
                        viewHex.DataContext = buffer;
                    }
                    catch
                    {
                    }
                }
            }
        }
Example #5
0
        private void btnExport3dsMax_Click(object sender, RoutedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section))
            {
                section = this.DataContext as RW4Section;
                RW4Material vertexArraySection = section.obj as RW4Material;

                System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
                dlg.Description = "Select where to extract the files to...";
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string selectedPath = dlg.SelectedPath;
                    foreach (MaterialTextureReference mat in vertexArraySection.Materials)
                    {
                        DatabaseIndex imageIndex = DatabaseManager.Instance.Indices.Find(idx => idx.InstanceId == mat.TextureInstanceId && idx.TypeId == PropertyConstants.RasterImageType);
                        if (imageIndex != null)
                        {
                            using (MemoryStream imageByteStream = new MemoryStream(imageIndex.GetIndexData(true)))
                            {
                                using (FileStream strm = File.Create(selectedPath + "\\" + imageIndex.InstanceId.ToHex() + ".png"))
                                {
                                    RasterImage img;
                                    if (mat.Unknown1 == 1)
                                    {
                                        img = RasterImage.CreateFromStream(imageByteStream, RasterChannel.FacadeColor);
                                    }
                                    else
                                    {
                                        img = RasterImage.CreateFromStream(imageByteStream, RasterChannel.All);
                                    }
                                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                                    encoder.Frames.Add(BitmapFrame.Create(img.MipMaps[0] as BitmapSource));
                                    encoder.Save(strm);
                                }
                            }
                        }

                        imageIndex = DatabaseManager.Instance.Indices.Find(idx => idx.InstanceId == mat.TextureInstanceId && idx.TypeId == PropertyConstants.RW4ImageType);
                        if (imageIndex != null)
                        {
                            using (MemoryStream imageByteStream = new MemoryStream(imageIndex.GetIndexData(true)))
                            {
                                RW4Model model = new RW4Model();
                                model.Read(imageByteStream);
                                Texture         text   = model.Textures[0];
                                WriteableBitmap bitmap = text.ToImage(true);
                                using (FileStream strm = File.Create(selectedPath + "\\" + imageIndex.InstanceId.ToHex() + ".png"))
                                {
                                    //panelTextures.Children.Add(new Image() { Source = img.MipMaps[0], Stretch = Stretch.None });
                                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                                    encoder.Frames.Add(BitmapFrame.Create(bitmap as BitmapSource));
                                    encoder.Save(strm);
                                }
                            }
                        }
                    }
                }
            }
        }
Example #6
0
        private void mnuImportDDS_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section))
                {
                    //get the section
                    RW4Section section = this.DataContext as RW4Section;
                    SporeMaster.RenderWare4.Texture textureSection = section.obj as SporeMaster.RenderWare4.Texture;

                    OpenFileDialog dlg = new OpenFileDialog();
                    dlg.Filter = "DirectDraw Surface .DDS|*.dds";
                    if (dlg.ShowDialog().GetValueOrDefault(false))
                    {
                        uint textureBlobNumber = textureSection.texData.section.Number;

                        //Import as DXT5
                        if (textureSection.textureType != 0x15)
                        {
                            DXT5Image img = new DXT5Image();
                            using (Stream strm = File.OpenRead(dlg.FileName))
                            {
                                img.Read(strm);

                                var texture = new SporeMaster.RenderWare4.Texture()
                                {
                                    width       = (ushort)img.Width,
                                    height      = (ushort)img.Height,
                                    mipmapInfo  = (uint)(0x100 * img.MipMaps + 0x08),
                                    textureType = img.TextureType,
                                    texData     = new TextureBlob()
                                    {
                                        blob = img.GetAsByteArray()
                                    },
                                    unk1 = 0
                                };

                                texture.texData.section = new RW4Section()
                                {
                                    Number = textureBlobNumber
                                };
                                texture.texData.section.obj = new TextureBlob()
                                {
                                    blob = texture.texData.blob
                                };

                                section.obj = texture;
                            }
                        }
                    }
                    section.Changed();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("An exception occurred: {0}", ex.Message));
            }
        }
Example #7
0
        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section))
            {
                RW4Section section = this.DataContext as RW4Section;
                SporeMaster.RenderWare4.VertexFormat vertexFormatSection = section.obj as SporeMaster.RenderWare4.VertexFormat;

                dataGridVertices.ItemsSource = vertexFormatSection.VertexElements;
            }
        }
Example #8
0
        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();
        }
Example #9
0
        private void mnuExportBMP_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section))
                {
                    RW4Section section = this.DataContext as RW4Section;
                    SporeMaster.RenderWare4.Texture textureSection = section.obj as SporeMaster.RenderWare4.Texture;

                    SaveFileDialog dlg = new SaveFileDialog();
                    dlg.Filter = "BMP|*.bmp";
                    if (dlg.ShowDialog().GetValueOrDefault(false))
                    {
                        {
                            WriteableBitmap newBitmap = new WriteableBitmap(textureSection.width, textureSection.height, 96, 96, PixelFormats.Pbgra32, null);

                            int i = 0;
                            int j = 0;
                            using (MemoryStream reader = new MemoryStream(textureSection.texData.blob, 0, textureSection.texData.blob.Length, true))
                            {
                                while (j < textureSection.height)
                                {
                                    byte B = reader.ReadU8();
                                    byte G = reader.ReadU8();
                                    byte R = reader.ReadU8();
                                    byte A = reader.ReadU8();

                                    newBitmap.SetPixel(i, j, Color.FromArgb(A, R, G, B));

                                    i++;
                                    if (i >= textureSection.width)
                                    {
                                        i = 0;
                                        j++;
                                    }
                                }
                            }

                            using (FileStream stream5 = new FileStream(dlg.FileName, FileMode.Create))
                            {
                                BmpBitmapEncoder encoder5 = new BmpBitmapEncoder();
                                encoder5.Frames.Add(BitmapFrame.Create(newBitmap));
                                encoder5.Save(stream5);
                                stream5.Close();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("An exception occurred: {0}", ex.Message));
            }
        }
Example #10
0
        private void mnuExportDDS_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section))
                {
                    RW4Section section = this.DataContext as RW4Section;
                    SporeMaster.RenderWare4.Texture textureSection = section.obj as SporeMaster.RenderWare4.Texture;

                    SaveFileDialog dlg = new SaveFileDialog();
                    dlg.Filter = "DirectDraw Surface .DDS|*.dds";
                    if (dlg.ShowDialog().GetValueOrDefault(false))
                    {
                        using (Stream stream = File.Create(dlg.FileName))
                        {
                            DXT5Image img = new DXT5Image();
                            img.Height      = textureSection.height;
                            img.Width       = textureSection.width;
                            img.TextureType = textureSection.textureType;
                            img.MipMaps     = (int)textureSection.mipmapInfo;

                            img.SetData(textureSection.texData.blob);

                            img.Write(stream);

                            GraphicsDeviceService.AddRef(new WindowInteropHelper(Application.Current.MainWindow).Handle);
                            Texture2D texture;

                            try
                            {
                                DDSLib.DDSFromStream(stream, GraphicsDeviceService.Instance.GraphicsDevice, 0, true, out texture);
                                texturePreview.Source = DDSLib.Texture2Image(texture);

                                texturePreview.Visibility = System.Windows.Visibility.Visible;
                                textBlockError.Visibility = System.Windows.Visibility.Hidden;
                            }
                            catch (Exception ex)
                            {
                                texturePreview.Visibility = System.Windows.Visibility.Hidden;
                                textBlockError.Visibility = System.Windows.Visibility.Visible;
                                textBlockError.Text       = ex.Message;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("An exception occurred: {0}", ex.Message));
            }
        }
Example #11
0
        private void CreateUnitModel()
        {
            if (UnitFileEntry.LOD1 != null)
            {
                //get the 3d model to show in this scene
                KeyProperty   lod1  = UnitFileEntry.LOD1;
                DatabaseIndex index = DatabaseManager.Instance.Indices.Find(k => k.InstanceId == lod1.InstanceId && k.GroupContainer == lod1.GroupContainer && k.TypeId == lod1.TypeId);

                if (index != null)
                {
                    RW4Model _rw4model = new RW4Model();
                    using (Stream stream = new MemoryStream(index.GetIndexData(true)))
                    {
                        _rw4model.Read(stream);
                    }

                    RW4Section section = _rw4model.Sections.First(s => s.TypeCode == SectionTypeCodes.Mesh);
                    if (section != null)
                    {
                        SporeMaster.RenderWare4.RW4Mesh mesh = section.obj as SporeMaster.RenderWare4.RW4Mesh;

                        meshMain.TriangleIndices.Clear();
                        meshMain.Positions.Clear();
                        meshMain.Normals.Clear();
                        meshMain.TextureCoordinates.Clear();

                        try
                        {
                            foreach (var v in mesh.vertices.vertices)
                            {
                                meshMain.Positions.Add(new Point3D(v.Position.X, v.Position.Y, v.Position.Z));
                            }
                            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 { }
                    }

                    if (UnitFileEntry.ModelSize != null)
                    {
                        ScaleTransform3D scaleTransform = new ScaleTransform3D(UnitFileEntry.ModelSize.Value, UnitFileEntry.ModelSize.Value, UnitFileEntry.ModelSize.Value);
                        modelMain.Transform = scaleTransform;
                    }
                }
            }
        }
Example #12
0
        private void mnuImportDXT8_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.DefaultExt = "dds";
            dlg.Filter     = "DDS|*.dds";

            //get the section
            RW4Section section = this.DataContext as RW4Section;

            SporeMaster.RenderWare4.Texture textureSection = section.obj as SporeMaster.RenderWare4.Texture;

            //needs to be an 8.8.8.8 ARGB texture
            if (dlg.ShowDialog().GetValueOrDefault(false))
            {
                uint textureBlobNumber = textureSection.texData.section.Number;

                DXT8Image img = new DXT8Image();
                using (Stream strm = File.OpenRead(dlg.FileName))
                {
                    img.Read(strm);

                    var texture = new SporeMaster.RenderWare4.Texture()
                    {
                        width       = (ushort)img.Width,
                        height      = (ushort)img.Height,
                        mipmapInfo  = (uint)(0x120),
                        textureType = img.TextureType,
                        texData     = new TextureBlob()
                        {
                            blob = img.GetAsByteArray()
                        },
                        unk1 = 0x0121afec
                    };

                    texture.texData.section = new RW4Section()
                    {
                        Number = textureBlobNumber
                    };
                    texture.texData.section.obj = new TextureBlob()
                    {
                        blob = texture.texData.blob
                    };

                    section.obj = texture;

                    section.Changed();
                }
            }
        }
Example #13
0
        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext != null)
            {
                try
                {
                    DatabaseIndexData index = (DatabaseIndexData)this.DataContext;

                    _rw4model = new RW4Model();
                    using (Stream stream = new MemoryStream(index.Data))
                    {
                        _rw4model.Read(stream);
                    }
                    //textBlockRW4Type.Text = model.FileType.ToString();

                    // gridHeaderDetails.DataContext = _rw4model.Header;
                    txtHeaderSectionBegin.Text = _rw4model.Header.SectionIndexBegin.ToString();
                    txtHeaderPadder.Text       = _rw4model.Header.SectionIndexPadding.ToString();
                    txtHeaderEnd.Text          = _rw4model.Header.SectionIndexEnd.ToString();
                    txtHeaderEndPos.Text       = _rw4model.Header.HeaderEnd.ToString();

                    dataGrid1.ItemsSource = _rw4model.Sections;

                    _rw4model.Sections.ForEach(t => t.SectionChanged += new EventHandler(section_SectionChanged));

                    RW4Section sec = _rw4model.Sections.FirstOrDefault(s => s.TypeCode == SectionTypeCodes.Mesh);
                    if (sec != null)
                    {
                        dataGrid1.SelectedItem = sec;
                    }
                    else
                    {
                        RW4Section sec2 = _rw4model.Sections.FirstOrDefault(s => s.TypeCode == SectionTypeCodes.Texture);
                        dataGrid1.SelectedItem = sec2;
                    }
                }
                catch
                {
                }
            }
        }
Example #14
0
        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);
                }
            }
        }
Example #15
0
        private void viewHex_DataChangedHandler(object sender, EventArgs e)
        {
            if (dataGrid1.SelectedItem != null)
            {
                if (dataGrid1.SelectedItem.GetType() == typeof(RW4Section))
                {
                    RW4Section section = (RW4Section)dataGrid1.SelectedItem;

                    DatabaseIndexData index = (DatabaseIndexData)this.DataContext;

                    byte[] buffer = viewHex.DataContext as byte[];
                    //   using (Stream stream = new MemoryStream(index.Data))
                    //  {
                    //      stream.Seek((int)section.Pos, SeekOrigin.Begin);
                    //      stream.Write(buffer, 0, (int)section.Size);
                    //  }
                    (section.obj as RW4Blob).blob = buffer;

                    SaveRW4Model();
                }
            }
        }
Example #16
0
        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
                        {
                        }
                    }
                }
            }
        }
Example #17
0
        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();
            }
        }
Example #18
0
        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section))
            {
                RW4Section section = this.DataContext as RW4Section;
                SporeMaster.RenderWare4.Texture textureSection = section.obj as SporeMaster.RenderWare4.Texture;

                if (textureSection.textureType != 21)
                {
                    if (textureSection != null)
                    {
                        using (var stream = new MemoryStream())
                        {
                            DXT5Image img = new DXT5Image();
                            img.Height      = textureSection.height;
                            img.Width       = textureSection.width;
                            img.TextureType = textureSection.textureType;
                            img.MipMaps     = (int)textureSection.mipmapInfo;

                            img.SetData(textureSection.texData.blob);

                            img.Write(stream);

                            if (textureSection.textureType == 0x21)
                            {
                                mnuExportBMP.IsEnabled = true;
                                mnuExportDDS.IsEnabled = false;
                                mnuImportBMP.IsEnabled = true;
                                mnuImportDDS.IsEnabled = false;
                            }
                            else
                            {
                                mnuExportBMP.IsEnabled = false;
                                mnuExportDDS.IsEnabled = true;
                                mnuImportBMP.IsEnabled = false;
                                mnuImportDDS.IsEnabled = true;
                            }


                            GraphicsDeviceService.AddRef(new WindowInteropHelper(Application.Current.MainWindow).Handle);
                            Texture2D texture;

                            try
                            {
                                DDSLib.DDSFromStream(stream, GraphicsDeviceService.Instance.GraphicsDevice, 0, true, out texture);
                                texturePreview.Source = DDSLib.Texture2Image(texture);

                                texturePreview.Visibility = System.Windows.Visibility.Visible;
                                textBlockError.Visibility = System.Windows.Visibility.Hidden;
                            }
                            catch (Exception ex)
                            {
                                texturePreview.Visibility = System.Windows.Visibility.Hidden;
                                textBlockError.Visibility = System.Windows.Visibility.Visible;
                                textBlockError.Text       = ex.Message;
                            }
                        }
                    }
                }
                else
                {
                    using (MemoryStream byteStream = new MemoryStream(textureSection.texData.blob, 0, textureSection.texData.blob.Length))
                    {
                        for (int i = 0; i < textureSection.mipmapInfo; i++)
                        {
                            //  uint blockSize = byteStream.ReadU32().Swap();
                            WriteableBitmap bitmap = new WriteableBitmap((int)textureSection.width, (int)textureSection.height, 300, 300, PixelFormats.Pbgra32, BitmapPalettes.Halftone64);
                            if (textureSection.textureType == 21)
                            {
                                for (int j = 0; j < (byteStream.Length / 4); j++)
                                {
                                    byte r = byteStream.ReadU8();
                                    byte g = byteStream.ReadU8();
                                    byte b = byteStream.ReadU8();
                                    byte a = byteStream.ReadU8();


                                    try
                                    {
                                        if ((j / textureSection.width) < textureSection.height)
                                        {
                                            bitmap.SetPixel((int)(j % textureSection.width), (int)(j / textureSection.width), a, r, g, b);
                                        }
                                    }
                                    catch
                                    {
                                    }
                                }

                                texturePreview.Source = bitmap;
                                break;
                            }
                        }
                    }
                }

                if (textureSection != null)
                {
                    txtHeight.Text = textureSection.height.ToString();
                    txtWidth.Text  = textureSection.width.ToString();

                    txtTextureType.Text = textureSection.textureType.ToHex();
                    txtMipMapInfo.Text  = textureSection.mipmapInfo.ToHex();

                    txtSection.Text = textureSection.texData.section.Number.ToString();
                    txtUnknown.Text = textureSection.unk1.ToHex();
                }

                //  .AddObject(texture.texData, TextureBlob.type_code);
            }
        }
Example #19
0
        private void mnuImportBMP_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section))
                {
                    //get the section
                    RW4Section section = this.DataContext as RW4Section;
                    SporeMaster.RenderWare4.Texture textureSection = section.obj as SporeMaster.RenderWare4.Texture;

                    OpenFileDialog dlg = new OpenFileDialog();
                    dlg.Filter = "BMP|*.bmp";
                    if (dlg.ShowDialog().GetValueOrDefault(false))
                    {
                        uint textureBlobNumber = textureSection.texData.section.Number;
                        {
                            BitmapSource source = new BitmapImage(new Uri(dlg.FileName));
                            SimCityPak.ExtensionMethods.PixelColor[,] colors = source.GetPixels();

                            byte[] colorsData = new byte[((int)source.PixelHeight * (int)source.PixelWidth) * 4];

                            using (MemoryStream writer = new MemoryStream(colorsData, 0, colorsData.Length, true))
                            {
                                foreach (SimCityPak.ExtensionMethods.PixelColor color in colors)
                                {
                                    writer.WriteU8(color.Blue);
                                    writer.WriteU8(color.Green);
                                    writer.WriteU8(color.Red);
                                    writer.WriteU8(color.Alpha);
                                }
                            }

                            SporeMaster.RenderWare4.Texture texture = new SporeMaster.RenderWare4.Texture()
                            {
                                width       = (ushort)source.PixelWidth,
                                height      = (ushort)source.PixelHeight,
                                mipmapInfo  = 0x120,
                                textureType = 0x15,
                                texData     = new TextureBlob()
                                {
                                    blob = colorsData
                                },
                                unk1 = 0
                            };

                            texture.texData.section = new RW4Section()
                            {
                                Number = textureBlobNumber
                            };
                            texture.texData.section.obj = new TextureBlob()
                            {
                                blob = colorsData
                            };

                            section.obj = texture;
                        }
                    }
                    section.Changed();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("An exception occurred: {0}", ex.Message));
            }
        }
Example #20
0
        private void SaveRW4Model()
        {
            //Save the new thing to a stream!

            if (this.DataContext != null)
            {
                //  try
                //  {

                ModifiedRW4File   modifiedData = new ModifiedRW4File();
                DatabaseIndexData index        = (DatabaseIndexData)this.DataContext;

                using (Stream stream = new MemoryStream(index.Data))
                {
                    //first read the current RW4model

                    List <RW4Section> sections = dataGrid1.ItemsSource as List <RW4Section>;
                    _rw4model.Sections = sections;

                    foreach (RW4Section section in _rw4model.Sections)
                    {
                        if (section.TypeCode == SectionTypeCodes.Texture)
                        {
                            SporeMaster.RenderWare4.Texture tex = section.obj as SporeMaster.RenderWare4.Texture;
                            _rw4model.Sections[(int)tex.texData.section.Number].obj = tex.texData;
                        }
                    }

                    RW4Section meshSection = _rw4model.Sections.Find(s => s.TypeCode == SectionTypeCodes.Mesh);
                    if (meshSection != null)
                    {
                        SporeMaster.RenderWare4.RW4Mesh mesh = meshSection.obj as SporeMaster.RenderWare4.RW4Mesh;

                        //update the bounding box

                        /*RW4Section bboxSection = _rw4model.Sections.Find(s => s.TypeCode == SectionTypeCodes.BBox);
                         * if (bboxSection != null)
                         * {
                         *  RW4BBox boundingBox = bboxSection.obj as RW4BBox;
                         *  if (meshSection != null)
                         *  {
                         *      boundingBox.minx = mesh.vertices.vertices.Min(v => v.X);
                         *      boundingBox.miny = mesh.vertices.vertices.Min(v => v.Y);
                         *      boundingBox.minz = mesh.vertices.vertices.Min(v => v.Z);
                         *
                         *      boundingBox.maxx = mesh.vertices.vertices.Max(v => v.X);
                         *      boundingBox.maxy = mesh.vertices.vertices.Max(v => v.Y);
                         *      boundingBox.maxz = mesh.vertices.vertices.Max(v => v.Z);
                         *
                         *      bboxSection.obj = boundingBox;
                         *  }
                         * }*/

                        _rw4model.Sections[(int)mesh.vertices.section.Number].obj  = mesh.vertices;
                        _rw4model.Sections[(int)mesh.triangles.section.Number].obj = mesh.triangles;

                        _rw4model.Sections[(int)mesh.vertices.vertices.section.Number].obj   = mesh.vertices.vertices.section.obj;
                        _rw4model.Sections[(int)mesh.triangles.triangles.section.Number].obj = mesh.triangles.triangles.section.obj;
                    }



                    //save back the model

                    using (MemoryStream writer = new MemoryStream())
                    {
                        _rw4model.Write(writer);

                        modifiedData.RW4FileData = writer.ToArray();
                        index.Index.ModifiedData = modifiedData;
                        index.Index.IsModified   = true;
                        index.Index.Compressed   = false;
                    }
                }

                //ViewHexDiff hex = new ViewHexDiff(index.Data, modifiedData.RW4FileData);
                //hex.ShowDialog();
            }
        }