gpt4 book ai didi

c# - SlimDX : How to draw multiple objects?

转载 作者:行者123 更新时间:2023-11-30 22:11:19 25 4
gpt4 key购买 nike

好的 - 我是 SlimDX、DirectX 和一般图形编程的新手,我需要加快速度。

我正在尝试开发一个原型(prototype)应用程序来可视化和调试我最终将在非图形应用程序中使用的数据。

我已经学习了三个 SlimDX 教程,并且开始掌握所使用的方法,但是我遇到了障碍,因为我不知道如何绘制多个对象。 ...

SimpleTriangle 示例展示了如何绘制单个三角形。我正在尝试扩展该示例以绘制两个三角形。我现在对使用实例等不感兴趣,因为通常我的应用程序中的对象都是不同的——我在这里只使用两个三角形,因为修改现有的顶点数据很容易....

我在这里包含了示例的修改源,我希望有人能帮助我克服这个基本障碍。

我所做的是,

1) 创建第二个顶点数据数组(将第一个三角形沿 x 轴偏移一个单位)2)创建第二个缓冲区并用我的新数组中的顶点数据填充它3) 创建绑定(bind)顶点数组的 VertexBufferBindings4)将两个VertexBufferBdings放入数组中5) 将 VertexBufferBinding 数组发送到 Input Assembler6) 绘制

我在屏幕上看到的只是第一个三角形。如果我切换三角形的顺序,我确实会看到偏移三角形。我只是不能同时看到两者....

这是代码。

如果有任何帮助,我将不胜感激......

道格

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();
}
}
}

最佳答案

您应该设置彼此独立的顶点缓冲区:

//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);

当您在一次绘制调用中需要来自这些缓冲区的数据时,可以同时将两个顶点缓冲区绑定(bind)到输入汇编程序。例如。如果您在一个缓冲区中有位置数据而在另一个缓冲区中有颜色数据。

关于c# - SlimDX : How to draw multiple objects?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20305336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com