暗影LWJGL至GLM / GLFW / GLAD

时间:2019-09-22 18:33:56

标签: c++ opengl glfw glm-math glad

我正在使用OpenGL和C ++制作有趣的引擎,我发现了一个非常好的阴影映射教程。 (https://www.youtube.com/watch?v=o6zDfDkOFIc

阴影无法正确渲染。

经过大量调试,我发现他的问题可能是calculateFrustum方法。

java代码是:

     */
    private Vector4f[] calculateFrustumVertices(Matrix4f rotation, Vector3f forwardVector,
            Vector3f centerNear, Vector3f centerFar) {
        Vector3f upVector = new Vector3f(Matrix4f.transform(rotation, UP, null));
        Vector3f rightVector = Vector3f.cross(forwardVector, upVector, null);
        Vector3f downVector = new Vector3f(-upVector.x, -upVector.y, -upVector.z);
        Vector3f leftVector = new Vector3f(-rightVector.x, -rightVector.y, -rightVector.z);
        Vector3f farTop = Vector3f.add(centerFar, new Vector3f(upVector.x * farHeight,
                upVector.y * farHeight, upVector.z * farHeight), null);
        Vector3f farBottom = Vector3f.add(centerFar, new Vector3f(downVector.x * farHeight,
                downVector.y * farHeight, downVector.z * farHeight), null);
        Vector3f nearTop = Vector3f.add(centerNear, new Vector3f(upVector.x * nearHeight,
                upVector.y * nearHeight, upVector.z * nearHeight), null);
        Vector3f nearBottom = Vector3f.add(centerNear, new Vector3f(downVector.x * nearHeight,
                downVector.y * nearHeight, downVector.z * nearHeight), null);
        Vector4f[] points = new Vector4f[8];
        points[0] = calculateLightSpaceFrustumCorner(farTop, rightVector, farWidth);
        points[1] = calculateLightSpaceFrustumCorner(farTop, leftVector, farWidth);
        points[2] = calculateLightSpaceFrustumCorner(farBottom, rightVector, farWidth);
        points[3] = calculateLightSpaceFrustumCorner(farBottom, leftVector, farWidth);
        points[4] = calculateLightSpaceFrustumCorner(nearTop, rightVector, nearWidth);
        points[5] = calculateLightSpaceFrustumCorner(nearTop, leftVector, nearWidth);
        points[6] = calculateLightSpaceFrustumCorner(nearBottom, rightVector, nearWidth);
        points[7] = calculateLightSpaceFrustumCorner(nearBottom, leftVector, nearWidth);
        return points;
    }


    private Vector4f calculateLightSpaceFrustumCorner(Vector3f startPoint, Vector3f direction,
            float width) {
        Vector3f point = Vector3f.add(startPoint,
                new Vector3f(direction.x * width, direction.y * width, direction.z * width), null);
        Vector4f point4f = new Vector4f(point.x, point.y, point.z, 1f);
        Matrix4f.transform(lightViewMatrix, point4f, point4f);
        return point4f;
    }

但是我没有设法将其转换为C ++。

这些点以NaN的形式返回,但是经过大量调试后,我不知道为什么是NaN,因为对我来说这似乎是个数字。

有人知道吗?

0 个答案:

没有答案