PDA

View Full Version : OpenGL style matrix to DirectX matrix


Dias
03-12-2006, 08:56 AM
Hey :)
Im doing some matrix coding and have run into a small problem.
I have a generic 4x4 matrix class which is the same type as OpenGL uses.
I also have a function in my program called setMatrix() which takes a matrix of this type and applies it to directx which from what i know, uses a mirrored/different style matrix.

So far ive added the translation of the matrix like shown below, but how do i apply the x, y, z axis rotations to it?
Any code samples would be appreciated :)

// C# code
public override void setMatrix(Matrix4 matrix)
{
// Apply the translation of the passed matrix
device.Transform.World = Matrix.Translation(matrix.m[0,3], matrix.m[1,3], matrix.m[2,3]);
}

/Cheers

Nils Pipenbrinck
03-12-2006, 09:16 AM
just transform all matrix element.

for (int u=0; u<4; u++)
for (int v=0; v<4; v++)
DxMatrix[u,v] = glMatrix[v,u];

Dias
03-12-2006, 10:02 AM
The device.Transform.World doesnt have a public [] operator to it,
thats why im asking. Since im pretty new to DX though is there some other function which i can use to do what you described?

Reedbeta
03-12-2006, 10:09 AM
Matrix4 transpose;
for (blah) tranpose[u,v] = matrix[v,u];
Device.Transform.World = transpose;

Axel
03-12-2006, 01:02 PM
You could also use GL_ARB_transpose_matrix

Dias
03-12-2006, 01:41 PM
Matrix4 transpose;
for (blah) tranpose[u,v] = matrix[v,u];
Device.Transform.World = transpose;

That wont work, Device.Transform.World doesnt take my custom Matrix4 class as a parameter because it only works with DirectX matrices, which is called Matrix.

Also, Axel, i do not want to use any opengl api specific code because this function is inside a directx implementation of my renderer.

let me explain this a bit further..

The reason i dont use directx matrices all over my project is because i want to work with 1 matrix class only independantly of which rendering api i am using. I am making a generic renderer class which should transparently cover the directx/ogl specific code within their respective implementation.
The function should simply convert from my Matrix4 class into a directx matrix. Since the matrix elements in a directx Matrix is read only i cant set its elements directly (or can i?) so instead im calling the translate, rotate etc functions of directx manually to set it up.

Nils Pipenbrinck
03-12-2006, 02:25 PM
I don't know about the C# access to directx, but a matrix class that can't be written to sounds a bit useless to me.

If the matrix is the same as the c# Mobile d3d, then you can write to M11 M12, M13 and so on.

juhnu
03-12-2006, 08:16 PM
If you can't modify the values, just create a new matrix using a constructor which takes 16 values then.

Mjolnir
03-12-2006, 10:34 PM
namespace Microsoft.DirectX
{
public struct Matrix
{
public float M11;
public float M12;
public float M13;
public float M14;
public float M21;

...

//
// Summary:
// Returns the matrix transpose of a matrix.
//
// Parameters:
// source:
public void Transpose( Matrix source );
}
}

Thus, you can access/modify the elements, just create an instance:

Matrix mx = new Matrix();
mx.M11 = ...;
...
device.Transform.World = mx;

Axel
03-13-2006, 09:33 AM
Also, Axel, i do not want to use any opengl api specific code because this function is inside a directx implementation of my renderer.Sorry, I misunderstood your question.

Dias
03-13-2006, 09:59 AM
Yeah thanks guys thats what i was after!
I was trying to set the M11, M12 etc elements of the device.Transform.World directly and that gave me an error so i assumed it wouldnt work with a Matrix instance either.