Sign in to follow this  
vcGamer

Multiple viewports

Recommended Posts

hi friends What's the best way to make a screen with normal render target (i.e. the full screen) plus another small viewport (something like the rear mirror in car games)? Can I use viewports? or you suggest another method? for instance a rectangle in the present method, can you give me a sample with code example. thanks www.galaxyroad.com

Share this post


Link to post
Share on other sites
I'm at work now so don't have time to dig out any sample code, but you can probably do what you want by modifying the D3DVIEWPORT9 fields. The bit I can't remember right now is if you also have to tweak the projection matrix as well [oh]

I would suggest looking into render-to-texture features to compose the contents of your mirror, then integrate that with the 3D scene. That way you can probably avoid any problems with projection matrices and especially any depth buffering issues incurred by simply "masking off" part of the display.

hth
Jack

Share this post


Link to post
Share on other sites
Multiple viewports are pretty easy. Here is a tutorial from CodeSampler:
DX9/C++
DX9/C#

It shows you how to create 2 viewports and render a teapot to each on (with the same rotations, but different background/clear colors).

Its pretty simple though:

D3DVIEWPORT9 View1, View2;
ZeroMemory(&View1, sizeof(View1));
View1.X = 0;
View1.Y = 0;
View1.Width = 800;
View1.Height = 300;
View1.MaxZ = 1.0f;
ZeroMemory(&View2, sizeof(View2));
View2.X = 0;
View2.Y = 300;
View2.Width = 800;
View2.Height = 300;
View2.MaxZ = 1.0f;

//In render loop
g_pDevice->SetViewport(&View1);
g_pDevice->Clear(0, 0, D3DCLEAR_TARGET, 0xff00ff00, View1.MaxZ, 0);
RenderScreen();
g_pDevice->Present(0, 0, 0, 0);

g_pDevice->Setviewport(&View2);
g_pDevice->Clear(0, 0, D3DCLEAR_TARGET, 0xffff0000, View2.MaxZ, 0);
RenderScreen();
g_pDevice->Present(0, 0, 0, 0);

Share this post


Link to post
Share on other sites
I would suggest a texture as well. Use shaders to draw to a texture then just switch to 2d drawing mode (disable zbuffe etc) and draw a textured quad onto the desired location. In directx just create a texture with the (c# code: Usage.RenderTarget) flag. then use mytex.GetSurfaceLevel(0) to get a surface and then set your render target to this surface in order to draw to your other view.

Share this post


Link to post
Share on other sites
If you wish to draw a mirror on top of the main scene, viewports will only help if it's completely rectangular. This would be very easy to render:
1)Set Viewport to whole screen.
2)Render main scene.
3)Set Viewport to small region for mirror.
4)Render again (remember you need to make the camera look the other way!).

The other way is to render the scene a 2nd time to a texture, then draw triangles using this texture for the mirror.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this