Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in perspective when zNear == 0 #71

Closed
Groovounet opened this issue Apr 21, 2013 · 0 comments
Closed

Bug in perspective when zNear == 0 #71

Groovounet opened this issue Apr 21, 2013 · 0 comments
Assignees
Labels
Milestone

Comments

@Groovounet
Copy link
Member

I find a bug in fucntion "perspective" which located at line 245 ~ 257 in glm/gtc/matrix_transform.inl.
When zNear = 0.0, the perspective matrix result is wrong.
because:
Result[0][0] = (2 * zNear) / (right - left)-----------------(1)
= (2* zNear) / (2.0 * tan(rad/2) * zNear aspect)--(2)
= 1.0 / (aspect * tan(rad/2))----------------(3)
Result[1][1] = (2 * zNear) / (right - left)------------------(1)
= (2
zNear) / (2.0 * tan(rad/2) * zNear )----(2)
= 1.0 / (tan(rad/2)) --------------------------(3)
your code use equation 2, when zNear = 0, it is make wrong result.
But if directly use equation 3, will reslut correct answer when zNear=0.
I don't know how to give this code into repository, so i send the modified
code in attachment.

template <typename valType> 
GLM_FUNC_QUALIFIER detail::tmat4x4<valType> perspective
(
    valType const & fovy, 
    valType const & aspect, 
    valType const & zNear, 
    valType const & zFar
)
{

ifdef GLM_FORCE_RADIANS

    valType const rad = fovy;

else

    valType const rad = glm::radians(fovy);

endif

// the bug for zNear = 0.0, because of float err accumulate
// valType range = tan(rad / valType(2)) * zNear;
// valType left = -range * aspect;
// valType right = range * aspect;
// valType bottom = -range;
// valType top = range;
// detail::tmat4x4 Result(valType(0));
// Result[0][0] = (valType(2) * zNear) / (right - left);
// Result[1][1] = (valType(2) * zNear) / (top - bottom);
// Result[2][2] = - (zFar + zNear) / (zFar - zNear);
// Result[2][3] = - valType(1);
// Result[3][2] = - (valType(2) * zFar * zNear) / (zFar - zNear);
// return Result;

    // fix the bug before
    valType tanHalfFovy = tan(rad / valType(2));
    detail::tmat4x4<valType> Result(valType(0));
    Result[0][0] = valType(1.0f) / (aspect * tanHalfFovy);
    Result[1][1] = valType(1.0f) / (tanHalfFovy);
    Result[2][2] = - (zFar + zNear) / (zFar - zNear);
    Result[2][3] = - valType(1);
    Result[3][2] = - (valType(2) * zFar * zNear) / (zFar - zNear);
    return Result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant