Domanda

I am trying to export a scene from a direct-mode OpenGL program to VRML.

In both OpenGL and VRML, faces can be grouped into objects with their own translation, rotation and scale relative to the scene origin.

In OpenGL these are stored as a 4-matrix representing a translation and a rotation/scale in one matrix [1]. In VRML these must be represented as separate translation, rotation and scaling vectors [2][3].

How can I decompose an arbitrary 4-matrix in this way?

I would prefer to use a library function to do this, to minimise the code I have to maintain and to reduce the chance of errors. I am using C# on Windows and can access any CLR or windows native DLL.

È stato utile?

Soluzione

Take a look at the Matrix.Decompose code from the Monogame sources.

Remember - OpenGL matrices are column-major and DirectX/XNA are row-major.

Hope that helps.

Altri suggerimenti

How can I decompose an arbitrary 4-matrix in this way?

The rightmost column of the transformation is the translation vector T.

The scale and rotation are a bit more intricate to extract. You need to find the rotation axis and rotation angle. Basically you're dealing with an eigenvector problem here: Find the vector that goes unchanged under transformation of the matrix. This is the case for the eigenvector. Rotations are orthogonal transformations. So any non-normal part of a rotation matrix is a scaling. You can extract this scaling by first orthonormalizing the matrix, yielding your the scaling factors S. Finally you can solve the eigenvector problem to find two antiparallel eigenvectors, being the rotation axis. The corresponding eigenvalue designates the cos(α) of the rotation, which gives you the rotation axis and angle R.

SFMatrix4f/d and SFMatrix3/d have build in functions to decompose a matrix in its components. Here is the page from the specification: http://www.web3d.org/documents/specifications/19777-1/V3.0/Part1/functions.html#t-Matrix4Functions.

In JavaScript you can type:

function foo (matrix4f)
{
   var translation = new SFVec3f ();
   var rotation    = new SFRotation ();
   var scale       = new SFVec3f ();

   matrix4f .getTransform (translation, rotation, scale);
}

Some browsers support as fourth and fifth parameter to getTransform a scaleOrientation and center argument.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top