How do I keep the screen from squishing and stretching?

Started by
4 comments, last by Gavin Williams 11 years, 5 months ago
I've programmed a simple little app that renders a triangle to the screen. Whenever I maximize the screen or drag the resize bars the triangle squishes and stretches along with the window. I wrote a function that uses IDXGISwapChain::ResizeBuffers() to resize the swap chain's buffers in response to a WM_SIZE message but it doesn't seem to fix the distortion. Am I misunderstanding the purpose of IDXGISwapChain::ResizeBuffers()?

Here's my OnResize() function. I'm either doing something wrong, or ResizeBuffers doesn't do what I though it did. Sorry if this is a dumb question wacko.png

[source lang="cpp"]HRESULT OnResize(LPARAM lParam)
{
g_pImmediateContext->OMSetRenderTargets(0, 0, 0);

// Release any extraneous instances of swap chain's buffers. The render
// target view typically holds references to swap chain's buffers.
g_pRenderTargetView->Release();

UINT width = LOWORD(lParam);
UINT height = HIWORD(lParam);

// Resize swap chain's buffers.
HRESULT hr = g_pSwapChain->ResizeBuffers(1, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, 0);

if(FAILED(hr))
{
MessageBox(NULL, "[ERROR] Failed to resize buffers.\nOnResize() in rederer.cpp",
"ERROR", MB_ICONERROR | MB_OK);
return hr;
}

// Get buffer and create render target view.
ID3D11Texture2D* pBackBuffer = nullptr;
hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&pBackBuffer);

if(FAILED(hr))
{
MessageBox(NULL, "[ERROR] Failed to get back buffer.\nOnResize() in renderer.cpp",
"ERROR", MB_ICONERROR | MB_OK);
return hr;
}

hr = g_pD3DDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_pRenderTargetView);

pBackBuffer->Release();

if(FAILED(hr))
{
MessageBox(NULL, "[ERROR] Failed to create render target view."
"\nOnResize() in renderer.cpp", "ERROR", MB_ICONERROR | MB_OK);
return hr;
}

g_pImmediateContext->OMSetRenderTargets(1, &g_pRenderTargetView, NULL);

// Set up viewport.
D3D11_VIEWPORT vp;
vp.Width = (float)width;
vp.Height = (float)height;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;

g_pImmediateContext->RSSetViewports(1, &vp);

return S_OK;
}[/source]


And here's the little snippet inside of WndProc() where I call it.

[source lang="cpp"]case WM_SIZE:
if(g_pSwapChain)
{
OnResize(lParam);
}
return 0;[/source]


Thanks for the help.
Advertisement
Eeek. It doesn't look like the code hilighting came out right. Sorry about that guys :/
I think I might be able to better clarify what I'm getting at. I've rendered an equalateral trianlge with vertices (0.0, 0.5, 0.5), (0.5, -0.5, 0.5), and (-0.5, -0.5, 0.5) without any projection transformation. How can I make it so that the the triangle remains equalateral whenever the user resizes the window? Isn't this what ResizeBuffers() is supposed to do or would this require a projection transformation or something like that?
Are you updating your projection matrix?

Are you updating your projection matrix?


There's no projection transformation. This is actually just a slightly modified version of the triangle tutorial in the SDK docs. I (perhaps stupidly) thought that resizing the swap chain's buffers would cause the triangle to retain it's shape when the window is resized. I'm guessing that would require a projection transformation? If I wanted to make a 2D game for example where the sprites were rendered to a square, would I need to do a projection transformation on the square. Pardon my ignorance.
I'd say you are working in clip space, which is -1 to 1 in x & y, and that space will stretch. So you will need to use world / view / projection matrices for getting everything the way you want it.

This topic is closed to new replies.

Advertisement