コード例 #1
0
ファイル: Renderer.cs プロジェクト: dzamkov/DUIP
        /// <summary>
        /// Gets a procedure that renders the given figure with the given color modulation.
        /// </summary>
        public Procedure GetProcedure(Figure Figure, Environment Environment)
        {
            // See if there is a stored procedure for it
            Procedure res;
            if(this._Procedures.TryGetValue(Figure, out res))
            {
                return res;
            }

            // Hint figure
            HintFigure hint = Figure as HintFigure;
            if (hint != null)
            {
                CacheHintFigure cachehint = hint as CacheHintFigure;
                if (cachehint != null)
                {
                    IRenderCache cache = this._Cache;

                    Disposable<InStream> cacheread = cache.Read(cachehint.Name);
                    if (!cacheread.IsNull)
                    {
                        Environment.CacheRead = cacheread;
                        Procedure result = this.GetProcedure(hint.Source, Environment);
                        Environment.CacheRead = null;
                        cacheread.Dispose();
                        return result;
                    }

                    Disposable<OutStream> cachewrite = cache.Update(cachehint.Name);
                    if (!cachewrite.IsNull)
                    {
                        Environment.CacheWrite = cachewrite;
                        Procedure result = this.GetProcedure(hint.Source, Environment);
                        Environment.CacheWrite = null;
                        cachewrite.Dispose();
                        return result;
                    }
                }

                return this.GetProcedure(hint.Source, Environment);
            }

            // Translated figure
            TranslatedFigure translated = Figure as TranslatedFigure;
            if (translated != null)
            {
                return new ProjectionProcedure(View.Translation(translated.Offset), this.GetProcedure(translated.Source, Environment));
            }

            // Scaled figure
            ScaledFigure scaled = Figure as ScaledFigure;
            if (scaled != null)
            {
                return new ProjectionProcedure(View.Scale(scaled.Factor), this.GetProcedure(scaled.Source, Environment));
            }

            // Rotated figure
            RotatedFigure rotated = Figure as RotatedFigure;
            if (rotated != null)
            {
                return new ProjectionProcedure(View.Rotation(rotated.Angle), this.GetProcedure(rotated.Source, Environment));
            }

            // Projected figure
            ProjectedFigure projected = Figure as ProjectedFigure;
            if (projected != null)
            {
                return new ProjectionProcedure(projected.Projection, this.GetProcedure(projected.Source, Environment));
            }

            // Modulated figure
            ModulatedFigure modulated = Figure as ModulatedFigure;
            if (modulated != null)
            {
                return new ModulateProcedure(modulated.Modulation, this.GetProcedure(modulated.Source, Environment));
            }

            // Superimposed figure
            SuperimposedFigure superimposed = Figure as SuperimposedFigure;
            if (superimposed != null)
            {
                return this.GetProcedure(superimposed.Under, Environment) + this.GetProcedure(superimposed.Over, Environment);
            }

            // Compound figure
            CompoundFigure compound = Figure as CompoundFigure;
            if (compound != null)
            {
                Procedure[] components = new Procedure[compound.Components.Count()];
                int i = 0;
                foreach (Figure component in compound.Components)
                {
                    components[i++] = this.GetProcedure(component, Environment);
                }
                return new CompoundProcedure(components);
            }

            // Shape figure
            ShapeFigure shape = Figure as ShapeFigure;
            if (shape != null)
            {
                return this.GetShapeProcedure(shape.Shape, shape.Source, Environment);
            }

            // Mesh figure
            MeshFigure mesh = Figure as MeshFigure;
            if (mesh != null)
            {
                return
                    BindTextureProcedure.Null +
                    SetColorProcedure.White +
                    new RenderGeometryProcedure(BeginMode.Triangles, new MeshGeometry(mesh));
            }

            // System font glyph
            SystemFontGlyph sfg = Figure as SystemFontGlyph;
            if (sfg != null)
            {
                SystemFont font = sfg.Font;
                BitmapTypeface typeface = font.Typeface as BitmapTypeface;
                if (typeface != null)
                {
                    Texture tex;
                    Rectangle src;
                    Rectangle dst;
                    typeface.GetGlyph(sfg.Character).GetRenderInfo(font.Size, out tex, out src, out dst);
                    return
                        new BindTextureProcedure(tex) +
                        new SetColorProcedure(font.Color) +
                        new RenderGeometryProcedure(BeginMode.Quads, new BufferGeometry(
                            new Point[] { dst.TopLeft, dst.TopRight, dst.BottomRight, dst.BottomLeft },
                            null,
                            new Point[] { src.TopLeft, src.TopRight, src.BottomRight, src.BottomLeft }));
                }
            }

            // Sampled figure
            SampledFigure sampled = Figure as SampledFigure;
            if(sampled != null)
            {
                if(sampled.Tiled)
                {
                    int size = 256;
                    byte[] data = new byte[size * size * 4];
                    Texture tex;
                    unsafe
                    {
                        fixed (byte* ptr = data)
                        {
                            if (Environment.CacheRead != null)
                            {
                                Environment.CacheRead.Read(data, 0, data.Length);
                            }
                            else
                            {
                                Texture.WriteImageARGB(sampled, new View(Rectangle.UnitSquare), size, size, ptr);
                                if (Environment.CacheWrite != null)
                                {
                                    Environment.CacheWrite.Write(data, 0, data.Length);
                                }
                            }
                            tex = Texture.Create();
                            Texture.SetImage(Texture.Format.BGRA32, 0, size, size, ptr);
                            Texture.GenerateMipmap();
                            Texture.SetFilterMode(TextureMinFilter.LinearMipmapLinear, TextureMagFilter.Linear);
                        }
                    }
                    Procedure proc =
                        new BindTextureProcedure(tex) +
                        SetColorProcedure.White +
                        RenderViewProcedure.Singleton;
                    this._Procedures[sampled] = proc;
                    return proc;
                }
            }

            // No rendering method available
            throw new NotImplementedException();
        }
コード例 #2
0
ファイル: Procedure.cs プロジェクト: dzamkov/DUIP
 public CompositeProcedure(Procedure First, Procedure Second)
 {
     this.First = First;
     this.Second = Second;
 }
コード例 #3
0
ファイル: Procedure.cs プロジェクト: dzamkov/DUIP
 public ProjectionProcedure(View Projection, Procedure Inner)
 {
     this.Inner = Inner;
     this.Projection = Projection;
 }
コード例 #4
0
ファイル: Procedure.cs プロジェクト: dzamkov/DUIP
 public ModulateProcedure(Color Modulation, Procedure Inner)
 {
     this.Modulation = Modulation;
     this.Inner = Inner;
 }
コード例 #5
0
ファイル: Procedure.cs プロジェクト: dzamkov/DUIP
 public DisjunctiveProcedure(Procedure A, Procedure B)
 {
     this.A = A;
     this.B = B;
 }
コード例 #6
0
ファイル: Procedure.cs プロジェクト: dzamkov/DUIP
 public CompoundProcedure(Procedure[] Components)
 {
     this.Components = Components;
 }