I'm trying to setup my renderer and I'm configuring the matrices. I currently have a View Matrix and a Projection Matrix which I then multiply and send to the shader(there's no need for model matrix since I'm just testing).
The problem is I just can't seem to get the projection matrix right. My model of a sphere appers cut in half in the center of the screen, I can move it around but once the Z value for the vertices seems to go beyond 1(Vulkan's max value for NDC) the model cuts off.
This is the code for the view matrix:
ViewMatrix(3, 0) = CamPos.X;
ViewMatrix(3, 1) = -CamPos.Y;
ViewMatrix(3, 2) = CamPos.Z;
The numbers on the left are the rows and those on the right the columns. Matrices are row-major. And my renderer's coordinate system is left handed.
This is the code for the projection matrix, which is taken from The Vulkan Cookbook:
auto f = 1 / tan_half_fov;
_Matrix(0, 0) = f / _AspectRatio;
_Matrix(1, 1) = -f;
_Matrix(2, 2) = _Far / (_Near - _Far);
_Matrix(2, 3) = -1;
_Matrix(3, 2) = (_Near * _Far) / (_Near - _Far);
_Matrix(3, 3) = 0;
The matrix is just an identity matrix and the every frame it gets set those values. tan_half_fov
is 0.41421356, since it's tan(45 deg / 2). _Near
is 1, and _Far
is 500.
I also have code for a projection matrix from GLM, this is the left handed, 0 to 1 version:
_Matrix(0, 0) = 1 / (_AspectRatio * tan_half_fov);
_Matrix(1, 1) = 1 / tan_half_fov;
_Matrix(2, 2) = _Far / (_Far - _Near);
_Matrix(3, 2) = 1;
_Matrix(2, 3) = -(_Far * _Near) / (_Far - _Near);
_Matrix(3, 3) = 0;
The vertex shader:
void main()
{
gl_Position = inData.Pos * vec4(inPos, 1.0);
}
inPos
is the vertex data input for vertex position and inData.Pos
is the ViewProjection matrix.
The matrix multiplication code is working alright, I tested it. When sending to the shader just the view matrix the sphere appears all flat on the screen and moves around correctly. I am using Vulkan.
Any help is appreciated!