Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,680 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,490 people online right now. Registration is fast and FREE... Join Now!




opengl w/ mfc, scene distorts when window resizes

 
Reply to this topicStart new topic

opengl w/ mfc, scene distorts when window resizes, How do I calculate the aspect ratio?

Brendan Davis
post 15 Nov, 2005 - 11:33 PM
Post #1


New D.I.C Head

*
Joined: 4 Nov, 2005
Posts: 4


My Contributions


I'm trying to adjust the opengl viewport in the event of a window resize, but I get a distortion. I think the problem is with the aspect ratio (cx/cy). It seems to have the same value no matter what I try to set it to.

CODE

void CMfc_basicView::OnSize(UINT nType, int cx, int cy)
{

CView::OnSize(nType, cx, cy);
   
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();

   wglMakeCurrent(m_hDC,m_hRC);    

   glViewport(0, 0, cx, cy);    
   
   gluPerspective(60.0f,
   (GLdouble)cx / (GLdouble)cy,  
   0.1f,
   1000.0f);

}



This post has been edited by Brendan Davis: 15 Nov, 2005 - 11:40 PM
User is offlineProfile CardPM

Go to the top of the page


NealC
post 9 Dec, 2005 - 09:04 PM
Post #2


New D.I.C Head

*
Joined: 9 Dec, 2005
Posts: 1


My Contributions


Here the code I use.
CODE

// ********************************************************************************
****************************************************
 void CMainWindow::OnSize(UINT nType, INT iX, INT iY)
// ********************************************************************************
****************************************************
//
//
 {
   // Set up the OpenGL size.

   if (SizeOpenGL(iX, iY)) m_bExitApplication = TRUE;

   // Default with a redraw.

   CWnd::OnSize(nType, iX, iY);

   Invalidate(FALSE);
 }

// ********************************************************************************
****************************************************
 BOOL CMainWindow::SizeOpenGL(INT iX, INT iY)
// ********************************************************************************
****************************************************
//
//
 {
   // Variable declaration.

   GLdouble gldAspect;
   GLsizei glnWidth, glnHeight;

   CPaintDC cPaintDC(this);

   // Select the rendering context.

   wglMakeCurrent(cPaintDC.m_ps.hdc, m_hRC);

   // Get the size of the client window.

   glnWidth  = GLsizei(iX);
   glnHeight = GLsizei(iY);
   gldAspect = GLdouble(glnWidth) / GLdouble(glnHeight);

   // Set up a projection matrix to fill the client window.

   glMatrixMode(GL_PROJECTION);

   glLoadIdentity();

   gluPerspective(     30.0,   // Field-of-view angle
                  gldAspect,   // Aspect ratio of view volume
                        0.1,   // Distance to near clipping plane
                   1.0e+10F ); // Distance to far clipping plane

   glViewport(0, 0, glnWidth, glnHeight);

   // Choose smooth and blended drawing.

   glEnable(GL_DEPTH_TEST);
   glEnable(GL_LINE_SMOOTH);
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

   // Set the light.

   GLfloat glfLocalAmbient[] = {0.20F, 0.20F, 0.20F, 1.0F};

   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glfLocalAmbient);

   GLfloat glfSpecular[] = {0.8F, 0.8F, 0.8F, 1.0F};
   GLfloat glfAmbient [] = {0.8F, 0.8F, 0.8F, 1.0F};
   GLfloat glfDiffuse [] = {0.7F, 0.7F, 0.7F, 1.0F};

   glEnable(GL_LIGHT0);

   glLightfv(GL_LIGHT0, GL_AMBIENT , glfAmbient );
   glLightfv(GL_LIGHT0, GL_DIFFUSE , glfDiffuse );
   glLightfv(GL_LIGHT0, GL_SPECULAR, glfSpecular);

   // Deselect the rendering context.

   wglMakeCurrent(NULL, NULL);

   return FALSE;
 }
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 07:43AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month