Question

I am attempting to make it so that when the window is expanded the objects in it maintain size.

My 2D ortho that is drawn on top (white icons in pictures) work fine in resize, keeping size and position, whilst the 3D that is drawn first is not keeping size nor position.

Here is my Perspective Matrix function:

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 );

}

This is how I get the matrix in my render function:

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

Ortho Matrix:

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 );

}

How I get Ortho in renderer:

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

Picture Examples:

Smaller 512x512

Smaller 512x512

Full screen (1080)

Full Screen (1080)

Was it helpful?

Solution

I have found the answer to this. It has to due with the fov (field of view)

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

You would put this reshape() function that is called by the GLEventListener when the GLCanvas is resized.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top