opengl - Preserve aspect ratio of 2D object on window resize -
i have windows (xp) application needs display two-dimensional rectangle within window. rectangle must not clipped (i.e. must lie within viewport), , must preserve aspect ratio on resize. currently, method handles layout distorts aspect ratio of rectangle match window. want rectangle scale window , centered in window (again, without clipping). method stands below. lwindist , lmaxdepth width , height of rectangle displayed (in 48ths of inch, if 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 x axis glmatrixmode( gl_modelview ); glloadidentity(); releasedc( pdc ) ; } }
this 1 should scale expected:
// g++ main.cpp -lglut -lgl #include <gl/glut.h> void display() { glclear( gl_color_buffer_bit | gl_depth_buffer_bit ); glmatrixmode( gl_projection ); glloadidentity(); double w = glutget( glut_window_width ); double h = glutget( glut_window_height ); double ar = w / h; glortho( -2 * ar, 2 * ar, -2, 2, -1, 1); glmatrixmode( gl_modelview ); glloadidentity(); glcolor3ub( 255, 0, 0 ); glbegin( gl_quads ); glvertex2i( -1, -1 ); glvertex2i( 1, -1 ); glvertex2i( 1, 1 ); glvertex2i( -1, 1 ); glend(); glutswapbuffers(); } int main( int argc, char **argv ) { glutinit( &argc, argv ); glutinitdisplaymode( glut_rgba | glut_depth | glut_double ); glutinitwindowsize( 640, 480 ); glutcreatewindow( "aspect ratio" ); glutdisplayfunc( display ); glutmainloop(); return 0; }
Comments
Post a Comment