Funny glViewport

I hope someone can explain this to me because I feel rather stupid not being able to understand it. I create a window with another subwindow in it using glut. My main functions is this:

int mainWindow, infoWindow;

glutInitWindowPosition(0, 0);
glutInitWindowSize(600, 700);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInit(&argc, argv);
mainWindow = glutCreateWindow(“GLUT Window”);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(kInput);
glutMouseFunc(mInput);
init();
infoWindow = glutCreateSubWindow(mainWindow, 0, 600, 600, 100);
glutDisplayFunc(info_display);
glutReshapeFunc(info_reshape);

glutMainLoop();

My main window reshape (reshape) function is this:

glViewport(0, 50, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,
(GLdouble)w / (GLdouble)h, 10.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

and my info window reshape function (info_reshape) is this:

glClearColor(1, 1, 1, 1);
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-300, 300, -50, 50, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);

My problem is why do I have to write glViewport(0, 50, w, h) for my main window to appear centered instead of glViewport(0, 100, w, h)? I mean I create my main Window having w x h: 600x700. Then I create the subwindow on the bottom 100 pixels (glutCreateSubWindow(mainWindow, 0, 600, 600, 100)). Why the viewport coordinates are half of the window coordinates? Thanks in advance!

Every time in the display functions you should set correct window as active window.

so in mainDisplay();
glutSetActiveWindow(main_window_id);

and in infoDisplay();
glutSetActiveWindow(info_window_id);

R u doing this already?

  • Chetan

No, that was not the problem. I believe glut can keep track of what window to refresh depending on the function. Thanks for your help though. I found the solution, which was rather silly. The correct piece of code is:

glViewport(0, 100, w, h - 100);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,
(GLdouble)w / ((GLdouble)h - 100), 10.0f, 100.0f);

I was setting the lower left corner of the window at 0, 100 but the upper right had to be 600, 600, not 600, 700. Also I had to correct the aspect ratio at gluPerspective.