1

So I started to make my own engine, and got my Z axis pointing away from my camera. I don't know why it happened, I followed this tutorial pretty much.

I'll post my persprojection and view calculations of my camera, please comment what other code should I post, and I'll update my post.

PersProjection

void Camera::setPerspective(float width, float height, float nearZ, float farZ, float angleDeg) {
perspectiveTrans.toZero();

float ar = width / height;

angleDeg = Math3D::ToRadian(angleDeg);

perspectiveTrans(0, 0) = 1.0f / (ar * tanf(angleDeg / 2.0f));
perspectiveTrans(1, 1) = 1.0f / tanf(angleDeg / 2.0f);
perspectiveTrans(2, 2) = (-nearZ - farZ) / (nearZ - farZ);
perspectiveTrans(2, 3) = (2.0f * farZ * nearZ) / (nearZ - farZ);
perspectiveTrans(3, 2) = 1.0f;
}

View and Projection calculations

Matrix4x4f Camera::getViewProjection() {
Matrix4x4f cameraRotationTrans(MatrixType::IDENTITY);

Vector3f N = target;
N.normalize();
Vector3f U = up;
U.normalize();
U = U.cross(target);
Vector3f V = N.cross(U);

cameraRotationTrans.setRow(0, Vector4f(U, 0));
cameraRotationTrans.setRow(1, Vector4f(V, 0));
cameraRotationTrans.setRow(2, Vector4f(N, 1));

Matrix4x4f cameraTranslationTrans(MatrixType::IDENTITY);
cameraTranslationTrans.setCol(3, Vector4f(-position, 1));

return perspectiveTrans * cameraRotationTrans * cameraTranslationTrans;
}
Ferenc Dajka
  • 1,052
  • 3
  • 17
  • 45

1 Answers1

0

You're missing a minus sign in this expression:

perspectiveTrans(2, 3) = (2.0f * nearZ * farZ) / -(nearZ - farZ);

If you look up the corresponding matrix elements in the documentation for glFrustum, this matrix element is:

- (2 * far * near / (far - near)

In your notation, that is:

perspectiveTrans(2, 3) = (-2.0f * nearZ * farZ) / -(nearZ - farZ);
Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • Before posting I was playing with the minus signs and of course I've posted a wrong code, thanks for pointing out the problem anyways. I've updated my code according to [this formula](http://ogldev.atspace.co.uk/www/tutorial12/12_11.png), (the whole article is [here](http://ogldev.atspace.co.uk/www/tutorial12/tutorial12.html)). Still no luck. Any suggestions what part of my code should I check? – Ferenc Dajka Aug 30 '16 at 10:37
  • It's not really the idea of this site that you change your question in a way that invalidates answers that were already posted. In any case, the matrix you linked is not the projection matrix that is typically used with OpenGL. That's why your coordinate system is left-handed. Just use the one I linked, and it will work. – Reto Koradi Aug 31 '16 at 04:23
  • You're right. Sorry about that. Could you post an answer about your formula, so I can accept it? – Ferenc Dajka Aug 31 '16 at 12:38