# OpenGL style matrix to DirectX matrix

10 replies to this topic

### #1Dias

New Member

• Members
• 24 posts

Posted 12 March 2006 - 04:56 PM

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

### #2Nils Pipenbrinck

Senior Member

• Members
• 597 posts

Posted 12 March 2006 - 05:16 PM

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

### #3Dias

New Member

• Members
• 24 posts

Posted 12 March 2006 - 06:02 PM

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?

### #4Reedbeta

DevMaster Staff

• 5307 posts
• LocationBellevue, WA

Posted 12 March 2006 - 06:09 PM

Matrix4 transpose;
for (blah) tranpose[u,v] = matrix[v,u];
Device.Transform.World = transpose;
reedbeta.com - developer blog, OpenGL demos, and other projects

### #5Axel

Valued Member

• Members
• 119 posts

Posted 12 March 2006 - 09:02 PM

You could also use GL_ARB_transpose_matrix

### #6Dias

New Member

• Members
• 24 posts

Posted 12 March 2006 - 09:41 PM

Reedbeta said:

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.

### #7Nils Pipenbrinck

Senior Member

• Members
• 597 posts

Posted 12 March 2006 - 10: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.

### #8juhnu

Valued Member

• Members
• 292 posts

Posted 13 March 2006 - 04:16 AM

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

### #9Mjolnir

Member

• Members
• 42 posts

Posted 13 March 2006 - 06:34 AM

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;

### #10Axel

Valued Member

• Members
• 119 posts

Posted 13 March 2006 - 05:33 PM

Dias said:

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

### #11Dias

New Member

• Members
• 24 posts

Posted 13 March 2006 - 05:59 PM

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.

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users