2013-08-20 66 views
3

我发现this question在OpenGL和DirectX之间执行转换,但从Unity3D执行相同的事情时似乎并不奏效。从Unity3D到DirectX的投影矩阵

我希望有人在这里了解OpenGL投影矩阵和Unity3D投影矩阵(和视图矩阵!)之间的差异。

我也尝试过使用this article,但它也没有工作。

在Unity3D我打电话给我自己的原生C++插件:

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

在我的C++插件我试着通过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]; 
} 

我知道的东西绘制,因为使用视图和投影矩阵的单位矩阵给我一个顶点的三角形:{0,0,0},{0,1,0},{1,0,0}。移动相机时我也会看到各种剪切效果。

编辑 - 更多信息:
Unity3D Documentation告诉我们它使用OpenGL符号。

编辑 - 示例项目:
如果有人想尝试我的代码,我创建了一个test project并将其上传到统一论坛。

回答

0

article似乎工作得很好,但似乎在我所有的冲动和尝试中,我在世界矩阵中设置投影矩阵。

SetProjection(MatrixToArray(Matrix4x4.identity)); 
SetWorld(MatrixToArray(Camera.current.projectionMatrix)); 

应该已经

SetWorld(MatrixToArray(Matrix4x4.identity)); 
SetProjection(MatrixToArray(Camera.current.projectionMatrix)); 

我已经把对有关test project团结论坛的更多信息。