日期:2014-05-20 浏览次数:20907 次
var pps = new PresentParameters();
pps.AutoDepthStencilFormat = Format.D16;
pps.EnableAutoDepthStencil = true;
pps.BackBufferWidth = (int)SystemParameters.PrimaryScreenWidth;
pps.BackBufferHeight = (int)SystemParameters.PrimaryScreenHeight;
pps.BackBufferFormat = Format.X8R8G8B8;
pps.SwapEffect = SwapEffect.Discard;
pps.Windowed = windowed;
pps.PresentationInterval = PresentInterval.Default;
pps.Multisample = MultisampleType.FourSamples;
device = new Device(d3d, 0, DeviceType.Hardware, IntPtr.Zero, CreateFlags.HardwareVertexProcessing | CreateFlags.FpuPreserve, pps);
Surface surface = Surface.CreateRenderTarget(device, width, height, Format.X8R8G8B8, MultisampleType.None, 0, false);
back = device.GetRenderTarget(0); //保存前一渲染目标
device.SetRenderTarget(0, surface);//设置当前渲染目标
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, 0, 1.0f, 0);
//设置视图、透视变换矩阵啥的略
device.SetRenderState(RenderState.Lighting, true);
device.SetRenderState(RenderState.ZEnable, true); //开启Z缓存
device.SetRenderState(RenderState.MultisampleAntialias, true);
device.SetRenderState(RenderState.NormalizeNormals, true);
device.SetRenderState(RenderState.Ambient, Scene.Ambient.ToArgb());
device.SetRenderState(RenderState.CullMode, SlimDX.Direct3D10.CullMode.Front);
device.SetRenderState(RenderState.FillMode, FillMode.Solid);
//设置灯光略
device.BeginScene();
//循环绘制每个模型
for(...)
{
device.VertexDeclaration = data.VertexDeclaration; //顶点格式
device.SetStreamSource(0, data.VertexBuffer, 0, data.VertexSize);//顶点数据
device.Indices = data.IndexBuffer; //索引数据
device.SetTransform(TransformState.World, data.Transform; //世界变换
device.Material = data.Material; //材质
//纹理
for (int i = 0; i < data.Textures.Count; i++)
{
device.SetTexture(i, data.Textures[i]);
}
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, model.VertexBuffer.Count, 0, model.IndexBuffer.Count / 3);
}
device.EndScene();
device.Present();
device.SetRenderTarget(0, back);//还原前一渲染目标