user3085497
user3085497

Reputation: 121

C++ OpenGL Line Rotate on its axis

trying to use glRotatef to turn lines around on their own axis, like a drill, but unable to figure it out.

|   (p2).__________.(p3)
|       /  ^-line2
|      /
|     /<--line1
|    /
|   /
|  .(p1)
|_______________

I want line 2 to spin around line 1 no matter what angle line 1 is at.

using OpenGLs glRotatef(), has p2 and p3 spin around p1.

Variables I use for ines.

 struct Line{
     float x,y,z, angle,length;
     int parent;
  };
  vector<Line>line;
  //Read file set everthing for lines...

Upvotes: 2

Views: 2177

Answers (2)

Andon M. Coleman
Andon M. Coleman

Reputation: 43319

First you need to translate line 2 so that (p2) is at the origin before rotating it, because what you actually want to do here is pivot line 2 around (p2).

Then, you need to define the axis of rotation, this will basically be (p2-p1) and will form the x,y,z parameters in glRotatef (...).

After that, you rotate some angle around said axis and finally translate the line back to its original position.

Your solution will probably look something like this:

//
// Draw Line 1
//

float rX, rY, rZ;

rX = p2.x - p1.x;
rY = p2.y - p1.y;
rZ = p2.z - p1.z;

glPushMatrix   ();

  glTranslatef (        p2.x,  p2.y,  p2.z);
  glRotatef    (angle,    rX,    rY,   rZ );
  glTranslatef (       -p2.x, -p2.y, -p2.z);

  //
  // Draw Line 2
  //

glPopMatrix    ();

Before you get the wrong idea and tell me I did this in reverse, keep in mind that OpenGL uses column-major matrices and post-multiplication. This means that the effective order transformations are applied in actually reads from bottom-to-top. The matrix operations still occur in the order written, the math just works somewhat counter-intuitively due to the way GL implements matrix multiplication.

Also, the order of p2 and p1 when you compute the axis of rotation is important, because if you reverse them you will reverse the clockwise/counterclockwise orientation. So if your drill is spinning in the wrong direction, you should have a pretty good idea why ;)

Upvotes: 1

AudioGL
AudioGL

Reputation: 504

You can either do all of the math in C++, or you can use the matrices inside of OpenGL (glTranslatef, glRotatef, etc).

Bear in mind - those functions (glTranslatef and glRotatef) are deprecated, and are no longer the correct way to use OpenGL. Currently, the correct way is to get a c++ math library, and calculate the matrices yourself. Then send them up to a shader. It's hard to learn, but you'll thank yourself later.


If you want to calculate your rotation using simple math, just break out some typical trigonometry formulas, and calculate the positions of the points of each line. Then draw the lines.

Or, to use (the deprecated functions in) OpenGL, do some reading on how glTranslatef and glRotatef work. This is all covered in the OpenGL Red Book.

http://www.glprogramming.com/red/chapter03.html

Move down to the Viewing and Modeling Transformations heading in that chapter, and you will easily figure out how to answer your question.

Upvotes: 0

Related Questions