Reputation: 716
I've got a dialog that I'd basically like to implement as a texture viewer using DirectX. The source texture can either come from a file on-disk or from an arbitrary D3D texture/surface in memory. The window will be resizable, so I'll need to be able to scale its contents accordingly (preserving aspect ratio, while not necessary, would be useful to know).
What would be the best way to go about implementing the above?
Upvotes: 0
Views: 637
Reputation: 35039
IMHO the easiest way to do this is to create a quad (or two triangles) whose vertices contain the correct UV-coordinates. The XYZ coordinates you set to the viewing cube coordinates. This only works if the identity matrix is set as projection. You can use -1 to 1 on both X and Y axes.
EDIT: Here an example turorial:
Upvotes: 1
Reputation: 292
This is the code I use to preserve size and scaling for resizeable dialogue. My texture is held in a memory bitmap. I am sure you can adapt if you do not have a memory bitmap. The important bits is the way I determine the right scaling factor to preserve the aspect ratio for any client area size
CRect destRect( 0, 0, frameRect.Width(), frameRect.Height() );
if( txBitmapInfo.bmWidth <= frameRect.Width() && txBitmapInfo.bmHeight <= frameRect.Height() )
{
destRect.left = ( frameRect.Width() - txBitmapInfo.bmWidth ) / 2;
destRect.right = destRect.left + txBitmapInfo.bmWidth;
destRect.top = ( frameRect.Height() - txBitmapInfo.bmHeight ) / 2;
destRect.bottom = destRect.top + txBitmapInfo.bmHeight;
}
else
{
double hScale = static_cast<double>( frameRect.Width() ) / txBitmapInfo.bmWidth;
double vScale = static_cast<double>( frameRect.Height() ) / txBitmapInfo.bmHeight;
if( hScale < vScale )
{
int height = static_cast<int>( frameRect.Width() * ( static_cast<double>(txBitmapInfo.bmHeight) / txBitmapInfo.bmWidth ) );
destRect.top = ( frameRect.Height() - height ) / 2;
destRect.bottom = destRect.top + height;
}
else
{
int width = static_cast<int>( frameRect.Height() * ( static_cast<double>(txBitmapInfo.bmWidth) / txBitmapInfo.bmHeight ) );
destRect.left = ( frameRect.Width() - width ) / 2;
destRect.right = destRect.left + width;
}
}
Hope this helps!
Upvotes: 1