PDA

View Full Version : rectangle by specifying center point



clintCJM
03-08-2007, 06:34 AM
I've got a 2D openGL window. I want to draw some rectangles (actually perfect squares), but they need to be rotated. I'm having trouble with this. This is what I've tried:

//rotate the window
glRotated(dblRotationAngle, 0.0, 0.0, 1.0);

//draw the rectangle
glRectd(x1, y1, x2, y2);

//restore original amount of rotation
glRotated(-dblRotationAngle, 0.0, 0.0, 1.0);

The problem is trying to determine what the rectangle vertices should be, now that the window is rotated. What would make this REALLY REALLY EASY is if there was a rectangle drawing function where only the center point of the square was passed in, as well as the length of each side, like this:

glRect(X,Y, sideLength);

Is there such a function? If not, how do I determine the two vertices of my rectangle once my window has been rotated?

Thanks very much.

k_szczech
03-08-2007, 12:53 PM
Try:

glPushMatrix();
glTranslatef(centerX, centerY, centerZ);
glRotatef(angle, 0, 0, 1);
glBegin(GL_QUADS);
glVertex2f(-size/2.0f, -size/2.0f);
glVertex2f( size/2.0f, -size/2.0f);
glVertex2f( size/2.0f, size/2.0f);
glVertex2f(-size/2.0f, size/2.0f);
glEnd();
glPopMatrix();You may also want to add:

glMatrixMode(GL_PROJECTION];
glLoadIdentity();
glScalef(0.5f / windowWidth, 0.5f / windowHeight, 1.0f);
glTranslatef(1.0f, 1.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();To get coordinates mapped to pixels on screen rather than to sandard (-1.0; 1.0) range.