into one file. Let's organize the code some more by creating a Class for the Triangle. I added a Folder
for Renderables, where I will place all further Classes, that are responsible for holding the resources and methods to render things.
First let's copy and paste the code for rendering the triangle into the contructor of the Triangle class a method called render. After adding the needed using directices the Triangle class looks like this:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using SlimDX.D3DCompiler; using SlimDX; using SlimDX.Direct3D11; using SlimDX.DXGI; namespace Apparat.Renderables { class Triangle { ShaderSignature inputSignature; VertexShader vertexShader; PixelShader pixelShader; public Triangle() { #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 } public void render() { } public void dispose() { pixelShader.Dispose(); vertexShader.Dispose(); inputSignature.Dispose(); } } }
Now we move all the code that configures the input assembler and sets the shaders to the render method.
If we have more objects, shaders have to be set for every object on every render call.
Further the calls to context and device have to be adjusted, because those are member variables of the DeviceManager.
So for example
context.VertexShader.Set(vertexShader);becomes
DeviceManager.Instance.context.VertexShader.Set(vertexShader);
Finally we have to create an object of the triangle and call its render method in the render loop.
public void renderScene() { Renderables.Triangle triangle = new Renderables.Triangle(); while (true) { DeviceManager dm = DeviceManager.Instance; dm.context.ClearRenderTargetView(dm.renderTarget, new Color4(0.25f, 0.75f, 0.25f)); triangle.render(); dm.swapChain.Present(0, PresentFlags.None); } }
You can download the source code here: http://apparat.codeplex.com/SourceControl/changeset/changes/3882dce2a134
No comments:
Post a Comment