I tried to implement my own perspective matrix and it doesn't work. I don't understand why. With just with my view matrix (without Perspective Matrix) I can see my mesh.
Here my view matrix :
Vec3f position(0.0,0.0,-5.0);
Vec3f target(0.0,0.0,0.0);
Mat4f Camera::LookAt(Vec3f target, Vec3f position)
{
//ce dont j'ai besoin pour calculer les parametre de ma camera...
Vec3f up_world = Vec3f(0.0,1.0,0.0);
Vec3f forward = (position - target).normalize();
Vec3f right = up_world.cross(forward).normalize();
Vec3f up = forward.cross(right).normalize();
Mat3f R;
R.setCol(2,forward);
R.setCol(0,right);
R.setCol(1,up);
R.transpose();
Mat4f R4(R);
Vec3f mp = position * -1.0f;
Mat4f P4;
P4.setCol(3,mp);
_LookAt = R4 *P4;
return _LookAt;
}
And my Perspective Matrix :
Mat4f Camera::perspective(float fovy, float aspect, float zNear, float zFar)
{
float theta = fovy * 0.5f * (180.f / M_PI);
float range = zFar - zNear;
float invtan = 1.f/tanf(theta);
Mat4f projMat(invtan / aspect, 0.0f, 0.0f, 0.0f,
0.0f, invtan, 0.0f, 0.0f,
0.0f, 0.0f, -(zFar + zNear) / range, -2 * zNear * zFar / range,
0.0f, 0.0f, -1.0f, 0.0f);
_perspectiveCam = projMat;
std::cout << _perspectiveCam << std::endl;
return _perspectiveCam;
}
Mat4f perspective(float fovy = M_PI/2, float aspect = (1280.f/720.f)*1.f, float zNear = 0.1f, float zFar = 100.f);
My shader is very simple so it can't be the problem..
#version 410 core
layout(location = 0) in vec3 V_position;
layout(location = 1) in vec3 V_color;
layout(location = 2) in vec3 V_normal;
uniform mat4 view_matrix;
uniform mat4 proj_matrix;
out vec3 color;
void main()
{
color = V_color;
gl_Position = proj_matrix * view_matrix * vec4(V_position, 1.);
}
when I initialise _perspectiveCam with the unit matrix 4x4 it displays my triangle.. but when I implement _perspectiveCam like in the function it does not display my triangle
Here my Set col function :
template <typename TYPE>
void Mat4<TYPE>::setCol(int i, Vec4<TYPE>& vec4)
{
for (size_t k = 0; k < 4; k++)
{
setElement(k,i, vec4.getElement(k));
}
}
void setElement(int i,int j, TYPE var){element[i*4 + j] = var;}
TYPE getElement(int i, int j) const {return element[i*4 + j];}