assimp / assimp-net

Automatically exported from code.google.com/p/assimp-net
196 stars 83 forks source link

ApplyPostProcessing after Scene.ToUnmanagedScene() returns IntPtr.Zero #65

Open JulianVallee opened 3 years ago

JulianVallee commented 3 years ago

Using AssimpNet 4.1.0 I want to load a model from file, clear it's normals and then apply post processing to regenerate smooth normals. For this to work I have to jump through hoops converting the scene from managed to unmanaged memory. The IntPtr to the unmanaged scene is getting corrupted and returned as Zero from AssimpLibrary.Instance.AppyPostProcessing() when it's called on the IntPtr that comes from Scene.ToUnmanagedScene().

Using AssimpNet 5.0.0-beta1 it doesn't return a Ptr.Zero but crashes instead.

For the sake of keeping the examples smaller cleaning up of unmanaged memory is not included, but I have tested if that makes any difference and it doesn't from what I've seen.

Any help or even a different approach to force smooth normals would be greatly appreciated!

Expected Behavior

Variable ptr2 should have a non-zero value.

IntPtr ptr = Scene.ToUnmanagedScene(scene)
IntPtr ptr2 = AssimpLibrary.Instance.ApplyPostProcessing(ptr , PostProcessSteps.Whatever)
// Result: ptr2 != Ptr.Zero

Current Behavior

Variable ptr2 is always zero.

IntPtr ptr = Scene.ToUnmanagedScene(scene)
IntPtr ptr2 = AssimpLibrary.Instance.ApplyPostProcessing(ptr , PostProcessSteps.Whatever)
// Result: ptr2 == Ptr.Zero

Steps to Reproduce

Importing as managed scene

// 1. Import the model without any additional processing
Scene scene = importer.ImportFile(file, PostProcessSteps.Triangulate);

// 2. Clear normals (uncommented for debugging)
//for(int i = 0; i < scene.Meshes.Count; i++)
//{
//    scene.Meshes[i].Normals.Clear();
//}

// 3. Get pointer to unmanaged scene
IntPtr ptrBeforePP = Scene.ToUnmanagedScene(scene);

// 4. Apply post processing after manually manipulated the meshes
IntPtr ptrAfterPP = AssimpLibrary.Instance.ApplyPostProcessing(ptrBeforePP, PostProcessSteps.GenerateSmoothNormals);

assimp-intptr-managed

Importing as unmanaged scene

// 1. Import the model as unmanaged scene
IntPtr ptrBeforeFirstPP = AssimpLibrary.Instance.ImportFile(file, PostProcessSteps.None, IntPtr.Zero, IntPtr.Zero);

// 2. Apply first postprocessing, returning a valid IntPtr
IntPtr ptrAfterFirstPP = AssimpLibrary.Instance.ApplyPostProcessing(ptrBeforeFirstPP, PostProcessSteps.Triangulate);

// 3. Convert unmanaged scene to managed scene and clear normals (uncommented for debugging)
Scene scene = Scene.FromUnmanagedScene(ptrAfterFirstPP);
//for(int i = 0; i < scene.Meshes.Count; i++)
//{
//    scene.Meshes[i].Normals.Clear();
//}

// 4. Convert managed scene to unmanaged scene
IntPtr ptrBeforeSecondPP = Scene.ToUnmanagedScene(scene);

// 5. Apply second postprocessing, returning IntPtr.Zero
IntPtr ptrAfterSecondPP = AssimpLibrary.Instance.ApplyPostProcessing(ptrBeforeSecondPP, PostProcessSteps.GenerateSmoothNormals);

assimp-intptr-unmanaged

Context (Environment)