java - 调整窗口大小后,四边形的缩放不正确。怎么修?

标签 java opengl glsl

如果我尝试在不调整窗口大小的情况下以相同的 x 和 y 缩放比例渲染一个正方形,一切都很好。但是在调整窗口大小后,不再呈现正方形。相反,您可以看到一个矩形,即使每次窗口的宽度或高度发生变化时我都在重新计算投影矩阵并将该矩阵传递给着色器。 使用不同的宽度和高度,正方形的缩放比例是不正确的。在我的代码中更改窗口大小而不调整窗口大小不会更改缩放比例。

我不知道错误在哪里。也许我错过了什么。

坐标:

float[] positions = new float[] {0, 1, 0, 0, 1, 1, 1, 0};

计算投影矩阵:

 public static void createProjectionMatrix() {

    Vector2f windowSize = DisplayManager.getWindowSize();

    float aspectRatio = windowSize.x / windowSize.y;
    float halfWidth = 1.0f;
    float halfHeight = halfWidth / aspectRatio;

    float left = -halfWidth;
    float right = halfWidth;
    float bottom = -halfHeight;
    float top = halfHeight;
    float far = -1f;
    float near = 1f;

    Matrix4f matrix = new Matrix4f();

    matrix.setIdentity();

    matrix.m00 = 2f / (right - left);
    matrix.m11 = 2f / (top - bottom);
    matrix.m22 = -2f / (far - near);
    matrix.m32 = (far + near) / (far - near);
    matrix.m30 = (right + left) / (right - left);
    matrix.m31 = (top + bottom) / (top - bottom);

    projectionMatrix = matrix;
 }

计算变换(不使用 Vector2f worldScale):

private Vector2f getDisplayCoords(Vector2f percentage) {
    Vector2f v = DisplayManager.getWindowSize();
    return new Vector2f(v.x * percentage.x, v.y * percentage.y);    
}

public void loadTransformationMatricies() { 
    Vector2f displaySize = getDisplayCoords(size);
    Vector2f displayPos = getDisplayCoords(position);
    Vector2f display = DisplayManager.getWindowSize();

    Vector2f worldPos = Mouse.getWorldPos(displayPos);
    Vector2f worldScale = new Vector2f(displaySize.x / (display.x / 2.0f), displaySize.y / (display.y / 2));

    float x, y;
    x = worldPos.x;
    y = worldPos.y - CORNER_SCALE;
    //y is calculated correct. Moving the mouse to the same y value as calculated shows that everything is correctly calculated
    System.out.println(y + " | " + Mouse.getWorldPos().y);
    transforms[0] = Maths.getTransformationMatrix(new Vector2f(x, y), new Vector2f(CORNER_SCALE, CORNER_SCALE));
}

检查尺寸:

GLFW.glfwSetWindowSizeCallback(WINDOW, new GLFWWindowSizeCallback() {

        @Override
        public void invoke(long arg0, int arg1, int arg2) {
            resized = true;
        }
});

渲染(通常会渲染更多对象):

@Override
protected void render() {
    shader.start();
    if(DisplayManager.isResized()) {
        shader.loadProjectionMatrix(MasterRenderer.getProjectionMatrix());
    }
    bindModel(Loader.getQuad(), new int[] {0});
    for(GUI gui : guis) {
        if(DisplayManager.isResized()) {
            gui.loadTransformationMatricies();
        }
        bindTexture(gui.getTexture().getTopLeftCorner(), GL13.GL_TEXTURE0);
        Matrix4f[] transforms = gui.getTransformations();
        for(int i = 0; i < 1; i++) {
            shader.loadTransformationMatrix(transforms[i]);
            drawSTRIP(Loader.getQuad());
        }
    }
    unbind(new int[] {0});
    shader.stop();
}

顶点着色器:

#version 400 core

in vec2 position;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;

out vec2 textureCoords;

void main (void){

    gl_Position =  projectionMatrix * transformationMatrix * vec4(position, 0, 1.0);
    textureCoords = vec2((position.x+1.0)/2.0, 1 - (position.y+1.0)/2.0);
}

如果您需要更多信息,可以在 Github 上找到完整代码,但我只想知道如何解决这个特定问题。 https://github.com/StackOverflowEx/GameEngine2D

最佳答案

窗口大小改变后,视口(viewport)矩形必须调整到新的大小:

使用glViewport设置视口(viewport)矩形。

向类 DisplayManager 添加方法 updateViewPort

public static void updateViewPort() {
    IntBuffer pWidth = stack.mallocInt(1);
    IntBuffer pHeight = stack.mallocInt(1);

    GLFW.glfwGetFramebufferSize(WINDOW, pWidth, pHeight);
    GL11.glViewport(0, 0, pWidth.get(0), pHeight.get(0)); 
}

MasterRenderer类的方法prepare中调用DisplayManager.updateViewPort:

private void prepare() {

    if(DisplayManager.isResized()) {
        DisplayManager.updateViewPort();
        createProjectionMatrix();
    }
    Camera.calcViewMatrix();

    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
    GL11.glClearColor(1, 0, 0, 1);
}

另见 Java Code Examples for org.lwjgl.glfw.GLFW.glfwGetFramebufferSize() .

关于java - 调整窗口大小后,四边形的缩放不正确。怎么修?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54487163/

相关文章:

java - 应用每隔几分钟卡在 futex_wait_queue_me() 上

java - Android rtl 支持无需重新加载 Activity

Java 1.5 ExitO OutOfMemory?

glsl - WebGL:具有动态数据的2D片段着色器

c++ - 实例化的立方体仅显示一次

java - 在本地网络中使用 Sonatype nexus

c++ - 动态链接库(dll/so)中char*类型的函数参数

opengl glMaterialfv 与 glMaterialiv

java - LWJGL png纹理透明度(textureColour.a白色而不是黑色)

c++ - OpenGL Compute Shader 渲染到 3D 纹理似乎没有做任何事情