Exemplo n.º 1
0
 public IObservable <TVertex[]> Process <TVertex>(IObservable <TVertex[]> source) where TVertex : struct
 {
     return(Observable.Defer(() =>
     {
         Mesh mesh = null;
         return source.CombineEither(
             ShaderManager.ReserveMaterial(ShaderName).Do(material =>
         {
             material.Update(() =>
             {
                 mesh = new Mesh();
                 VertexHelper.BindVertexAttributes(
                     mesh.VertexBuffer,
                     mesh.VertexArray,
                     BlittableValueType <TVertex> .Stride,
                     vertexAttributes);
             });
         }),
             (input, material) =>
         {
             material.Update(() =>
             {
                 mesh.DrawMode = DrawMode;
                 mesh.VertexCount = VertexHelper.UpdateVertexBuffer(mesh.VertexBuffer, input, Usage);
                 mesh.Draw();
             });
             return input;
         });
     }));
 }
Exemplo n.º 2
0
        public override IObservable <Mat> Process(IObservable <Mat> source)
        {
            return(Observable.Defer(() =>
            {
                Mesh mesh = null;
                return source.CombineEither(
                    ShaderManager.ReserveMaterial(ShaderName),
                    (input, material) =>
                {
                    material.Update(() =>
                    {
                        if (mesh == null)
                        {
                            mesh = new Mesh();
                            VertexHelper.BindVertexAttributes(
                                mesh.VertexBuffer,
                                mesh.VertexArray,
                                input.Cols * input.ElementSize,
                                vertexAttributes);
                        }

                        mesh.DrawMode = DrawMode;
                        mesh.VertexCount = VertexHelper.UpdateVertexBuffer(mesh.VertexBuffer, input, Usage);
                        mesh.Draw();
                    });
                    return input;
                }).Finally(() =>
                {
                    if (mesh != null)
                    {
                        mesh.Dispose();
                    }
                });
            }));
        }
Exemplo n.º 3
0
 IObservable <Mesh> Process <TVertex, TIndex>(IObservable <Tuple <TVertex[], TIndex[]> > source, DrawElementsType elementType)
     where TVertex : struct
     where TIndex : struct
 {
     return(source.Select(input =>
     {
         var mesh = new Mesh();
         var indices = input.Item2;
         VertexHelper.BindVertexAttributes(
             mesh.VertexBuffer,
             mesh.VertexArray,
             BlittableValueType <TVertex> .Stride,
             vertexAttributes);
         mesh.EnsureElementArray();
         mesh.DrawMode = DrawMode;
         mesh.VertexCount = indices.Length;
         mesh.ElementArrayType = elementType;
         VertexHelper.UpdateVertexBuffer(mesh.VertexBuffer, input.Item1, Usage);
         GL.BindBuffer(BufferTarget.ElementArrayBuffer, mesh.ElementArray);
         GL.BufferData(BufferTarget.ElementArrayBuffer,
                       new IntPtr(indices.Length * BlittableValueType <TIndex> .Stride),
                       indices,
                       Usage);
         GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
         return mesh;
     }));
 }
Exemplo n.º 4
0
        public override IObservable <Mat> Process(IObservable <Mat> source)
        {
            return(Observable.Create <Mat>(observer =>
            {
                var name = MeshName;
                if (string.IsNullOrEmpty(name))
                {
                    throw new InvalidOperationException("A mesh name must be specified.");
                }

                MeshInstanced instance = null;
                return source.CombineEither(
                    ShaderManager.ReserveMaterial(ShaderName),
                    (input, material) =>
                {
                    material.Update(() =>
                    {
                        if (instance == null && input != null)
                        {
                            try
                            {
                                var mesh = material.Window.ResourceManager.Load <Mesh>(name);
                                instance = new MeshInstanced(mesh);
                                BindInstanceAttributes(
                                    instance,
                                    input.Cols * input.ElementSize,
                                    instanceAttributes);
                            }
                            catch (Exception ex)
                            {
                                observer.OnError(ex);
                                return;
                            }
                        }

                        if (instance == null)
                        {
                            return;
                        }
                        if (input != null)
                        {
                            instance.InstanceCount = VertexHelper.UpdateVertexBuffer(instance.VertexBuffer, input, Usage);
                        }
                        instance.Draw();
                    });
                    return input;
                }).Finally(() =>
                {
                    if (instance != null)
                    {
                        instance.Dispose();
                    }
                }).SubscribeSafe(observer);
            }));
        }
Exemplo n.º 5
0
        IObservable <Tuple <TVertex[], TIndex[]> > Process <TVertex, TIndex>(IObservable <Tuple <TVertex[], TIndex[]> > source, DrawElementsType elementType)
            where TVertex : struct
            where TIndex : struct
        {
            return(Observable.Create <Tuple <TVertex[], TIndex[]> >(observer =>
            {
                var name = MeshName;
                if (string.IsNullOrEmpty(name))
                {
                    throw new InvalidOperationException("A mesh name must be specified.");
                }

                Mesh mesh = null;
                return source.CombineEither(
                    ShaderManager.WindowSource.Do(window =>
                {
                    window.Update(() =>
                    {
                        try
                        {
                            mesh = window.ResourceManager.Load <Mesh>(name);
                            VertexHelper.BindVertexAttributes(
                                mesh.VertexBuffer,
                                mesh.VertexArray,
                                BlittableValueType <TVertex> .Stride,
                                vertexAttributes);
                        }
                        catch (Exception ex) { observer.OnError(ex); }
                    });
                }),
                    (input, window) =>
                {
                    window.Update(() =>
                    {
                        var indices = input.Item2;
                        mesh.EnsureElementArray();
                        mesh.DrawMode = DrawMode;
                        mesh.VertexCount = indices.Length;
                        mesh.ElementArrayType = elementType;
                        VertexHelper.UpdateVertexBuffer(mesh.VertexBuffer, input.Item1, Usage);
                        GL.BindBuffer(BufferTarget.ElementArrayBuffer, mesh.ElementArray);
                        GL.BufferData(BufferTarget.ElementArrayBuffer,
                                      new IntPtr(indices.Length * BlittableValueType <TIndex> .Stride),
                                      indices,
                                      Usage);
                        GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
                    });
                    return input;
                }).SubscribeSafe(observer);
            }));
        }
Exemplo n.º 6
0
 public IObservable <Mesh> Process <TVertex>(IObservable <TVertex[]> source) where TVertex : struct
 {
     return(source.Select(input =>
     {
         var mesh = new Mesh();
         VertexHelper.BindVertexAttributes(
             mesh.VertexBuffer,
             mesh.VertexArray,
             BlittableValueType <TVertex> .Stride,
             vertexAttributes);
         mesh.DrawMode = DrawMode;
         mesh.VertexCount = VertexHelper.UpdateVertexBuffer(mesh.VertexBuffer, input, Usage);
         return mesh;
     }));
 }
Exemplo n.º 7
0
 public IObservable <Mesh> Process(IObservable <Mat> source)
 {
     return(source.Select(input =>
     {
         var mesh = new Mesh();
         VertexHelper.BindVertexAttributes(
             mesh.VertexBuffer,
             mesh.VertexArray,
             input.Cols * input.ElementSize,
             vertexAttributes);
         mesh.DrawMode = DrawMode;
         mesh.VertexCount = VertexHelper.UpdateVertexBuffer(mesh.VertexBuffer, input, Usage);
         return mesh;
     }));
 }
Exemplo n.º 8
0
        public override IObservable <Mat> Process(IObservable <Mat> source)
        {
            return(Observable.Create <Mat>(observer =>
            {
                var name = MeshName;
                if (string.IsNullOrEmpty(name))
                {
                    throw new InvalidOperationException("A mesh name must be specified.");
                }

                Mesh mesh = null;
                return source.CombineEither(
                    ShaderManager.WindowSource,
                    (input, window) =>
                {
                    window.Update(() =>
                    {
                        if (mesh == null)
                        {
                            try
                            {
                                mesh = window.ResourceManager.Load <Mesh>(name);
                                VertexHelper.BindVertexAttributes(
                                    mesh.VertexBuffer,
                                    mesh.VertexArray,
                                    input.Cols * input.ElementSize,
                                    vertexAttributes);
                            }
                            catch (Exception ex)
                            {
                                observer.OnError(ex);
                                return;
                            }
                        }

                        mesh.DrawMode = DrawMode;
                        mesh.VertexCount = VertexHelper.UpdateVertexBuffer(mesh.VertexBuffer, input, Usage);
                    });
                    return input;
                }).SubscribeSafe(observer);
            }));
        }
Exemplo n.º 9
0
        public IObservable <TVertex[]> Process <TVertex>(IObservable <TVertex[]> source) where TVertex : struct
        {
            return(Observable.Create <TVertex[]>(observer =>
            {
                var name = MeshName;
                if (string.IsNullOrEmpty(name))
                {
                    throw new InvalidOperationException("A mesh name must be specified.");
                }

                Mesh mesh = null;
                return source.CombineEither(
                    ShaderManager.WindowSource.Do(window =>
                {
                    window.Update(() =>
                    {
                        try
                        {
                            mesh = window.ResourceManager.Load <Mesh>(name);
                            VertexHelper.BindVertexAttributes(
                                mesh.VertexBuffer,
                                mesh.VertexArray,
                                BlittableValueType <TVertex> .Stride,
                                vertexAttributes);
                        }
                        catch (Exception ex) { observer.OnError(ex); }
                    });
                }),
                    (input, window) =>
                {
                    window.Update(() =>
                    {
                        mesh.DrawMode = DrawMode;
                        mesh.VertexCount = VertexHelper.UpdateVertexBuffer(mesh.VertexBuffer, input, Usage);
                    });
                    return input;
                }).SubscribeSafe(observer);
            }));
        }