Ejemplo n.º 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);
        }
Ejemplo n.º 2
0
        private static unsafe byte[] SetMaterialString(String value, byte[] existing)
        {
            if (String.IsNullOrEmpty(value))
            {
                return(null);
            }

            int stringSize = Encoding.UTF8.GetByteCount(value);

            if (stringSize < 0)
            {
                return(null);
            }

            int size = stringSize + 1 + sizeof(int);

            byte[] data = existing;

            if (existing == null || existing.Length != size)
            {
                data = new byte[size];

                fixed(byte *bytePtr = &data[0])
                {
                    MemoryHelper.Write <int>(new IntPtr(bytePtr), ref stringSize);
                    byte[] utfBytes = Encoding.UTF8.GetBytes(value);
                    MemoryHelper.Write <byte>(new IntPtr(bytePtr + sizeof(int)), utfBytes, 0, utfBytes.Length);
                    //Last byte should be zero
                }

                return(data);
        }
Ejemplo n.º 3
0
        private UIntPtr OnAiFileReadProc(IntPtr file, IntPtr dataRead, UIntPtr sizeOfElemInBytes, UIntPtr numElements)
        {
            if (m_filePtr != file)
            {
                return(UIntPtr.Zero);
            }

            long longSize = (long)sizeOfElemInBytes.ToUInt64();
            long longNum  = (long)numElements.ToUInt64();
            long count    = longSize * longNum;

            byte[] byteBuffer = GetByteBuffer(longSize, longNum);

            long actualCount = 0;

            try
            {
                actualCount = Read(byteBuffer, count);

                if (actualCount > 0)
                {
                    MemoryHelper.Write <byte>(dataRead, byteBuffer, 0, (int)actualCount);
                }
            }
            catch (Exception) { /*Assimp will report an IO error*/ }

            return(new UIntPtr((ulong)actualCount / (ulong)longSize));
        }
Ejemplo n.º 4
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 <Node, AiNode> .ToNative(IntPtr thisPtr, out AiNode nativeValue)
        {
            nativeValue.Name           = new AiString(m_name);
            nativeValue.Transformation = m_transform;
            nativeValue.Parent         = IntPtr.Zero;

            nativeValue.NumMeshes = (uint)m_meshes.Count;
            nativeValue.Meshes    = IntPtr.Zero;
            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);
            }

            if (nativeValue.NumMeshes > 0)
            {
                nativeValue.Meshes = MemoryHelper.ToNativeArray <int>(m_meshes.ToArray());
            }

            //Now descend through the children
            nativeValue.NumChildren = (uint)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   = m_children[i];

                    IntPtr childPtr = IntPtr.Zero;

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

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

            //Finally finish writing to the native struct
            nativeValue.Children = childrenPtr;
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        private static unsafe byte[] SetMaterialString(String value, byte[] existing)
        {
            if (String.IsNullOrEmpty(value))
            {
                return(null);
            }

            int stringSize = Encoding.UTF8.GetByteCount(value);

            if (stringSize < 0)
            {
                return(null);
            }

            int size = stringSize + 1 + sizeof(int);

            byte[] data = existing;

            if (existing == null || existing.Length != size)
            {
                data = new byte[size];

                fixed(byte *bytePtr = &data[0])
                {
                    MemoryHelper.Write <int>(new IntPtr(bytePtr), ref stringSize);
                    byte[] utfBytes = Encoding.UTF8.GetBytes(value);
                    MemoryHelper.Write <byte>(new IntPtr(bytePtr + sizeof(int)), utfBytes, 0, utfBytes.Length);
                    //Last byte should be zero
                }

                return(data);
        }

        #region IMarshalable Implementation

        /// <summary>
        /// Gets if the native value type is blittable (that is, does not require marshaling by the runtime, e.g. has MarshalAs attributes).
        /// </summary>
        bool IMarshalable <MaterialProperty, AiMaterialProperty> .IsNativeBlittable {
            get { return(true); }
        }

        /// <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 <MaterialProperty, AiMaterialProperty> .ToNative(IntPtr thisPtr, out AiMaterialProperty nativeValue)
        {
            nativeValue.Key        = new AiString(m_name);
            nativeValue.Type       = m_type;
            nativeValue.Index      = (uint)m_texIndex;
            nativeValue.Semantic   = m_texType;
            nativeValue.Data       = IntPtr.Zero;
            nativeValue.DataLength = 0;

            if (m_rawValue != null)
            {
                nativeValue.DataLength = (uint)m_rawValue.Length;
                nativeValue.Data       = MemoryHelper.ToNativeArray <byte>(m_rawValue);
            }
        }

        /// <summary>
        /// Reads the unmanaged data from the native value.
        /// </summary>
        /// <param name="nativeValue">Input native value</param>
        void IMarshalable <MaterialProperty, AiMaterialProperty> .FromNative(ref AiMaterialProperty nativeValue)
        {
            m_name     = nativeValue.Key.GetString();
            m_type     = nativeValue.Type;
            m_texIndex = (int)nativeValue.Index;
            m_texType  = nativeValue.Semantic;
            m_rawValue = null;

            if (nativeValue.DataLength > 0 && nativeValue.Data != IntPtr.Zero)
                m_rawValue = MemoryHelper.FromNativeArray <byte>(nativeValue.Data, (int)nativeValue.DataLength); }
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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);
        }