I found this question to perform a transformation between OpenGL and DirectX, but it doesn't seem to be working when performing the same things from Unity3D.
I was hoping that there is someone here who knows anything about the differences between an OpenGL projection matrix and the Unity3D projection matrix (and view matrix!).
I have tried to use this article as well, but it didn't work either.
In Unity3D I'm calling to my own native C++ plugin:
SetView(MatrixToArray(Camera.current.worldToCameraMatrix));
SetProjection(MatrixToArray(Matrix4x4.identity));
SetWorld(MatrixToArray(Camera.current.projectionMatrix));
private static float[] MatrixToArray(Matrix4x4 _matrix)
{
float[] result = new float[16];
for (int _row = 0; _row < 4; _row++)
{
for (int _col = 0; _col < 4; _col++)
{
result[_col + _row * 4] = _matrix[_row, _col];
}
}
return result;
}
And in my C++ plugin I try to convert these matrices via the article:
extern "C" void EXPORT_API SetProjection(float a[])
{
g_Proj._11 = a[0];
g_Proj._21 = a[1];
g_Proj._31 = -a[2];
g_Proj._41 = a[3];
g_Proj._12 = a[4];
g_Proj._22 = a[5];
g_Proj._32 = -a[6];
g_Proj._42 = a[7];
g_Proj._13 = a[8];
g_Proj._23 = a[9];
g_Proj._33 = -a[10];
g_Proj._43 = a[11];
g_Proj._14 = a[12];
g_Proj._24 = a[13];
g_Proj._34 = -a[14];
g_Proj._44 = a[15];
}
extern "C" void EXPORT_API SetView(float a[])
{
g_View._11 = a[0];
g_View._21 = a[1];
g_View._31 = -a[2];
g_View._41 = a[3];
g_View._12 = a[4];
g_View._22 = a[5];
g_View._32 = -a[6];
g_View._42 = a[7];
g_View._13 = -a[8];
g_View._23 = -a[9];
g_View._33 = a[10];
g_View._43 = -a[11];
g_View._14 = a[12];
g_View._24 = a[13];
g_View._34 = a[14];
g_View._44 = a[15];
}
I know things are drawn, because using the identity matrix for view and projection matrix do give me a triangle for vertices: {0,0,0},{0,1,0},{1,0,0}. And I also see all sorts of shearing effects when moving my camera.
EDIT - more info:
Unity3D Documentation tells us that it uses OpenGL notation.
EDIT - example project:
If anyone wants to try out my code, I've created a test project and uploaded it to the unity forum.
The article seems to be working just fine, but it seems that in all my rushing and trying I set the projection matrix in the world matrix.
SetProjection(MatrixToArray(Matrix4x4.identity));
SetWorld(MatrixToArray(Camera.current.projectionMatrix));
should have been
SetWorld(MatrixToArray(Matrix4x4.identity));
SetProjection(MatrixToArray(Camera.current.projectionMatrix));
I've put some more information on the unity forum about the test project.