public override void Update() { const float left = 0; const float top = 0; float right = Size.X; float bottom = Size.Y; Vector4 color = _color.ToVector4(); _vertices[0] = new VertexDefinition.PositionColor { position = new Vector3(left, top, 0), color = color }; _vertices[1] = new VertexDefinition.PositionColor { position = new Vector3(right, bottom, 0), color = color }; _vertices[2] = new VertexDefinition.PositionColor { position = new Vector3(left, bottom, 0), color = color }; _vertices[3] = new VertexDefinition.PositionColor { position = new Vector3(right, top, 0), color = color }; DataStream mappedResource; DeviceContext.MapSubresource(VertexBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); mappedResource.WriteRange(_vertices); DeviceContext.UnmapSubresource(VertexBuffer, 0); }
public void Render(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix) { if (_changed) { float left = (float)((ScreenSize.X / 2) * -1) + (float)Position.X; float right = left + (float)Size.X; float top = (float)(ScreenSize.Y / 2) - (float)Position.Y; float bottom = top - (float)Size.Y; _vertices[0] = new VertexDefinition.PositionColor { position = new Vector3(left, top, Depth), color = _color }; _vertices[1] = new VertexDefinition.PositionColor { position = new Vector3(right, bottom, Depth), color = _color }; _vertices[2] = new VertexDefinition.PositionColor { position = new Vector3(left, bottom, Depth), color = _color }; _vertices[3] = new VertexDefinition.PositionColor { position = new Vector3(right, top, Depth), color = _color }; DataStream mappedResource; deviceContext.MapSubresource(_vertexBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out mappedResource); mappedResource.WriteRange(_vertices); deviceContext.UnmapSubresource(_vertexBuffer, 0); _changed = false; } // Set vertex buffer stride and offset. int stride = Utilities.SizeOf <VertexDefinition.PositionColor>(); //Gets or sets the stride between vertex elements in the buffer (in bytes). deviceContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(_vertexBuffer, stride, 0)); deviceContext.InputAssembler.SetIndexBuffer(_indexBuffer, Format.R32_UInt, 0); deviceContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; _shader.Render(deviceContext, _indexCount, worldMatrix, viewMatrix, projectionMatrix); }