Horde3D http://horde3d.org/forums/ |
|
horde with c# wrapper: convert intptr to float array http://horde3d.org/forums/viewtopic.php?f=2&t=829 |
Page 1 of 1 |
Author: | dubbyconqueror [ 27.06.2009, 11:24 ] |
Post subject: | horde with c# wrapper: convert intptr to float array |
Hey guys, i'm using the .NET wrapper for horde. I want to use the getNodeTransformMatrices function to save a transform matrix in my own c#-matrix class. In C++, this would be easy by saving the returned matrix in a Pointer, which could be used as a 2-dimensional float-pointer - you can use this pointer at once as a matrix type. I'm not a good C++-programmer (thats why i use the .net-wrapper), but our prof gave us this solution how to handle the matrix in C++: Code: float *pxf ; Horde3D :: getNodeTransformMatrices (plane , NULL , ( const float **) &pxf ); Matrix44f planeXform ; planeXform .set (pxf ); In the C#-Wrapper, the getNodeTransformMatrices needs a struct of the type IntPtr to save its values to. Code: IntPtr hordePointer; // write Horde matrix to Intpointer Horde3D.getNodeTransformMatrices(env, out test, out hordePointer); Now i have the matrix returned by horde in one Intpointer. I found out that the size of this pointer (returned by Marshal.SizeOf(hordePointer) is 4. So i guess this Intpointer points itself to 4 Intpointers. Each of this Intpointers should contain 4 float values, that represent the Transformation Matrix. Code: IntPtr[] testArray = new IntPtr[4]; // get the 4 intpointers out of the hordepointer for (int i = 0; i < Marshal.SizeOf(hordePointer); i++) { testArray[i] = Marshal.ReadIntPtr(hordePointer, i * Marshal.SizeOf(typeof (IntPtr))); } Now i have 4 Intpointers. My problem now: how do i extract the 4 float values out of each intpointer? I guess i'm not far away from a solution... Anyone here good some experience in handling pointers in C#? Thanks very much in advance for your help! |
Author: | Expandable [ 27.06.2009, 16:38 ] |
Post subject: | Re: horde with c# wrapper: convert intptr to float array |
Well, I'm just guessing here as I don't have a compiler available right now... but the sizeof function most likely gives you the size in BYTES (like the C sizeof function). 4 bytes is the pointer size on 32bit Windows. So the IntPtr probably points to the first element of the matrix. Since you know it's a matrix with 16 floats, you should be able to get all values using the returned IntPtr. By the way, I think you might be confused by the naming of IntPtr. It does not mean (in C-syntax) int*. An IntPtr is just a wrapper around a pointer and its size is 32bits (4bytes) on 32bit systems and 64bit (8bytes) on 64 bit systems. Maybe the MSDN article helps you: http://msdn.microsoft.com/en-us/library ... tptr(VS.90).aspx |
Author: | dubbyconqueror [ 27.06.2009, 16:49 ] |
Post subject: | Re: horde with c# wrapper: convert intptr to float array |
yeah thats right, thank you, of course its 4 bytes - my fault... But i still don't know how to access the elements of this intPtr, although i know it has 16 elements. What i have now: Code: IntPtr nullPointer; IntPtr hordePointer; // nullPointer wird nicht benötigt Horde3D.getNodeTransformMatrices(env, out nullPointer, out hordePointer); for (int i = 0; i < 16; i++) { Console.WriteLine(Marshal.ReadIntPtr(hordePointer, i * Marshal.SizeOf(typeof(IntPtr)))); } this gives me the output Code: 1050253722 0 0 0 0 1050253722 0 0 0 0 1050253722 0 0 0 0 1065353216 Seems to me like he returns memory addresses? Anyone knows how i can get the floats out of this? |
Author: | marciano [ 27.06.2009, 17:38 ] |
Post subject: | Re: horde with c# wrapper: convert intptr to float array |
Does that post help you? |
Author: | dubbyconqueror [ 28.06.2009, 10:12 ] |
Post subject: | Re: horde with c# wrapper: convert intptr to float array |
Thank you, but no that post also didn't help. Maybe my question is more related to C#-development and Pointers in C# in common, so i posted my question in a C#-Forum. I hope i'm getting a working solution soon, when i do i'll post it here. |
Author: | dubbyconqueror [ 28.06.2009, 12:05 ] |
Post subject: | Re: horde with c# wrapper: convert intptr to float array |
what a fight! After a long discussion in a c#-forum, i got a working solution! Here it is: Code: IntPtr nullPointer; IntPtr hordePointer; // don't need the relMat Horde3D.getNodeTransformMatrices(_cam, out nullPointer, out hordePointer); float[] floats = new float[16]; Marshal.Copy(hordePointer, floats, 0, 16); // Print all 16 values of the Transformation Matrix for (int i = 0; i < 16; i++) { Console.Writeline(floats[i]); } Now i can save the horde-matrix in my own c#-matrix-class. |
Author: | dubbyconqueror [ 28.06.2009, 13:11 ] |
Post subject: | Re: horde with c# wrapper: convert intptr to float array |
in case that any c#-horde developer will stumble over this post: I managed a good way to handle the matrices from horde in C#. A good (and simple but not too simple) matrix library for C# is the DotNetMatrix: http://www.codeproject.com/KB/recipes/psdotnetmatrix.aspx I use this library for my matrix-ops. I convert the matrix i get from getNodeTransformMatrices with this function: Code: public static GeneralMatrix GetTransformMatrix(int node) { IntPtr nullPointer; IntPtr hordePointer; // nullPointer wird nicht benötigt Horde3D.getNodeTransformMatrices(node, out nullPointer, out hordePointer); float[] floats = new float[16]; Marshal.Copy(hordePointer, floats, 0, 16); GeneralMatrix m = new GeneralMatrix(4, 4); for (int i = 0; i < 16; i++) { m.SetElement(i / 4, i % 4, floats[i]); } return m; } To push the changes you make in the matrix back to horde (e.g. setNodeTransformMatrix) i wrote this function to convert the generalmatrix to a float[] (needed by the c#-wrapper-functions): Code: public static float[] MatrixToFloatArray(GeneralMatrix m) { float[] result = new float[16]; for (int i = 0; i < 16; i++) { result[i] = (float)m.GetElement(i / 4, i % 4); } return result; } Maybe my approach is not the fastest and best, but if i wanted something fast i would rather use C++ |
Page 1 of 1 | All times are UTC + 1 hour |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |