I have tried to do a reshape function in OpenGL to resize my figure, but when I resize the window the figure is deformed and I don't know why.
The code is below:
void myReshape(int width, int height){
// Calculates the ratio
//
GLfloat ratio;
// We adjust viewport to new dimensions
//
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Checking dimensions
// Case 1: Width > Heigth
// Case 2: Height > Width
//
if (width > height)
{
ratio = (GLfloat) width / (GLfloat) height;
printf("Ratio1: %f\n", ratio);
glOrtho(-ratio, ratio, -1.0f, 1.0f, 0.0f, 0.0f);
}
else
{
ratio = (GLfloat) height / (GLfloat) width;
printf("Ratio2: %f\n", ratio);
glOrtho(-1.0f, 1.0f, -ratio, ratio, 0.0f, 0.0f);
}
glMatrixMode(GL_MODELVIEW);}
glOrtho(-dx,dx,-dy,dy,-dz,dz) defines a "box", which is adjusted by OpenGL internals to fit the viewport(width, height). This is, "dx" will be fitted to "width" and "dy" to "height".
If you want no deformation, but a simple scale, then the ratio in measurements must be observed: ratio = width/height = dx/dy
. So, avoid that if-else based on the ratio .
Use:
ratio = (GLFloat)width / height;
glOrtho(-ratio, ratio, -1.0f, 1.0f, 0.0f, 0.0f); //this near and far, better -1, 1
If you don't want to resize your figure when the viewport changes, then use the same distances as viewport:
GLfloat dx = (GLfloat)width / 2;
GLfloat dy = (GLfloat)heigth / 2;
glOrtho(-dx, dx, -dy, dy, -1, 1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.