java - 正交投影 - 使物体适合屏幕?

标签 java opengl matrix lwjgl projection

我正在使用 opengl (lwjgl) 进行编程并构建我自己的迷你库。我的相机采用投影类型,构建其投影矩阵如下:

this.aspect = (float) Display.getWidth() / (float) Display.getHeight();
this.top = (float) (Math.tan(Math.toRadians(fov) / 2));
this.bottom = -top;
this.right = top * aspect;
this.left = -right;
if(type == AGLProjectionType.PERSPECTIVE){
    float aspect = 800.0f / 600.0f;
    final double f = (1.0 / Math.tan(Math.toRadians(fov / 2.0)));
    projection = new Matrix4f();
    projection.m00 = (float) (f / aspect);
    projection.m11 = (float) f;
    projection.m22 = (far + near) / (near - far);
    projection.m23 = -1;
    projection.m32 = (2 * far + near) / (near - far);
    projection.m33 = 0;
}
else if(type == AGLProjectionType.ORTHOGONAL){
    projection.m00 = 2 / (right - left);
    projection.m03 = -(right + left) / (right - left);
    projection.m11 = 2 / (top - bottom);  
    projection.m13 = -(top + bottom) / (top - bottom);
    projection.m22 = -2 / (far - near);

}

到目前为止一切顺利。 现在,VBO 输入,因此对象的原始网格 - 例如四边形 - 我保持在标准化尺寸中,因此值在 [ -1 | 范围内1]。 如果我想缩放它,我会将模型矩阵缩放到一个值,并移动它,我会平移模型矩阵。 我的问题是:这些都是相对值。如果我说“matrix.scale(0.5f, 0.5f, 0.5f)”,该对象将占据其先前大小的一半。但是,如果我想要一个宽度为 500 像素的对象怎么办?我该如何计算这个?或者,如果我希望对象为 Screen.width/heiht,并且 x = -Screen.width * 0.5 且 y = -Screen.height * 0.5 - 那么一个对象会填充屏幕并使其位置位于左上角屏幕的?我必须借助投影矩阵来计算一些东西 - 对吗?但如何呢?

最佳答案

不完全是你要问的,但也许有帮助。使用此代码设置相机,以便屏幕坐标与世界坐标匹配,并且视口(viewport)的左下角对于 X 和 Y 为零。正交投影。

        case TwoD:
        {
            projectionMatrix.resetToZero();

            projectionMatrix._11 = 2.0f/(float)this.viewPort.Width;
            projectionMatrix._22 = 2.0f/(float)this.viewPort.Height;
            projectionMatrix._33 = -2.0f/(this.farClip-this.nearClip);
            projectionMatrix._43 =  -1* this.nearClip;

            projectionMatrix._44 = 1.0f;

            float tx = -0.5f* (float)this.viewPort.Width;
            float ty = -0.5f* (float)this.viewPort.Height;  
            float tz = this.nearClip +0.1f;     //why +0.1f >> so that an object with Z = 0 is still displayed

            viewMatrix.setIdetity();
            viewMatrix._22 = 1.0f;
            viewMatrix._41 = tx;
            viewMatrix._42 = ty;
            viewMatrix._43 = -tz;
            break;
        }

至于您的问题:您必须将所需的屏幕坐标放入 View 投影矩阵的逆矩阵中。当您从 2D 过渡到 3D 时,您必须添加深度信息。很抱歉,但我无法帮助您进行数学计算。

关于java - 正交投影 - 使物体适合屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24023628/

相关文章:

c# - Shell 脚本与高级解释语言(C#/Java/等)的性能比较

java - 有没有一种首选方法可以将列表流收集到一个平面列表中?

c++ - Unresolved inclusion <GL/glut.h>

c++ - OpenGL - 纹理显示不正确(夹紧?)

python - 如何在 numpy 中创建反对角单位矩阵(其中对角线从左向右翻转)

java - Android 使用 onClickListener 开始 Activity ?

java - 有没有一种简单的方法可以将我的 XML 对象转换回 Java 中的 String?

opengl - 为 OpenGL 子类型化数字参数的最佳方法是什么?

R - 根据另一个矩阵索引更改矩阵值

list - 将列表矩阵转换为正则矩阵