我正在尝试计算正交投影下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)
然后通过沿着左轴和上轴投影这个宽度向量来获取gl场景的宽度和高度。然后将纵横比与我正在显示此场景的画布相匹配,并使用宽度和高度计算正交视锥体的边界(使用1.15缩放因子添加填充)。
这对于除等角视图之外的所有视图都很有效。以下是此方法运作的一些屏幕截图:
任何帮助都将不胜感激。谢谢!