I have a Windows (XP) application which needs to display a two-dimensional rectangle within a window. The rectangle must not be clipped (i.e. must always lie completely within the viewport), and must preserve its aspect ratio on resize. Currently, the method which handles layout distorts the aspect ratio of the rectangle to match the window. I want the rectangle to scale to the window and be centered in the window (again, without clipping). The method as it stands is below. lWinDist and lMaxDepth are the width and height of the rectangle to be displayed (in 48ths of an inch, if it matters):
void CRoRRecView::RedoLayout( long lWinDist, long lMaxDepth )
{
CDC* pDC = GetDC() ;
if ( pDC != NULL )
{
m_lWinDist = lWinDist;
GetClientRect( m_rectClient ) ;
int nClientWidth = m_rectClient.Width();
int nClientHeight = m_rectClient.Height();
glViewport( 0, 0, nClientWidth, nClientHeight );
glMatrixMode( GL_PROJECTION);
glLoadIdentity();
m_fWinXDist = (float) lWinDist ;
m_fWinYDist = lMaxDepth ;
m_fAspectRatio = m_fWinXDist / m_fWinYDist;
glOrtho(0.0, m_fWinXDist, 0.0, m_fWinYDist, -1, 1 ) ;
glRotatef(180.0, 0,1,0);
glTranslatef( (float)(-1 * lWinDist),0,0 ); // Translate across the x axis
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
ReleaseDC( pDC ) ;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…