Formeln

Thursday, May 17, 2012

Rendering a Triangle

Now let's integrate the last of the SlimDX tutorials into our Engine. It is called
Direct3D11 - SimpleTriangle. I will start by hardcoding the relevant code from
the SlimDX tutorial into the Engine. I will not go into any detail about shaders, vertices and
the render pipeline in this post.

Take the following code and copy-paste it into the DeviceManager Class of our Engine
just behind the line


context.Rasterizer.SetViewports(viewport);


#region shader and triangle


// 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 vertex layout and buffer
var elements = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0) };
var layout = new InputLayout(device, inputSignature, elements);
var vertexBuffer = new SlimDX.Direct3D11.Buffer(device, vertices, 12 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 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, new VertexBufferBinding(vertexBuffer, 12, 0));

// set the shaders
context.VertexShader.Set(vertexShader);
context.PixelShader.Set(pixelShader);

#endregion

Add this using directive to your usings:


using SlimDX.D3DCompiler;

Add the following declarations to the member variables of the DeviceManager Class:


ShaderSignature inputSignature;
VertexShader vertexShader;
PixelShader pixelShader;

And add the following lines at the beginning of our shutDown Method in our DeviceManager Class:


pixelShader.Dispose();
vertexShader.Dispose();
inputSignature.Dispose();

Now add a new Element to our Engine Class Library by right-clicking on it and select Add->New Item.
Type "triangle.fx" as name and create the element.

Open the file in your Solution Explorer, delete all content and copy-paste the following code:


float4 VShader(float4 position : POSITION) : SV_POSITION
{
 return position;
}

float4 PShader(float4 position : SV_POSITION) : SV_Target
{
 return float4(1.0f, 1.0f, 0.0f, 1.0f);
}

Right-click on the file "triangle.fx" in your Solution Explorer and click on Properies.
Select "Copy to Output Directory" and select "copy always".

Change the code in the RenderManager Class in the renderScene Method and call
dm.context.Draw(3,0) :


public void renderScene()
{
  while (true)
  {
    DeviceManager dm = DeviceManager.Instance;
    dm.context.ClearRenderTargetView(dm.renderTarget, new Color4(0.25f, 0.75f, 0.25f));
    dm.context.Draw(3, 0);
    dm.swapChain.Present(0, PresentFlags.None);
  }
}

After compiling the Class Library you can recompile your Main Project and should get the following result:


You can download the Project here: http://apparat.codeplex.com/SourceControl/changeset/changes/a13132898eaa

No comments:

Post a Comment