【问题标题】:Java OpenGL when window resized 3D objects don't maintain size当窗口调整大小的 3D 对象不保持大小时,Java OpenGL
【发布时间】:2014-03-06 03:33:45
【问题描述】:

我正在尝试使它在窗口展开时保持其大小。

我在顶部绘制的 2D 正交(图片中的白色图标)在调整大小、保持大小和位置方面工作正常,而首先绘制的 3D 既不保持大小也不保持位置。

这是我的透视矩阵函数:

public static FloatMatrix getPerspectiveMatrix(
    Double fov, float w, float h, float near, float far
){
    float asp = w/h;
    float fov_cos = (float) Math.cos( fov / 2.0d );
    float fov_sin = (float) Math.sin( fov / 2.0d );
    float fov_cot = fov_cos/fov_sin;
    float a_0  = fov_cot/asp;
    float a_3  = (far + near)/(near-far);
    float a_43 = (2.0f * far * near)/(near-far);
    float[] an = {
            a_0,  0.0f,    0.0f, 0.0f,
            0.0f, fov_cot, 0.0f, 0.0f,
            0.0f, 0.0f,    a_3,  -1.0f,
            0.0f, 0.0f,    a_43, 0.0f,
    };
    return new FloatMatrix( an, 4, 4 );

}

这就是我在渲染函数中获取矩阵的方式:

FloatMatrix proj = FloatMatrix.getPerspectiveMatrix(
    Math.PI / 2.0d, this.width, this.height, 0.1f, 200.0f
);

正交矩阵:

public static FloatMatrix getOrtographicMatrix(
    float left, float right, float bottom, float top, float near, float far
){
    float a_0 = 2.0f / (right - left);
    float a_22 = 2/(top - bottom);
    float a_33 = -2/(far - near);
    float tx = -(right + left)/(right - left);
    float ty = -(top + bottom)/(top - bottom);
    float tz = -(far + near)/(far - near);
    float[] an = {
        a_0,  0.0f, 0.0f, 0.0f,
        0.0f, a_22, 0.0f, 0.0f,
        0.0f, 0.0f, a_33, 0.0f, 
        tx,   ty,   tz,   1.0f
    };
    return new FloatMatrix( an, 4, 4 );

}

如何在渲染器中获取 Ortho:

FloatMatrix.getOrtographicMatrix(
-this.width/30.0f, this.width/30.0f, -this.height/30.0f, this.height/30.0f, 1.0f, 1.1f
);

图片示例:

较小的 512x512

全屏 (1080)

【问题讨论】:

    标签: java opengl matrix jogl perspective


    【解决方案1】:

    我已经找到了答案。 它必须与 fov(视野)有关

    h0 = original height of the window
    h1 = new height of the window
    fov0 = original field of view
    fov1 = new field of view
    
    fov1 = atan( tan( fov0 / 2.0 ) * h1 / h0 ) * 2.0;
    

    Source

    当调整 GLCanvas 的大小时,您可以放置​​ GLEventListener 调用的 reshape() 函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      相关资源
      最近更新 更多