Viewport Property

GraphicsDevice.Viewport Property

Gets or sets a viewport identifying the portion of the render target to receive draw calls.

Namespace: Microsoft.Xna.Framework.Graphics
Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)

public Viewport Viewport { get; set; }

Property Value

The viewport to set or get.

Viewport can be used to draw on part of the screen. It should be set before any geometry is drawn so the viewport parameters will take effect.

To draw multiple views within a scene, repeat setting Viewport and draw a geometry sequence for each view.

This code sample, from How To: Use Viewports for Split Screen Gaming, demonstrates how to use the Viewport property to display different scenes to different parts of the screen.

Viewport defaultViewport;
Viewport leftViewport;
Viewport rightViewport;
Matrix projectionMatrix;
Matrix halfprojectionMatrix;
protected override void LoadContent()
    // Create a new SpriteBatch, which can be used to draw textures.
    spriteBatch = new SpriteBatch(GraphicsDevice);

    defaultViewport = GraphicsDevice.Viewport;
    leftViewport = defaultViewport;
    rightViewport = defaultViewport;
    leftViewport.Width = leftViewport.Width / 2;
    rightViewport.Width = rightViewport.Width / 2;
    rightViewport.X = leftViewport.Width + 1;

    Ring = Content.Load<Model>("redtorus");

    projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 4.0f / 3.0f, 1.0f, 10000f);
    halfprojectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 2.0f / 3.0f, 1.0f, 10000f);
protected override void Draw(GameTime gameTime)
    GraphicsDevice.Viewport = defaultViewport;

    GraphicsDevice.Viewport = leftViewport;
    DrawScene(gameTime, Camera1.ViewMatrix, halfprojectionMatrix);
    GraphicsDevice.Viewport = rightViewport;
    DrawScene(gameTime, Camera2.ViewMatrix, halfprojectionMatrix);



Xbox 360, Windows XP SP2, Windows Vista, Zune

Community Additions

© 2016 Microsoft