调整窗口大小不会影响内容

地球:

我在调整LWJGL3和GLFW的窗口大小时遇到​​问题。我可以调整窗口的大小,但是呈现给它的大小却不能调整,这很可能是坐标问题。我已经列出了我的Display和Main类。

public static void init() {
    // Setup an error callback. The default implementation
    // will print the error message in System.err.
    GLFWErrorCallback.createPrint(System.err).set();

    // Initialize GLFW. Most GLFW functions will not work before doing this.
    if ( !glfwInit() )
        throw new IllegalStateException("Unable to initialize GLFW");

    // Configure GLFW
    glfwDefaultWindowHints(); // optional, the current window hints are already the default
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable

    // Create the window
    window = glfwCreateWindow(800, 500, "Game Indev", NULL, NULL);
    if ( window == NULL )
        throw new RuntimeException("Failed to create the GLFW window");

    // Setup a key callback. It will be called every time a key is pressed, repeated or released.
    glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
        if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
            glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop
    });

    // Get the thread stack and push a new frame
    try ( MemoryStack stack = stackPush() ) {
        IntBuffer pWidth = stack.mallocInt(1); // int*
        IntBuffer pHeight = stack.mallocInt(1); // int*

        // Get the window size passed to glfwCreateWindow
        glfwGetWindowSize(window, pWidth, pHeight);

        // Get the resolution of the primary monitor
        GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());

        // Center the window
        glfwSetWindowPos(
                window,
                (vidmode.width() - pWidth.get(0)) / 2,
                (vidmode.height() - pHeight.get(0)) / 2
        );
    } // the stack frame is popped automatically

    // Make the OpenGL context current
    glfwMakeContextCurrent(window);
    // Enable v-sync
    glfwSwapInterval(1);

    // Make the window visible
    glfwShowWindow(window);
}

我的主要班级在这里:

// The window handle

public static void main(String[] args) {
    System.out.println(Version.getVersion());

    Display.init();

    Loader loader = new Loader();
    Renderer renderer = new Renderer();
    // This line checks the context and capabilities
    GL.createCapabilities();
    float[] vertices = {

            -0.5f, 0.5f, 0f,
            -0.5f, -0.5f, 0f,
            0.5f, -0.5f, 0f,
            0.5f, -0.5f, 0f,
            0.5f, 0.5f, 0f,
            -0.5f, 0.5f, 0f
    };
    RawModel model = loader.loadToVao(vertices);

    // Set the clear color initially
    glClearColor(1.0f, 0.0f, 0.0f, 0.0f);

    /*
    THIS
    IS
    THE
    MAIN
    GAME
    LOGIC
    ---------------------------------------------------------------------------------
     */
    while ( !glfwWindowShouldClose(Display.window) ) {
        GL11.glClearColor(GameState.stateR, GameState.stateG, GameState.stateB, GameState.stateA);
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

        renderer.renderModel(model);

        glfwSwapBuffers(Display.window); // swap the color buffers


        GameState.getStateInput();
        GameState.renderGameState(loader, renderer);
        glfwPollEvents();

    }

    //End of main loop -----------------------------------------------------------------

    // Free the window callbacks and destroy the window
    glfwFreeCallbacks(Display.window);
    glfwDestroyWindow(Display.window);

    // Terminate GLFW and free the error callback
    glfwTerminate();
    glfwSetErrorCallback(null).free();
}
// --------------------------------------------------------------------------------------------

我也对我在说什么有印象。

调整 大小之前调整大小之后显然,我希望四边形随屏幕调整大小,使全屏模式在中间有一个大四边形,而不是在左下角坐标中具有相同的大小。我该怎么做?

Tymo Tymo:

为了正确调整窗口的内容大小,您需要使用以下命令响应窗口的调整大小:

glfwSetWindowSizeCallback(window, new GLFWWindowSizeCallback() {
        @Override
        public void invoke(long window, int argWidth, int argHeight) {
            resizeWindow(argWidth, argHeight);
        }
});

在resizeWindow(argWidth,argHeight)中,您需要设置视口并重新计算投影矩阵(仅在使用时)。

private void resizeWindow(int argWidth, int argHeight) {
    glViewport(0, 0, argWidth,argHeight);

    adjustProjectionMatrix(width, height); // recalculating projection matrix (only if you are using one)
}

编辑:

如果您使用的是LWJGL 2:

if (Display.wasResized()) {
    GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());

    adjustProjectionMatrix(Display.getWidth(), Display.getHeight()); // only if you are using one
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在不影响内容的情况下调整窗口大小

浏览器调整大小不会影响HTML内容

除非调整大小,否则JFrame窗口不会显示任何内容

ScalaFX(JavaFX):在调整窗口大小时,舞台内容不会调整大小

除非调整浏览器窗口的大小,否则Angular ui网格不会显示内容

调整窗口大小时,高度不会更新

jQuery窗口调整大小不会触发

调整窗口大小不会更改Java值

阻止ListBox调整内容大小,但不调整窗口大小

根据内容调整弹出窗口的大小 - CSS

根据内容调整SmartGWT窗口的大小

div以包装内容,即使窗口调整大小

调整窗口大小时,图像高度不会调整

C程序的窗口大小调整会影响元素位置

JavaFX拖动事件受窗口大小调整的影响

调整大小不会影响PyQt5中的布局

调整Windows分区的大小而不会影响双启动中的Ubuntu

根据窗口大小调整div以及内部内容的大小

动态调整pixi阶段的大小,其内容在窗口大小调整和窗口加载上

ViewController内容大小不会更改弹出窗口大小

Java:在调整窗口大小之前图像不会显示

用于调整窗口大小的 React Hook 不会更新

当窗口变为全屏时,NSView不会调整大小

在调整窗口大小之前,jQuery函数不会触发

调整窗口大小之前,JMenu不会出现

在重新调整窗口大小之前,不会触发砌体布局

Firefox扩展弹出窗口不会调整大小

NSView防止窗口/内容视图水平调整大小

Bootstrap <选择>宽度,调整窗口大小时内容太宽