Studying the book OpenGL SuperBible fram Addison-Wesley
, I read:
each call to glTranslate is cumulative on the modelview matrix
what does it mean?
Does it mean that for example this code:
glTranslatef(2.0,3.0,0);
glTranslatef(4.0,5.0,0);
first moves an object that is on the origin to the point (2,3,0)
and then translates it from the (2,3,0)
to (2+4,3+5,0+0) = (6,8,0)
not from the origin again?
Is this true about glScalef
and glRotatef
too?
for example this code:
glScalef(2.0,3.0,4.0);
glScalef(3.0,4.0,5.0);
first turn a 1x1x1
cuboid to a 2x3x4
cubic rectangle and then turns this cubic rectangle to a 6x12x20
one?
And at last, Does this code mean that a total 75 degrees rotation around the x-axis?
glRotatef(30.0,1,0,0);
glRotatef(45.0,1,0,0);
the most importantant: Does calling glLoadIdentity() before each call of these functions cancels these feature?
I mean Do you think this code assures that each time translates will be done from the origin?
, scale changes will be done from the initial state?
void COpenGLControl::ZoomToFullExtent()
{
float zoom1 = (float)oglWindowWidth/(float)ImageWidth;
float zoom2 = (float)oglWindowHeight/(float)ImageHeight;
m_fZoom = min(zoom1,zoom2);
m_fZoomInverse = 1/m_fZoom;
m_fPosX = 0;
m_fPosY = 0;
OnDraw(NULL);
}
void COpenGLControl::FixedZoomIn()
{
m_fZoom = 2*m_fZoom;
m_fZoomInverse = 1/m_fZoom;
OnDraw(NULL);
}
void COpenGLControl::FixedZoomOut()
{
m_fZoom = 0.5*m_fZoom;
m_fZoomInverse = 1/m_fZoom;
OnDraw(NULL);
}
void COpenGLControl::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (WantToPan)
{
if (m_fLastX < 0.0f && m_fLastY < 0.0f)
{
m_fLastX = (float)point.x;
m_fLastY = (float)point.y;
}
diffX = (int)(point.x - m_fLastX);
diffY = (int)(point.y - m_fLastY);
m_fLastX = (float)point.x;
m_fLastY = (float)point.y;
if (nFlags & MK_MBUTTON)
{
m_fPosX += (float)0.2f*m_fZoomInverse*diffX;
m_fPosY += (float)0.2f*m_fZoomInverse*diffY;
}
OnDraw(NULL);
}
CWnd::OnMouseMove(nFlags, point);
}
void COpenGLControl::OnDraw(CDC *pDC)
{
// TODO: Camera controls
wglMakeCurrent(hdc,hrc);
glLoadIdentity();
gluLookAt(0,0,1,0,0,0,0,1,0);
glScalef(m_fZoom,m_fZoom,1.0);
glTranslatef(m_fPosX, m_fPosY, 0.0f);
wglMakeCurrent(NULL, NULL);
}
Pay special attention to OpenGL API functions that include the description: "does ... to the current ...".
OpenGL is a glorified state machine, and things like bound objects and matrices (in legacy OpenGL) retain their state. When you make a call to
glTranslatef (...)
it multiplies the current matrix (defined by the matrix mode and the top of your matrix stack). Unless you issueglLoadMatrixf (...)
,glLoadIdentity (...)
or modify the matrix stack,glTranslatef (...)
will simply accumulate everytime you call it.glLoadIdentity (...)
will replace the current matrix with its identity:If you setup your transform matrices every frame, it is generally required that you do this. Otherwise, all of your transformations will be relative to the previous state (though this is sometimes desired).