我在调整窗口大小时遇到问题。 当我尝试调整窗口大小时,屏幕未正确更改。 这是窗口的原始大小:
但是当我调整窗口大小时,屏幕会变成这样:
每次调整窗口大小但仍然无法使用时,我都会调用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(),Data.getWindowHeight(),0);
} else {
GLFW.glfwSetWindowMonitor(window,windowPosY[0],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;
}
}