2

I made a fancy shape with OpenGL and I draw that shape this function:

drawShape(const Point & center, char radius, int points, int rotation)

Inside the function I have code that tells OpenGL where the vertexes are:

glBegin(GL_LINE_LOOP);
  glColor3f(1.0, 1.0, 1.0);
  glVertex2f(center.getX() + 0.0, center.getY() + 1.0);
  // more vertices
glEnd();

Now when I add glRotatef(rotation, 0.0, 0.0, 1.0), I would like just this shape that I have drawn to rotate on the screen. However, if I add it above glBegin() it rotates everything in the window. If I include all the code between glPushMatrix() and glPopMatrix() rotates the object, but around the center of the window. How can I rotate just the object that I have drawn?

1 Answer 1

5

You're doing OpenGL's job by having center.getX and such added to the values.

What you want is this:

glPushMatrix();
glTranslatef(center.getX(), center.getY(), 0.0f);
glRotatef(rotation, 0.0, 0.0, 1.0);

glBegin(GL_LINE_LOOP);
  glColor3f(1.0, 1.0, 1.0);
  glVertex2f(0.0, 1.0);
  // more vertices
glEnd();

glPopMatrix();

You can probably apply the radius by using a glScale matrix and assuming a radius of 1.0 in your glVertex calls.

1
  • In addition I suggest you learn to use vertex arrays and vertex buffer objects, and do away with immediate mode (i.e. no longer use glBegin…glEnd).
    – datenwolf
    Mar 27, 2012 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.