Setting up GLUT - 2
glutReshapeFunc(ChangeSize)
void ChangeSize(int w, int h)
{
GLfloat windowHeight,windowWidth;
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Keep the square square.
// Window is higher than wide
if (w <= h)
{ windowHeight = 250.0f*h/w;
windowWidth = 250.0f;
}
// Window is wider than high
else
{ windowWidth = 250.0f*w/h;
windowHeight = 250.0f;
}
// Set the clipping volume
glOrtho(-windowWidth, windowWidth, -windowHeight, windowHeight, 1.0f, -1.0f);
// All future transformation will affect our models (what we draw)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity();
}
Explanation
1) We check to see if h == 0, if it is, we make it 1
2) We call void glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
x - The number of pixels from the left side of the window to start the viewport
y - The number of pixels from the bottom side of hte window to star tthe viewport
This function sets the portion of a window that can be drawn in by openGL (clipping window)
In our case here, we want the entire window to be drawable by openGL, so we supply glViewPort(0, 0, w, h)
3) Reset the viewing volume - call glLoadIdentity()
A call to glLoadIdentity() is needed because the glOrtho function (which is used to set the clipping volume of our drawing window is accumulative, meaning it will be influenced by any previous settings to the clipping volume). [ToDo: How is it accumulative?]
void ChangeSize(int w, int h)
{
GLfloat windowHeight,windowWidth;
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Keep the square square.
// Window is higher than wide
if (w <= h)
{ windowHeight = 250.0f*h/w;
windowWidth = 250.0f;
}
// Window is wider than high
else
{ windowWidth = 250.0f*w/h;
windowHeight = 250.0f;
}
// Set the clipping volume
glOrtho(-windowWidth, windowWidth, -windowHeight, windowHeight, 1.0f, -1.0f);
// All future transformation will affect our models (what we draw)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity();
}
Explanation
1) We check to see if h == 0, if it is, we make it 1
2) We call void glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
x - The number of pixels from the left side of the window to start the viewport
y - The number of pixels from the bottom side of hte window to star tthe viewport
This function sets the portion of a window that can be drawn in by openGL (clipping window)
In our case here, we want the entire window to be drawable by openGL, so we supply glViewPort(0, 0, w, h)
3) Reset the viewing volume - call glLoadIdentity()
A call to glLoadIdentity() is needed because the glOrtho function (which is used to set the clipping volume of our drawing window is accumulative, meaning it will be influenced by any previous settings to the clipping volume). [ToDo: How is it accumulative?]

0 Comments:
Post a Comment
<< Home