glReadPixels() 无法填充 ByteBuffer
好吧,我正在尝试使用 LWJGL 为 OpenGL 中的窗口截取屏幕截图。这是代码:
ByteBuffer pixels = ByteBuffer.allocateDirect(800*600*4);
pixels.order(ByteOrder.nativeOrder());
while(!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render();
Display.update();
// "Screenshot" block
if(Keyboard.isKeyDown(Keyboard.KEY_Q)) {
pixels.clear();
glReadPixels(0, 0, 800, 600, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
pixels.flip();
// pixels.position() and pixels.limit() tells us that there is nothing in the buffer
// ...
}
}
并且...
我已经尝试了各种版本的代码,例如将“Screenshot”块放在 Display.update() 之前。并且使用 glReadBuffer(GL_BACK/GL_FRONT) 和 glDrawBuffer(GL_BACK/GL_FRONT) 的组合也无济于事。
我已禁用所有 OpenGL 状态和渲染,以便仅出现空白屏幕并尝试使用 glReadPixels。缓冲区中应该有一个空白屏幕,但缓冲区中没有任何内容。
glGetError() 不会产生任何错误。
我有一个类似的 C+ 版本,可以正常工作。
我在 NVIDIA Corporation GeForce GTS 450/PCI/SSE2 上运行 Windows 7、OpenGL 4.10 和 LWJGL 版本 2.7.1。
我
那么,问题出在哪里呢?有什么想法吗?提前致谢。
Well, I'm trying to take a screenshot for a window in OpenGL using LWJGL. Here's the code:
ByteBuffer pixels = ByteBuffer.allocateDirect(800*600*4);
pixels.order(ByteOrder.nativeOrder());
while(!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render();
Display.update();
// "Screenshot" block
if(Keyboard.isKeyDown(Keyboard.KEY_Q)) {
pixels.clear();
glReadPixels(0, 0, 800, 600, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
pixels.flip();
// pixels.position() and pixels.limit() tells us that there is nothing in the buffer
// ...
}
}
And...
I've already tried various versions of the code, like placing the "Screenshot" block before Display.update(). And using combinations of glReadBuffer(GL_BACK/GL_FRONT) and glDrawBuffer(GL_BACK/GL_FRONT) to no avail.
I've disabled all OpenGL states and rendering, so that only a blank screen appears and tried using glReadPixels. A blank screen supposes to be in the buffer, but nothing is in the buffer.
glGetError() doesn't produce any error.
I have a similar C+ version that works fine.
I'm running Windows 7, OpenGL 4.10 and LWJGL version 2.7.1 on NVIDIA Corporation GeForce GTS 450/PCI/SSE2.
So, what's the problem? Any ideas? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是吗?我认为更简单的方法是查看缓冲区的内容。
此外,
Display.update
会交换缓冲区。交换后,后台缓冲区的内容未定义。因此,您应该在交换之前从前台缓冲区读取(这不是一个好主意)或读取后台缓冲区。Do they? I think a more foolproof method would be to look at the contents of the buffer.
Also,
Display.update
swaps buffers. The contents of the back buffer are undefined after a swap. So you should either read from the front buffer (not a good idea) or read the back buffer before swapping.我进一步研究了尼科波拉斯的答案。并意识到 glReadPixels 确实返回了信息,尽管首先交换或不交换帧缓冲区。
这就是我复制字节缓冲区的方法。
我太快下结论了,glReadPixels() 没有仅根据其位置和限制返回任何内容。感谢尼可波拉斯提供的有用意见。 =D
I further researched Nico Bolas's answer. And realized glReadPixels indeed returned information despite swapping or not swapping the frame buffers first.
So, this is how I copied the byte buffer out.
I jumped to conclusion too quickly, that glReadPixels() didn't return anything based on its position and limit alone. Thanks, Nicol Bolas for the helpful input. =D