본문 바로가기

old drawer/OpenGL

[OpenGL] 윈도우 사이즈 변경에 따른 왜곡 방지

void MyReshape(int NewWidth, int NewHeight){
 glViewport(0,0,NewWidth, NewHeight);
 GLfloat WidthFactor = (GLfloat)NewWidth / (GLfloat)400;
 GLfloat HeightFactor = (GLfloat)NewHeight / (GLfloat)400;

 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glOrtho(-1.0*WidthFactor, 1.0*WidthFactor, -1.0*HeightFactor, 1.0*HeightFactor, -1.0, 1.0);
}

int main(int argc, char** argv){
 glutInit(&argc, argv);
 glutInitDisplayMode(GLUT_RGB);
 glutInitWindowSize(400, 400);
 glutInitWindowPosition(300, 300);
 
 glutCreateWindow("OpenGL Drawing Example");
 glClearColor(1.0,1.0,1.0,0.0);
 glutDisplayFunc(MyDisplay);
 
 //윈도우 사이즈 변경에 따른 왜곡 방지
 glutReshapeFunc(MyReshape);

 glutMainLoop();
 return 0;
}