好的 - 我是SlimDX,DirectX和图形编程的全新手,我需要加快速度。
我正在尝试开发一个原型应用程序来可视化和调试我最终将在非图形应用程序中使用的数据。
我已经完成了三个SlimDX教程,并且我开始掌握所使用的方法,但是我遇到了障碍,因为我无法弄清楚如何绘制多个对象。 ......
SimpleTriangle示例显示了如何绘制单个三角形。我正在尝试扩展该示例以绘制两个三角形。我现在对使用实例等不感兴趣,因为通常我的应用程序中的对象都会有所不同 - 我这里只使用两个三角形,因为它很容易修改现有的顶点数据....
我在这里已经包含了修改过的样本来源,我希望有人可以帮我解决这个基本的障碍。
我所做的是,
1)创建第二个顶点数据数组(沿x轴偏移第一个三角形一个单位) 2)创建第二个缓冲区并用我的新数组中的顶点数据填充它 3)创建绑定顶点数组的VertexBufferBindings 4)将两个VertexBufferBdings放入一个数组中 5)将VertexBufferBinding数组发送到Input Assembler 6)画画
我在屏幕上看到的只是第一个三角形。如果我切换三角形的顺序,我会看到偏移三角形。我只是不能同时看到两个......
这是代码。
我非常感谢任何帮助......
道格
using System.Windows.Forms;
using SlimDX;
using SlimDX.D3DCompiler;
using SlimDX.Direct3D11;
using SlimDX.DXGI;
using SlimDX.Windows;
using Device = SlimDX.Direct3D11.Device;
using Resource = SlimDX.Direct3D11.Resource;
namespace SimpleTriangle
{
static class Program
{
static void Main()
{
Device device;
SwapChain swapChain;
ShaderSignature inputSignature;
VertexShader vertexShader;
PixelShader pixelShader;
var form = new RenderForm("Tutorial 3: Simple Triangle");
var description = new SwapChainDescription()
{
BufferCount = 2,
Usage = Usage.RenderTargetOutput,
OutputHandle = form.Handle,
IsWindowed = true,
ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
SampleDescription = new SampleDescription(1, 0),
Flags = SwapChainFlags.AllowModeSwitch,
SwapEffect = SwapEffect.Discard
};
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out device, out swapChain);
// create a view of our render target, which is the backbuffer of the swap chain we just created
RenderTargetView renderTarget;
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
// setting a viewport is required if you want to actually see anything
var context = device.ImmediateContext;
var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);
context.OutputMerger.SetTargets(renderTarget);
context.Rasterizer.SetViewports(viewport);
// load and compile the vertex shader
using (var bytecode = ShaderBytecode.CompileFromFile("triangle.fx", "VShader", "vs_4_0", ShaderFlags.None, EffectFlags.None))
{
inputSignature = ShaderSignature.GetInputSignature(bytecode);
vertexShader = new VertexShader(device, bytecode);
}
// load and compile the pixel shader
using (var bytecode = ShaderBytecode.CompileFromFile("triangle.fx", "PShader", "ps_4_0", ShaderFlags.None, EffectFlags.None))
pixelShader = new PixelShader(device, bytecode);
// create test vertex data, making sure to rewind the stream afterward
var vertices = new DataStream(12 * 3, true, true);
vertices.Write(new Vector3(0.0f, 0.5f, 0.5f));
vertices.Write(new Vector3(0.5f, -0.5f, 0.5f));
vertices.Write(new Vector3(-0.5f, -0.5f, 0.5f));
vertices.Position = 0;
//Create the second triangle - offset from the first
var vertices2 = new DataStream(12 * 3, true, true);
vertices2.Write(new Vector3(1.0f, 0.5f, 0.5f));
vertices2.Write(new Vector3(1.5f, -0.5f, 0.5f));
vertices2.Write(new Vector3(0.5f, -0.5f, 0.5f));
vertices2.Position = 0;
// create the vertex layout
var elements = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0) };
var layout = new InputLayout(device, inputSignature, elements);
// Create the vertex buffers
var vertexBuffer = new Buffer(device, vertices, 12 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
var vertexBuffer2 = new Buffer(device, vertices2, 12 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
// Bind the vertex buffer
var vertexBufferBindings = new VertexBufferBinding[2];
vertexBufferBindings[0] = new VertexBufferBinding(vertexBuffer, 12, 0);
vertexBufferBindings[1] = new VertexBufferBinding(vertexBuffer2, 12, 0);
// configure the Input Assembler portion of the pipeline with the vertex data
context.InputAssembler.InputLayout = layout;
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
context.InputAssembler.SetVertexBuffers(0, vertexBufferBindings);
// set the shaders
context.VertexShader.Set(vertexShader);
context.PixelShader.Set(pixelShader);
// prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
using (var factory = swapChain.GetParent<Factory>())
factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAltEnter);
// handle alt+enter ourselves
form.KeyDown += (o, e) =>
{
if (e.Alt && e.KeyCode == Keys.Enter)
swapChain.IsFullScreen = !swapChain.IsFullScreen;
};
// handle form size changes
form.UserResized += (o, e) =>
{
renderTarget.Dispose();
swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
context.OutputMerger.SetTargets(renderTarget);
};
MessagePump.Run(form, () =>
{
// clear the render target to a soothing blue
context.ClearRenderTargetView(renderTarget, new Color4(0.5f, 0.5f, 1.0f));
// draw the triangle
context.Draw(3, 0);
swapChain.Present(0, PresentFlags.None);
});
// clean up all resources
// anything we missed will show up in the debug output
vertices.Close();
vertices2.Close();
vertexBuffer.Dispose();
layout.Dispose();
inputSignature.Dispose();
vertexShader.Dispose();
pixelShader.Dispose();
renderTarget.Dispose();
swapChain.Dispose();
device.Dispose();
}
}
}
答案 0 :(得分:2)
您应该相互独立地设置顶点缓冲区:
//initializing...
var vbb1 = new VertexBufferBinding[] { new VertexBufferBinding(vertexBuffer, 12, 0) };
var vbb2 = new VertexBufferBinding[] { new VertexBufferBinding(vertexBuffer2, 12, 0) };
//in the render loop
context.InputAssembler.SetVertexBuffers(0, vbb1);
context.Draw(3, 0);
context.InputAssembler.SetVertexBuffers(0, vbb2);
context.Draw(3, 0);
在一次绘制调用中需要来自这些缓冲区的数据时,将使用两个顶点缓冲区同时绑定到输入汇编程序。例如。如果你在一个缓冲区中有位置数据而在另一个缓冲区中有颜色数据。
答案 1 :(得分:1)
虽然我不使用SlimDX,但我确实使用了DirectX并且已经做了一段时间了。看一下教程,我想你需要分别绘制每个顶点缓冲区。
所以,像这样:
MessagePump.Run(form, () =>
{
// clear the render target to a soothing blue
context.ClearRenderTargetView(renderTarget, new Color4(0.5f, 0.5f, 1.0f));
// draw the first triangle
context.InputAssembler.SetVertexBuffers(0, vertexBufferBindings[0]);
context.Draw(3, 0);
// draw the second triangle
context.InputAssembler.SetVertexBuffers(0, vertexBufferBindings[1]);
context.Draw(3, 0);
swapChain.Present(0, PresentFlags.None);
});