Share via


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.Graphics (in microsoft.xna.framework.graphics.dll)

Syntax

public Viewport Viewport { get; set; }

Property Value

The viewport to set or get.

Remarks

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.

Example

This code sample 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;
    //            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.Clear(Color.CornflowerBlue);

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

    base.Draw(gameTime);

}

See Also

Reference

GraphicsDevice Class
GraphicsDevice Members
Microsoft.Xna.Framework.Graphics Namespace

Platforms

Xbox 360, Windows 7, Windows Vista, Windows XP