Esempio n. 1
0
        public void Execute(GraphicsDevice device, RenderPipelineStage stage, Dictionary <string, object> parameters)
        {
            // TODO: implement
            throw new NotImplementedException();

            // set projection to orthographic
            Dictionary <string, IUniformValue> uniforms = stage.GetShaderUniforms(parameters["material"] as string);

            uniforms["projection"] = UniformValue.Create(Matrix4.CreateOrthographic(1f, 1f, -1f, 1f));
            uniforms["modelView"]  = UniformValue.Create(Matrix4.CreateTranslation(0, 0, 0));

            // draw a full screen quad.
            if (_fsQuad == null)
            {
                // create the quad VertexArray.
                VertexBuffer vbo = VertexBuffer.Create(VertexFormat.PositionTexture, new VertexPositionTexture[] {
                    new VertexPositionTexture(new Vector3(0f, 0f, 0f), new Vector2(0.0f, 1.0f)),
                    new VertexPositionTexture(new Vector3(1.0f, 0f, 0f), new Vector2(1.0f, 1.0f)),
                    new VertexPositionTexture(new Vector3(1.0f, 0f, 0f), new Vector2(1.0f, 0.0f)),
                    new VertexPositionTexture(new Vector3(0f, 1.0f, 0f), new Vector2(0.0f, 0.0f))
                });
                IndexBuffer ibo = IndexBuffer.Create(new byte[] { 0, 1, 2, 2, 3, 0 });
                _fsQuad = new VertexArray(vbo, ibo);
            }

            // get material from pipeline cache

            // set materials uniforms

            // draw full screen quad with material
        }
Esempio n. 2
0
        private void DrawTextInternal(BitmapFont font, VertexPositionColorTexture[] vertices, ushort[] indices)
        {
            if (_textVao == null)
            {
                VertexBuffer vbo = new VertexBuffer(VertexFormat.PositionColorTexture, ushort.MaxValue, BufferUsageHint.StreamDraw, BeginMode.Triangles);
                IndexBuffer  ibo = new IndexBuffer(BufferUsageHint.StreamDraw, DrawElementsType.UnsignedShort, 0);
                _textVao = new VertexArray(vbo, ibo);
            }

            Program shader = font.Shader;

            if (_lastTexture == null || font.Texture != _lastTexture)
            {
                Sampler sampler = (Sampler)Sampler.Create().Clone();
                sampler.Texture = font.Texture;
                shader.Bind();
                shader.BindUniforms(new Dictionary <string, IUniformValue>()
                {
                    { "Texture", UniformValue.Create(sampler) }
                });

                _lastTexture = font.Texture;
            }

            _textVao.VertexBuffer.SetData(vertices, true);
            _textVao.IndexBuffer.SetData(indices, true);
            _graphicsDevice.World = Matrix4.Identity;
            _graphicsDevice.DrawVertexArray(_textVao, shader);
        }
Esempio n. 3
0
 public void Bind()
 {
     _shader.Bind();
     _shader.BindUniforms(new Dictionary <string, IUniformValue>()
     {
         { "Texture", UniformValue.Create(Sampler.Create(_texture)) }
     });
 }
Esempio n. 4
0
        public bool GetUniformValue(UniformUsage usage, out IUniformValue value)
        {
            value = null;
            if (usage == UniformUsage.World)
            {
                value = UniformValue.Create(World);
            }
            else if (usage == UniformUsage.WorldView)
            {
                if (_camera != null)
                {
                    value = UniformValue.Create(Matrix4.Mult(World, _camera.View));
                }
                else
                {
                    value = UniformValue.Create(World);
                }
            }
            else if (usage == UniformUsage.WorldViewProj)
            {
                if (_camera != null)
                {
                    value = UniformValue.Create(Matrix4.Mult(World, _viewProjection));
                }
                else
                {
                    value = UniformValue.Create(World);
                }
            }
            else if (usage == UniformUsage.ViewPort)
            {
                if (_camera != null)
                {
                    value = UniformValue.Create(_camera.Viewport.GetAsVector4());
                }
                else
                {
                    value = UniformValue.Create(new Vector4(0, 0, Width, Height));
                }
            }
            else if (usage == UniformUsage.NearFar)
            {
                if (_camera != null)
                {
                    value = UniformValue.Create(new Vector2(_camera.Near, _camera.Far));
                }
                else
                {
                    value = UniformValue.Create(new Vector2(1f, 100f));
                }
            }

            return(value != null);
        }
Esempio n. 5
0
        private void Setup()
        {
            _graphicsDevice.RenderState = _renderState;
            Viewport vp = _graphicsDevice.ViewPort;

            Matrix4 projection = Matrix4.CreateOrthographicOffCenter(0, vp.Width, vp.Height, 0, 0, 1);

            projection = Matrix4.Mult(_matrix, projection);

            _shader.Bind();
            _shaderUniforms["MatrixTransform"] = UniformValue.Create(projection);
            _shader.BindUniforms(_shaderUniforms);
        }
Esempio n. 6
0
 public void Execute(GraphicsDevice device, RenderPipelineStage stage, Dictionary<string, object> parameters)
 {
     float[] values = parameters["value"].ToString().Split(new char[] { ',' }).Select(f => float.Parse(f)).ToArray();
       IUniformValue value;
       if(values.Length == 1)
     value = UniformValue.Create(values[0]);
       else if(values.Length == 2)
     value = UniformValue.Create(values[0], values[1]);
       else if(values.Length == 3)
     value = UniformValue.Create(values[0], values[1], values[2]);
       else if(values.Length == 4)
     value = UniformValue.Create(values[0], values[1], values[2], values[3]);
       else
     throw new ArgumentException("value parameter must contain 1 - 4 float values.");
       stage.AddShaderUniform(parameters["material"].ToString(), parameters["uniform"].ToString(), value);
 }
Esempio n. 7
0
        public void DrawBatch(Comparison <SpriteBatchItem> sortFunction, Program shader, Sampler sampler)
        {
            if (_batchItems.Count == 0)
            {
                return; // no batch items to draw.
            }
            // sort the batch items.
            _batchItems.Sort(sortFunction);
            _batchItems.Sort(sortFunction);
            // for some reson the above sort makes no change
            //List<SpriteBatchItem> batches = _batchItems;
            //batches.Sort(sortFunction);

            // Determine how many iterations through the drawing code we need to make.
            int batchIndex = 0;
            int batchCount = _batchItems.Count;

            // Iterate through the batches, doing short.MaxValue sets of verticies at a time.
            while (batchCount > 0)
            {
                int       verticesIndex = 0;
                int       indicesIndex  = 0;
                Texture2D texture       = null;

                int numBatchesToProcess = batchCount;
                if (numBatchesToProcess > MaxBatchSize)
                {
                    numBatchesToProcess = MaxBatchSize;
                }
                EnsureArrayCapacity(numBatchesToProcess);

                // draw the batches.
                for (int i = 0; i < numBatchesToProcess; i++, batchIndex++)
                {
                    SpriteBatchItem item = _batchItems[batchIndex];
                    //SpriteBatchItem item = batches[batchIndex];
                    // if the texture changed, we need to draw the current vertecies and bind the new texture.
                    if (!ReferenceEquals(item.Texture, texture))
                    {
                        FlushVertexArray(indicesIndex, verticesIndex, shader);

                        texture       = item.Texture;
                        verticesIndex = 0;
                        indicesIndex  = 0;
                        shader.Bind();
                        //sampler.Texture = texture;
                        Sampler s = (Sampler)sampler.Clone();
                        s.Texture = texture;
                        _shaderUniforms["Texture"] = UniformValue.Create(s);
                        shader.BindUniforms(_shaderUniforms);
                        //Sampler s = (Sampler)sampler.Clone();
                        //s.Texture = texture;
                        //shader.BindUniforms(new Dictionary<string, IUniformValue>()
                        //{
                        //  {"Texture", UniformValue.Create(s)}
                        //});
                        shader.Unbind();
                        //_device.Textures[0] = texture;
                    }

                    Vector4  color = new Vector4(item.Color.R, item.Color.G, item.Color.B, item.Color.A);
                    ushort[] itemIndices;
                    VertexPositionColorTexture[] itemVertieces = item.Sprite.GetVertices(item.X, item.Y, item.Width, item.Height, item.Depth, item.Color, out itemIndices);

                    if (item.SpriteEffect != SpriteEffect.None)
                    {
                        for (int j = 0; j < itemVertieces.Length; j++)
                        {
                            Vector2 tmp = itemVertieces[j].TextureCoordinate;
                            if (item.SpriteEffect == SpriteEffect.FlipVertically)
                            {
                                tmp.Y = 1.0f - tmp.Y;
                            }
                            if (item.SpriteEffect == SpriteEffect.FlipHorizontally)
                            {
                                tmp.X = 1.0f - tmp.X;
                            }
                            itemVertieces[j].TextureCoordinate = tmp;
                        }
                    }

                    for (int j = 0; j < itemIndices.Length; j++)
                    {
                        itemIndices[j] = (ushort)(itemIndices[j] + verticesIndex);
                    }
                    Array.Copy(itemIndices, 0, _index, indicesIndex, itemIndices.Length);
                    indicesIndex += itemIndices.Length;
                    Array.Copy(itemVertieces, 0, _vertexArray, verticesIndex, itemVertieces.Length);
                    verticesIndex += itemVertieces.Length;

                    // release the texture and return the item to the queue.
                    item.Sprite = null;
                    _freeBatchItems.Enqueue(item);
                }

                // flush the remaining vertex array
                FlushVertexArray(indicesIndex, verticesIndex, shader);

                // update the batch count to continue the process with larger batches
                batchCount -= numBatchesToProcess;
            }
            _batchItems.Clear();
        }