ungerik / go3d

A performance oriented 2D/3D math package for Go
MIT License
303 stars 49 forks source link

mat4.AssignPerspectiveProjection? #27

Open thedaneeffect opened 4 years ago

thedaneeffect commented 4 years ago

It appears AssignPerspectiveProjection is creating a Frustum.

go3d:

// AssignPerspectiveProjection assigns a perspective projection transformation.
func (mat *T) AssignPerspectiveProjection(left, right, bottom, top, znear, zfar float32) *T {
    near2 := znear + znear
    ooFarNear := 1 / (zfar - znear)

    mat[0][0] = near2 / (right - left)
    mat[1][0] = 0
    mat[2][0] = (right + left) / (right - left)
    mat[3][0] = 0

    mat[0][1] = 0
    mat[1][1] = near2 / (top - bottom)
    mat[2][1] = (top + bottom) / (top - bottom)
    mat[3][1] = 0

    mat[0][2] = 0
    mat[1][2] = 0
    mat[2][2] = -(zfar + znear) * ooFarNear
    mat[3][2] = -2 * zfar * znear * ooFarNear

    mat[0][3] = 0
    mat[1][3] = 0
    mat[2][3] = -1
    mat[3][3] = 0

    return mat
}

https://github.com/g-truc/glm/blob/416fa93e42f8fe1d85a93888a113fecd79e01453/glm/ext/matrix_clip_space.inl#L159-L171

template<typename T>
GLM_FUNC_QUALIFIER mat<4, 4, T, defaultp> frustumRH_NO(T left, T right, T bottom, T top, T nearVal, T farVal)
{
    mat<4, 4, T, defaultp> Result(0);
    Result[0][0] = (static_cast<T>(2) * nearVal) / (right - left);
    Result[1][1] = (static_cast<T>(2) * nearVal) / (top - bottom);
    Result[2][0] = (right + left) / (right - left);
    Result[2][1] = (top + bottom) / (top - bottom);
    Result[2][2] = - (farVal + nearVal) / (farVal - nearVal);
    Result[2][3] = static_cast<T>(-1);
    Result[3][2] = - (static_cast<T>(2) * farVal * nearVal) / (farVal - nearVal);
    return Result;
}

https://github.com/g-truc/glm/blob/416fa93e42f8fe1d85a93888a113fecd79e01453/glm/ext/matrix_clip_space.inl#L238-L252

template<typename T>
GLM_FUNC_QUALIFIER mat<4, 4, T, defaultp> perspectiveRH_NO(T fovy, T aspect, T zNear, T zFar)
{
    assert(abs(aspect - std::numeric_limits<T>::epsilon()) > static_cast<T>(0));

    T const tanHalfFovy = tan(fovy / static_cast<T>(2));

    mat<4, 4, 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;
}