///////////////////////////////////////////////////////////////////////////////// // Ctor & Dtor public Entity(EntityDescription desc) { Device = desc.Device; Context = desc.Context; PrimitiveTopology = desc.PrimitiveTopology; VertexShader = desc.VertexShader; VertexShaderSignature = desc.VSInputSignature; PixelShader = desc.PixelShader; ResourceProviders = desc.PSResourceProviders; }
public void Prepare() { var context = _device.ImmediateContext; // setup resources _providers.Add("at1", new TestTextureProvider(_device, "res/alphatest.png")); _providers.Add("bg1", new TestTextureProvider(_device, "res/xLayer.png")); _providers.Add("wb1", new WebTexture(_device, "http://www.libpng.org/pub/png/pngintro.html".ToUri())); _providers.Add("yt1", new WebTexture(_device, "http://www.youtube.com/watch?v=KxWBv4y3m4w".ToUri())); _providers.Add("yt2", new WebTexture(_device, "http://www.youtube.com/watch?v=0EurfDnlD5A".ToUri())); _providers.Add("yt3", new WebTexture(_device, "http://www.youtube.com/watch?v=qdwUkYrHosk".ToUri())); _providers.Add("yt4", new WebTexture(_device, "http://www.youtube.com/watch?v=9w5RHGBvf9A".ToUri())); // descriptor easifies the creation of entities, e.g. rectangles. EntityDescription entityDescription = new EntityDescription { Device = _device, Context = _device.ImmediateContext, VertexShader = _vertexShader, VSInputSignature = _vsInputSignature, PrimitiveTopology = PrimitiveTopology.TriangleStrip }; entityDescription.PixelShader = _pixelShaders["Simple"]; entityDescription.PSResourceProviders = new ResourceProvider[] { _providers["bg1"] }; var rect1 = new Rectangle(new Vector2(0f, 0f), new Vector2(1f, 1f), entityDescription); entityDescription.PSResourceProviders = new ResourceProvider[] { _providers["yt1"] }; var rect2 = new Rectangle(new Vector2(0f, 0f), new Vector2(.5f, .5f), entityDescription); entityDescription.PixelShader = _pixelShaders["AlphaMap"]; entityDescription.PSResourceProviders = new ResourceProvider[] { _providers["yt2"], _providers["at1"] }; var rect3 = new Rectangle(new Vector2(0, .5f), new Vector2(.5f, .5f), entityDescription); entityDescription.PSResourceProviders = new ResourceProvider[] { _providers["yt3"], _providers["wb1"] }; var rect4 = new Rectangle(new Vector2(.5f, 0), new Vector2(.5f, .5f), entityDescription); entityDescription.PixelShader = _pixelShaders["GrayscaleAlphaMap"]; entityDescription.PSResourceProviders = new ResourceProvider[] { _providers["yt4"], _providers["yt2"] }; var rect5 = new Rectangle(new Vector2(.5f, .5f), new Vector2(.5f, .5f), entityDescription); _rectangles.Add(rect1); _rectangles.Add(rect2); _rectangles.Add(rect3); _rectangles.Add(rect4); _rectangles.Add(rect5); }
///////////////////////////////////////////////////////////////////////////////// // CONSTRUCTOR & DESTRUCTOR /// <summary> /// TODO doc /// </summary> /// <param name="origin"></param> /// <param name="span"></param> /// <param name="device"></param> /// <param name="context"></param> /// <param name="vertexShader"></param> /// <param name="vsSignature"></param> /// <param name="pixelShader"></param> /// <param name="resourceProviders"></param> public Rectangle( Vector2 origin, Vector2 span, EntityDescription desc) : base(desc) { // create rect vertices int vertexLength = Vector3.SizeInBytes; // should be 12: 3 * 4 bytes int uvCoordLength = Vector2.SizeInBytes; // should be 8: 2 * 4 bytes DataStream data = new DataStream((vertexLength + uvCoordLength) * 4, true, true); origin.X = origin.X * 2 - 1; origin.Y = 1 - origin.Y * 2; float originPlusSpanX = origin.X + span.X * 2; float originPlusSpanY = origin.Y - span.Y * 2; // write 4 times: position vector & uv-coord vector ... data.Write(new Vector3(origin.X, origin.Y, 0)); // vertex data.Write(new Vector2(0, 0)); // UV coord data.Write(new Vector3(originPlusSpanX, origin.Y, 0)); // vertex data.Write(new Vector2(1, 0)); // UV coord data.Write(new Vector3(origin.X, originPlusSpanY, 0)); // ... data.Write(new Vector2(0, 1)); data.Write(new Vector3(originPlusSpanX, originPlusSpanY, 0)); data.Write(new Vector2(1, 1)); data.Position = 0; // create the vertex layout and buffer var elements = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0), new InputElement("TEXTCOORD", 0, Format.R32G32_Float, vertexLength, 0) }; InputLayout = new InputLayout(Device, VertexShaderSignature, elements); VertexBuffer = new D3DBuffer(Device, data, (int)data.Length, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0); VertexBufferBinding = new VertexBufferBinding(VertexBuffer, vertexLength + uvCoordLength, 0); data.Close(); data.Dispose(); }