首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >LWJGL 3窗口调整大小

LWJGL 3窗口调整大小
EN

Stack Overflow用户
提问于 2019-11-13 01:17:17
回答 2查看 883关注 0票数 1

我对调整窗口大小有问题。当我尝试调整窗口大小时,屏幕没有正确改变。这是窗口的原始大小:

但是当我调整窗口大小时,屏幕变成这样:

每次调整窗口大小但仍然不起作用时,我都会调用updateProjectionMatrix()和glVievport()。

我的代码:

代码语言:javascript
运行
复制
public class Window {

    private static long window;
    private static long time;
    private static String title = "Game";
    private static Input input;

    public static Matrix4f projectionMatrix;
    private static boolean isResized;
    private static boolean isFullscreen = false;
    private static int frames;
    private static long lastFrameTime;
    private static float delta;
    private static int fps;
    private static int[] windowPosX = new int[1], windowPosY = new int[1];
    private static GLFWWindowSizeCallback sizeCallback;

    public static void createDisplay(){ 

        if(!GLFW.glfwInit()) {
            System.err.println("cD REEOR");
            return;
        }
        window = GLFW.glfwCreateWindow((int)Data.getWindowWidth(), (int)Data.getWindowHeight(), "TEST", isFullscreen?GLFW.glfwGetPrimaryMonitor():0, 0);
        input =new Input(window);
        if (window == 0) {
            System.err.println("ERROR: Window wasn't created");
            return;
        }
        GLFWVidMode videoMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
        windowPosX[0] = (videoMode.width() - Data.getWindowWidth()) / 2;
        windowPosY[0] = (videoMode.height() - Data.getWindowHeight()) / 2;
        GLFW.glfwSetWindowPos(window, windowPosX[0], windowPosY[0]);

        GLFW.glfwMakeContextCurrent(window);
        GLFW.glfwShowWindow(window);
        GL.createCapabilities();
        //ContextAttribs attribs = new ContextAttribs(3,2).withForwardCompatible(true).withProfileCore(true);
        GLFW.glfwSwapInterval(1);
        createCallbacks();
        createProjectionMatrix();
    }


    public static Matrix4f getProjectionMatrix() {
        return projectionMatrix;
    }


    public static void setProjectionMatrix(Matrix4f projectionMatrix) {
        Window.projectionMatrix = projectionMatrix;
    }


    public static long getWindow() {
        return window;
    }


    public static void setWindow(long window) {
        Window.window = window;
    }


    public static void updateDisplay( ){
        if (isResized) {
            GL11.glViewport(100, 15, Data.getWindowWidth(), Data.getWindowHeight());
            isResized = false;
            updateProjectionMatrix();
        }
        GL.createCapabilities();
        GLFW.glfwPollEvents();
        frames++;
        if (System.currentTimeMillis() > time + 1000) {
            fps=frames;
            time = System.currentTimeMillis();
            frames = 0;
        }
        long currentFrameTime = getCurrentTime();
        delta = (currentFrameTime - lastFrameTime)/1000f;
        lastFrameTime = currentFrameTime;         
    }
    private static void createCallbacks() {
        sizeCallback = new GLFWWindowSizeCallback() {
            public void invoke(long window, int w, int h) {
                Data.setWindowWidth(w);
                Data.setWindowWidth(h);
                isResized = true;

            }
        };
        GLFW.glfwSetKeyCallback(window, input.getKeyboardCallback());
        GLFW.glfwSetCursorPosCallback(window, input.getMouseMoveCallback());
        GLFW.glfwSetMouseButtonCallback(window, input.getMouseButtonsCallback());
        GLFW.glfwSetScrollCallback(window, input.getMouseScrollCallback());
        GLFW.glfwSetWindowSizeCallback(window, sizeCallback);
    }

    public static boolean isFullscreen() {
        return isFullscreen;
    }


    public static void setFullscreen(boolean isFullscreen) {
        Window.isFullscreen = isFullscreen;
        isResized = true;
        if (isFullscreen) {
            GLFW.glfwGetWindowPos(window, windowPosX, windowPosY);
            GLFW.glfwSetWindowMonitor(window, GLFW.glfwGetPrimaryMonitor(), 0, 0, Data.getWindowWidth(), Data.getWindowHeight(), 0);
        } else {
            GLFW.glfwSetWindowMonitor(window, 0, windowPosX[0], windowPosY[0], Data.getWindowWidth(), Data.getWindowHeight(), 0);
        }
    }


    public static void swapBuffers() {
        GLFW.glfwSwapBuffers(window);
    }
    public static float getFrameTimeSeconds() {
        return delta;
    }

    public static void closeDisplay(){

        //Display.destroy();

    }
    public static long getCurrentTime() {
        float time = (float)(GLFW.glfwGetTimerValue()*1000/(GLFW.glfwGetTimerFrequency()));
        //System.out.println(time);
        return (long) time;

    }
    public static boolean shouldClose() {
        return GLFW.glfwWindowShouldClose(window);
    }


    public static int getFps() {
        return fps;
    }


    public static void setFps(int fps) {
        Window.fps = fps;
    }
    public static void setTimer() {
        lastFrameTime = getCurrentTime();
    }
    private static void createProjectionMatrix() {
        float aspectRatio = (float) Data.getWindowWidth() / (float) Data.getWindowHeight();
        float y_scale = (float) ((1f / Math.tan(Math.toRadians(Data.getProjectionFov() / 2f))) * aspectRatio);
        float x_scale = y_scale / aspectRatio;
        float frustum_length = Data.getProjectionFarPlane() - Data.getProjectionNearPlane();

        projectionMatrix = new Matrix4f();
        projectionMatrix.m00 = x_scale;
        projectionMatrix.m11 = y_scale;
        projectionMatrix.m22 = -((Data.getProjectionFarPlane() + Data.getProjectionNearPlane()) / frustum_length);
        projectionMatrix.m23 = -1;
        projectionMatrix.m32 = -((2 * Data.getProjectionNearPlane() * Data.getProjectionFarPlane()) / frustum_length);
        projectionMatrix.m33 = 0;
    }
    private static void updateProjectionMatrix() {
        float aspectRatio = (float) Data.getWindowWidth() / (float) Data.getWindowHeight();
        float y_scale = (float) ((1f / Math.tan(Math.toRadians(Data.getProjectionFov() / 2f))) * aspectRatio);
        float x_scale = y_scale / aspectRatio;
        projectionMatrix.m00 = x_scale;
        projectionMatrix.m11 = y_scale;
    }
}
EN

回答 2

Stack Overflow用户

发布于 2019-11-13 01:33:44

这一行是罪魁祸首:

代码语言:javascript
运行
复制
float y_scale = (float) ((1f / Math.tan(Math.toRadians(Data.getProjectionFov() / 2f))) * aspectRatio);

它应该是:

代码语言:javascript
运行
复制
float y_scale = (float) ((1f / Math.tan(Math.toRadians(Data.getProjectionFov() / 2f))) );
票数 0
EN

Stack Overflow用户

发布于 2020-06-18 04:14:16

我解决了这个问题。那是个愚蠢的错误。我改变了这个

sizeCallback = new GLFWWindowSizeCallback() { public void invoke(long window,int w,int h) { Data.setWindowWidth(w);Data.setWindowWidth(h);isResized = true;} };

进入到这个

sizeCallback = new GLFWWindowSizeCallback() { public void invoke(long window,int w,int h) { Data.setWindowWidth(w);Data.setWindowHeight(h);isResized = true;} };

是的,我写的是Data.setWindowWidth(h);而不是Data.setWindowHeight(h);:D

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58823611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档