Archived

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

Off screen Viewport problem

This topic is 4947 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'm currently trying to use viewport to display 3D objects over a 2D menu. Using viewport is cool because it give the same control over 3D objects than over 2D images of the menu (placing in screen coordinates, like "put the image in the 0,0 320,200 rect) But the problem is that viewports can't be off screen, I mean its crashing if I'm trying to render to a viewport extending from -100, -100 to 100, 100 for instance. (first picture) I can clip the viewport myself, but the output gets distorted as the viewport gets smaller than how it really should be (0,0 to 100,100 in the previous case) (second picture) I tried the technique exposed here but alright the image gets the size it should be, but its not a clipped image, its just a scaled image centered on the new viewport (third picture) Please help !! -- SharkOne [edited by - SharkOne on May 28, 2004 6:35:06 AM] [edited by - SharkOne on May 28, 2004 6:35:45 AM]

Share this post


Link to post
Share on other sites
Yeah, the idea I talked about in the post you referred to wasn''t meant to clip things that were outside the scenes field of view. The application I was working on already performed clipping and I was just trying to optimize it further by only rendering to the part of the screen that was necessary.

Viewports and scissor rectangles must have their coordinates be inside the bounds of the screen. If you set them to be outside this area, you will have "unexpected results."

In this case, it would be better to let D3D do the clipping for you. I know you want to be able to specify a particular pixel on the screen that you want the 3D object rendered to. One way to do this could be to set the World and View matrices to be identity. Depending on how you set up your menus, you could use either a perspective or orthographic projection matrix. Then you would add a translation matrix to your projection matrix.

Remember that after the projection matrix has been applied, all geometry is in the range of [-1..1] on the x and y. [0..1] on z. Say you want your 3D object to be rendered at location (400,500) on the screen. And let''s say your resolution is 1024 X 768. You could then build your matrix like this:

// Build your projection matrix (either perspective or ortho).
//
D3DXMATRIX matTrans;
D3DXMatrixTranslation(&matTrans, 400.0f*(2.0f/1024.0f)-1.0f, 500.0f*(2.0f/768.0f)-1.0f, 0.0);
D3DXMatrixMultiply(&matProjFinal, &matProj, &matTrans);
m_pd3dDevice->SetTransform(D3DTS_PROJ, &matProjFinal);
// Render 3D model on top of your 2D menu....


I haven''t tried this out but I believe it will work.

neneboricua

Share this post


Link to post
Share on other sites
Well, I initially tried that way, I mean try to find the right matrix which allows me to translate my objects so they are projected where I want in screen space. But it''s not a really good solution for me because I would also like to have the real width and height on screen of the projected object because my menu allows centering and stuff...

So this afternoon I tried using Render to Texture to do what I want. I mean, I have the same benefits as with the viewport approch but I can now clip easily (actually I''m not using Direct3D but RenderWare so I don''t really know how what I''m currently is really done inside with D3D), but this approch needs more power and I have to deal with creating textures on the fly and I have to decide their size.

--
SharkOne

Share this post


Link to post
Share on other sites
quote:
Original post by SharkOne
< snip >I would also like to have the real width and height on screen of the projected object because my menu allows centering and stuff...


I''m not sure what you mean by this part. Do you mean that you don''t want your 3D object projected onto the menu? Or do you mean that you want the 3D object to appear to be full size when you render it onto your menu?

I''ll try to help as much as I can and I''m sure others on this board will as well.

neneboricua

Share this post


Link to post
Share on other sites
Hmm, what I meant is that the important feature I need, is to be able to know the exact dimensions in pixels of what I'm going to render, because the menu allows automatic centering of controls

Ex:

-----------
| |
| abcdede |
| |
-----------

This text is centered because I have his width, and I can place it so that the middle point of the text is at the middle point of the screen. But what if I want to do this :

-----------
| |
|XXXX dede|
| |
-----------


XXXX being a 3D object, if I want the whole to be centered, I need to have the width of the text, and the width of the 3D object, add them, find the middle point, then place both so that this middle point is in the middle of the screen

I hope it's clearer now =]

--
SharkOne

[edited by - SharkOne on May 28, 2004 7:37:25 PM]

[edited by - SharkOne on May 28, 2004 7:37:47 PM]

Share this post


Link to post
Share on other sites