OpenGL style matrix to DirectX matrix

919cdb65748ac9d3c6aa6ccea59ad6ad
0
Dias 101 Mar 12, 2006 at 16:56

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

10 Replies

Please log in or register to post a reply.

B91eae75cd6245bd8074bd0c3f1cc495
0
Nils_Pipenbrinck 101 Mar 12, 2006 at 17:16

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];

919cdb65748ac9d3c6aa6ccea59ad6ad
0
Dias 101 Mar 12, 2006 at 18:02

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?

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Mar 12, 2006 at 18:09

Matrix4 transpose;
for (blah) tranpose[u,v] = matrix[v,u];
Device.Transform.World = transpose;

Da26e799270ce5e8b62659ed77b11cef
0
Axel 101 Mar 12, 2006 at 21:02

You could also use GL_ARB_transpose_matrix

919cdb65748ac9d3c6aa6ccea59ad6ad
0
Dias 101 Mar 12, 2006 at 21:41

@Reedbeta

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.

B91eae75cd6245bd8074bd0c3f1cc495
0
Nils_Pipenbrinck 101 Mar 12, 2006 at 22:25

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.

46462f88a1670d7e9cbbfa360aa20134
0
juhnu 101 Mar 13, 2006 at 04:16

If you can’t modify the values, just create a new matrix using a constructor which takes 16 values then.

15d017c2df38127527fc5981b4af1c73
0
Mjolnir 101 Mar 13, 2006 at 06:34
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;
Da26e799270ce5e8b62659ed77b11cef
0
Axel 101 Mar 13, 2006 at 17:33

@Dias

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.

919cdb65748ac9d3c6aa6ccea59ad6ad
0
Dias 101 Mar 13, 2006 at 17:59

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.