0

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!

Facundo Villa
  • 111
  • 2
  • 12
  • don't use _ in the beginning of any c++ name, they [are reserved in C++](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) Probably not your issue, but I would highly recommend changing that. – Krupip Nov 06 '19 at 15:02
  • @opa Oh, those are function parameter names. Is that is still "wrong"? Because I believe I've seen it used elsewhere. – Facundo Villa Nov 06 '19 at 20:14
  • Yes it is still wrong, it doesn't matter the scope, don't use underscores for the beginning of names in C++. It's fine in other languages like python, because they don't have a rule about those names being reserved, but you're taking a huge risk in using _ in C++ in the beginning of a name. It doesn't matter what other people do in C++, the spec specifically calls out prefix underscores as being reserved. _name for example, is reserved on some platforms and can cause silent bugs in your program. – Krupip Nov 06 '19 at 20:19
  • C++ has no standard style convention, The stdlib itself uses snake_case, with macros often being capital snake case. You've already bucked this convention. If you want to use private variables of the same name with parameters of the same name you have a few options. First: `this->my_member_variable`. Second, rename parameters with `t_` prefix, you can keep your prototype having the same name, your prototype and definition don't have to have matching variable names. Otherwise prefix with `m_` ie `m_my_member_variable`, this allows better IDE auto completion. – Krupip Nov 06 '19 at 20:28

0 Answers0