gpt4 book ai didi

opengl - Macbook Pro 上的 glFrustum 给出了意想不到的结果

转载 作者:行者123 更新时间:2023-12-02 01:28:21 27 4
gpt4 key购买 nike

我只是使用 LWJGL 3 设置一个简单的渲染器,当它在我的外部显示器上运行时,它看起来像我预期的那样,但是当在我的 Macbook 上调整它的大小时,它会缩小视口(viewport)。然后,如果我移动窗口,它就会修复它。这是我在初始化 GL 内容和调整窗口大小时运行的代码。

public void resize(int width, int height)
{
val near = 0.1
val far = 100.0

GL11.glViewport(0, 0, width, height);

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();

float aspect = width / (float)height
float fov = (float)(45.0 / 360.0 * Math.PI)

float fH = Math.tan(fov) * near
float fW = fH * aspect
GL11.glFrustum(-fW, fW, -fH, fH, near, far);

GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
}

这是我看到的

错误 Looks Wrong正确 Looks Right

如果我在外接显示器上运行它,它不会改变,它总是正确的。

最佳答案

读取帧缓冲区的大小就是这里的答案。我使用用户传入的像素大小创建窗口,然后读取 frameBuffer 大小以传递给 glViewport。

public Window(String title, int width, int height)
{
_errorCallback = GLFWErrorCallback.createPrint(System.err);
GLFW.glfwSetErrorCallback(_errorCallback);

if (GLFW.glfwInit() != GLFW.GLFW_TRUE)
{
throw IllegalStateException("Unable to initialize GLFW");
}

GLFW.glfwDefaultWindowHints();
GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE);

_window = GLFW.glfwCreateWindow(width, height, title ?: "", 0, 0);
if (_window == 0L)
{
throw RuntimeException("Failed to create window");
}

// Setup Callbacks

// Get the resolution of the primary monitor
GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());

// Center our window
GLFW.glfwSetWindowPos(_window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);

// Make the OpenGL context current
GLFW.glfwMakeContextCurrent(_window);

// Enable v-sync
GLFW.glfwSwapInterval(1);

// Make the window visible
GLFW.glfwShowWindow(_window);
}

然后我读取帧缓冲区大小以传递到 glViewport 和 glFrustum。

public Vector2 frameBufferSize()
{
IntBuffer bufferWidth = BufferUtils.createIntBuffer(4);
IntBuffer bufferHeight = BufferUtils.createIntBuffer(4);

GLFW.glfwGetFramebufferSize(_window, bufferWidth, bufferHeight);

return Vector2(bufferWidth.get(0), bufferHeight.get(0));
}

关于opengl - Macbook Pro 上的 glFrustum 给出了意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35638229/

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