Cambiar el tamaño de una ventana OpenGL hace que se desmorone
Pregunta
Por alguna razón, cuando cambio el tamaño de mis ventanas OpenGL, todo se desmorona. La imagen está distorsionada, las coordenadas no funcionan y todo simplemente se desmorona. Estoy cantando Glut para configurarlo.
//Code to setup glut
glutInitWindowSize(appWidth, appHeight);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutCreateWindow("Test Window");
//In drawing function
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
//Resize function
void resize(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, h, 0);
}
La aplicación OpenGL es estrictamente 2D.
Así es como se ve inicialmente: http://www.picgarage.net/images /Corre_53880_651.jpeg
así es como se ve después de cambiar el tamaño: http://www.picgarage.net/ images / wrong_53885_268.jpeg
Solución
No debe olvidarse de enganchar el evento GLUT 'remodelar':
glutReshapeFunc(resize);
Y restablece tu ventana gráfica:
void resize(int w, int h)
{
glViewport(0, 0, width, height); //NEW
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, h, 0);
}
Una proyección en perspectiva debería tener en cuenta la nueva relación de aspecto:
void resizeWindow(int width, int height)
{
double asratio;
if (height == 0) height = 1; //to avoid divide-by-zero
asratio = width / (double) height;
glViewport(0, 0, width, height); //adjust GL viewport
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(FOV, asratio, ZMIN, ZMAX); //adjust perspective
glMatrixMode(GL_MODELVIEW);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow