Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JulianVallee opened this issue Nov 12, 2020 · 0 comments

Comments

@JulianVallee
Copy link

JulianVallee commented Nov 12, 2020

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)

  • .NET Core 3.1
  • Issue is independent from the model or file format being used
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant