Ejemplo n.º 1
0
        /// <summary>
        /// Frees unmanaged memory created by <see cref="IMarshalable{Metadata, AiMetadata}.ToNative"/>.
        /// </summary>
        /// <param name="nativeValue">Native value to free</param>
        /// <param name="freeNative">True if the unmanaged memory should be freed, false otherwise.</param>
        public static void FreeNative(IntPtr nativeValue, bool freeNative)
        {
            if (nativeValue == IntPtr.Zero)
            {
                return;
            }

            AiMetadata aiMetadata = MemoryHelper.MarshalStructure <AiMetadata>(nativeValue);

            if (aiMetadata.keys != IntPtr.Zero)
            {
                MemoryHelper.FreeMemory(aiMetadata.keys);
            }

            if (aiMetadata.Values != IntPtr.Zero)
            {
                AiMetadataEntry[] entries = MemoryHelper.FromNativeArray <AiMetadataEntry>(aiMetadata.Values, (int)aiMetadata.NumProperties);

                foreach (AiMetadataEntry entry in entries)
                {
                    if (entry.Data != IntPtr.Zero)
                    {
                        MemoryHelper.FreeMemory(entry.Data);
                    }
                }

                MemoryHelper.FreeMemory(aiMetadata.Values);
            }

            if (freeNative)
            {
                MemoryHelper.FreeMemory(nativeValue);
            }
        }
Ejemplo n.º 2
0
        //Transforms the root node of the scene and writes it back to the native structure
        private bool TransformScene(IntPtr scene)
        {
            BuildMatrix();

            try
            {
                if (!m_scaleRot.IsIdentity)
                {
                    AiScene aiScene = MemoryHelper.MarshalStructure <AiScene>(scene);
                    if (aiScene.RootNode == IntPtr.Zero)
                    {
                        return(false);
                    }

                    IntPtr matrixPtr = MemoryHelper.AddIntPtr(aiScene.RootNode, MemoryHelper.SizeOf <AiString>()); //Skip over Node Name

                    Matrix4x4 matrix = MemoryHelper.Read <Matrix4x4>(matrixPtr);                                   //Get the root transform
                    matrix = matrix * m_scaleRot;                                                                  //Transform

                    //Write back to unmanaged mem
                    MemoryHelper.Write <Matrix4x4>(matrixPtr, ref matrix);

                    return(true);
                }
            }
            catch (Exception)
            {
            }

            return(false);
        }
Ejemplo n.º 3
0
        //Transforms the root node of the scene and writes it back to the native structure
        private unsafe bool TransformScene(IntPtr scene)
        {
            BuildMatrix();

            try
            {
                if (m_scaleRot != Matrix.Identity)
                {
                    AiScene aiScene = MemoryHelper.MarshalStructure <AiScene>(scene);
                    if (aiScene.RootNode == IntPtr.Zero)
                    {
                        return(false);
                    }

                    IntPtr matrixPtr = IntPtr.Add(aiScene.RootNode, MemoryHelper.SizeOf <AiString>()); //Skip over Node Name

                    Matrix matrix = *((Matrix *)matrixPtr);                                            //Get the root transform
                    matrix = matrix * m_scaleRot;                                                      //Transform

                    //Write back to unmanaged mem
                    *((Matrix *)matrixPtr) = matrix;

                    return(true);
                }
            }
            catch (Exception)
            {
            }

            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a new ExportDataBlob.
        /// </summary>
        /// <param name="dataBlob">Unmanaged structure.</param>
        internal ExportDataBlob(ref AiExportDataBlob dataBlob)
        {
            m_name = dataBlob.Name.GetString();

            if(dataBlob.Size.ToUInt32() > 0 && dataBlob.Data != IntPtr.Zero)
                m_data = MemoryHelper.FromNativeArray<byte>(dataBlob.Data, (int) dataBlob.Size.ToUInt32());

            m_next = null;

            if(dataBlob.NextBlob != IntPtr.Zero)
            {
                AiExportDataBlob nextBlob = MemoryHelper.MarshalStructure<AiExportDataBlob>(dataBlob.NextBlob);
                m_next = new ExportDataBlob(ref nextBlob);
            }
        }
        /// <summary>
        /// Frees unmanaged memory created by <see cref="IMarshalable{MeshAnimationAttachment, AiAnimMesh}.ToNative"/>.
        /// </summary>
        /// <param name="nativeValue">Native value to free</param>
        /// <param name="freeNative">True if the unmanaged memory should be freed, false otherwise.</param>
        public static void FreeNative(IntPtr nativeValue, bool freeNative)
        {
            if (nativeValue == IntPtr.Zero)
            {
                return;
            }

            AiMesh aiMesh = MemoryHelper.MarshalStructure <AiMesh>(nativeValue);

            if (aiMesh.NumVertices > 0)
            {
                if (aiMesh.Vertices != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.Vertices);
                }

                if (aiMesh.Normals != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.Normals);
                }

                if (aiMesh.Tangents != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.Tangents);
                }

                if (aiMesh.BiTangents != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.BiTangents);
                }

                //Vertex Color channels
                IntPtr[] colors = aiMesh.Colors;

                if (colors != null)
                {
                    for (int i = 0; i < colors.Length; i++)
                    {
                        IntPtr colorPtr = colors[i];

                        if (colorPtr != IntPtr.Zero)
                        {
                            MemoryHelper.FreeMemory(colorPtr);
                        }
                    }
                }

                //Texture coordinate channels
                IntPtr[] texCoords = aiMesh.TextureCoords;

                if (texCoords != null)
                {
                    for (int i = 0; i < texCoords.Length; i++)
                    {
                        IntPtr texCoordsPtr = texCoords[i];

                        if (texCoordsPtr != IntPtr.Zero)
                        {
                            MemoryHelper.FreeMemory(texCoordsPtr);
                        }
                    }
                }
            }

            if (freeNative)
            {
                MemoryHelper.FreeMemory(nativeValue);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Frees unmanaged memory created by <see cref="IMarshalable{Mesh, AiMesh}.ToNative"/>.
        /// </summary>
        /// <param name="nativeValue">Native value to free</param>
        /// <param name="freeNative">True if the unmanaged memory should be freed, false otherwise.</param>
        public static void FreeNative(IntPtr nativeValue, bool freeNative)
        {
            if (nativeValue == IntPtr.Zero)
            {
                return;
            }

            AiMesh aiMesh = MemoryHelper.MarshalStructure <AiMesh>(nativeValue);

            if (aiMesh.NumVertices > 0)
            {
                if (aiMesh.Vertices != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.Vertices);
                }

                if (aiMesh.Normals != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.Normals);
                }

                if (aiMesh.Tangents != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.Tangents);
                }

                if (aiMesh.BiTangents != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.BiTangents);
                }

                //Vertex Color channels
                IntPtr[] colors = aiMesh.Colors;

                if (colors != null)
                {
                    for (int i = 0; i < colors.Length; i++)
                    {
                        IntPtr colorPtr = colors[i];

                        if (colorPtr != IntPtr.Zero)
                        {
                            MemoryHelper.FreeMemory(colorPtr);
                        }
                    }
                }

                //Texture coordinate channels
                IntPtr[] texCoords = aiMesh.TextureCoords;

                if (texCoords != null)
                {
                    for (int i = 0; i < texCoords.Length; i++)
                    {
                        IntPtr texCoordsPtr = texCoords[i];

                        if (texCoordsPtr != IntPtr.Zero)
                        {
                            MemoryHelper.FreeMemory(texCoordsPtr);
                        }
                    }
                }
            }

            //Faces
            if (aiMesh.NumFaces > 0 && aiMesh.Faces != IntPtr.Zero)
            {
                MemoryHelper.FreeNativeArray <AiFace>(aiMesh.Faces, (int)aiMesh.NumFaces, Face.FreeNative);
            }

            //Bones
            if (aiMesh.NumBones > 0 && aiMesh.Bones != IntPtr.Zero)
            {
                MemoryHelper.FreeNativeArray <AiBone>(aiMesh.Bones, (int)aiMesh.NumBones, Bone.FreeNative, true);
            }

            //Attachment meshes
            if (aiMesh.NumAnimMeshes > 0 && aiMesh.AnimMeshes != IntPtr.Zero)
            {
                MemoryHelper.FreeNativeArray <AiAnimMesh>(aiMesh.AnimMeshes, (int)aiMesh.NumAnimMeshes, MeshAnimationAttachment.FreeNative, true);
            }

            if (freeNative)
            {
                MemoryHelper.FreeMemory(nativeValue);
            }
        }