投影机拼接融合技术--UE4拼接

           这个是正在开发的一个项目“UE4拼接技术”,先拿一部分成果出来展示一下。

一、基本思路

编程语言:C++
DX版本:D3dx11
使用技术:HOOK
UE4版本:4.24
基本思路:使用DX11hook到UE4.24的游戏主体,建立新的拼接窗口覆盖老的窗口,从UE4的IDXGISwapChain拿到
图像数据(GetBuffer),将图像数据转换成DXGI_FORMAT_R8G8B8A8_UNORM,显示到拼接窗口上,原理就是一张
一张截图,之间贴到拼接窗口上去。也可以理解成视频播放,将游戏画面一张一张播放出来

 二、代码示例

//==========================================================================================================================
void CaptureScreen(IDXGISwapChain* pSwapChain)
{
	///DXGI_FORMAT_R10G10B10A2_UNORM
	ID3D11Texture2D* pSurface;
	HRESULT hr = pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), reinterpret_cast< void** >(&pSurface));
	if (pSurface)
	{
		D3D11_TEXTURE2D_DESC BackBufferTextureDesc;
		pSurface->GetDesc(&BackBufferTextureDesc);
        
		/
		D3D11_TEXTURE2D_DESC description= BackBufferTextureDesc;
		description.BindFlags = 0;
		description.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
		description.Usage = D3D11_USAGE_STAGING;
		
		ID3D11Texture2D* pNewTexture = NULL;
		HRESULT hr = pDevice->CreateTexture2D(&description, NULL, &pNewTexture);
		if (pNewTexture)
		{
			pContext->CopyResource(pNewTexture, pSurface);
			D3D11_MAPPED_SUBRESOURCE resource;
			unsigned int subresource = D3D11CalcSubresource(0, 0, 0);
			HRESULT hr = pContext->Map(pNewTexture, subresource, D3D11_MAP_READ_WRITE, 0, &resource);

			auto data = std::unique_ptr<uint32_t[]>(new uint32_t[description.Width * description.Height]);
			auto src = static_cast<uint8_t*>(resource.pData);
			uint32_t* dest = data.get();
			uint32_t * OriginalPtr = dest;
			for (UINT y = 0; y < description.Height; ++y)
			{
				auto sptr = reinterpret_cast<uint32_t*>(src);
				for (UINT x = 0; x < description.Width; ++x)
				{
					uint32_t t = *(sptr++);
					uint32_t r = (t & 0x000003ff) >> 2;
					uint32_t g = (t & 0x000ffc00) >> 12;
					uint32_t b = (t & 0x3ff00000) >> 22;

					// Upscale alpha
					// 11xxxxxx -> 11111111 (255)
					// 10xxxxxx -> 10101010 (170)
					// 01xxxxxx -> 01010101 (85)
					// 00xxxxxx -> 00000000 (0)
					t &= 0xc0000000;
					uint32_t a = (t >> 24) | (t >> 26) | (t >> 28) | (t >> 30);
					// Convert to DXGI_FORMAT_R8G8B8A8_UNORM
					*(dest++) = r | (g << 8) | (b << 16) | (a << 24);
				}
				src += resource.RowPitch;
			}
			//dest=OriginalPtr;
			MySaveBmp("out.bmp", dest, 1920, 1080);
			pContext->Unmap(pNewTexture, subresource);
			SAFE_RELEASE(pNewTexture);
			SAFE_RELEASE(pSurface);
			return;
		}
	}
}

原画面

HOOK截图效果如下(左右镜像了,反转一下就行)

再加一个最终效果

 

 三、参考链接


c++ - UE4 capture frame using ID3D11Texture2D and convert to R8G8B8 bitmap - Stack Overflowhttps://stackoverflow.com/questions/66769652/ue4-capture-frame-using-id3d11texture2d-and-convert-to-r8g8b8-bitmap

 RGB数据保存为BMP图片_yixianfeng41的专栏-CSDN博客_rgb数据保存为bmp一、BMP文件由文件头、位图信息头、颜色信息和图形数据四部分组成。1、BMP文件头(14字节)[cpp] view plain copy typedef struct                       /**** BMP file header structure ****/  {      unsigned inthttps://blog.csdn.net/yixianfeng41/article/details/52591585

猜你喜欢

转载自blog.csdn.net/qq_36251561/article/details/121555483