0
\$\begingroup\$

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;
}
\$\endgroup\$
  • \$\begingroup\$ yeah sorry, I modified the title \$\endgroup\$ – Frank Cranston Aug 30 '16 at 14:46
-1
\$\begingroup\$

Not to be glib but obviously you have something out of order in your operations or some other basic error like that. It is not immediately clear where that is happening though.

https://sourceforge.net/projects/ogl-math/

You might consider using GLM at least as a starting point. That way you won't always be wondering whether it is your current code that has an issue, or your base code. Then later when you are confident you have lots of stuff working you can switch it out completely.

http://www.learnopengl.com/#!Getting-started/Transformations

This tutorial starts off kind of basic but talks about glm and shows how to get the perspective etc. working with it. You can also compare the implemenation to what you are doing and see if anything jumps out as obviously wrong.

| improve this answer | |
\$\endgroup\$

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.