0
\$\begingroup\$

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];}
\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to GDSE. I suggest editing to include the shader as well. Even simple code can hide errors. And if it is simple, it won't take much room. \$\endgroup\$ – Pikalek Nov 23 '18 at 16:09
  • \$\begingroup\$ My long and bitter experience tells me: everytime somebody says "it can't be the problem" - it turns out to actually be the problem. So yes, you should post your shader too. \$\endgroup\$ – Maximus Minimus Nov 23 '18 at 17:16
  • \$\begingroup\$ haha maybe, sorry for my late response i did not have connection i will edit my post with the shader \$\endgroup\$ – Soof Benzaii Nov 25 '18 at 9:30
2
\$\begingroup\$

First off: "It doesn't work" is not very descriptive. What are you observing? A black screen? Distorted geometry?

Next: What is the convention for your Mat4f class? Can you show setCol() implementation? Is your translation part stored in [3],[7],[11] or [12],[13],[14]?

Because I suspect that your projmat may have flipped col/row?

In my implementation, the projmat (same values as yours) is laid out differently, like so:

    mout[0] = f / aspect;
    mout[1] = 0.0f;
    mout[2] = 0.0f;
    mout[3] = 0.0f;

    mout[4] = 0.0f;
    mout[5] = f;
    mout[6] = 0.0f;
    mout[7] = 0.0f;

    mout[8] = 0.0f;
    mout[9] = 0.0f;
    mout[10] = (zFar+zNear) / (zNear-zFar);
    mout[11] = -1.0f;

    mout[12] = 0.0f;
    mout[13] = 0.0f;
    mout[14] = 2 * zFar * zNear /  (zNear-zFar);
    mout[15] = 0.0f;

And after doing proj * view * model, I feed it into the shader with glUniformMatrix4fv().

In my shader, I post-multiply the position, like so:

gl_Position = modelcamviewprojmat * position;

My matrix class has translation in [12], [13], [14]

The things I would try:

  1. Switch multiplication order, to see if it helps: sometimes it is model * view * proj, sometimes it is proj * view * model.
  2. Make sure the layout of your proj matrix, above, is correct.
\$\endgroup\$
  • \$\begingroup\$ Thank you for your answer, i have forgotten to transpose my lookAt and perspective Matrix.. I thought Open GL works with column major matrix but apparently it 's not the case .. So thank you \$\endgroup\$ – Soof Benzaii Nov 26 '18 at 10:24

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.