So, I tried to use matrices in OpenTK, my Transformation class has a rotation (quaternion) and Vector3 for the position. It offers the following fields:
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);
}
}
This creates the matrix of the view and model:
public virtual Matrix4 GetMatrix()
{
Matrix4 translation = Matrix4.CreateTranslation(Position);
Matrix4 rotation = Matrix4.CreateFromQuaternion(Rotation);
return translation * rotation;
}
Projection:
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);
}
}
Matrix Multiplication:
public Matrix4 GetModelViewProjectionMatrix(Transform model)
{
return model.GetMatrix()* Transform.GetMatrix() * projectionMatrix;
}
Shader:
[Shader vertex]
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 matrices are transposed, so the order of multiplication.
The problem with this setting is that the entire axis and direction vectors, as well as the position of the object, are mixed, mirrored, and turned upside down.
Now, after many attempts, I managed to get a useful conversion and the following came out:
I had to invert the quaternion in the directions:
public virtual Vector3 Right
{
get
{
return Vector3.Transform(Vector3.UnitX, Rotation.Inverted());
}
}
public virtual Vector3 Forward
{
get
{
return Vector3.Transform(-Vector3.UnitZ, Rotation.Inverted());
}
}
public virtual Vector3 Up
{
get
{
return Vector3.Transform(Vector3.UnitY, Rotation.Inverted());
}
}
And I had to redesign the view matrix as follows:
public virtual Matrix4 GetMatrixView()
{
Matrix4 translation = Matrix4.CreateTranslation(Position*2).Inverted();
Matrix4 rotation = Matrix4.CreateFromQuaternion(Rotation);
return translation * rotation;
}
, , . F?
: . / :
[LOG]: (0; 0; 10) →
[LOG]: FW (0; 0; -1)
[LOG]: R (-1; 0; 0)
[LOG]: UP (0; 1; 0)
, Z 10, 0,0,0. - x -1, . Up - Y 1, . ? ?