Archived

This topic is now archived and is closed to further replies.

4 viewports and other basic questions

This topic is 5145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have just started out with direct3d and i''m just trying to get my head around it. My understanding so far is that in order to render anything in direct3d, you need to create a device and have a vertex buffer. So far I have a window with a colored triangle in it What i am aiming for is to have an app with 4 viewports all rendering the same scene, like other 3d apps(max, maya) ect Would this invlove having 4 devices all rendering the same vertex buffer stuff? I am using manged dx and c#, I seen on gotdotnet.com a tutorial that lets you render in a picture box control. Is this advised? Does it slow things down? Could someone give me a hint on how to get 4 veiwports on my form. Any help appreciated, Thanks

Share this post


Link to post
Share on other sites
Check out D3DVIEWPORT9 structure in the DXSDK docs. Basically you can create multiple viewports in your window, then use SetViewport() method (of the IDirect3DDevice interface) to make a viewport active. Once a certain viewport is active, you can set up your viewing parameters to whatever you want (ie. perspective, top, right, front like in a 3D package), and render your scene to the active viewport.

So you don''t need four devices, just define four viewports. For example define a viewport for the top left 1/4 of the screen like:

D3DVIEWPORT9 vp;
vp.X = 0;
vp.Y = 0;
vp.Width = RenderTarget.Width/2;
vp.Height = RenderTarget.Height/2;
vp.MinZ = 0.0f;
vp.MaxZ = 1.0f;

Share this post


Link to post
Share on other sites
Thanks DGates, I wasn''t aware that there was a viewport structure, very handy
I''m using managed dx and it doesnt seem to have an interface for the device class. Anyone know how i can set the viewport in mdx?
Thanks again

Share this post


Link to post
Share on other sites
Cool, I figured out how to use the viewport. Now my app is a window with the top left corner rendering a piece of a triangle and the rest of the window is black. Is this normal or have i done something wrong?
I mean, i thought the topleft viewport would be black with the triangle and the rest of the app wouldn''t be black or whatever couler i clear the device to.

Share this post


Link to post
Share on other sites
Along these same lines, I also wanted to be able to place arbitrary viewports in my app. I ended up subclassing System.Windows.Forms.Control to create a Viewer control. I created a static global DirectX device object (with a single backbuffer as large as the desktop), so when I render each control I simply set the viewport of the device to (0,0,control width, control height), and make sure to call present with the correct parameters to make it paint inside my control. It works fantastic.

Share this post


Link to post
Share on other sites
When doing a Present() to another window, the entire back buffer is stretched to fit into your other window... You might be drawing 1024x768 to fill a 64x16 button.

Look into CreateAdditionalSwapChain(), which allows you to create another backbuffer, associated with another window. Search this site, or the net, for tutorials if you need them. Ensure you don''t use autodepthstencil with the new chains as there is no way obtain a handle to the Z surface from a swap chain.

Render to this swapchain like you''d render to a texture. When done, call present on the swapchain.

Share this post


Link to post
Share on other sites
Actually, I''m calling present and passing in just the rectangle I need to to copy to my control, so if my control is 64x64, I make a call to present with a source rectangle of (0,0,64,64). It seems to work fine. I don''t think that would copy the entire buffer would it?

I also found problems with swapchains, specifically that they hog memory and you still get limited to a total backbuffer space of however large your card can handle.

Share this post


Link to post
Share on other sites
Ok, choosing a rectangle should be fine, and not scaling the entire backbuffer. Just making sure you''re not doing something bad without realizing it. I don''t know if there''s any performance penalty in presenting a rectangle rather than the entire back buffer, but I guess if it was bad you''d have noticed by now.

Share this post


Link to post
Share on other sites
Yeah, and the code is generic enough that it helps in the odd situation where you have a backbuffer smaller than your control, it just stretches it, and although it looks blurry, it''s a lot better than an error message.

Share this post


Link to post
Share on other sites