sanjeev mk
sanjeev mk

Reputation: 4336

OpenGl : Rotate line about endpoint

I'm new to OpenGL , and trying out some experimental code. I'm using Open GL 4+ . I was able to draw a line using this :

    glBegin(GL_LINES);
    glVertex3f(0.0,0.0,0.0);
    glVertex3f(1.0,1.0,0.0);
    glEnd();

Now I want to rotate this line about one of it's endpoints, like the hand of a clock. How do I go about doing this? I've searched online, but there's nothing that actually demonstrates simple line rotation. I tried using glRotatef but I think I'm missing out on some key thing, because the glRotatef had no effect on the line drawn. All tutorials seem to start with Triangles (not points and lines)

What is the OpenGL workflow for doing these things? I keep coming across sample code where matrices are being used, but I don't exactly get the purpose.. I mean, do I have to explicitly keep track of my latest matrices, or is abstracted away by OpenGL and internally taken care of?

I understand the math and the role of transformation matrices, but I'm getting confused about the matrices' role in writing OpenGL code. If there are functions like glRotatef , why explicitly specify matrices?

It'd be really helpful to have some resources that explain everything from the very basic - points, lines then polygons etc

Upvotes: 0

Views: 4381

Answers (1)

ratchet freak
ratchet freak

Reputation: 48186

standard disclaimer: don't use glBegine/glEnd family of functions instead migrate to the shader based setup.

but here is the way to rotate (left out the parameters)

glPushMatrix();
glTranslate();//tranlate by -p where p is the point you want to rotate about
glRotate();//rotate by some degrees
glTranslate();//tranlate back by p

glBegin(GL_LINES);
glVertex3f(0.0,0.0,0.0);
glVertex3f(1.0,1.0,0.0);
glEnd();

glPopMatrix();

Upvotes: 5

Related Questions