Sign in to follow this  
george7378

Stretched pixels in windowed mode

Recommended Posts

george7378    1441

Hi all,

 

I've created a simple program using DX9 in windowed mode. It uses a simple diffuse shader to render a mesh in a 640x480 window. The program seems to work fine but there's a strange array of stretched pixels which distort the image. Here's a shot:

 

[attachment=21955:pix1.png]

 

If you can't see them, here's the same image after edge-detect:

 

[attachment=21956:pix2.png]

 

Is there anything I'm overlooking that needs to be done to render in windowed mode (I've been doing fullscreen stuff until now). Thanks!

Share this post


Link to post
Share on other sites
L. Spiro    25615

The pixels aren’t being stretched, they are being squashed.

When the backbuffer and the window are not the same size the image will be rescaled upon presenting, which is what is happening here.

 

Most likely you set the window to 640-480 and you set the back buffer to the same value without taking into account the borders (hence it is squashed more vertically than horizontally, as vertically you have extra window border spent on the title bar).

The client area of a window will be smaller than the actual window size.  Your back buffer must be the same size as the client rectangle to avoid scaling.

 

If you want the client area to be 640-480, adjust the window size using AdjustWindowRect().

 

 

L. Spiro

Edited by L. Spiro

Share this post


Link to post
Share on other sites
IkarusDowned    291

You should also keep in mind that the ratio of buffer size to window size is important. If your backbuffer is set to say 640 x 480 but your window rectangle is 600 x 480, then the result will look "squashed" as you put it. If the window rectangle was, say, 700 x 480, then it would look "stretched."

 

As a rule I like to not optimize unless I must, and also try to keep things simple until it needs to be changed...thus I would suggest that your backbuffer and window rect keep the width and height in constant ratio of each other. I like to use multiples of the back buffer. Again, this isn't "optimal" as much as I just follow the rule: "get it to look right before getting to be fast."

Share this post


Link to post
Share on other sites
Buckeye    10747

As it appears that you're coding a windowed app, a better approach might be to size the backbuffer to the client area, rather than the other way 'round. Using a window means the user may move or resize the window for his/her own convenience, and you should respect that. So, when the window is resized:

RECT rect;
GetClientRect(hwnd, &rect);
params.BackBufferWidth = rect.right - rect.left;
params.BackBufferHeight = rect.bottom - rect.top;
OnDeviceLost(); // prepare objects for device reset
device->Reset( &params );
OnDeviceRestored(); // restore objects after the reset
ResetProjectionAspectRatio(); // account for change in the aspect ratio


Perhaps even better than setting the width/height yourself, unless you have some specific reason to size the backbuffer to a particular size, set the BackBufferWidth and BackBufferHeight to 0, and let D3DX set the buffer to the client area dimensions itself. You can do that when creating the device or when the window is resized (device reset). Just remember to check the aspect ratio of your projection matrix whenever the client/backbuffer is resized.

Edited by Buckeye

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