【发布时间】:2015-05-27 07:21:54
【问题描述】:
我制作了一个小应用程序,用于从任何窗口游戏中截取屏幕截图并将其发送到 iPhone 以创建虚拟现实应用程序,例如 oculus rift(请参阅https://github.com/gagagu/VR-Streamer-Windows-Server 了解更多信息)。 将使用 SharpDX 捕获图像,并且一切正常。 现在我想实现这样的镜头校正(桶形失真),我正在寻找最快的方法来实现它。我正在寻找许多有关桶形失真信息的互联网站点,我认为最快的方法是使用着色器,但我对sharpdx 很陌生(并且对着色器一无所知)而且我不知道如何实现我的代码的着色器。大多数教程将着色器应用于对象(如立方体)而不是捕获的图像,所以我不知道该怎么做。
[STAThread]
public System.Drawing.Bitmap Capture()
{
isInCapture = true;
try
{
// init
bool captureDone = false;
bitmap = new System.Drawing.Bitmap(captureRect.Width, captureRect.Height, PixelFormat.Format32bppArgb);
// the capture needs some time
for (int i = 0; !captureDone; i++)
{
try
{
//capture
duplicatedOutput.AcquireNextFrame(-1, out duplicateFrameInformation, out screenResource);
// only for wait
if (i > 0)
{
using (var screenTexture2D = screenResource.QueryInterface<Texture2D>())
device.ImmediateContext.CopyResource(screenTexture2D, screenTexture);
mapSource = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, MapFlags.None);
mapDest = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, captureRect.Width, captureRect.Height),
ImageLockMode.WriteOnly, bitmap.PixelFormat);
sourcePtr = mapSource.DataPointer;
destPtr = mapDest.Scan0;
// set x position offset to rect.x
int rowPitch = mapSource.RowPitch - offsetX;
// set pointer to y position
sourcePtr = IntPtr.Add(sourcePtr, mapSource.RowPitch * captureRect.Y);
for (int y = 0; y < captureRect.Height; y++) // needs to speed up!!
{
// set pointer to x position
sourcePtr = IntPtr.Add(sourcePtr, offsetX);
// copy pixel to bmp
Utilities.CopyMemory(destPtr, sourcePtr, pWidth);
// incement pointert to next line
sourcePtr = IntPtr.Add(sourcePtr, rowPitch);
destPtr = IntPtr.Add(destPtr, mapDest.Stride);
}
bitmap.UnlockBits(mapDest);
device.ImmediateContext.UnmapSubresource(screenTexture, 0);
captureDone = true;
}
screenResource.Dispose();
duplicatedOutput.ReleaseFrame();
}
catch//(Exception ex) // catch (SharpDXException e)
{
//if (e.ResultCode.Code != SharpDX.DXGI.ResultCode.WaitTimeout.Result.Code)
//{
// // throw e;
//}
return new Bitmap(captureRect.Width, captureRect.Height, PixelFormat.Format32bppArgb);
}
}
}
catch
{
return new Bitmap(captureRect.Width, captureRect.Height, PixelFormat.Format32bppArgb);
}
isInCapture = false;
return bitmap;
}
从愿意提供帮助的人那里获得一点启动帮助真的很棒。 我在 inet 上找到了一些着色器,但它是为 opengl (https://github.com/dghost/glslRiftDistort/tree/master/libovr-0.4.x/glsl110) 编写的。我也可以使用 directx (sharpdx) 吗?
感谢您的帮助!
【问题讨论】:
-
基本上,您将着色器应用于覆盖整个屏幕的矩形......在该着色器中,对于每个纹素,您应用偏移量(我现在无法帮助您进行计算,但应该不难)到纹理的 UV,具体取决于 XY 位置。
标签: c# shader sharpdx distortion