Ejemplo n.º 1
0
 public static SBoundingBox Default()
 {
     return(new SBoundingBox()
     {
         m_oOriginBB = SPoint3D.Default(),
         m_oEdge1 = SVector3D.Default(),
         m_oEdge2 = SVector3D.Default(),
         m_oEdge3 = SVector3D.Default()
     });
 }
Ejemplo n.º 2
0
 public static SInstrumentExtrinsics Default()
 {
     return(new SInstrumentExtrinsics()
     {
         m_pcReferenceFrame = NotSet.ToPtr(),
         m_oPosition = SPoint3D.Default(),
         m_oLookAt = SVector3D.Default(),
         m_oUp = SVector3D.Default(),
         m_oBoundingBox = SBoundingBox.Default()
     });
 }
Ejemplo n.º 3
0
        public void readBuffer(CAnimationBufferBitwiseCompressed buffer, int selectedAnimIdx, CR2WFile animFile)
        {
            uint numFrames = buffer.NumFrames.val;
            float animDuration = buffer.Duration?.val ?? 1.0f;
            animationSpeed = numFrames / animDuration;
            uint keyFrame = 0;
            byte[] data;
            //data = (chunk.GetVariableByName("fallbackData") as CByteArray).Bytes;
            currentAnimName = AnimationNames[selectedAnimIdx].Key;
            exportData.name = AnimationNames[selectedAnimIdx].Key;
            exportData.duration = animDuration;
            exportData.numFrames = numFrames;
            exportData.dt = buffer.Dt?.val ?? 0.03333333f;
            DeferredDataBuffer deferredData = buffer.DeferredData;
            var streamingOption = buffer.StreamingOption;

            if (deferredData != null && deferredData.Bufferdata.val != 0)
                if (streamingOption.WrappedEnum == Enums.SAnimationBufferStreamingOption.ABSO_PartiallyStreamable)
                    data = ConvertAnimation.Combine(buffer.Data.Bytes,
                    File.ReadAllBytes(animFile.FileName + "." + deferredData.Bufferdata.val + ".buffer"));
                else
                    data = File.ReadAllBytes(animFile.FileName + "." + deferredData.Bufferdata.val + ".buffer");
            else
                data = buffer.Data.Bytes;
            using (MemoryStream ms = new MemoryStream(data))
            using (BinaryReader br = new BinaryReader(ms))
            {
                foreach (SAnimationBufferBitwiseCompressedBoneTrack bone in buffer.Bones)
                {
                    List<uint> currkeyframe = new List<uint>();
                    List<Quaternion> currorient = new List<Quaternion>();
                    List<Vector3Df> currorientEuler = new List<Vector3Df>();
                    currentBones.Add(bone);

                    br.BaseStream.Position = bone.Orientation.DataAddr.val;
                    int orientNumFrames = bone.Orientation.NumFrames.val;

                    for (uint idx = 0; idx < orientNumFrames; idx++)
                    {
                        keyFrame = idx;
                        //keyFrame += numFrames;
                        currkeyframe.Add(keyFrame);
                        //bone.GetVariableByName("position");
                        byte[] odata = br.ReadBytes(6);
                        ulong bits = (ulong)odata[0] << 40 | (ulong)odata[1] << 32 | (ulong)odata[2] << 24 | (ulong)odata[3] << 16 | (ulong)odata[4] << 8 | odata[5];

                        ushort[] orients = new ushort[4];
                        float[] quart = new float[4];
                        orients[0] = (ushort)((bits & 0x0000FFF000000000) >> 36);
                        orients[1] = (ushort)((bits & 0x0000000FFF000000) >> 24);
                        orients[2] = (ushort)((bits & 0x0000000000FFF000) >> 12);
                        orients[3] = (ushort)((bits & 0x0000000000000FFF));

                        for (int i = 0; i < orients.Length; i++)
                        {
                            float fVal = (2047.0f - orients[i]) * (1 / 2048.0f);
                            quart[i] = fVal;
                        }
                        quart[3] = -quart[3];

                        Quaternion orientation = new Quaternion(quart[0], quart[1], quart[2], quart[3]);
                        currorient.Add(orientation);
                        Vector3Df euler = orientation.ToEuler();
                        currorientEuler.Add(euler);
                        //Console.WriteLine("Euler : x=%f, y=%f, z=%f", euler.X, euler.Y, euler.Z);
                    }

                    orientKeyframes.Add(currkeyframe);
                    orientations.Add(currorient);
                    orientationsEuler.Add(currorientEuler);

                    // TODO: Refactor
                    List<Vector3Df> currposition = new List<Vector3Df>();
                    currkeyframe = new List<uint>();
                    int compression = 0;
                    var compr = bone.Position.Compression;
                    if (compr != null)
                        compression = compr.val;
                    var addr = bone.Position.DataAddr;
                    if (addr != null)
                        br.BaseStream.Position = addr.val;
                    else
                        br.BaseStream.Position = 0;
                    var posNumFrames = bone.Position.NumFrames.val;
                    for (uint idx = 0; idx < posNumFrames; idx++)
                    {
                        keyFrame = idx;
                        //keyFrame += numFrames;
                        currkeyframe.Add(keyFrame);
                        var vec = new SVector3D(null, null, "");
                        vec.Read(br, compression);
                        Vector3Df pos = new Vector3Df(vec.X.val, vec.Y.val, vec.Z.val);
                        currposition.Add(pos);
                    }
                    positionsKeyframes.Add(currkeyframe);
                    positions.Add(currposition);

                    List<Vector3Df> currscale = new List<Vector3Df>();
                    currkeyframe = new List<uint>();
                    compression = 0;
                    compr = bone.Scale.Compression;
                    if (compr != null)
                        compression = compr.val;
                    addr = bone.Scale.DataAddr;
                    if (addr != null)
                        br.BaseStream.Position = addr.val;
                    else
                        br.BaseStream.Position = 0;
                    var scaleNumFrames = bone.Scale.NumFrames.val;
                    for (uint idx = 0; idx < scaleNumFrames; idx++)
                    {
                        keyFrame = idx;
                        //keyFrame += numFrames;
                        currkeyframe.Add(keyFrame);
                        var vec = new SVector3D(null, null, "");
                        vec.Read(br, compression);
                        Vector3Df scale = new Vector3Df(vec.X.val, vec.Y.val, vec.Z.val);
                        currscale.Add(scale);
                    }
                    scalesKeyframes.Add(currkeyframe);
                    scales.Add(currscale);
                }
            }
        }
Ejemplo n.º 4
0
 public static V3d ToV3d(this SVector3D self)
 {
     return(new V3d(self.m_dX, self.m_dY, self.m_dZ));
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Read animations and animbuffers data.
        /// </summary>
        public void SelectAnimation(CR2WFile animFile, int selectedAnimIdx)
        {
            // *************** READ ANIMATION DATA ***************
            if (animFile != null && selectedAnimIdx != -1)
            {
                positions.Clear();
                positionsKeyframes.Clear();
                orientations.Clear();
                orientKeyframes.Clear();
                scales.Clear();
                scalesKeyframes.Clear();
                foreach (var chunk in animFile.chunks)
                {
                    if (chunk.REDType == "CAnimationBufferBitwiseCompressed" && chunk.ChunkIndex == AnimationNames[selectedAnimIdx].Value &&
                        chunk.data is CAnimationBufferBitwiseCompressed buffer)
                    {
                        uint  numFrames    = buffer.NumFrames.val;
                        float animDuration = buffer.Duration?.val ?? 1.0f;
                        animationSpeed = numFrames / animDuration;
                        uint   keyFrame = 0;
                        byte[] data;
                        var    deferredData    = buffer.DeferredData;
                        var    streamingOption = buffer.StreamingOption;
                        if (deferredData != null && deferredData.Bufferdata.val != 0)
                        {
                            if (streamingOption != null && streamingOption.ToString() == "ABSO_PartiallyStreamable")
                            {
                                data = ConvertAnimation.Combine(buffer.Data.Bytes,
                                                                File.ReadAllBytes(animFile.FileName + "." + deferredData.Bufferdata.val + ".buffer"));
                            }
                            else
                            {
                                data = File.ReadAllBytes(animFile.FileName + "." + deferredData.Bufferdata.val + ".buffer");
                            }
                        }
                        else
                        {
                            data = buffer.Data.Bytes;
                        }
                        using (MemoryStream ms = new MemoryStream(data))
                            using (BinaryReader br = new BinaryReader(ms))
                            {
                                foreach (var bone in buffer.Bones)
                                {
                                    List <uint>       currkeyframe = new List <uint>();
                                    List <Quaternion> currorient   = new List <Quaternion>();

                                    br.BaseStream.Position = bone.Orientation.DataAddr.val;
                                    int orientNumFrames = bone.Orientation.NumFrames.val;
                                    for (uint idx = 0; idx < orientNumFrames; idx++)
                                    {
                                        keyFrame = idx;
                                        //keyFrame += numFrames;
                                        currkeyframe.Add(keyFrame);
                                        //bone.GetVariableByName("position");
                                        string cm = buffer.OrientationCompressionMethod.ToString() ?? "";
                                        if (cm.Contains("ABOCM_PackIn48bitsW"))
                                        {
                                            byte[] odata = br.ReadBytes(6);
                                            ulong  bits  = (ulong)odata[0] << 40 | (ulong)odata[1] << 32 | (ulong)odata[2] << 24 | (ulong)odata[3] << 16 | (ulong)odata[4] << 8 | odata[5];

                                            ushort[] orients = new ushort[4];
                                            float[]  quart   = new float[4];
                                            orients[0] = (ushort)((bits & 0x0000FFF000000000) >> 36);
                                            orients[1] = (ushort)((bits & 0x0000000FFF000000) >> 24);
                                            orients[2] = (ushort)((bits & 0x0000000000FFF000) >> 12);
                                            orients[3] = (ushort)((bits & 0x0000000000000FFF));

                                            for (int i = 0; i < orients.Length; i++)
                                            {
                                                float fVal = (2047.0f - orients[i]) * (1 / 2048.0f);
                                                quart[i] = fVal;
                                            }
                                            quart[3] = -quart[3];

                                            currorient.Add(new Quaternion(quart[0], quart[1], quart[2], quart[3]));
                                        }
                                        else
                                        {
                                            byte[] odata = br.ReadBytes(8);

                                            ushort[] plain = new ushort[4];
                                            float[]  quart = new float[4];

                                            plain[0] = BitConverter.ToUInt16(odata, 0);
                                            plain[1] = BitConverter.ToUInt16(odata, 2);
                                            plain[2] = BitConverter.ToUInt16(odata, 4);
                                            plain[3] = BitConverter.ToUInt16(odata, 6);

                                            for (int i = 0; i < plain.Length; i++)
                                            {
                                                float fVal = (32767.0f - plain[i]) * (1 / 32768.0f);
                                                quart[i] = fVal;
                                            }
                                            quart[3] = -quart[3];

                                            currorient.Add(new Quaternion(quart[0], quart[1], quart[2], quart[3]));
                                        }
                                    }

                                    orientKeyframes.Add(currkeyframe);
                                    orientations.Add(currorient);

                                    // TODO: Refactor
                                    List <Vector3Df> currposition = new List <Vector3Df>();
                                    currkeyframe = new List <uint>();
                                    int compression = 0;
                                    var compr       = bone.Position.Compression;
                                    if (compr != null)
                                    {
                                        compression = compr.val;
                                    }
                                    var addr = bone.Position.DataAddr;
                                    if (addr != null)
                                    {
                                        br.BaseStream.Position = addr.val;
                                    }
                                    else
                                    {
                                        br.BaseStream.Position = 0;
                                    }
                                    var posNumFrames = bone.Position.NumFrames.val;
                                    for (uint idx = 0; idx < posNumFrames; idx++)
                                    {
                                        keyFrame = idx;
                                        //keyFrame += numFrames;
                                        currkeyframe.Add(keyFrame);
                                        var vec = new SVector3D(null, null, "");
                                        vec.Read(br, compression);
                                        Vector3Df pos = new Vector3Df(vec.X.val, vec.Y.val, vec.Z.val);
                                        currposition.Add(pos);
                                    }
                                    positionsKeyframes.Add(currkeyframe);
                                    positions.Add(currposition);

                                    List <Vector3Df> currscale = new List <Vector3Df>();
                                    currkeyframe = new List <uint>();
                                    compression  = 0;
                                    compr        = bone.Scale.Compression;
                                    if (compr != null)
                                    {
                                        compression = compr.val;
                                    }
                                    addr = bone.Scale.DataAddr;
                                    if (addr != null)
                                    {
                                        br.BaseStream.Position = addr.val;
                                    }
                                    else
                                    {
                                        br.BaseStream.Position = 0;
                                    }
                                    var scaleNumFrames = bone.Scale.NumFrames;
                                    for (uint idx = 0; idx < scaleNumFrames.val; idx++)
                                    {
                                        keyFrame = idx;
                                        //keyFrame += numFrames;
                                        currkeyframe.Add(keyFrame);
                                        var vec = new SVector3D(null, null, "");
                                        vec.Read(br, compression);
                                        Vector3Df scale = new Vector3Df(vec.X.val, vec.Y.val, vec.Z.val);
                                        currscale.Add(scale);
                                    }
                                    scalesKeyframes.Add(currkeyframe);
                                    scales.Add(currscale);
                                }
                            }
                        break;
                    }
                }
            }
        }
        private void AddLayer(string layerFileName, string layerName, ref int meshId)
        {
            float RADIANS_TO_DEGREES = (float)(180 / Math.PI);

            CR2WFile layer;

            using (var fs = new FileStream(layerFileName, FileMode.Open, FileAccess.Read))
                using (var reader = new BinaryReader(fs))
                {
                    layer = new CR2WFile();
                    layer.Read(reader);
                    fs.Close();
                }

            TreeNode layerNode = new TreeNode(layerName);

            foreach (var chunk in layer.chunks)
            {
                if (chunk.REDType == "CSectorData")
                {
                    CSectorData sd = (CSectorData)chunk.data;

                    progressBar.Invoke((MethodInvoker) delegate
                    {
                        progressBar.Maximum = sd.BlockData.Count;
                    });

                    // only add sector node if there are meshes
                    foreach (var block in sd.BlockData)
                    {
                        if (block.packedObjectType == Enums.BlockDataObjectType.Mesh)
                        {
                            SVector3D  position = block.position;
                            CMatrix3x3 rot = block.rotationMatrix;
                            float      rx, ry, rz;
                            MatrixToEuler(rot, out rx, out ry, out rz); // radians

                            Vector3Df rotation    = new Vector3Df(rx * RADIANS_TO_DEGREES, ry * RADIANS_TO_DEGREES, rz * RADIANS_TO_DEGREES);
                            Vector3Df translation = new Vector3Df(position.X.val, position.Y.val, position.Z.val);

                            SBlockDataMeshObject mo = (SBlockDataMeshObject)block.packedObject;
                            ushort meshIndex        = mo.meshIndex.val;
                            if (meshIndex > sd.Resources.Count)
                            {
                                continue;
                            }
                            string meshName = sd.Resources[meshIndex].pathHash.val;

                            if (string.IsNullOrEmpty(meshName))
                            {
                                continue;
                            }

                            RenderMessage message = new RenderMessage(MessageType.ADD_MESH_NODE, meshName, translation, rotation, layerNode);
                            commandQueue.Enqueue(message);

                            progressBar.Invoke((MethodInvoker) delegate
                            {
                                progressBar.PerformStep();
                            });
                        }
                    }
                }
            }
        }