gpt4 book ai didi

java - LWJGL 3 调整窗口大小

转载 作者:太空宇宙 更新时间:2023-11-04 09:17:15 26 4
gpt4 key购买 nike

我在调整窗口大小时遇到​​问题。当我尝试调整窗口大小时,屏幕未正确更改。这是窗口的原始大小:

Screen work correctly

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

Screen is not resized

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

我的代码:

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

最佳答案

这行是罪魁祸首:

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

应该是:

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

关于java - LWJGL 3 调整窗口大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58823611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com