# Direct3D9 SetViewport not working properly?

## Recommended Posts

Hello,

It seems that no matter what I do, SetViewport in Direct3D9 doesn't work for me. For example, if I call SetViewport to map the viewport to the entire render area (Left:0, Top:0, Width: viewWidth, Height: viewHeight, MinZ: 0.0f, MaxZ: 1.0f), it will only render to approximately a quarter if the render area.

If I maximize the window, it will render the entire width, but a small gap remains at the bottom. If I resize it again to its original dimensions, it will render again in 1/4 area and the backbuffer is not cleared properly.

I would like to mention that I call SetViewport when the window is resized and, also, after the device has been reset. I tried calling SetViewport between BeginScene and EndScene, but with no success.

Has anybody had the same problem?

##### Share on other sites

Post your code for setting the window and backbuffer sizes, and for setting the viewport. Include in the code how you determine the size of the window and backbuffer.

My first guess is that you create a window with specified sizes which you use later, assuming those are the width and height of the window client, rather than actually getting the size of the client area.

Edited by Buckeye

##### Share on other sites

Hello,

The code is made a little complicated by the fact that I use a device enumeration class which handles device caps enumeration, window creation etc.

But I can tell you that the size of the viewport is determined when I recieve a WM_SIZE message. So the size of the viewport is:

-Width: LOWORD(lParam)

-Height: HIWORD(lParam)

I checked if the D3DVIEWPORT9 struct instance contains the correct values and indeed it does. Anyway, I will dig a little bit more. I am sure there must be something which I am missing. When I find a solution I will post it here. Thanks!

##### Share on other sites

Ok. I discovered what the problem was.

I was using a windowed mode device and I was setting the backbuffer dimensions to the screen resolution for a window of 800 by 600. Because the viewport dimensions were being set to the same size and because the backbuffer is much bigger, it makes sense that the backbuffer wasn't being filled properly.

Steps to handle window resize properly:

-set Backbuffer dimensions to 0 in the present params struct instance;

-reset device

OR check out Buckeye's post below ;)

Cheers!

Edited by XGT08

##### Share on other sites

I can tell you that the size of the viewport is determined when I recieve a WM_SIZE message

It's the relationship of the viewport to the backbuffer, not the client area, that's likely creating your problem. That is, the size of the viewport (if you want to Present the entire backbuffer) should match the size of the backbuffer, not necessarily the client area size. Commonly,  though, the backbuffer size is reset when the window is resized, and the viewport is set to the size of the backbuffer.

EDIT: cross-posting. Glad you found the problem.

EDIT2:

reset the device every time the window is resized and make sure the bacbuffer dimensions match the window's client area dimensions

The easiest way to ensure that is to set the present parameters BackBufferWidth and BackBufferHeight to 0. Then:

OnLostDevice(); // whatever your routine is.
HR( device->Reset(&present_params) ); // check for errors
OnResetDevice(); // whatever your routine is.
ResetViewportSize(present_params.BackBufferWidth, present_params.BackBufferHeight);


The device will use the current window client size to set the backbuffer. Use the params to set the viewport.

Edited by Buckeye

##### Share on other sites

Hello Buckeye,

Yes, that is entirely true. Your are right. The problem is caused by the fact that I am not using the entire portion of the backbuffer for rendering. It has nothing to do with stretching. I was hoping that nobody managed to read my last post :D. I was just about to edit it.

And again, you are right about handling thw window resize. Thanks a lot!

Cheers!

No problem.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628372
• Total Posts
2982305

• 10
• 9
• 13
• 24
• 11