1
votes

I have problem with resizing window. When I tryed to resize window, screen didn't change correctly. This is original size of window:

Screen work correctly

But when I resize window, screen turn into this:

Screen is not resized

I call updateProjectionMatrix() and glVievport() every time when window is resized but still not working.

My code:

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;
    }
}
2
You have a global variable named Data. What is that? - prideout
Class which have variables shared in whole program - MenTom_CZ

2 Answers

0
votes

This line is the culprit:

float y_scale = (float) ((1f / Math.tan(Math.toRadians(Data.getProjectionFov() / 2f))) * aspectRatio);

It should be:

float y_scale = (float) ((1f / Math.tan(Math.toRadians(Data.getProjectionFov() / 2f))) );
0
votes

I fixed that. That was stupid mistake. I changed this

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

into this

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

Yes, I wrote Data.setWindowWidth(h); instead of Data.setWindowHeight(h); :D