Drawing FullScreen Quads with XNA 4.0

Quads are very important for numerous effects like all Image processing techniques (Bloom, Blur,Fog ….), Deferred Shading, Some particles system technics and others …..

In this short tutorial we will show how to draw a simple quad (and the theory behind it =P) using xna 4.0

If you dont want to know about the theory behind the quads, no problems =P, go to the code section directly.

Instead of showing how to create a Quad, i will talk about a specific case called FullScreenQuad, cause it is simpler and more easy to follow than the general case. (The concepts are the same in both case).

In simple words FullScreenQuad is just two triangles that fills the entire screen (imagine a rectangle in 2D that cover all the screen =P).

The importance of drawing FullScreen Quads is that we can run the pixel shader exactly once for each pixel of the render target (can be the back buffer or a texture). One example of this usefull property is the PostProcessing phase (or Image Process phase) where we recieve one quad and one texture as inputs, in GPU we change each texel of this texture and save the changes to the render target.

The magic is that the constructed quad will be perfect aligned to the screen borders (a rectangle covering all of it), when it is rasterized, the pixel shader will be called exactely once for each pixel of the screen, then you can access each corresponding texel of the associated texture, change it (process) and output  it in the render target in the same position it has in the original texture. (Remember that Render targets can be used as textures in another shader pass)

There are LOTS of ways of drawing fullscreen quads. We will use one that is simple and flexible enough =P (and can be used to draw arbitrary size quads, not only fullscreen).

Our choice is to create the vertices in CPU already in Projection Space and send them to a by-pass Vertex Shader.
In Projection space (after applying the world, view and projection matrix) , the borders of the screen (using the DirectX/XNA Projection matrix convention) are:  X: [-1,1], Y:[-1,1] and Z[0,1] (primitives which vertices is out of this range are clipped).  For example, a point with (-1,-1,0) will be in the down, left of you screen (The Z being 0 mean that it is VERY close to the camera). A point in (0,0,0) will appear in the midle of the screen. The following image shows the projection coordinates domain: (imagine the screen being the front square). Remember that we are in the normalized space, the rasterizator unit will convert it to the real screen coordinates (resolution dependent one)

DirectX Projection Coordinates space

So we will just create two triangles (to fill the quad) already in projection space, pass them to vertex shader, dont apply transformation (cause they are already in projection space) and send the vertices to the rasterization unit.

The vertice will contain the positions in Projection Space  and the Texture Coordinates. The up, left point of the quad will have the (0,0) texture coordinate and the lower, right will be (1,1).

The following code shows an easy way to do this in XNA 4.0:

internal sealed class QuadRender
        private VertexPositionTexture[] verts;
        private GraphicsDevice myDevice;
        private short[] ib = null;

        /// Loads the quad.
The engine.
        public QuadRender(GraphicsDevice device)

            myDevice = device;         

            verts = new VertexPositionTexture[]
                            new VertexPositionTexture(
                                new Vector3(0,0,0),
                                new Vector2(1,1)),
                            new VertexPositionTexture(
                                new Vector3(0,0,0),
                                new Vector2(0,1)),
                            new VertexPositionTexture(
                                new Vector3(0,0,0),
                                new Vector2(0,0)),
                            new VertexPositionTexture(
                                new Vector3(0,0,0),
                                new Vector2(1,0))

             ib = new short[] { 0, 1, 2, 2, 3, 0 };


        /// Draws the fullscreen quad.
The effect.
        public void RenderFullScreenQuad(Effect effect)
            RenderQuad(Vector2.One * -1, Vector2.One);

        public void RenderQuad(Vector2 v1, Vector2 v2)

            verts[0].Position.X = v2.X;
            verts[0].Position.Y = v1.Y;

            verts[1].Position.X = v1.X;
            verts[1].Position.Y = v1.Y;

            verts[2].Position.X = v1.X;
            verts[2].Position.Y = v2.Y;

            verts[3].Position.X = v2.X;
            verts[3].Position.Y = v2.Y;

                (PrimitiveType.TriangleList, verts, 0, 4, ib, 0, 2);

If you dont want to draw a full screen quad, you can change the border values passed as function parameter

The Vertex Shader to process the FullScreen quad is:

texture colorMap;
sampler colorSampler = sampler_state
    Texture = (colorMap);
    AddressU = CLAMP;
    AddressV = CLAMP;
    MagFilter = LINEAR;
    MinFilter = LINEAR;
    Mipfilter = LINEAR;

struct VertexShaderInput
    float3 Position : POSITION0;
    float2 TexCoord : TEXCOORD0;

struct VertexShaderOutput
    float4 Position : POSITION0;
    float2 TexCoord : TEXCOORD0_centroid;

float2 halfPixel;
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
    VertexShaderOutput output;
    input.Position.x =  input.Position.x - 2*halfPixel.x;
    input.Position.y =  input.Position.y + 2*halfPixel.y;
    output.Position = float4(input.Position,1);
    output.TexCoord = input.TexCoord ;
    return output;


XNA 4.0 works under DirectX9.0. In this environment, we have to make a “somehow strange correction” to the position of our vertices in the Vertex Shader (subtract the halfPixel size) when processing quads. The reason for this is the algorithm used in the rasterization unit (More info about this is Here).

The Pixel Shader is where you process the image. For this sample i just multiply each texel by 0.7f. (make them darker):

float4 PixelShaderFunctionNormal(VertexShaderOutput input) : COLOR0
	float4 process = tex2D(colorSampler,input.TexCoord);
        return process * 0.7f;

Its done =P quite simple. The PostProcess samples (the simple ones) are just “diferent” pixel shaders that process the recovered color in diferent ways.


, , ,

  1. #1 by tantalo kondensatoriai on 28 de maio de 2017 - 7:51 am

    Your house is valueble for me. Thanks!…

  2. #2 by hermes belt replica on 28 de maio de 2017 - 8:16 am

    I had gotten this gift of my mom for the Xmas mainly because she is actually the precious jewelry freak. Some sort of something she cannot use far of was, bracelets. I purchased the lady this one charm bracelet and where she opened it yesterday she absolutley enjoyed they! Right now the issue was, this girl getting they in to off of through herself. Haha… on the whole ideal system, information technology shipped to arrived incredibly early to our mother was enjoying that. Thank we.

  3. #3 by http://www.ecova.es/weddingshoes.asp on 28 de maio de 2017 - 9:35 am

    it comes down in a pretty box appearance great still the slightly slight also to the mothers wrist however it appearances awesome really really want information technology ended up being longer

  4. #4 by schauen sie sich diese jetzt on 28 de maio de 2017 - 10:33 am

    Sehr belastbar und ganz treu ist deine Telefonsex Sklavin für dich am Telefon bereit willenlos zu dienen.

  5. #5 by Wholesale Jerseys From China on 28 de maio de 2017 - 10:38 am

    Other common nicknames which have applied to Georgia Tech teams include Engineers, which is still used by some writers; the Techs, the first known nickname which was phased out sometime around 1910; and the Blacksmiths, which was common between 1902 and 1904.This dark roast can be found in both whole bean and ground coffee and both variations give you an extraordinary coffee experience. Curry has been named the FIRST Head Coach for the Atlanta University in establishing and building a football program, beginning play in 2010.”I’ve seen it in the past, and it’s been trickling in,” Mackanin said. Plouffe had two more hits and batted .
    Wholesale Jerseys From China http://www.cheapestjerseysonwholesale.com/

  6. #6 by FfgrneapNB on 28 de maio de 2017 - 10:55 am

    online payday loan – money loan (cash loan)

  7. #7 by http://news.saveindex.xyz/saveindexxyz/493.asp on 28 de maio de 2017 - 10:55 am

    I had gotten this one headphonesconcerning my personal mother towards mothers day, and she absolutely enjoyed this! The completely sweet headphonesand stating in the card you already know goes within the box is really emotional!! And quality of beads is ideal!

1 694 695 696
(não será publicado)