Beispiel #1
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);
        }
        /// <summary>
        /// Gets the property raw data as a Color4D.
        /// </summary>
        /// <returns>Color4D</returns>
        public Color4D GetColor4DValue()
        {
            if (m_type != PropertyType.Float || m_rawValue == null)
            {
                return(new Color4D());
            }

            //We may have a Color that's RGB, so still read it and set alpha to 1.0
            unsafe
            {
                fixed(byte *ptr = m_rawValue)
                {
                    if (m_rawValue.Length >= MemoryHelper.SizeOf <Color4D>())
                    {
                        return(MemoryHelper.Read <Color4D>(new IntPtr(ptr)));
                    }
                    else if (m_rawValue.Length >= MemoryHelper.SizeOf <Color3D>())
                    {
                        return(new Color4D(MemoryHelper.Read <Color3D>(new IntPtr(ptr)), 1.0f));
                    }
                }
            }

            return(new Color4D());
        }
Beispiel #3
0
        /// <summary>
        /// Constructs a new IOStream.
        /// </summary>
        /// <param name="pathToFile">Path to file given by Assimp</param>
        /// <param name="fileMode">Desired file access mode</param>
        public IOStream(String pathToFile, FileIOMode fileMode)
        {
            m_pathToFile = pathToFile;
            m_fileMode   = fileMode;

            m_writeProc    = OnAiFileWriteProc;
            m_readProc     = OnAiFileReadProc;
            m_tellProc     = OnAiFileTellProc;
            m_fileSizeProc = OnAiFileSizeProc;
            m_seekProc     = OnAiFileSeekProc;
            m_flushProc    = OnAiFileFlushProc;

            AiFile file;

            file.WriteProc    = Marshal.GetFunctionPointerForDelegate(m_writeProc);
            file.ReadProc     = Marshal.GetFunctionPointerForDelegate(m_readProc);
            file.TellProc     = Marshal.GetFunctionPointerForDelegate(m_tellProc);
            file.FileSizeProc = Marshal.GetFunctionPointerForDelegate(m_fileSizeProc);
            file.SeekProc     = Marshal.GetFunctionPointerForDelegate(m_seekProc);
            file.FlushProc    = Marshal.GetFunctionPointerForDelegate(m_flushProc);
            file.UserData     = IntPtr.Zero;

            m_filePtr = MemoryHelper.AllocateMemory(MemoryHelper.SizeOf <AiFile>());
            Marshal.StructureToPtr(file, m_filePtr, false);
        }
Beispiel #4
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);
        }
        private unsafe T GetValueAs <T>() where T : struct
        {
            int size = MemoryHelper.SizeOf <T>();

            if (m_rawValue != null && m_rawValue.Length >= size)
            {
                fixed(byte *ptr = m_rawValue)
                {
                    return(MemoryHelper.Read <T>(new IntPtr(ptr)));
                }
            }

            return(default(T));
        }
Beispiel #6
0
        private unsafe bool SetValueAs <T>(T value) where T : struct
        {
            int size = MemoryHelper.SizeOf <T>();

            //Resize byte array if necessary
            if (m_rawValue == null || m_rawValue.Length != size)
            {
                m_rawValue = new byte[size];

                fixed(byte *ptr = m_rawValue)
                MemoryHelper.Write <T>(new IntPtr(ptr), value);

                return(true);
        }
Beispiel #7
0
        /// <summary>
        /// Constructs a new IOSystem.
        /// </summary>
        public IOSystem()
        {
            m_openProc  = OnAiFileOpenProc;
            m_closeProc = OnAiFileCloseProc;

            AiFileIO fileIO;

            fileIO.OpenProc  = Marshal.GetFunctionPointerForDelegate(m_openProc);
            fileIO.CloseProc = Marshal.GetFunctionPointerForDelegate(m_closeProc);
            fileIO.UserData  = IntPtr.Zero;

            m_fileIOPtr = MemoryHelper.AllocateMemory(MemoryHelper.SizeOf <AiFileIO>());
            Marshal.StructureToPtr(fileIO, m_fileIOPtr, false);

            m_openedFiles = new Dictionary <IntPtr, IOStream>();
        }
        private unsafe T[] GetValueArrayAs <T>(int count) where T : struct
        {
            int size = MemoryHelper.SizeOf <T>();

            if (m_rawValue != null && (m_rawValue.Length >= (size * count)))
            {
                T[] array = new T[count];
                fixed(byte *ptr = m_rawValue)
                {
                    MemoryHelper.Read <T>(new IntPtr(ptr), array, 0, count);
                }

                return(array);
            }

            return(null);
        }
Beispiel #9
0
        private void Initialize(LoggingCallback callback, String userData)
        {
            if (userData == null)
            {
                userData = String.Empty;
            }

            m_assimpCallback = OnAiLogStreamCallback;
            m_logCallback    = callback;
            m_userData       = userData;

            AiLogStream logStream;

            logStream.Callback = Marshal.GetFunctionPointerForDelegate(m_assimpCallback);
            logStream.UserData = IntPtr.Zero;

            m_logstreamPtr = MemoryHelper.AllocateMemory(MemoryHelper.SizeOf <AiLogStream>());
            Marshal.StructureToPtr(logStream, m_logstreamPtr, false);
        }
Beispiel #10
0
        private unsafe bool SetValueArrayAs <T>(T[] data) where T : struct
        {
            if (data == null || data.Length == 0)
            {
                return(false);
            }

            int size = MemoryHelper.SizeOf <T>(data);

            //Resize byte array if necessary
            if (m_rawValue == null || m_rawValue.Length != size)
            {
                m_rawValue = new byte[size];

                fixed(byte *ptr = m_rawValue)
                MemoryHelper.Write <T>(new IntPtr(ptr), data, 0, data.Length);

                return(true);
        }
Beispiel #11
0
        /// <summary>
        /// Frees unmanaged memory created by <see cref="IMarshalable{Bone, AiBone}.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;
            }

            AiBone aiBone     = MemoryHelper.Read <AiBone>(nativeValue);
            int    numWeights = MemoryHelper.Read <int>(MemoryHelper.AddIntPtr(nativeValue, MemoryHelper.SizeOf <AiString>()));
            IntPtr weightsPtr = MemoryHelper.AddIntPtr(nativeValue, MemoryHelper.SizeOf <AiString>() + sizeof(uint));

            if (aiBone.NumWeights > 0 && aiBone.Weights != IntPtr.Zero)
            {
                MemoryHelper.FreeMemory(aiBone.Weights);
            }

            if (freeNative)
            {
                MemoryHelper.FreeMemory(nativeValue);
            }
        }
Beispiel #12
0
        /// <summary>
        /// Writes the managed data to the native value.
        /// </summary>
        /// <param name="thisPtr">Optional pointer to the memory that will hold the native value.</param>
        /// <param name="nativeValue">Output native value</param>
        void IMarshalable <Metadata, AiMetadata> .ToNative(IntPtr thisPtr, out AiMetadata nativeValue)
        {
            nativeValue = new AiMetadata();
            nativeValue.NumProperties = (uint)Count;

            AiString[]        keys    = new AiString[Count];
            AiMetadataEntry[] entries = new AiMetadataEntry[Count];
            int index = 0;

            foreach (KeyValuePair <String, Entry> kv in this)
            {
                AiMetadataEntry entry = new AiMetadataEntry();
                entry.DataType = kv.Value.DataType;

                switch (kv.Value.DataType)
                {
                case MetaDataType.Bool:
                    entry.Data = MemoryHelper.AllocateMemory(sizeof(bool));
                    bool boolValue = (bool)kv.Value.Data;
                    MemoryHelper.Write <bool>(entry.Data, ref boolValue);
                    break;

                case MetaDataType.Float:
                    entry.Data = MemoryHelper.AllocateMemory(sizeof(float));
                    float floatValue = (float)kv.Value.Data;
                    MemoryHelper.Write <float>(entry.Data, ref floatValue);
                    break;

                case MetaDataType.Int:
                    entry.Data = MemoryHelper.AllocateMemory(sizeof(int));
                    int intValue = (int)kv.Value.Data;
                    MemoryHelper.Write <int>(entry.Data, ref intValue);
                    break;

                case MetaDataType.String:
                    entry.Data = MemoryHelper.AllocateMemory(MemoryHelper.SizeOf <AiString>());
                    AiString aiStringValue = new AiString(kv.Value.Data as String);
                    MemoryHelper.Write <AiString>(entry.Data, ref aiStringValue);
                    break;

                case MetaDataType.UInt64:
                    entry.Data = MemoryHelper.AllocateMemory(sizeof(UInt64));
                    UInt64 uint64Value = (UInt64)kv.Value.Data;
                    MemoryHelper.Write <UInt64>(entry.Data, ref uint64Value);
                    break;

                case MetaDataType.Vector3D:
                    entry.Data = MemoryHelper.AllocateMemory(MemoryHelper.SizeOf <Vector3D>());
                    Vector3D vectorValue = (Vector3D)kv.Value.Data;
                    MemoryHelper.Write <Vector3D>(entry.Data, ref vectorValue);
                    break;
                }

                keys[index]    = new AiString(kv.Key);
                entries[index] = entry;
                index++;
            }

            nativeValue.keys   = MemoryHelper.ToNativeArray <AiString>(keys);
            nativeValue.Values = MemoryHelper.ToNativeArray <AiMetadataEntry>(entries);
        }
Beispiel #13
0
        private IntPtr ToNativeRecursive(IntPtr parentPtr, Node node)
        {
            if (node == null)
            {
                return(IntPtr.Zero);
            }

            int sizeofNative = MemoryHelper.SizeOf <AiNode>();

            //Allocate the memory that will hold the node
            IntPtr nodePtr = MemoryHelper.AllocateMemory(sizeofNative);

            //First fill the native struct
            AiNode nativeValue;

            nativeValue.Name           = new AiString(node.m_name);
            nativeValue.Transformation = node.m_transform;
            nativeValue.Parent         = parentPtr;

            nativeValue.NumMeshes = (uint)node.m_meshes.Count;
            nativeValue.Meshes    = MemoryHelper.ToNativeArray <int>(node.m_meshes.ToArray());
            nativeValue.MetaData  = IntPtr.Zero;

            //If has metadata, create it, otherwise it should be NULL
            if (m_metaData.Count > 0)
            {
                nativeValue.MetaData = MemoryHelper.ToNativePointer <Metadata, AiMetadata>(m_metaData);
            }

            //Now descend through the children
            nativeValue.NumChildren = (uint)node.m_children.Count;

            int numChildren = (int)nativeValue.NumChildren;
            int stride      = IntPtr.Size;

            IntPtr childrenPtr = IntPtr.Zero;

            if (numChildren > 0)
            {
                childrenPtr = MemoryHelper.AllocateMemory(numChildren * IntPtr.Size);

                for (int i = 0; i < numChildren; i++)
                {
                    IntPtr currPos = MemoryHelper.AddIntPtr(childrenPtr, stride * i);
                    Node   child   = node.m_children[i];

                    IntPtr childPtr = IntPtr.Zero;

                    //Recursively create the children and its children
                    if (child != null)
                    {
                        childPtr = ToNativeRecursive(nodePtr, child);
                    }

                    //Write the child's node ptr to our array
                    MemoryHelper.Write <IntPtr>(currPos, ref childPtr);
                }
            }

            //Finall finish writing to the native struct, and write the whole thing to the memory we allocated previously
            nativeValue.Children = childrenPtr;
            MemoryHelper.Write <AiNode>(nodePtr, ref nativeValue);

            return(nodePtr);
        }