窗口调整大小不会影响内容
Posted
技术标签:
【中文标题】窗口调整大小不会影响内容【英文标题】:Window resize doesn't effect contents 【发布时间】:2020-06-07 00:16:52 【问题描述】:我在调整 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();
// --------------------------------------------------------------------------------------------
我也有我在说什么的图像。
Before resizing After resizing 显然,我希望四边形随屏幕调整大小,使全屏模式在中间有一个大四边形,而不是在左下角坐标中具有相同的大小。我该怎么做?
【问题讨论】:
【参考方案1】:为了正确调整窗口内容的大小,您需要响应窗口的大小调整:
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
【讨论】:
我不完全理解这里的语法,这段代码放入我自己的代码时会抛出很多错误,很可能是由于 GLFWWindowSizeCallback 对象是抽象类的扩展或其他东西。我该如何解决?提前致谢。 @tymo tymo 你使用的是 LWJGL 2 还是 3,因为这是用 LWJGL 3 编写的。GLFWWindowSizeCallback 在 LWJGL 2 中不存在。 好的,所以我能够让代码不抛出任何错误并运行程序,但是当我调整窗口大小时四边形消失了@tymo tymo 你在使用着色器吗?投影矩阵(它的计算)可能有问题。对我来说一切正常。以上是关于窗口调整大小不会影响内容的主要内容,如果未能解决你的问题,请参考以下文章