Java OpenGL - 计算等轴测视图的正交平截头体

时间:2015-10-07 17:02:22

标签: java opengl jogl

我正在试图弄清楚如何在Orthographic Projection下正确计算我的openGL场景的平截头体边界。我用来执行此操作的代码如下:

public void setFrustum(GL gl, GLU glu){

    double[] forwardAxis = properties.getForwardAxis();
    double[] leftAxis = VecMath.normalVector(properties.getUpDir(), forwardAxis);
    double[] upAxis = VecMath.normalVector(forwardAxis, leftAxis);

    double[] v = bounds.getWidthVector();

    double width = VecMath.magnitude(VecMath.project(v, leftAxis));
    double height = VecMath.magnitude(VecMath.project(v, upAxis));

    double modelRatio = width / height;
    double canvasRatio = properties.getAspectRatio();
    // -- height bigger than width --
    if (modelRatio < canvasRatio){
        // -- update width --
        width = height * canvasRatio;
    } else{
        // -- update height --
        height = width / canvasRatio;
    }

    double l = width / 2. * 1.15;
    double b = height / 2. * 1.15;
    double n = properties.getNear();
    double f = properties.getFar();
    gl.glOrtho(-l, l, -b, b, n, f);
}

所以你可以看到我得到了我的轴和宽度向量:

widthVector = (xWidth, yWidth, zWidth)

然后通过沿leftAxis和upAxis投影此宽度向量来获取gl场景的宽度和高度。然后我将aspectRatio与我正在显示此场景的画布匹配,并使用宽度和高度来计算正字截头体的边界(使用1.15比例因子添加填充)。

除了等轴测视图外,这对所有视图都很有效。以下是这方面的一些屏幕截图:

BottomView LeftView FrontView

IsometricView

如您所见,底部,左侧和前部视图都很好。然而,等轴测视图被切断(并且在拍摄屏幕截图时没有我没有错误地剪辑它!)。我必须将我使用的填充比例因子增加到1.5而不是1.15,但我必须在这里做错事。

非常感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:2)

这是我的fitAllIn简化

public void fitAllIn(float[] boundingMinMaxWorldSpace) {

        Vec4 centerWorldSpace = new Vec4((boundingMinMaxWorldSpace[0] + boundingMinMaxWorldSpace[1]) / 2,
                (boundingMinMaxWorldSpace[2] + boundingMinMaxWorldSpace[3]) / 2,
                (boundingMinMaxWorldSpace[4] + boundingMinMaxWorldSpace[5]) / 2, 1f);

        Vec3 min = new Vec3(boundingMinMaxWorldSpace[0], boundingMinMaxWorldSpace[2], boundingMinMaxWorldSpace[4]);
        Vec3 max = new Vec3(boundingMinMaxWorldSpace[1], boundingMinMaxWorldSpace[3], boundingMinMaxWorldSpace[5]);

        float diameter = (float) Math.sqrt(
                (max.x - min.x) * (max.x - min.x)
                + (max.y - min.y) * (max.y - min.y)
                + (max.z - min.z) * (max.z - min.z));

        if (getCurrView().orthographicProj) {

            if (glViewer.getAspect() > 1) {
                // projection * scale = y
                getCurrView().scale = diameter / 2 / projectionSide;

            } else {
                // projection * aspect * scale = x
                getCurrView().scale = diameter / 2 / (projectionSide * glViewer.getAspect());
            }
            getCurrView().scale = viewScale.clampScale(projectionSide, getCurrView().scale);
        }
    getCurrView().targetPos = new Vec3(centerWorldSpace);

    glViewer.refresh();

基本上我通过了世界空间中的边界框,我计算了我的相机将要定位的中心,我计算了最小点(minX,minY,minZ)和最大点。我得到了直径,然后是一个简单的等式来检索缩放。

scaleprojectionSide稍后将用于详细说明我的拼写矩阵

        float x = projectionSide * glViewer.getAspect() * getCurrView().scale;
        float y = projectionSide * getCurrView().scale;
        float z = projectionSide;

        return Jglm.orthographic(-x, x, -y, y, -z, z);

答案 1 :(得分:1)

感谢elect的建议,这是我更新的setFrustum例程:

public void setFrustum(GL gl, GLU glu) {

    double[] widthVec = bounds.getWidthVector();

    double diameter = VecMath.magnitude(widthVec);

    double canvasRatio = properties.getAspectRatio();

    double scale = canvasRatio > 1 ? diameter / 2 : diameter / 2 / canvasRatio;

    double x = canvasRatio * scale;
    double y = scale;

    double n = properties.getNear();
    double f = properties.getFar();
    gl.glOrtho(-x, x, -y, y, n, f);
}

完美无缺。谢谢选举!