/// <summary> /// Marshals a managed scene to unmanaged memory. The unmanaged memory must be freed with a call to /// <see cref="FreeUnmanagedScene"/>, the memory is owned by AssimpNet and cannot be freed by the native library. /// </summary> /// <param name="scene">Scene data</param> /// <returns>Unmanaged scene or NULL if the scene is null.</returns> public static IntPtr ToUnmanagedScene(Scene scene) { if (scene == null) { return(IntPtr.Zero); } return(MemoryHelper.ToNativePointer <Scene, AiScene>(scene)); }
/// <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; }
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); }
/// <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 <Scene, AiScene> .ToNative(IntPtr thisPtr, out AiScene nativeValue) { nativeValue.Flags = m_flags; nativeValue.Materials = IntPtr.Zero; nativeValue.RootNode = IntPtr.Zero; nativeValue.Meshes = IntPtr.Zero; nativeValue.Lights = IntPtr.Zero; nativeValue.Cameras = IntPtr.Zero; nativeValue.Textures = IntPtr.Zero; nativeValue.Animations = IntPtr.Zero; nativeValue.PrivateData = IntPtr.Zero; nativeValue.NumMaterials = (uint)MaterialCount; nativeValue.NumMeshes = (uint)MeshCount; nativeValue.NumLights = (uint)LightCount; nativeValue.NumCameras = (uint)CameraCount; nativeValue.NumTextures = (uint)TextureCount; nativeValue.NumAnimations = (uint)AnimationCount; //Write materials if (nativeValue.NumMaterials > 0) { nativeValue.Materials = MemoryHelper.ToNativeArray <Material, AiMaterial>(m_materials.ToArray(), true); } //Write scenegraph if (m_rootNode != null) { nativeValue.RootNode = MemoryHelper.ToNativePointer <Node, AiNode>(m_rootNode); } //Write meshes if (nativeValue.NumMeshes > 0) { nativeValue.Meshes = MemoryHelper.ToNativeArray <Mesh, AiMesh>(m_meshes.ToArray(), true); } //Write lights if (nativeValue.NumLights > 0) { nativeValue.Lights = MemoryHelper.ToNativeArray <Light, AiLight>(m_lights.ToArray(), true); } //Write cameras if (nativeValue.NumCameras > 0) { nativeValue.Cameras = MemoryHelper.ToNativeArray <Camera, AiCamera>(m_cameras.ToArray(), true); } //Write textures if (nativeValue.NumTextures > 0) { nativeValue.Textures = MemoryHelper.ToNativeArray <EmbeddedTexture, AiTexture>(m_textures.ToArray(), true); } //Write animations if (nativeValue.NumAnimations > 0) { nativeValue.Animations = MemoryHelper.ToNativeArray <Animation, AiAnimation>(m_animations.ToArray(), true); } }