OpenGL/D3D:如何获取在 Windows 中全屏运行的游戏的屏幕截图? [英] OpenGL/D3D: How do I get a screen grab of a game running full screen in Windows?

查看:58
本文介绍了OpenGL/D3D:如何获取在 Windows 中全屏运行的游戏的屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个全屏运行的 OpenGL 游戏(Left 4 Dead 2).我想以编程方式获取它的屏幕截图,然后将其写入视频文件.

Suppose I have an OpenGL game running full screen (Left 4 Dead 2). I'd like to programmatically get a screen grab of it and then write it to a video file.

我尝试过 GDI、D3D 和 OpenGL 方法(例如 glReadPixels),但在捕获流中接收到空白屏幕或闪烁.

I've tried GDI, D3D, and OpenGL methods (eg glReadPixels) and either receive a blank screen or flickering in the capture stream.

有什么想法吗?

就其价值而言,与我正在努力实现的类似的典型示例是 Fraps.

For what it's worth, a canonical example of something similar to what I'm trying to achieve is Fraps.

推荐答案

有几种方法可以解决这个问题.它们中的大多数都令人讨厌,这完​​全取决于您要定位的图形 API 类型以及目标应用程序使用哪些功能.大多数 DirectX、GDI+ 和 OpenGL 应用程序都是双缓冲或三缓冲的,因此它们都调用:

There are a few approaches to this problem. Most of them are icky, and it totally depends on what kind of graphics API you want to target, and which functions the target application uses. Most DirectX, GDI+ and OpenGL applications are double or tripple-buffered, so they all call:

void SwapBuffers(HDC hdc)

在某个时候.每当应绘制窗口时,它们还会在其消息队列中生成 WM_PAINT 消息.这为您提供了两种选择.

at some point. They also generate WM_PAINT messages in their message queue whenever the window should be drawn. This gives you two options.

  • 您可以将全局钩子或线程本地钩子安装到目标进程中并捕获 WM_PAINT 消息.这允许您在绘制发生之前从设备上下文中复制内容.可以通过枚举系统上的所有进程并查找已知的窗口名称或已知的模块句柄来找到该进程.

  • You can install a global hook or thread-local hook into the target process and capture WM_PAINT messages. This allows you to copy the contents from the device context just before the painting happens. The process can be found by enumerating all the processes on the system and look for a known window name, or a known module handle.

您可以将代码注入目标进程的 SwapBuffers 本地副本.在 Linux 上,这很容易通过 LD_PRELOAD 环境变量或通过显式调用 ld-linux.so.2 来完成,但在 Windows 上没有等效项.幸运的是,Microsoft Research 提供了一个名为 Detours 的框架,可以为您执行此操作.您可以在此处找到:链接.

You can inject code into the target process's local copy of SwapBuffers. On Linux this would be easy to do via the LD_PRELOAD environmental variable, or by calling ld-linux.so.2 explicitly, but there is no equivalient on Windows. Luckily there is a framework from Microsoft Research which can do this for you called Detours. You can find this here: link.

demoscene 小组 Farbrausch 制作了一个名为 kkapture 的演示捕获工具,它利用了 Detours 库.然而,他们的工具针对不需要用户输入的应用程序,因此他们基本上通过挂钩所有可能的时间函数,如 timeGetTime()、GetTickCount() 和 QueryPerformanceCounter() 以固定帧速率运行演示.这完全是rad.可以在 此处找到由 ryg(我认为?)撰写的关于 kkapture 内部结构的演示文稿.我认为您对此很感兴趣.

The demoscene group Farbrausch made a demo-capturing tool named kkapture which makes use of the Detours library. Their tool targets applications that require no user input however, so they basically run the demos at a fixed framerate by hooking into all the possible time functions, like timeGetTime(), GetTickCount() and QueryPerformanceCounter(). It's totally rad. A presentation written by ryg (I think?) regarding kkapture's internals can be found here. I think that's of interest to you.

有关 Windows 挂钩的更多信息,请参见 此处此处.

For more information about Windows hooks, see here and here.

这个想法引起了我的兴趣,所以我使用 Detours 来连接 OpenGL 应用程序并处理图形.这是添加了绿雾的 Quake 2:

This idea intrigued me, so I used Detours to hook into OpenGL applications and mess with the graphics. Here is Quake 2 with green fog added:

Detours 在两个层面上起作用.实际的挂钩仅在与目标进程相同的进程空间中起作用.因此,Detours 具有将 DLL 注入进程并强制其 DLLMain 也运行的函数,以及应该在该 DLL 中使用的函数.当 DLLMain 运行时,DLL 应调用 DetourAttach() 以指定要挂钩的函数以及绕行"函数.函数,这是您要覆盖的代码.

Detours works on two levels. The actual hooking only works in the same process space as the target process. So Detours has a function for injecting a DLL into a process and force its DLLMain to run too, as well as functions that are supposed to be used in that DLL. When DLLMain is run, the DLL should call DetourAttach() to specify the functions to hook, as well as the "detour" function, which is the code you want to override with.

所以它基本上是这样工作的:

So it basically works like this:

  • 您有一个启动器应用程序,它的唯一任务是调用 DetourCreateProcessWithDll().它的工作方式与 CreateProcessW 相同,只是有一些额外的参数.这会将 DLL 注入进程并调用其 DllMain().
  • 您实现了一个调用 Detour 函数并设置蹦床函数的 DLL.这意味着先调用 DetourTransactionBegin()、DetourUpdateThread()、DetourAttach(),然后调用 DetourTransactionEnd().
  • 使用启动器将您实现的 DLL 注入进程.

不过有一些警告.运行 DllMain 时,稍后使用 LoadLibrary() 导入的库尚不可见.因此,您不必在 DLL 附件事件期间设置所有内容.一种解决方法是跟踪到目前为止被覆盖的所有函数,并尝试初始化这些函数中您已经可以调用的其他函数.这样,一旦 LoadLibrary 将新函数映射到进程的内存空间,您就会发现新函数.不过,我不太确定这对 wglGetProcAddress 的效果如何.(也许这里的其他人对此有想法?)

There are some caveats though. When DllMain is run, libraries that are imported later with LoadLibrary() aren't visible yet. So you can't necessarily set up everything during the DLL attachment event. A workaround is to keep track of all the functions that are overridden so far, and try to initialize the others inside these functions that you can already call. This way you will discover new functions as soon as LoadLibrary have mapped them into the memory space of the process. I'm not quite sure how well this would work for wglGetProcAddress though. (Perhaps someone else here has ideas regarding this?)

某些 LoadLibrary() 调用似乎失败.我使用 Quake 2 进行了测试,但由于某些原因,DirectSound 和 waveOut API 未能初始化.我还在调查这个.

Some LoadLibrary() calls seem to fail. I tested with Quake 2, and DirectSound and the waveOut API failed to initalize for some reason. I'm still investigating this.

这篇关于OpenGL/D3D:如何获取在 Windows 中全屏运行的游戏的屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆