C# SharpDx direct3d11如何开始渲染
我想在C#上使用directx,我正在使用SharpDX包装器。我有一本书叫Direct3D渲染食谱,我从中得到了基本代码。我想创建一个三维世界视图。为此,我需要一个摄影机视图和一个网格,可以帮助识别世界位置,就像在Autodesk Maya中一样,但我不知道如何做到这一点。我的脑子里乱七八糟的,我该怎么开始呢 在这里,我有一些代码,可以呈现我认为:C# SharpDx direct3d11如何开始渲染,c#,direct3d,sharpdx,direct3d11,C#,Direct3d,Sharpdx,Direct3d11,我想在C#上使用directx,我正在使用SharpDX包装器。我有一本书叫Direct3D渲染食谱,我从中得到了基本代码。我想创建一个三维世界视图。为此,我需要一个摄影机视图和一个网格,可以帮助识别世界位置,就像在Autodesk Maya中一样,但我不知道如何做到这一点。我的脑子里乱七八糟的,我该怎么开始呢 在这里,我有一些代码,可以呈现我认为: using System; using SharpDX.Windows; using SharpDX.DXGI; using SharpDX.Di
using System;
using SharpDX.Windows;
using SharpDX.DXGI;
using SharpDX.Direct3D11;
using Device = SharpDX.Direct3D11.Device;
using Device1 = SharpDX.Direct3D11.Device1;
namespace CurrencyConverter
{
static class Program
{[STAThread]
static void Main()
{
// Enable object tracking
SharpDX.Configuration.EnableObjectTracking = true;
SharpDX.Animation.Timer timer = new SharpDX.Animation.Timer();
#region Direct3D Initialization
// Create the window to render to
Form1 form = new Form1();
form.Text = "D3DRendering - EmptyProject";
form.Width = 640;
form.Height = 480;
// Declare the device and swapChain vars
Device device;
SwapChain swapChain;
// Create the device and swapchain
// First create a regular D3D11 device
using (var device11 = new Device(
SharpDX.Direct3D.DriverType.Hardware,
DeviceCreationFlags.None,
new[] {
SharpDX.Direct3D.FeatureLevel.Level_11_1,
SharpDX.Direct3D.FeatureLevel.Level_11_0,
}))
{
// Query device for the Device1 interface (ID3D11Device1)
device = device11.QueryInterfaceOrNull<Device1>();
if (device == null)
throw new NotSupportedException(
"SharpDX.Direct3D11.Device1 is not supported");
}// Rather than create a new DXGI Factory we reuse the
// one that has been used internally to create the device
using (var dxgi = device.QueryInterface<SharpDX.DXGI.Device2>())
using (var adapter = dxgi.Adapter)
using (var factory = adapter.GetParent<Factory2>())
{
var desc1 = new SwapChainDescription1()
{
Width = form.ClientSize.Width,
Height = form.ClientSize.Height,
Format = Format.R8G8B8A8_UNorm,
Stereo = false,
SampleDescription = new SampleDescription(1, 0),
Usage = Usage.BackBuffer | Usage.RenderTargetOutput,
BufferCount = 1,
Scaling = Scaling.Stretch,
SwapEffect = SwapEffect.Discard,
};
swapChain = new SwapChain1(factory,
device,
form.Handle,
ref desc1,
new SwapChainFullScreenDescription()
{
RefreshRate = new Rational(60, 1),
Scaling = DisplayModeScaling.Centered,
Windowed = true
},
// Restrict output to specific Output (monitor)
adapter.Outputs[0]);
}
// Create references for backBuffer and renderTargetView
var backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain,
0);
var renderTargetView = new RenderTargetView(device,
backBuffer);
#endregion
// Setup object debug names
device.DebugName = "The Device";
swapChain.DebugName = "The SwapChain";
backBuffer.DebugName = "The Backbuffer";
renderTargetView.DebugName = "The RenderTargetView";
#region Render loop
// Create and run the render loop
RenderLoop.Run(form, () =>
{
// Clear the render target with...
var lerpColor = SharpDX.Color.Lerp(SharpDX.Color.White,
SharpDX.Color.DarkBlue,
(float)((timer.Time) / 10.0 % 1.0));
device.ImmediateContext.ClearRenderTargetView(
renderTargetView,
lerpColor);
// Execute rendering commands here...
//...
//I DO NOT HAVE ANY IDEA
//...
// Present the frame
swapChain.Present(0, PresentFlags.RestrictToOutput);
});
#endregion
#region Direct3D Cleanup
// Release the device and any other resources created
renderTargetView.Dispose();
backBuffer.Dispose();
device.Dispose();
swapChain.Dispose();
#endregion
}
}
}
使用系统;
使用SharpDX.Windows;
使用SharpDX.DXGI;
使用SharpDX.Direct3D11;
使用设备=SharpDX.Direct3D11.Device;
使用Device1=SharpDX.Direct3D11.Device1;
名称空间电流转换器
{
静态类程序
{[statthread]
静态void Main()
{
//启用对象跟踪
SharpDX.Configuration.EnableObjectTracking=true;
SharpDX.Animation.Timer Timer=新的SharpDX.Animation.Timer();
#区域Direct3D初始化
//创建要渲染到的窗口
Form1 form=新Form1();
form.Text=“D3DRendering-EmptyProject”;
形状.宽度=640;
外形高度=480;
//声明设备和交换链变量
装置装置;
SwapChain SwapChain;
//创建设备和交换链
//首先创建一个常规的D3D11设备
使用(var设备11=新设备(
SharpDX.Direct3D.DriverType.Hardware,
DeviceCreationFlags。无,
新[]{
SharpDX.Direct3D.FeatureLevel.Level_11_1,
SharpDX.Direct3D.FeatureLevel.Level_11_0,
}))
{
//Device1接口的查询设备(ID3D11Device1)
device=device11.QueryInterfaceOrNull();
如果(设备==null)
抛出新的NotSupportedException(
“不支持SharpDX.Direct3D11.Device1”);
}//我们不再创建新的DXGI工厂,而是重用
//一种在内部用于创建设备的设备
使用(var dxgi=device.QueryInterface())
使用(var adapter=dxgi.adapter)
使用(var factory=adapter.GetParent())
{
var desc1=新的SwapChainDescription1()
{
宽度=form.ClientSize.Width,
高度=form.ClientSize.Height,
Format=Format.R8G8B8A8_UNorm,
立体声=假,
SampleDescription=新的SampleDescription(1,0),
用法=Usage.BackBuffer | Usage.RenderTargetOutput,
BufferCount=1,
缩放=缩放。拉伸,
SwapEffect=SwapEffect.丢弃,
};
swapChain=新SwapChain1(工厂、,
装置,
形式,处理,,
参考说明1,
新的SwapChainFullScreenDescription()
{
刷新率=新的有理数(60,1),
缩放=显示模式缩放。居中,
加窗=真
},
//将输出限制为特定输出(监视器)
适配器输出[0]);
}
//为backBuffer和renderTargetView创建参照
var backBuffer=来自swapChain(swapChain,
0);
var renderTargetView=新的renderTargetView(设备,
backBuffer);
#端区
//设置对象调试名称
device.DebugName=“设备”;
swapChain.DebugName=“swapChain”;
backBuffer.DebugName=“backBuffer”;
renderTargetView.DebugName=“renderTargetView”;
#区域渲染循环
//创建并运行渲染循环
RenderLoop.Run(表单,()=>
{
//用…清除渲染目标。。。
var lerpColor=SharpDX.Color.Lerp(SharpDX.Color.White,
SharpDX.Color.DarkBlue,
(浮动)((计时器时间)/10.0%1.0));
device.ImmediateContext.ClearRenderTargetView(
renderTargetView,
彩色);
//在此处执行渲染命令。。。
//...
//我不知道
//...
//呈现框架
swapChain.Present(0,PresentFlags.RestrictToOutput);
});
#端区
#区域Direct3D清理
//释放设备和创建的任何其他资源
renderTargetView.Dispose();
backBuffer.Dispose();
device.Dispose();
swapChain.Dispose();
#端区
}
}
}
一般来说,使用Direct3D,您需要大量的代码才能在屏幕上显示任何内容
在SharpDX存储库中,您有一个样本,它包含了绘制3d场景所需的所有元素,足以让您真正开始
我建议特别注意:
- 深度缓冲区创建(DepthStencilView)
- fx文件,因为您需要着色器在屏幕上显示任何内容(不再具有固定功能)
- 如何创建顶点缓冲区,需要将几何体拆分为三角形(在常见情况下,还有其他可能)
- 不要忘记SetViewport(省略它是很常见的)
- 引用输入汇编程序的调用正在分配geo