Horde3D

Next-Generation Graphics Engine
It is currently 27.11.2024, 16:57

All times are UTC + 1 hour




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: 27.06.2009, 11:24 
Offline

Joined: 02.05.2009, 10:30
Posts: 8
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!


Top
 Profile  
Reply with quote  
PostPosted: 27.06.2009, 16:38 
Offline

Joined: 22.01.2009, 19:49
Posts: 14
Location: Germany
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


Top
 Profile  
Reply with quote  
PostPosted: 27.06.2009, 16:49 
Offline

Joined: 02.05.2009, 10:30
Posts: 8
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?


Top
 Profile  
Reply with quote  
PostPosted: 27.06.2009, 17:38 
Offline
Engine Developer

Joined: 10.09.2006, 15:52
Posts: 1217
Does that post help you?


Top
 Profile  
Reply with quote  
PostPosted: 28.06.2009, 10:12 
Offline

Joined: 02.05.2009, 10:30
Posts: 8
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.


Top
 Profile  
Reply with quote  
PostPosted: 28.06.2009, 12:05 
Offline

Joined: 02.05.2009, 10:30
Posts: 8
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.


Top
 Profile  
Reply with quote  
PostPosted: 28.06.2009, 13:11 
Offline

Joined: 02.05.2009, 10:30
Posts: 8
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++ ;)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 51 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group