編輯:好吧,我寫的代碼現在完全直觀,這就是結果: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);
}
我不看看我做錯了什麼:/
好吧,不管我嘗試什麼,一些軸似乎總是倒過來。我認爲openTK Matrix4實現有些奇怪。我會盡快更新。 – pixartist
嗯...好吧,只能想到我能想到的是,OpenTK Matrix4的存儲方式與OpenGL不同(OpenTK是行優先,OpenGL是列專業)。但是你的轉變看起來不錯。也許試着將它們拖動一下,看看它有什麼作用... – vesan
如果你使用行主矩陣,那麼你需要告訴OpenGL你傳遞的矩陣是行主要的。例如,查看glUniformMatrix4fv的第三個參數。另外,正如vesan所說,相機的默認前向矢量是(0,0,-1)。這意味着如果常規對象使用(0,0,1)作爲其正向方向,則不能爲相機和常規對象使用相同的lookAt函數。您可以創建投影矩陣,以便相機沿着正Z軸向下看。這將允許你使用相同的lookAt功能的一切。 – Homar