Reputation: 7
I try to make perspecive matrix for graphics, this part of code works fine and I have a sphere showing up, but it seems that it doesn't work proparly to create depth, now it is more orthographic.(Actually, I used lots code examples, but nothing ever moved on z Axis) As a base, I used glm perspective function, but with couple changes. And also i attached setting for the camera. Usful articles would be also great)
Matrix4 MATRIX::Perspective(float Width, float Height, float ZNear, float ZFar, float fieldOfView) {
Matrix4 Result = Identity();
Result[0][0] = (Height / Width) * fieldOfView;
Result[1][1] = fieldOfView;
Result[2][2] = -(ZFar + ZNear) / (ZFar - ZNear);
Result[2][3] = -1.0f;
Result[3][2] = -(2.0f * ZFar * ZNear) / (ZFar - ZNear);
return Result;
}
Camera::Camera()
: position(Vec3()), fieldOfView(45.0f), forward(Vec3(0.0f, 0.0f, -1.0f)), up(Vec3(0.0f, 1.0f, 0.0f)),
right(Vec3()), worldUp(up), nearPlane(2.0f), farPlane(50.0f),
yaw(45.0f), pitch(45.0f), perspective(Matrix4()) {
perspective = Perspective(APP_INIT_WINDOW_WIDTH,
APP_INIT_WINDOW_HEIGHT,
nearPlane, farPlane, fieldOfView);
UpdateCameraVector();
}
This is the function from glm, but it wasn't working completely
template <typename T>
GLM_FUNC_QUALIFIER detail::tmat4x4<T, defaultp> perspective
(
T const & fovy,
T const & aspect,
T const & zNear,
T const & zFar
)
{
assert(aspect != static_cast<T>(0));
assert(zFar != zNear);
T const rad = fovy;
T const tanHalfFovy = tan(rad / static_cast<T>(2));
detail::tmat4x4<T, defaultp> Result(static_cast<T>(0));
Result[0][0] = static_cast<T>(1) / (aspect * tanHalfFovy);
Result[1][1] = static_cast<T>(1) / (tanHalfFovy);
Result[2][2] = - (zFar + zNear) / (zFar - zNear);
Result[2][3] = - static_cast<T>(1);
Result[3][2] = - (static_cast<T>(2) * zFar * zNear) / (zFar - zNear);
return Result;
}
In the result I want the sphere to get more depth.
Upvotes: 0
Views: 39