I am new to openGL programming, and I got a problem, which, despite my extensive search on the Internet and loads of tries, I can’t solve yet. It has to do with rotating a simple QUADS around its center. Here is my ReshapeFunc (the actual code is written in Fortran 2003 using the f03gl bindings, but that is irrelevant to the problem; I post the equivalent C++ code here):
... double w_d,h_d; w_d=(double)w; h_d=(double)d; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0d, w_d, h_d, 0.0d, 0.0d, -1.0d, 1.0d); glMatrixMode(GL_MODELVIEW); ...
I am not interested on 3D for now, so the window just shows the x-y plane. Now, the rendering (and IdleFunc) function is as follows:
... glPushMatrix(); glRotatef(angle, 0., 0., -1.); glBegin(GL_QUADS); glColor3f(0.5, 0.0, 1.0); glVertex2f(100., 100.); glColor3f(1.0, 0.0, 0.0); glVertex2f(100., 300.); glColor3f(0.0, 1.0, 0.0); glVertex2f(300., 300.); glColor3f(1.0, 1.0, 0.0); glVertex2f(300., 100.); glEnd(); glPopMatrix(); ...
where angle is updated each time, adding 1.0. Window size is (400,400). The above code works as expected: it sets the (x=0,y=0) point at the top left corner of the window, draws a 200x200 pixels rectangle at the center of the screen, and rotates it counterclockwise around (x=0,y=0). However, what I want is to rotate the rectangle around the center of the window, (x=200,y=200). I would expect that changing the glRotatef command to
glRotatef(angle, 200., 200., -1.);
would do. Unfortunately, it doesn’t work (the rectangle seems to rotate around another axis than a vector vertical to the center of the screen). Apparently, I am missing something important here. If the above code wouldn’t work as expected with glRotatef(angle, 0., 0., -1.), I would say I set viewing point incorrectly. However, it works perfectly with glRotatef(angle, 0., 0., -1.) but not with glRotatef(angle, 200., 200., -1.).
Note that if I change the code so that (0,0) is the center of the window,
glRotatef(angle, 0., 0., -1.);
will indeed work, rotating the rectangle around its center. However, what I want is to permanently place the (x=0,y=0) point at the top left corner of the window (as it is customary in all multimedia SDKs).
Any help will be greatly appreciated.