0

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

sanjeev mk
  • 4,276
  • 6
  • 44
  • 69
  • If you're using `glBegin`, you're not using OpenGL 4. Please try to learn the newer API. – Overv Aug 13 '14 at 14:45
  • I'm using `glBegin` from the tutorial I came across. My system has both the libraries, so this code was able to compile. – sanjeev mk Aug 13 '14 at 14:46
  • You can use `glBegin` because you're using a *compatibility* OpenGL context, but it's not a modern OpenGL API. – Overv Aug 13 '14 at 14:49
  • Yes, I got that. This is just some trial code, but I do eventually have to use the new API only.. But the question is more about the general workflow for transformations.. – sanjeev mk Aug 13 '14 at 14:50
  • A good start is for example to use the GLM (OpenGL Mathematics) library so you can work with your own matrices, points and vectors. – Grimmy Aug 14 '14 at 17:36

1 Answers1

5

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();
ratchet freak
  • 47,288
  • 5
  • 68
  • 106
  • Thanks.. How do I do the same with the shader based setup? Can you point me to some good resources for the new API? – sanjeev mk Aug 13 '14 at 14:54
  • [their wiki has a good setup](http://www.opengl.org/wiki/Main_Page) to do this in a shader based setup you will need to maintain your own matrices and multiply the vertexes by it in the vertex shader – ratchet freak Aug 13 '14 at 14:56