Example #1
0
 public void Render(SpriteBatch spriteBatch, GraphicsDevice Device)
 {
     Device.SetRenderTarget(this.rt2d);
     Device.Clear(Color.Transparent);
     spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, null, null);
     foreach (Enemy x in this.lxEnemies)
     {
         x.xRenderComponent.Render(spriteBatch);
     }
     foreach (RenderComponent x2 in this.lxRenderComponents)
     {
         x2.Render(spriteBatch);
     }
     spriteBatch.End();
     Device.SetRenderTarget(null);
     if (this.iFramesToRender > 0 && this.iFramesRendered < this.iFramesToRender && this.iCounter % this.iRenderStep == 0)
     {
         if (!Directory.Exists("../Slashas"))
         {
             Directory.CreateDirectory("../Slashas");
         }
         FileStream mos = new FileStream("../Slashas/0" + this.iFramesRendered + ".png", FileMode.Create, FileAccess.Write);
         this.rt2d.SaveAsPng(mos, this.rt2d.Width, this.rt2d.Height);
         mos.Close();
         this.iFramesRendered++;
         if (this.iFramesRendered == this.iFramesToRender)
         {
             this.iFramesToRender = 0;
         }
     }
 }
Example #2
0
        protected override void Initialize()
        {
            graphics.IsFullScreen = false;
            graphics.PreferredBackBufferWidth = width;
            graphics.PreferredBackBufferHeight = height;
            graphics.ApplyChanges();
            Window.Title = "Exercise I.7";

            this.IsMouseVisible = true;

            spriteBatch = new SpriteBatch(GraphicsDevice);
            device = graphics.GraphicsDevice;
            rt = new RenderTarget2D(GraphicsDevice,
                width,
                height,
                false,
                device.PresentationParameters.BackBufferFormat,
                DepthFormat.Depth24,
                0,
                RenderTargetUsage.PreserveContents);

            device.SetRenderTarget(rt);
            device.Clear(Color.Black);
            device.SetRenderTarget(null);

            Drawing.init(device, spriteBatch);
            Stats.init();

            w = new Walker(width, height);

            base.Initialize();
        }
Example #3
0
        public void addarea(int left, int top, int width, int height,GraphicsDevice device,SpriteBatch batch)
        {
            for (int I = left; I < left + width; I++)
            {
                for (int J = top; J < top + height; J++)
                {
                    tilearray[I, J] = new tile();
                }
            }

            RenderTarget2D target = new RenderTarget2D(device, 4096, 4096);
            device.SetRenderTarget(target);
            device.Clear(Color.Transparent);
            batch.Begin();

            for (int X = 0; X < 256; X++)
            {
                for (int Y = 0; Y < 256; Y++)
                {
                    if (tilearray[X, Y] != null)
                    {
                        batch.Draw(tiletexture, new Vector2(X*8, Y*8), Color.White);
                    }
                }
            }
            batch.End();

            tileoverlay = (Texture2D)target;

            device.SetRenderTarget(null);
            device.Clear(Color.CornflowerBlue);
        }
Example #4
0
        public static Texture2D FromText(string text, SpriteFont font, Color color, Size size, bool multiLine, int lineStart, GraphicsDevice device)
        {
            string[] drawAbleText = multiLine ? text.Split(new string[1] { "\n" }, StringSplitOptions.None) : new string[1] { text };
            RenderTarget2D target = new RenderTarget2D(device, size.Width, size.Height);
            SpriteBatch sb = new SpriteBatch(device);

            device.SetRenderTarget(target);
            device.Clear(Color.Transparent);

            sb.Begin();
            for (int i = lineStart; i < drawAbleText.Length; i++)
            {
                float y = 1 + (i - lineStart) * font.GetHeight();
                sb.DrawString(font, drawAbleText[i], new Vector2(1, y), color, 0f, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }
            sb.End();

            device.SetRenderTarget(null);

            Texture2D texture = new Texture2D(device, size.Width, size.Height);
            Color[] colorData = target.GetColorData();
            texture.SetData(colorData);

            target.Dispose();
            sb.Dispose();

            return texture;
        }
        public TextureAtlasAsset CreateTextureAtlasAsset(
            string name,
            GraphicsDevice graphicsDevice,
            IEnumerable<TextureAsset> textures)
        {
            if (name == null) throw new ArgumentNullException("name");
            if (graphicsDevice == null) throw new ArgumentNullException("graphicsDevice");
            if (textures == null) throw new ArgumentNullException("textures");

            var textureArray = textures.ToArray();
            var size = this.CalculateSizeForTextures(textureArray);

            var mappings = new Dictionary<string, Rectangle>();
            var renderTarget = new RenderTarget2D(graphicsDevice, (int)size.X, (int)size.Y);

            try
            {
                var x = 0;
                var y = 0;
                graphicsDevice.SetRenderTarget(renderTarget);
                graphicsDevice.Clear(Color.Transparent);

                using (var spriteBatch = new SpriteBatch(graphicsDevice))
                {
                    spriteBatch.Begin();

                    foreach (var texture in textureArray)
                    {
                        if (texture.Texture.Width == 16 ||
                            texture.Texture.Height == 16)
                        {
                            spriteBatch.Draw(texture.Texture, new Vector2(x, y));
                            mappings.Add(texture.Name, new Rectangle(x, y, 16, 16));
                            x += 16;
                            if (x >= size.X)
                            {
                                x = 0;
                                y += 16;
                            }
                        }
                    }

                    spriteBatch.End();
                }
            }
            catch (InvalidOperationException)
            {
            }

            graphicsDevice.SetRenderTarget(null);
            graphicsDevice.BlendState = BlendState.Opaque;
            graphicsDevice.DepthStencilState = DepthStencilState.Default;
            graphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
            graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;

            return new TextureAtlasAsset(
                name,
                renderTarget,
                mappings);
        }
        private Texture2D CreateBackground(GraphicsDevice gd, SpriteBatch sb, ContentManager cm)
        {
            RenderTarget2D target = new RenderTarget2D(gd, 2048, 2048);
            //tell the GraphicsDevice we want to render to the gamesMenu rendertarget (an in-memory buffer)
            gd.SetRenderTarget(target);

            //clear the background
            gd.Clear(Color.Transparent);

            //begin drawing
            sb.Begin();
            for (int x = 0; x < 8; x++)
            {
                for (int y = 0; y < 8; y++)
                {
                    sb.Draw(cm.Load<Texture2D>("Backgrounds/" + _level.ToString()), new Vector2(x * 400, y * 256), Color.White);
                }
            }

            sb.End();
            //reset the GraphicsDevice to draw on the backbuffer (directly to the backbuffer)
            gd.SetRenderTarget(null);

            return (Texture2D)target;
        }
Example #7
0
        private void AccumulateLights(IEnumerable<ILightProvider> lights, SpriteBatch sb, GraphicsDevice graphicsDevice)
        {
            graphicsDevice.SetRenderTarget(_accumulatorRT);
            graphicsDevice.Clear(Color.Black);

            foreach (var light in lights)
            {
                sb.Begin(SpriteSortMode.Immediate, BlendState.Additive, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, _lightAccumulatorFX, GameVariables.CameraZoomMatrix);
                var normalizedPosition = new Vector2(light.Position.X / _accumulatorRT.Width,
                                                     light.Position.Y / _accumulatorRT.Height);

                _lightAccumulatorFX.Parameters["lightPosition"].SetValue(normalizedPosition);
                _lightAccumulatorFX.Parameters["lightRadius"].SetValue(light.LightRadius);
                _lightAccumulatorFX.Parameters["lightIntensity"].SetValue(light.LightIntensity);

                sb.Draw(_screenTex, new Rectangle(0, 0, _accumulatorRT.Width, _accumulatorRT.Height), Color.White);
                sb.End();
            }

            graphicsDevice.SetRenderTarget(null);

            //if (lights.Any())
            //{
            //    using (var stream = new FileStream("output.png", FileMode.OpenOrCreate))
            //    {
            //        _accumulatorRT.SaveAsPng(stream, _accumulatorRT.Width, _accumulatorRT.Height);
            //    }
            //}
        }
Example #8
0
        private Texture2D CreateBackground(GraphicsDevice graphics, SpriteBatch spriteBatch, ContentManager Content)
        {
            RenderTarget2D target = new RenderTarget2D(graphics, 2048, 2048);
            //tell the GraphicsDevice we want to render to the gamesMenu rendertarget (an in-memory buffer)
            graphics.SetRenderTarget(target);

            //clear the background
            graphics.Clear(Color.Transparent);

            //begin drawing
            spriteBatch.Begin();
            for (int x = 0; x < 8; x++)
            {
                for (int y = 0; y < 8; y++)
                {
                    spriteBatch.Draw(Content.Load<Texture2D>("starBackground"), new Vector2(x * 256, y * 256), Color.White);
                }
            }

            spriteBatch.End();
            //reset the GraphicsDevice to draw on the backbuffer (directly to the backbuffer)
            graphics.SetRenderTarget(null);

            return target;
        }
Example #9
0
 public LineRenderer(GraphicsDevice p_gd, ContentManager p_content)
 {
     m_lineTexture = new RenderTarget2D(p_gd, 1, 1);
     p_gd.SetRenderTarget(m_lineTexture);
     p_gd.Clear(Color.White);
     p_gd.SetRenderTarget(null);
     m_aalineTexture = p_content.Load<Texture2D>("aaline");
 }
Example #10
0
        public LineRenderer(GraphicsDevice graphicsDevice)
        {
            lineTexture = new RenderTarget2D(graphicsDevice, 2, 3);

            graphicsDevice.SetRenderTarget(lineTexture);
            graphicsDevice.Clear(Color.White);
            graphicsDevice.SetRenderTarget(null);
        }
 //Sam was here
 //Do not use unless you plan on doing pixel perfect collision (I don't think we will but just in case)
 public Texture2D GetTexture(GraphicsDevice graphicsDevice, SpriteBatch spriteBatch)
 {
     RenderTarget2D renderTarget = new RenderTarget2D(graphicsDevice, frameWidth, frameHeight);
     graphicsDevice.SetRenderTarget(renderTarget);
     graphicsDevice.Clear(new Color(0, 0, 0, 0));
     spriteBatch.Begin();
     Draw(spriteBatch, new Rectangle(0, 0, frameWidth, frameHeight));
     spriteBatch.End();
     graphicsDevice.SetRenderTarget(null);
     return renderTarget;
 }
Example #12
0
        public Bar(Texture2D texture, Vector2 position,GraphicsDevice graphicsDevice)
            : base(texture, new Vector2(-100,-100), Microsoft.Xna.Framework.Color.White, 0.0f, Vector2.Zero, Vector2.One, SpriteEffects.None, 0.0f)
        {
            rt2d = new RenderTarget2D(graphicsDevice, texture.Width - 1, texture.Height - 1);
            graphicsDevice.SetRenderTarget(rt2d);
            graphicsDevice.Clear(Microsoft.Xna.Framework.Color.Red);
            graphicsDevice.SetRenderTarget(null);

            metervalue = 1;
            barmeter = new Microsoft.Xna.Framework.Rectangle((int)position.X, (int)position.Y, 0, (int)texture.Height - 2);
            width = texture.Width - 2;
        }
Example #13
0
        public void GenerateShadow(Texture2D shadowmap, SpriteBatch spriteBatch,GraphicsDevice graphicsDevice,List<Effect>EffectList)
        {
            #region Snatch texture
            graphicsDevice.SetRenderTarget(area);
            spriteBatch.Begin(SpriteSortMode.Immediate,BlendState.Opaque);
            graphicsDevice.Clear(Color.White);
            spriteBatch.Draw(shadowmap,
                new Rectangle(0, 0, range * 2, range * 2),
                RenderArea,
                Color.White);
            #endregion
            #region Calculate Fade
            graphicsDevice.SetRenderTarget(output[1]);
            EffectList[1].CurrentTechnique.Passes[0].Apply();
            spriteBatch.Draw(area, new Rectangle(0, 0, range * 2, range * 2), Color.White);
            #endregion
            #region Calculate Distorted
            graphicsDevice.SetRenderTarget(output[0]);

            EffectList[0].CurrentTechnique.Passes[0].Apply();
            spriteBatch.Draw(output[1], new Rectangle(0, 0, range * 2, range * 2), Color.White);
            #endregion

            #region Horizontal Reduction
            int order = 0;
            //represents the order of the power of 2 used in the reduction
            //first pass of the lap makes the pixel the min of itself and the pixel near it (2^0)
            //second pass makes the pixel the min of itself and the one two pixels to the right
            // and so on until 2^order>range
            EffectList[2].Parameters["range"].SetValue(range);
            EffectList[2].CurrentTechnique.Passes[0].Apply();
            while (Math.Pow(2, order) <= range)
            {
                graphicsDevice.SetRenderTarget(output[(order + 1) ]);
                EffectList[2].Parameters["order"].SetValue((float)(Math.Pow(2, order)) / (range * 2));
                spriteBatch.Draw(output[order ], new Rectangle(0, 0, range * 2, range * 2), Color.White);
                EffectList[2].CurrentTechnique.Passes[0].Apply();
                order++;
            }
            #endregion
            #region Shadow Resolve
            graphicsDevice.SetRenderTarget(area);
            EffectList[3].CurrentTechnique.Passes[0].Apply();
            spriteBatch.Draw(output[order  ], new Rectangle(0, 0, range * 2, range * 2), Color.White);
            #endregion region Shadow Resolve
            spriteBatch.End();
            graphicsDevice.SetRenderTarget(null);
        }
Example #14
0
        public static Texture2D FromLabels(KeyValuePair<string, Color>[] labels, SpriteFont font, Size size, GraphicsDevice device)
        {
            RenderTarget2D target = new RenderTarget2D(device, size.Width, size.Height);
            SpriteBatch sb = new SpriteBatch(device);

            device.SetRenderTarget(target);
            device.Clear(Color.Transparent);

            sb.Begin();
            for (int i = 0; i < labels.Length; i++)
            {
                string prevString = string.Join(" ", labels.Select(l => l.Key).ToArray(), 0, i);
                if (i > 0) prevString += " ";
                float x = 1 + font.MeasureString(prevString).X;

                sb.DrawString(font, labels[i].Key, new Vector2(x, 1), labels[i].Value, 0f, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            }
            sb.End();

            device.SetRenderTarget(null);

            Texture2D texture = new Texture2D(device, size.Width, size.Height);
            Color[] colorData = target.GetColorData();
            texture.SetData(colorData);

            target.Dispose();

            return texture;
        }
Example #15
0
        public void DrawReflectionMap(GraphicsDevice device, GameTime gameTime)
        {
            UpdateReflectionViewMatrix(DisplayController.Camera);

            Vector4 reflectionPlane = CreatePlane(terrainWater.WaterHeight, new Vector3(0, -1, 0), true);

            device.SetRenderTarget(terrainWater.ReflectionRenderTarget);

            device.Clear(ClearOptions.Target, Color.Black, 1.0f, 0);
            terrainWater.Board.GetDrawer().Draw(device, gameTime, reflectionPlane);

            Matrix cameraMatrix = DisplayController.Camera.CameraMatrix;

            DisplayController.Camera.CameraMatrix = terrainWater.ReflectionViewMatrix;

            foreach (GameObject o in terrainWater.MissionController.GetMissionObjects())
            {
                o.GetDrawer().Draw(device, gameTime, reflectionPlane);
            }

            DisplayController.Camera.CameraMatrix = cameraMatrix;

            terrainWater.ReflectionMap = terrainWater.ReflectionRenderTarget;
            /*using (FileStream fileStream = File.OpenWrite("reflectionmap.jpg"))
            {
                terrainWater.ReflectionMap.SaveAsJpeg(fileStream, terrainWater.ReflectionMap.Width, terrainWater.ReflectionMap.Height);
                fileStream.Close();
            }*/
        }
Example #16
0
 public static void ResolveRenderTarger(GraphicsDevice device)
 {
     device.SetRenderTarget(null);
     miniMap = minimap;
     Camera.upDownRot = MathHelper.ToRadians(-45);
     Camera.cameraPosition = new Vector3(30, 80, 100);
 }
Example #17
0
        public static RenderTarget2D GenerateShadows(RenderTarget2D shadowCaster,SpriteBatch spriteBatch,GraphicsDevice graphicsDevice)
        {
            foreach (LightSource CurrentLight in LightList)
            {
                if (CurrentLight.distance < 1000)
                {
                    CurrentLight.GenerateShadow(shadowCaster, spriteBatch, graphicsDevice, ShadowEffectList);
                }
            }
            graphicsDevice.SetRenderTarget(shadowMap);
            graphicsDevice.Clear(Color.Transparent);
            spriteBatch.Begin(SpriteSortMode.Immediate,BlendState.NonPremultiplied);
            //draw every small shadow map to the bigger one
            TestClass.DebugFrame.debugText += "Number of lights:" + LightList.Count().ToString()+"\n";
            foreach (LightSource CurrentLight in LightList)
            {

                    spriteBatch.Draw(CurrentLight.area,
                    CurrentLight.RenderArea,
                    Color.White);

            }
            spriteBatch.End();
            return shadowMap;
        }
Example #18
0
 public void PreRender(SpriteBatch spriteBatch, GraphicsDevice graphics)
 {
     graphics.SetRenderTarget(this.rt2dOverlay);
     graphics.Clear(Color.Transparent);
     Vector2 v2PlayerRenderedAt = Utility.Vector2_ToInts(this.xTransform.v2Pos + this.v2OffsetRenderPos) - Utility.Vector2_ToInts(this.xCamera.v2TopLeft * this.v2ParallaxFactor);
     v2PlayerRenderedAt.Y -= 8f;
     float fScale = this.xWatcher.fRadius / 200f;
     int iRadiusInInts = (int)(100f * fScale);
     spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, null, null);
     spriteBatch.Draw(this.txBlindWindow, v2PlayerRenderedAt, null, this.cColor, this.fRotation, new Vector2(100f, 100f), fScale, this.enSpriteEffect, 0f);
     if (v2PlayerRenderedAt.Y - (float)iRadiusInInts >= 0f)
     {
         spriteBatch.Draw(RenderMaster.txNoTex, new Rectangle(0, 0, 640, (int)v2PlayerRenderedAt.Y - iRadiusInInts), Color.Black);
     }
     if (360 - ((int)v2PlayerRenderedAt.Y - iRadiusInInts) > 0)
     {
         spriteBatch.Draw(RenderMaster.txNoTex, new Rectangle(0, (int)v2PlayerRenderedAt.Y + iRadiusInInts, 640, 360 - ((int)v2PlayerRenderedAt.Y - iRadiusInInts)), Color.Black);
     }
     if ((int)v2PlayerRenderedAt.X - iRadiusInInts > 0)
     {
         spriteBatch.Draw(RenderMaster.txNoTex, new Rectangle(0, (int)v2PlayerRenderedAt.Y - iRadiusInInts, (int)v2PlayerRenderedAt.X - iRadiusInInts, iRadiusInInts * 2), Color.Black);
     }
     if ((int)v2PlayerRenderedAt.X + iRadiusInInts < 640)
     {
         spriteBatch.Draw(RenderMaster.txNoTex, new Rectangle((int)v2PlayerRenderedAt.X + iRadiusInInts, (int)v2PlayerRenderedAt.Y - iRadiusInInts, 640 - ((int)v2PlayerRenderedAt.X + iRadiusInInts), iRadiusInInts * 2), Color.Black);
     }
     spriteBatch.End();
 }
        public UIRenderPlane(UISpriteBatch batch, Promise<Texture2D> texture)
        {
            this.GD = batch.GraphicsDevice;
            this.Target = batch.GetBuffer();
            this.Texture = texture;
            this.Batch = batch;

            //GD.PresentationParameters.RenderTargetUsage = RenderTargetUsage.PreserveContents;

            /** Switch the render target **/
            Batch.Pause();
            GD.SetRenderTarget(0, Target);
            GD.Clear(Color.TransparentBlack);
            Batch.Resume();

            /**
                batch.Pause();
                var buffer = batch.GetBuffer();
                var gd = GameFacade.GraphicsDevice;

                var renderTarget = gd.GetRenderTarget(0);
                gd.SetRenderTarget(0, buffer);
                batch.Resume();
                gd.render
                //gd.Clear(Color.TransparentBlack);**/
        }
Example #20
0
        public void DrawLightDarkness(GraphicsDevice graphicsDevice, SpriteBatch sb, RenderTarget2D sceneRt)
        {
            graphicsDevice.SetRenderTarget(null);

            sb.Begin(SpriteSortMode.Immediate, null, null, null, null, _lightCombinerFX);

            sb.Draw(_accumulatorRT, new Rectangle(0, 0, _accumulatorRT.Width, _accumulatorRT.Height), Color.White);

            sb.End();
        }
Example #21
0
        internal Pipeline(VGDevice device, VGSurface surface, VGState state)
        {
            Device = device;
            Surface = surface;
            State = state;

            _device = device.GraphicsDevice;
            _device.SetRenderTarget(surface.Target);
            _device.RasterizerState = State.RasterizerState;
        }
            public RenderTargetOperation(RenderTarget2D renderTarget, GraphicsDevice graphicsDevice, Color backgroundColor)
            {
                _graphicsDevice = graphicsDevice;
                _viewport = _graphicsDevice.Viewport;
                _previousRenderTargetUsage = _graphicsDevice.PresentationParameters.RenderTargetUsage;

                _graphicsDevice.PresentationParameters.RenderTargetUsage = RenderTargetUsage.PreserveContents;
                _graphicsDevice.SetRenderTarget(renderTarget);
                _graphicsDevice.Clear(backgroundColor);
            }
Example #23
0
 public override void Draw(GraphicsDevice device)
 {
     var oldTargets = device.GetRenderTargets();
     device.SetRenderTarget(Target);
     device.Clear(Color.Transparent);
     device.DepthStencilState = DepthStencilState.Default;
     Camera.ProjectionDirty();
     base.Draw(device);
     device.SetRenderTargets(oldTargets);
 }
Example #24
0
 public void PrintLightsOverTexture(RenderTarget2D renderTarget, SpriteBatch spriteBatch, GraphicsDevice graphics, Texture2D light, Texture2D underlyingTexture, float mixFactor0to1)
 {
     this.LightBlender.Parameters["MixFactor"].SetValue(mixFactor0to1);
     graphics.SetRenderTarget(renderTarget);
     spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.LinearClamp, null, RasterizerState.CullNone, this.LightBlender);
     graphics.Textures[1] = light;
     graphics.Textures[1].GraphicsDevice.SamplerStates[1] = SamplerState.LinearClamp;
     spriteBatch.Draw(underlyingTexture, Vector2.Zero, Color.White);
     spriteBatch.End();
 }
Example #25
0
		static public Texture2D LoadTexture (GraphicsDevice device, Stream input) {
			Texture2D file = Texture2D.FromStream(device, input);

			// Setup a render target to hold our final texture which will have premulitplied alpha values
			RenderTarget2D result = new RenderTarget2D(device, file.Width, file.Height);
			device.SetRenderTarget(result);
			device.Clear(Color.Black);

			// Multiply each color by the source alpha, and write in just the color values into the final texture
			BlendState blendColor = new BlendState();
			blendColor.ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue;
			blendColor.AlphaDestinationBlend = Blend.Zero;
			blendColor.ColorDestinationBlend = Blend.Zero;
			blendColor.AlphaSourceBlend = Blend.SourceAlpha;
			blendColor.ColorSourceBlend = Blend.SourceAlpha;

			SpriteBatch spriteBatch = new SpriteBatch(device);
			spriteBatch.Begin(SpriteSortMode.Immediate, blendColor);
			spriteBatch.Draw(file, file.Bounds, Color.White);
			spriteBatch.End();

			// Now copy over the alpha values from the PNG source texture to the final one, without multiplying them
			BlendState blendAlpha = new BlendState();
			blendAlpha.ColorWriteChannels = ColorWriteChannels.Alpha;
			blendAlpha.AlphaDestinationBlend = Blend.Zero;
			blendAlpha.ColorDestinationBlend = Blend.Zero;
			blendAlpha.AlphaSourceBlend = Blend.One;
			blendAlpha.ColorSourceBlend = Blend.One;

			spriteBatch.Begin(SpriteSortMode.Immediate, blendAlpha);
			spriteBatch.Draw(file, file.Bounds, Color.White);
			spriteBatch.End();

			// Release the GPU back to drawing to the screen
			device.SetRenderTarget(null);

#if IOS
			return result as Texture2D;
#else
			// RenderTarget2D are volatile and will be lost on screen resolution changes.
			// So instead of using this directly, we create a non-voliate Texture2D.
			// This is computationally slower, but should be safe as long as it is done
			// on load.
			Texture2D resultTexture = new Texture2D(device, file.Width, file.Height);
			Color[] resultContent = new Color[Convert.ToInt32(file.Width * file.Height)];
			result.GetData(resultContent);
			resultTexture.SetData(resultContent);
	
			// Dispose of the RenderTarget2D immediately.
            result.Dispose();
      			
			return resultTexture;
#endif
		}
        public void RenderEffect(Renderer renderer, GraphicsDevice device)
        {
            RenderTarget2D half0 = renderer.HalfDepth;
            //render to a half-res buffer
            device.SetRenderTarget(half0);

            Apply();

            device.BlendState = BlendState.Opaque;
            device.DepthStencilState = DepthStencilState.None;
            _quadRenderer.RenderQuad(device, -Vector2.One, Vector2.One);
        }
Example #27
0
        public ToneMapComponent(GraphicsDevice device)
        {
            quad = new Quad(device);
            var effect = Content.Load<Effect>("CalculateLuminance");
            calculateLuminance = new Material(effect.Clone(), "ExtractLuminance");
            adaptLuminance = new Material(effect.Clone(), "AdaptLuminance");
            readLuminance = new Material(effect.Clone(), "ReadLuminance");
            copyLuminance = new Material(effect.Clone(), "Copy");
            toneMap = new Material(Content.Load<Effect>("ToneMap"), null);
            bloom = Content.Load<Effect>("Bloom");
            gaussian = new Gaussian(device);
            scale = new Resample(device);

            adaptedLuminance = new RenderTarget2D[2];
            adaptedLuminance[0] = new RenderTarget2D(device, 1, 1, false, SurfaceFormat.Single, DepthFormat.None);
            adaptedLuminance[1] = new RenderTarget2D(device, 1, 1, false, SurfaceFormat.Single, DepthFormat.None);

            device.SetRenderTarget(adaptedLuminance[previous]);
            device.Clear(Color.Transparent);
            device.SetRenderTarget(null);
        }
        public void DrawLightBloomEnd(GraphicsDevice device)
        {
            int width = renderTarget1.Width;
            int height = renderTarget1.Height;

            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive);
            spriteBatch.Draw(previous, new Rectangle(0, 0, width, height), new Color(200, 200, 200));
            spriteBatch.End();

            // Pass 2: draw from rendertarget 1 into rendertarget 2,
            // using a shader to apply a horizontal gaussian blur filter.
            SetBlurEffectParameters(1.0f / (float)renderTarget1.Width, 0);

            device.SetRenderTarget(renderTarget2);
            DrawFullscreenQuad(renderTarget1, width, height, gaussianBlurEffect);

            // Pass 3: draw from rendertarget 2 back into rendertarget 1,
            // using a shader to apply a vertical gaussian blur filter.
            SetBlurEffectParameters(0, 1.0f / (float)renderTarget1.Height);

            device.SetRenderTarget(renderTarget1);
            DrawFullscreenQuad(renderTarget2, width, height, gaussianBlurEffect);

            // Pass 4: draw both rendertarget 1 and the original scene
            // image back into the main backbuffer, using a shader that
            // combines them to produce the final bloomed result.
            device.SetRenderTarget(null);
            previous = renderTarget2;

            EffectParameterCollection parameters = bloomCombineEffect.Parameters;
            parameters["BloomIntensity"].SetValue(bloomValue);
            parameters["BaseIntensity"].SetValue(baseValue);
            parameters["BloomSaturation"].SetValue(bloomSat);
            parameters["BaseSaturation"].SetValue(baseSat);

            device.Textures[1] = sceneRenderTarget;
            Viewport viewport = device.Viewport;

            DrawFullscreenQuad(renderTarget1, viewport.Width, viewport.Height, bloomCombineEffect);
        }
        // We get the destinations of our particles by drawing our font to a render target and
        // reading back which pixels were set.
        List<Vector2> GetParticlePositions(GraphicsDevice device, SpriteFont font, string text)
        {
            Vector2 size = font.MeasureString(text) + new Vector2(0.5f);
            int width = (int)size.X;
            int height = (int)size.Y;

            // Create a temporary render target and draw the font on it.
            RenderTarget2D target = new RenderTarget2D(device, width, height);
            device.SetRenderTarget(target);
            device.Clear(Color.Black);

            SpriteBatch spriteBatch = new SpriteBatch(device);
            spriteBatch.Begin();
            spriteBatch.DrawString(font, text, Vector2.Zero, Color.White);
            spriteBatch.End();

            device.SetRenderTarget(null);   // unset the render target

            // read back the pixels from the render target
            Color[] data = new Color[width * height];
            target.GetData<Color>(data);
            target.Dispose();

            // Return a list of points corresponding to pixels drawn by the font. The font size will affect the number of
            // points and the quality of the text.
            List<Vector2> points = new List<Vector2>();
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    // Add all points that are lighter than 50% grey. The text is white, but due to anti-aliasing pixels
                    // on the border may be shades of grey.
                    if (data[width * y + x].R > 128)
                        points.Add(new Vector2(x, y));
                }
            }

            return points;
        }
Example #30
0
        public static Texture2D LoadTexture(GraphicsDevice device, Stream input)
        {
            Texture2D file = Texture2D.FromStream(device, input);

            // Setup a render target to hold our final texture which will have premulitplied alpha values
            RenderTarget2D result = new RenderTarget2D(device, file.Width, file.Height);
            device.SetRenderTarget(result);
            device.Clear(Color.Black);

            // Multiply each color by the source alpha, and write in just the color values into the final texture
            BlendState blendColor = new BlendState();
            blendColor.ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue;
            blendColor.AlphaDestinationBlend = Blend.Zero;
            blendColor.ColorDestinationBlend = Blend.Zero;
            blendColor.AlphaSourceBlend = Blend.SourceAlpha;
            blendColor.ColorSourceBlend = Blend.SourceAlpha;

            SpriteBatch spriteBatch = new SpriteBatch(device);
            spriteBatch.Begin(SpriteSortMode.Immediate, blendColor);
            spriteBatch.Draw(file, file.Bounds, Color.White);
            spriteBatch.End();

            // Now copy over the alpha values from the PNG source texture to the final one, without multiplying them
            BlendState blendAlpha = new BlendState();
            blendAlpha.ColorWriteChannels = ColorWriteChannels.Alpha;
            blendAlpha.AlphaDestinationBlend = Blend.Zero;
            blendAlpha.ColorDestinationBlend = Blend.Zero;
            blendAlpha.AlphaSourceBlend = Blend.One;
            blendAlpha.ColorSourceBlend = Blend.One;

            spriteBatch.Begin(SpriteSortMode.Immediate, blendAlpha);
            spriteBatch.Draw(file, file.Bounds, Color.White);
            spriteBatch.End();

            // Release the GPU back to drawing to the screen
            device.SetRenderTarget(null);

            return result as Texture2D;
        }
Example #31
0
        public override void draw(
            Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch,
            Microsoft.Xna.Framework.Graphics.GraphicsDevice device,
            Microsoft.Xna.Framework.Graphics.RenderTarget2D[] render_targets)
        {
            base.draw(spriteBatch, device, render_targets);

            device.SetRenderTarget(render_targets[0]);
            device.Clear(Color.Transparent);

            spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
            Splash.draw(spriteBatch);

            if (DisclaimerIndex < DISCLAIMERS.Length)
                PreviewString.draw(spriteBatch);

            BlackScreen.draw(spriteBatch);
            spriteBatch.End();
        }