Implenting Viewports

Topics: User Forum
Apr 6, 2007 at 10:36 AM
If I want to have different viewports (split screen), will I have to do my own drawing or is there some way to do this with the library?
Coordinator
Apr 6, 2007 at 11:29 AM
You have full control over the view and projection matrices, so you would do it just as you would without the library.
Apr 6, 2007 at 1:58 PM
the problem is that i need to assign the second viewport to GraphicsDevice.Viewport between the drawing of the models.

GraphicsDevice.Viewport = viewport1;
DrawPlayerOneModels();
GraphicsDevice.Viewport = viewport2;
DrawPlayerTwoModels();

like that :)
Coordinator
Apr 6, 2007 at 2:27 PM
I see. Can't you just remove the ModelAnimator from the GameComponents collection (or set Visible to false) and then draw them manually with ModelAnimator.Draw?
Apr 6, 2007 at 4:04 PM
yeah, i'll do that. I just wanted to make sure I wasn't doing something stupid :)
Apr 19, 2007 at 10:17 PM
ThePatrickP: Did you get this worked out?

I have my screen split and the items rendered but when I pan the camera the first viewport does not update properly (it simply draws over the image that was there).


Help.... again...

Apr 20, 2007 at 8:13 AM
Never mind got it sorted.
Jul 1, 2007 at 1:18 PM

CorpseLike wrote:
ThePatrickP: Did you get this worked out?

I have my screen split and the items rendered but when I pan the camera the first viewport does not update properly (it simply draws over the image that was there).


Hey Corpse,
Could you share your code? Or, at least, a description of how you implemented this!

Thanks in advance
Jul 2, 2007 at 11:35 AM
Hi Ziaden,

There is a sample from Microsoft to perform the split screen (this is a very good tutorial)
but i'll try to summarise as best I can.

1) You need to implement multiple viewports:
private Viewport singlePlayer, leftView, rightView;


2) During the load setup the viewports
private void SetupDisplay()
{
//Viewport configuration
singlePlayer = game.gameDevice.Viewport;

//the view matrix represents the user eye view
singleView = Matrix.CreateLookAt(game.cameraPosition, Vector3.Zero, Vector3.Up);
//Projection view is what the user will see
singleProjection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), (float)singlePlayer.Width / singlePlayer.Height, 1.0f, 10000.0f);

game.players0.View = singleView;
game.players0.Projection = singleProjection;

//Only support 2 players
if (game.players.Count > 1)
{
leftView = singlePlayer;
rightView = singlePlayer;

//Split the screen horizontally
leftView.Width = singlePlayer.Width / 2;
rightView.Width = singlePlayer.Width / 2;
rightView.X = leftView.Width + 1;

//the view matrix represents the user eye view
game.players0.View = Matrix.CreateLookAt(game.cameraPosition, Vector3.Zero, Vector3.Up);
//Projection view is what the user will see
game.players0.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), (float)leftView.Width / leftView.Height, 1.0f, 10000.0f);

//the view matrix represents the user eye view
game.players1.View = Matrix.CreateLookAt(game.cameraPosition, Vector3.Zero, Vector3.Up);
//Projection view is what the user will see
game.players1.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), (float)rightView.Width / rightView.Height, 1.0f, 10000.0f);
}
}

3) As the animationcomponent handles all the drawing code for you (and this is what causes the blank split screen) you need to perform the Draw calls yourself during the screen render and if I remember correctly thats about it (but dont forget as you are doing multiscreen you need to draw into each viewport).

I am sure that was it. If you need a sample or any help just lemme know.


Kind regards,


Corpse.



Jul 3, 2007 at 12:33 AM
Corpse,
Thanks so much for the information.
Best of luck to you.

Ziaden
Jul 12, 2007 at 4:52 PM
How do you maintain the draw order of your Drawable Game Components when you handle the drawing of them on your own?
Jan 8, 2009 at 8:07 AM
Somebody can help me in displaying the same animation in different viewports. when i tried with different viewports, It is displying the animation only in the last set viewport (other static models & textures are working fine in both viewports).
Thank you.