Ejemplo n.º 1
0
        static void DoRewriteTest()
        {
            var f = "C:\\my\\proj\\mods\\spore\\SporeMaster\\SporeMaster\\bin\\Release\\spore.unpacked\\part_models~\\ce_sense_eye_weird_01.rw4\\raw.rw4";
            var m = new RW4Model();

            using (var s = File.OpenRead(f))
                m.Read(s);
            var an = (Animations)m.GetObjects(Animations.type_code)[0];

            an.animations = new Anim[] { an.animations[0] };
            for (int i = 0; i < an.animations[0].channel_frame_pose[0].Length; i++)
            {
                an.animations[0].channel_frame_pose[0][i].tx = 0;
            }
            var sections = new int[] { 1, 3, 4, 14, 15 };

            for (int i = sections.Length - 1; i >= 0; i--)
            {
                m.RemoveSection(m.Sections[sections[i]]);
            }
            foreach (var s in m.Sections)
            {
                s.alignment = 0x10;
            }
            m.Test();
            using (var s = File.Create("c:\\temp\\raw.rw4"))
                m.Write(s);
        }
Ejemplo n.º 2
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);
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
 public DDSPack(string inputFileName, Stream output)
 {
     using (var src = File.OpenRead(inputFileName))
     {
         var model = new RW4Model();
         model.New();
         pack(src, model);
         model.Write(output);
     }
 }
Ejemplo n.º 4
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;
                    }
                }
            }
        }
Ejemplo n.º 5
0
        static void CreateModel(string src_name, string out_name)
        {
            using (var stream = File.Create(out_name))
                new ModelPack(src_name, stream);

            Console.WriteLine("Checking model:");
            var tm = new RW4Model();

            using (var stream = File.Open(out_name, FileMode.Open))
                tm.Read(stream);
            foreach (var s in tm.Sections)
            {
                Console.WriteLine(String.Format("  #{0} 0x{1:x} - 0x{2:x}: 0x{3:x} {4}", s.number, s.pos, s.pos + s.size, s.type_code, s.obj.ToString().Substring("SporeMaster.RenderWare4.".Length)));
            }
        }
Ejemplo n.º 6
0
        void pack(Stream src, RW4Model model)
        {
            // Read the DDS
            src.expect(0x20534444, "DDS000");  // 'DDS '
            int headerSize = src.ReadS32();
            if (headerSize < 0x7C) throw new ModelFormatException(src, "DDS001", headerSize);
            var flags = src.ReadU32();
            var height = src.ReadS32();
            var width = src.ReadS32();
            var pitchOrLinearSize = src.ReadU32();
            src.expect(0, "DDS002");  // < depth
            var mipmaps = src.ReadS32();

            src.Seek(src.Position + 11 * 4, SeekOrigin.Begin);
            var pfsize = src.ReadS32();
            if (pfsize < 32) throw new ModelFormatException(src, "DDS011", pfsize);
            var pf_flags = src.ReadU32();
            if ((pf_flags & 4) == 0) throw new ModelFormatException(src, "DDS012", pf_flags);
            var fourcc = src.ReadU32();
            if (fourcc != Texture.DXT5)
                throw new NotSupportedException("Texture packing currently only supports DXT5 compressed input textures.");

            src.Seek(headerSize+4, SeekOrigin.Begin);
            var sizes = (from i in Enumerable.Range(0, mipmaps)
                            select Math.Max(width>>i,4)*Math.Max(height>>i,4)  // DXT5: 16 bytes per 4x4=16 pixels
                            ).ToArray();
            var all_mipmaps = new byte[ sizes.Sum() ];
            for (int offset=0, i = 0; i < mipmaps; i++) {
                if (src.Read( all_mipmaps, offset, sizes[i] ) != sizes[i])
                    throw new ModelFormatException(src, "Unexpected EOF reading .DDS file", null);
                offset += sizes[i];
            }

            // Build the RW4
            model.FileType = RW4Model.FileTypes.Texture;
            var texture = new Texture()
            {
                width = (ushort)width,
                height = (ushort)height,
                mipmapInfo = (uint)(0x100 * mipmaps + 0x08),
                textureType = fourcc,
                texData = new TextureBlob { blob = all_mipmaps },
                unk1 = 0
            };
            model.AddObject(texture.texData, TextureBlob.type_code);
            model.AddObject(texture, Texture.type_code);
        }
Ejemplo n.º 7
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
                {
                }
            }
        }
Ejemplo n.º 8
0
        static void ReadAnim(RW4Model model, string outname, string ogreSkeletonFile)
        {
            var anims     = (from anim in model.GetObjects(Anim.type_code) select(Anim) anim).ToArray();
            var skeletons = (from s in model.GetObjects(RW4Skeleton.type_code) select(RW4Skeleton) s).ToArray();

            if (anims.Length == 0 && skeletons.Length == 0)
            {
                return;
            }

            var only = new int[] { 1 };

            var x_skels =
                new XElement("skeletons",
                             from skel in skeletons
                             select new XElement("skeleton",
                                                 new XAttribute("name", SporeMaster.NameRegistry.Files.toName(skel.jointInfo.id)),
                                                 new XAttribute("unk1", "0x" + skel.unk1.ToString("X")),
                                                 new XElement("joints",
                                                              from joint in skel.jointInfo.items
                                                              let mat4 = skel.mat4.items[joint.index]
                                                                         let inv_bind                       = TMatrix.FromMat4(mat4)
                                                                                                   let bind = inv_bind.Inverse()
                                                                                                              let rotation = bind.Inverse().GetRotation() //< TODO: Why inverse?
                                                                                                                             select new XElement("joint",
                                                                                                                                                 new XAttribute("name", SporeMaster.NameRegistry.Files.toName(joint.name_fnv)),
                                                                                                                                                 new XAttribute("flags", "0x" + joint.flags.ToString("X")),
                                                                                                                                                 from _ in only where joint.parent != null
                                                                                                                                                 select new XAttribute("parent", SporeMaster.NameRegistry.Files.toName(joint.parent.name_fnv)),
                                                                                                                                                 new XElement("Transform",
                                                                                                                                                              new XElement("position", bind.GetColumn(3).toXML()),
                                                                                                                                                              new XElement("rotation", new XAttribute("angle", rotation.Angle), rotation.Axis.toXML())
                                                                                                                                                              ),
                                                                                                                                                 new XElement("RawInverseBindMatrix",
                                                                                                                                                              new XElement("rx", writeVector(mat4.m[0], mat4.m[1], mat4.m[2])),
                                                                                                                                                              new XElement("ry", writeVector(mat4.m[4], mat4.m[5], mat4.m[6])),
                                                                                                                                                              new XElement("rz", writeVector(mat4.m[8], mat4.m[9], mat4.m[10])),
                                                                                                                                                              new XElement("t", writeVector(mat4.m[12], mat4.m[13], mat4.m[15]))
                                                                                                                                                              )
                                                                                                                                                 )
                                                              )
                                                 )
                             );

            var x_anims =
                new XElement("anims",
                             from anim in anims
                             select new XElement("anim",
                                                 new XAttribute("name", SporeMaster.NameRegistry.Files.toName(anim.hash_name)),
                                                 new XAttribute("flags", "0x" + anim.flags.ToString("X")),
                                                 new XAttribute("length", anim.length),
                                                 new XAttribute("skeleton", SporeMaster.NameRegistry.Files.toName(anim.skeleton_id)),
                                                 //new XAttribute("padding", anim.padding),
                                                 new XAttribute("components", "0x" + anim.pose_components.ToString("X")),
                                                 new XElement("channels",
                                                              from i in Enumerable.Range(0, anim.channel_names.Length)
                                                              let name = anim.channel_names[i]
                                                                         select new XElement("channel",
                                                                                             new XAttribute("name", SporeMaster.NameRegistry.Files.toName(name)),
                                                                                             new XAttribute("padding", anim.padding[i]),
                                                                                             new XElement("frames",
                                                                                                          from pose in anim.channel_frame_pose[i]
                                                                                                          select new XElement("frame",
                                                                                                                              new XAttribute("time", pose.time),
                                                                                                                              new XElement("Translation",
                                                                                                                                           new XAttribute("x", pose.tx),
                                                                                                                                           new XAttribute("y", pose.ty),
                                                                                                                                           new XAttribute("z", pose.tz)),
                                                                                                                              new XElement("Rotation",
                                                                                                                                           new XAttribute("x", pose.qx),
                                                                                                                                           new XAttribute("y", pose.qy),
                                                                                                                                           new XAttribute("z", pose.qz),
                                                                                                                                           new XAttribute("s", pose.qs)),
                                                                                                                              new XElement("Scale",
                                                                                                                                           new XAttribute("x", pose.sx),
                                                                                                                                           new XAttribute("y", pose.sy),
                                                                                                                                           new XAttribute("z", pose.sz))
                                                                                                                              )
                                                                                                          )
                                                                                             )
                                                              )
                                                 )
                             );
            var output = new XElement("raw_animation_info",
                                      x_anims,
                                      x_skels);

            output.Save(outname);

            WriteOgreSkeleton(model).Save(ogreSkeletonFile);

            foreach (var s in skeletons)
            {
                foreach (var mat4 in s.mat4.items)
                {
                    if (mat4.m[3] != 0 || mat4.m[7] != 0 || mat4.m[15] != 0)
                    {
                        throw new Exception("Extra matrix cells nonzero.");
                    }
                }
            }
        }
Ejemplo n.º 9
0
        static XElement WriteOgreSkeleton(RW4Model model)
        {
            var anims  = (from anim in model.GetObjects(Anim.type_code) select(Anim) anim).ToArray();
            var rskels = model.GetObjects(RW4Skeleton.type_code);

            if (rskels.Count != 1)
            {
                throw new NotSupportedException("Exactly one skeleton required.");
            }
            var skel = (RW4Skeleton)rskels[0];

            Dictionary <uint, int> bone_name_lookup = new Dictionary <uint, int>();

            foreach (var j in skel.jointInfo.items)
            {
                bone_name_lookup[j.name_fnv] = j.index;
            }

            var inv_binds = (from m in skel.mat4.items select TMatrix.FromMat4(m)).ToArray();

            var rel_bind = (
                from joint in skel.jointInfo.items
                let inv_bind = TMatrix.FromMat4(skel.mat4.items[joint.index])
                               let bind = inv_bind.Inverse()
                                          let parent_inv_bind = joint.parent == null ? null : TMatrix.FromMat4(skel.mat4.items[joint.parent.index])
                                                                select parent_inv_bind == null ? bind : parent_inv_bind * bind
                ).ToArray();

            return(new XElement("skeleton",
                                new XElement("bones",
                                             from joint in skel.jointInfo.items
                                             let rel = rel_bind[joint.index]
                                                       let rotation = -rel.GetRotation() // < TODO: Why inverse?
                                                                      select new XElement("bone",
                                                                                          new XAttribute("id", joint.index),
                                                                                          new XAttribute("name", SporeMaster.NameRegistry.Files.toName(joint.name_fnv)),
                                                                                          new XElement("position", rel.GetColumn(3).toXML()),
                                                                                          new XElement("rotation",
                                                                                                       new XAttribute("angle", rotation.Angle),
                                                                                                       new XElement("axis", rotation.Axis.toXML()))
                                                                                          )
                                             ),
                                new XElement("bonehierarchy",
                                             from joint in skel.jointInfo.items
                                             where joint.parent != null
                                             select new XElement("boneparent",
                                                                 new XAttribute("bone", SporeMaster.NameRegistry.Files.toName(joint.name_fnv)),
                                                                 new XAttribute("parent", SporeMaster.NameRegistry.Files.toName(joint.parent.name_fnv)))),
                                new XElement("animations",
                                             from anim in anims
                                             select new XElement("animation",
                                                                 new XAttribute("name", SporeMaster.NameRegistry.Files.toName(anim.hash_name)),
                                                                 new XAttribute("length", anim.length),
                                                                 new XElement("tracks",
                                                                              from i in Enumerable.Range(0, anim.channel_names.Length)
                                                                              let name = anim.channel_names[i]
                                                                                         let poses = anim.channel_frame_pose[i]
                                                                                                     let rel = rel_bind[bone_name_lookup[name]]
                                                                                                               let bind_rot = rel.Inverse()
                                                                                                                              let bind_pos = rel.GetColumn(3)
                                                                                                                                             select new XElement("track",
                                                                                                                                                                 new XAttribute("bone", SporeMaster.NameRegistry.Files.toName(name)),
                                                                                                                                                                 new XElement("keyframes",
                                                                                                                                                                              from f in poses
                                                                                                                                                                              let scale = new Vector(f.sx, f.sy, f.sz)
                                                                                                                                                                                          let pose = TMatrix.Translation(new Vector(f.tx, f.ty, f.tz)) *
                                                                                                                                                                                                     (-new Quaternion {
                x = f.qx, y = f.qy, z = f.qz, w = f.qs
            }).ToMatrix()
                                                                                                                                                                                                     let pose_world = bind_rot * pose
                                                                                                                                                                                                                      let rot = -pose_world.GetRotation()
                                                                                                                                                                                                                                let pos = pose.GetColumn(3) - bind_pos
                                                                                                                                                                                                                                          select new XElement("keyframe",
                                                                                                                                                                                                                                                              new XAttribute("time", f.time),
                                                                                                                                                                                                                                                              new XElement("translate", pos.toXML()),
                                                                                                                                                                                                                                                              new XElement("rotate",
                                                                                                                                                                                                                                                                           new XAttribute("angle", rot.Angle),
                                                                                                                                                                                                                                                                           new XElement("axis", rot.Axis.toXML())),
                                                                                                                                                                                                                                                              new XElement("scale", scale.toXML())))))))));
        }
Ejemplo n.º 10
0
        static XElement WriteOgreSkeleton(RW4Model model)
        {
            var anims = (from anim in model.GetObjects(Anim.type_code) select (Anim)anim).ToArray();
            var rskels = model.GetObjects(RW4Skeleton.type_code);
            if (rskels.Count != 1) throw new NotSupportedException("Exactly one skeleton required.");
            var skel = (RW4Skeleton)rskels[0];

            Dictionary<uint, int> bone_name_lookup = new Dictionary<uint, int>();
            foreach (var j in skel.jointInfo.items) bone_name_lookup[j.name_fnv] = j.index;

            var inv_binds = (from m in skel.mat4.items select TMatrix.FromMat4(m)).ToArray();

            var rel_bind = (
                from joint in skel.jointInfo.items
                let inv_bind = TMatrix.FromMat4(skel.mat4.items[joint.index])
                let bind = inv_bind.Inverse()
                let parent_inv_bind = joint.parent == null ? null : TMatrix.FromMat4(skel.mat4.items[joint.parent.index])
                select parent_inv_bind == null ? bind : parent_inv_bind * bind
                ).ToArray();

            return new XElement("skeleton",
                new XElement("bones",
                    from joint in skel.jointInfo.items
                    let rel = rel_bind[joint.index]
                    let rotation = -rel.GetRotation()  // < TODO: Why inverse?
                    select new XElement("bone",
                        new XAttribute("id", joint.index),
                        new XAttribute("name", SporeMaster.NameRegistry.Files.toName(joint.name_fnv)),
                        new XElement("position", rel.GetColumn(3).toXML()),
                        new XElement("rotation",
                            new XAttribute("angle", rotation.Angle),
                            new XElement("axis", rotation.Axis.toXML()))
                        )
                    ),
                new XElement("bonehierarchy",
                    from joint in skel.jointInfo.items
                    where joint.parent != null
                    select new XElement("boneparent",
                        new XAttribute("bone", SporeMaster.NameRegistry.Files.toName(joint.name_fnv)),
                        new XAttribute("parent", SporeMaster.NameRegistry.Files.toName(joint.parent.name_fnv)))),
                new XElement("animations",
                    from anim in anims
                    select new XElement("animation",
                        new XAttribute("name", SporeMaster.NameRegistry.Files.toName(anim.hash_name)),
                        new XAttribute("length", anim.length),
                        new XElement("tracks",
                            from i in Enumerable.Range(0, anim.channel_names.Length)
                            let name = anim.channel_names[i]
                            let poses = anim.channel_frame_pose[i]
                            let rel = rel_bind[ bone_name_lookup[ name ] ]
                            let bind_rot = rel.Inverse()
                            let bind_pos = rel.GetColumn(3)
                            select new XElement("track",
                                new XAttribute("bone", SporeMaster.NameRegistry.Files.toName(name)),
                                new XElement("keyframes",
                                    from f in poses
                                    let scale = new Vector(f.sx, f.sy, f.sz)
                                    let pose = TMatrix.Translation(new Vector(f.tx, f.ty, f.tz)) *
                                        (-new Quaternion { x = f.qx, y = f.qy, z = f.qz, w = f.qs }).ToMatrix()
                                    let pose_world = bind_rot * pose
                                    let rot = -pose_world.GetRotation()
                                    let pos = pose.GetColumn(3) - bind_pos
                                    select new XElement("keyframe",
                                        new XAttribute("time", f.time),
                                        new XElement("translate", pos.toXML()),
                                        new XElement("rotate",
                                            new XAttribute("angle", rot.Angle),
                                            new XElement("axis", rot.Axis.toXML())),
                                        new XElement("scale", scale.toXML()))))))));
        }
Ejemplo n.º 11
0
        static void ReadAnim(RW4Model model, string outname, string ogreSkeletonFile)
        {
            var anims = (from anim in model.GetObjects(Anim.type_code) select (Anim)anim).ToArray();
            var skeletons = (from s in model.GetObjects(RW4Skeleton.type_code) select (RW4Skeleton)s).ToArray();
            if (anims.Length == 0 && skeletons.Length == 0) return;

            var only = new int[] { 1 };

            var x_skels =
                new XElement("skeletons",
                    from skel in skeletons
                    select new XElement("skeleton",
                        new XAttribute("name", SporeMaster.NameRegistry.Files.toName(skel.jointInfo.id)),
                        new XAttribute("unk1", "0x" + skel.unk1.ToString("X")),
                        new XElement("joints",
                            from joint in skel.jointInfo.items
                            let mat4 = skel.mat4.items[joint.index]
                            let inv_bind = TMatrix.FromMat4( mat4 )
                            let bind = inv_bind.Inverse()
                            let rotation = bind.Inverse().GetRotation()  //< TODO: Why inverse?
                            select new XElement("joint",
                                new XAttribute("name", SporeMaster.NameRegistry.Files.toName(joint.name_fnv)),
                                new XAttribute("flags", "0x" + joint.flags.ToString("X")),
                                from _ in only where joint.parent != null
                                    select new XAttribute("parent", SporeMaster.NameRegistry.Files.toName(joint.parent.name_fnv)),
                                new XElement("Transform",
                                        new XElement("position", bind.GetColumn(3).toXML()),
                                        new XElement("rotation", new XAttribute("angle", rotation.Angle), rotation.Axis.toXML() )
                                        ),
                                new XElement("RawInverseBindMatrix",
                                        new XElement("rx", writeVector(mat4.m[0], mat4.m[1], mat4.m[2])),
                                        new XElement("ry", writeVector(mat4.m[4], mat4.m[5], mat4.m[6])),
                                        new XElement("rz", writeVector(mat4.m[8], mat4.m[9], mat4.m[10])),
                                        new XElement("t",  writeVector(mat4.m[12], mat4.m[13], mat4.m[15]))
                                        )
                                )
                            )
                        )
                    );

            var x_anims =
                new XElement("anims",
                    from anim in anims
                    select new XElement("anim",
                        new XAttribute("name", SporeMaster.NameRegistry.Files.toName(anim.hash_name)),
                        new XAttribute("flags", "0x" + anim.flags.ToString("X")),
                        new XAttribute("length", anim.length),
                        new XAttribute("skeleton", SporeMaster.NameRegistry.Files.toName(anim.skeleton_id)),
                        //new XAttribute("padding", anim.padding),
                        new XAttribute("components", "0x"+anim.pose_components.ToString("X")),
                        new XElement("channels",
                            from i in Enumerable.Range(0, anim.channel_names.Length)
                            let name = anim.channel_names[i]
                            select new XElement("channel",
                                new XAttribute("name", SporeMaster.NameRegistry.Files.toName(name)),
                                new XAttribute("padding", anim.padding[i]),
                                new XElement("frames",
                                    from pose in anim.channel_frame_pose[i]
                                    select new XElement("frame",
                                        new XAttribute("time", pose.time),
                                        new XElement("Translation",
                                            new XAttribute("x", pose.tx),
                                            new XAttribute("y", pose.ty),
                                            new XAttribute("z", pose.tz)),
                                        new XElement("Rotation",
                                            new XAttribute("x", pose.qx),
                                            new XAttribute("y", pose.qy),
                                            new XAttribute("z", pose.qz),
                                            new XAttribute("s", pose.qs)),
                                        new XElement("Scale",
                                            new XAttribute("x", pose.sx),
                                            new XAttribute("y", pose.sy),
                                            new XAttribute("z", pose.sz))
                                        )
                                    )
                                )
                            )
                        )
                    );
            var output = new XElement("raw_animation_info",
                x_anims,
                x_skels);
            output.Save(outname);

            WriteOgreSkeleton(model).Save(ogreSkeletonFile);

            foreach(var s in skeletons)
                foreach(var mat4 in s.mat4.items)
                    if (mat4.m[3]!=0 || mat4.m[7]!=0 || mat4.m[15]!=0)
                        throw new Exception("Extra matrix cells nonzero.");
        }
Ejemplo n.º 12
0
 static void DoRewriteTest()
 {
     var f = "C:\\my\\proj\\mods\\spore\\SporeMaster\\SporeMaster\\bin\\Release\\spore.unpacked\\part_models~\\ce_sense_eye_weird_01.rw4\\raw.rw4";
     var m = new RW4Model();
     using (var s = File.OpenRead(f))
         m.Read(s);
     var an = (Animations)m.GetObjects(Animations.type_code)[0];
     an.animations = new Anim[] { an.animations[0] };
     for(int i=0; i<an.animations[0].channel_frame_pose[0].Length; i++)
         an.animations[0].channel_frame_pose[0][i].tx = 0;
     var sections = new int[] { 1, 3, 4, 14, 15 };
     for (int i = sections.Length - 1; i >= 0; i--)
         m.RemoveSection(m.Sections[sections[i]]);
     foreach (var s in m.Sections)
         s.alignment = 0x10;
     m.Test();
     using (var s = File.Create("c:\\temp\\raw.rw4"))
         m.Write(s);
 }
Ejemplo n.º 13
0
        static void CreateModel(string src_name, string out_name)
        {
            using (var stream = File.Create(out_name))
                new ModelPack(src_name, stream);

            Console.WriteLine("Checking model:");
            var tm = new RW4Model();
            using (var stream = File.Open(out_name, FileMode.Open))
                tm.Read(stream);
            foreach (var s in tm.Sections)
                Console.WriteLine(String.Format("  #{0} 0x{1:x} - 0x{2:x}: 0x{3:x} {4}", s.number, s.pos, s.pos + s.size, s.type_code, s.obj.ToString().Substring("SporeMaster.RenderWare4.".Length)));
        }
Ejemplo n.º 14
0
        public ModelPack(string inputFileName, Stream output)
        {
            var src_model = new OgreXmlReader(inputFileName);

            var model = new RW4Model();
            model.New();

            pack(src_model, model);

            model.Write(output);
        }
Ejemplo n.º 15
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);
        }