Пример #1
0
        public static Texture SetTexture(float[, ,] colors)
        {
            byte[] colorsData = new byte[(colors.GetLength(1) * colors.GetLength(0)) * 16];

            using (MemoryStream writer = new MemoryStream(colorsData, 0, colorsData.Length, true))
            {
                for (int y = 0; y < colors.GetLength(1); y++)
                {
                    for (int x = 0; x < colors.GetLength(0); x++)
                    {
                        writer.WriteF32(colors[x, y, 0]);
                        writer.WriteF32(colors[x, y, 1]);
                        writer.WriteF32(colors[x, y, 2]);
                        writer.WriteF32(colors[x, y, 3]);
                    }
                }
            }

            SporeMaster.RenderWare4.Texture texture = new SporeMaster.RenderWare4.Texture()
            {
                width       = (ushort)colors.GetLength(0),
                height      = (ushort)colors.GetLength(1),
                mipmapInfo  = 0x180,
                textureType = 0x74,
                texData     = new TextureBlob()
                {
                    blob = colorsData
                },
                unk1 = 0
            };

            return(texture);
        }
Пример #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
            {
            }
        }
Пример #3
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));
            }
        }
Пример #4
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));
            }
        }
Пример #5
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));
            }
        }
Пример #6
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();
                }
            }
        }
Пример #7
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();
            }
        }
Пример #8
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);
            }
        }
Пример #9
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));
            }
        }
Пример #10
0
        void pack(OgreXmlReader src_model, RW4Model model)
        {
            model.FileType = RW4Model.FileTypes.Model;

            RW4Skeleton skel = new RW4Skeleton();
            skel.unk1 = 0x8d6da0;
            skel.mat3 = new Matrices4x3()
            {
                items = new Mat4x3[] { new Mat4x3() { m = new float[] { 
                1.0f, 0.0f, 0.0f, 0.0f,
                0.0f, 1.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 1.0f, 0.0f
            } } }
            };
            skel.mat4 = new Matrices4x4()
            {
                items = new Mat4x4[] { new Mat4x4() { m = new float[] {
/*                1.0f, 0.0f, 0.0f, 0.0f,
                0.0f, 1.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 1.0f, 0.0f,*/
                0.0f, 1.0f, 0.0f, 0.0f,
               -1.0f, 0.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 1.0f, 0.0f,
                0.0f, 0.0f, 0.0f, 0.0f 
            } } }
            };
            skel.jointInfo = new RW4HierarchyInfo()
            {
                id = "skeleton1".FNV(),
                items = new RW4HierarchyInfo.Item[] {
                            new RW4HierarchyInfo.Item { index=0, name_fnv = "joint1".FNV(), flags=1, parent=null }
                        },
            };

            var anim = new Anim()
            {
                skeleton_id = skel.jointInfo.id,
                flags = 3,
                length = 1.25f,
                channel_names = (from j in skel.jointInfo.items select j.name_fnv).ToArray(),
            };
            anim.channel_frame_pose = new JointPose[,] {
                {
                    new JointPose{ qx=0, qy=0, qz=0, qs=1,
                                   tx=0, ty=0, tz=0,
                                   sx=1, sy=1, sz=1,
                                   time=1.25f }
                }
            };

            var mesh = new RW4Mesh()
            {
                vertices = new RW4VertexArray(),
                triangles = new RW4TriangleArray(),
            };
            mesh.vertices.unk2 = 0;  //< ?
            mesh.vertices.format = new VertexFormat() { blob = RW4Garbage.vertex_format };
            mesh.vertices.vertices = new Buffer<Vertex>(0);
            mesh.vertices.vertices.Assign(src_model.vertices);
            mesh.triangles.unk1 = 0;  //< ?
            mesh.triangles.triangles = new Buffer<Triangle>(0);
            mesh.triangles.triangles.Assign(src_model.triangles);

            var texture = new Texture()
            {
                width = 64,
                height = 64,
                mipmapInfo = 0x708,
                textureType = Texture.DXT5,
                unk1 = 0,
                texData = new TextureBlob() { blob = new byte[5488] }
            };
            var texture_format = new RW4TexMetadata()
            {
                unk_data_1 = RW4Garbage.texture_format_1,
                unk_data_2 = RW4Garbage.texture_format_2,
                texture = texture
            };
            var meshMaterial = new RWMeshMaterialAssignment()
            {
                mesh = mesh,
                mat = new RW4TexMetadata[] { texture_format }
            };

            model.AddObject(anim, Anim.type_code);
            model.AddObject(mesh.vertices.format, VertexFormat.type_code);
            model.AddObject(skel.mat4, Matrices4x4.type_code);
            model.AddObject(skel.mat3, Matrices4x3.type_code);
            skel.mat3.section.fixup_offsets.Add(16);   // !?
            model.AddObject(skel, RW4Skeleton.type_code);
            model.AddObject(mesh.triangles.triangles, Buffer<Triangle>.type_code);
            model.AddObject(mesh.triangles, RW4TriangleArray.type_code);
            model.AddObject(mesh, RW4Mesh.type_code);
            model.AddObject(texture_format, RW4TexMetadata.type_code);
            model.AddObject(meshMaterial, RWMeshMaterialAssignment.type_code);
            model.AddObject(skel.jointInfo, RW4HierarchyInfo.type_code);

            model.AddObject(mesh.vertices.vertices, Buffer<Vertex>.type_code);
            model.AddObject(mesh.vertices, RW4VertexArray.type_code);

            model.AddObject(texture.texData, TextureBlob.type_code);
            model.AddObject(texture, Texture.type_code);
        }