2014-07-05 35 views
-1

編輯:好吧,我寫的代碼現在完全直觀,這就是結果:X軸似乎倒在OpenTK

http://i.imgur.com/x5arJE9.jpg

立方體在0,0,0

正如你所看到的,攝像頭在z軸上的位置是負的,這表明我沿正z軸觀察,這與不匹配。 (fw是負數)

另外,立方體顏色表明我處於正z軸上,朝負方向看。也正x軸是向右(在模型空間)

角度被計算如下:

public virtual Vector3 Right 
    { 
     get 
     { 

      return Vector3.Transform(Vector3.UnitX, Rotation); 
     } 
    } 
    public virtual Vector3 Forward 
    { 
     get 
     { 
      return Vector3.Transform(-Vector3.UnitZ, Rotation); 
     } 
    } 
    public virtual Vector3 Up 
    { 
     get 
     { 
      return Vector3.Transform(Vector3.UnitY, Rotation); 
     } 
    } 

旋轉是四元數。

這是視圖和模型矩陣如何創建:

public virtual Matrix4 GetMatrix() 
    { 
     Matrix4 translation = Matrix4.CreateTranslation(Position); 
     Matrix4 rotation = Matrix4.CreateFromQuaternion(Rotation); 
     return translation * rotation; 
    } 

投影:

private void SetupProjection() 
    { 
     if(GameObject != null) 
     { 
      AspectRatio = GameObject.App.Window.Width/(float)GameObject.App.Window.Height; 
      projectionMatrix = Matrix4.CreatePerspectiveFieldOfView((float)((Math.PI * Fov)/180), AspectRatio, ZNear, ZFar); 

     } 
    } 

矩陣乘法:

public Matrix4 GetModelViewProjectionMatrix(Transform model) 
    { 
     return model.GetMatrix()* Transform.GetMatrix() * projectionMatrix; 
    } 

着色器:

[Shader vertex] 
#version 150 core 

in vec3 pos; 
in vec4 color; 
uniform float _time; 
uniform mat4 _modelViewProjection; 

out vec4 vColor; 

void main() { 
    gl_Position = _modelViewProjection * vec4(pos, 1); 
    vColor = color; 
} 

OpenTK矩陣轉置,因此乘法的順序。

任何想法爲什麼軸/位置都搞砸了?

編輯結束。原貼:

看一看這個圖片:http://i.imgur.com/Cjjr8jz.jpg

正如你可以看到,當(相機)正向矢量在Z軸,紅色立方體正面是負x軸,

float[] points = { 
// position (3) Color (3) 
-s, s, z, 1.0f, 0.0f, 0.0f, // Red point 
s, s, z, 0.0f, 1.0f, 0.0f, // Green point 
s, -s, z, 0.0f, 0.0f, 1.0f, // Blue point 
-s, -s, z, 1.0f, 1.0f, 0.0f, // Yellow point 
}; 

相機x位置似乎被反轉(立方體在圍繞這些點幾何着色器創建的)。換句話說,如果我沿着它的局部x軸增加相機位置,它將向左移動,反之亦然。

我通過變換矩陣是這樣的:

 if (DefaultAttributeLocations.TryGetValue("modelViewProjectionMatrix", out loc)) 
     { 
      if (loc >= 0) 
      { 
       Matrix4 mvMatrix = Camera.GetMatrix() * projectionMatrix; 
       GL.UniformMatrix4(loc, false, ref mvMatrix); 
      } 

     } 

的GetMatrix()方法是這樣的:

public virtual Matrix4 GetMatrix() 
    { 
     Matrix4 translation = Matrix4.CreateTranslation(Position); 
     Matrix4 rotation = Matrix4.CreateFromQuaternion(Rotation); 

     return translation * rotation; 
    } 

和投影矩陣:

private void SetupProjection() 
    { 
     AspectRatio = Window.Width/(float)Window.Height; 
     projectionMatrix = Matrix4.CreatePerspectiveFieldOfView((float)((Math.PI * Fov)/180), AspectRatio, ZNear, ZFar); 
    } 

我不看看我做錯了什麼:/

回答

1

這段代碼有點難以分辨,但我相信這是因爲在OpenGL中,攝像頭的默認前向向量沿着Z軸是負向的 - 你是積極的,這意味着你正在從背部。這就是爲什麼X座標似乎倒過來的原因。

+0

好吧,不管我嘗試什麼,一些軸似乎總是倒過來。我認爲openTK Matrix4實現有些奇怪。我會盡快更新。 – pixartist

+0

嗯...好吧,只能想到我能想到的是,OpenTK Matrix4的存儲方式與OpenGL不同(OpenTK是行優先,OpenGL是列專業)。但是你的轉變看起來不錯。也許試着將它們拖動一下,看看它有什麼作用... – vesan

+0

如果你使用行主矩陣,那麼你需要告訴OpenGL你傳遞的矩陣是行主要的。例如,查看glUniformMatrix4fv的第三個參數。另外,正如vesan所說,相機的默認前向矢量是(0,0,-1)。這意味着如果常規對象使用(0,0,1)作爲其正向方向,則不能爲相機和常規對象使用相同的lookAt函數。您可以創建投影矩陣,以便相機沿着正Z軸向下看。這將允許你使用相同的lookAt功能的一切。 – Homar

0

雖然這個問題還有幾年了,但我仍然想提出我的意見。

你遇到這個bug的原因是因爲OpenTK的矩陣是行重點。所有這一切真的意味着你必須做所有的矩陣數學是相反的。例如,變換矩陣就會成倍增加,像這樣:

public static Matrix4 CreateTransformationMatrix(Vector3 position, Quaternion rotation, Vector3 scale) 
{ 
    return Matrix4.CreateScale(scale) * 
      Matrix4.CreateFromQuaternion(rotation) * 
      Matrix4.CreateTranslation(position); 
} 

這也同樣適用於矩陣,所以如果你使用的Vector3的,而不是四元對您的旋轉將是這樣的:

public static Matrix4 CreateTransformationMatrix(Vector3 position, Vector3 rotation, Vector3 scale) 
{ 
    return Matrix4.CreateScale(scale) * 
      Matrix4.CreateRotationZ(rotation.Z) * 
      Matrix4.CreateRotationY(rotation.Y) * 
      Matrix4.CreateRotationX(rotation.X) * 
      Matrix4.CreateTranslation(position); 
} 

請注意,你的頂點着色器仍然會這樣相乘:

void main() 
{ 
    gl_Position = projection * view * transform * vec4(position, 1.0f); 
} 

我希望這有助於!