Rotating OpenGL about an object axis

I am struggling with object rotation. I want to illustrate the gyroscope readings from my AR Drone 2.0 with this little program. You may need to watch this short video first: http://youtu.be/HvrLS8Olr2c

Sliders for x, y, z rotation that are done with

glRotatef(rotationX, x_vect.x, x_vect.y, x_vect.z);
glRotatef(rotationY, y_vect.x, y_vect.y, y_vect.z);
glRotatef(rotationZ, z_vect.x, z_vect.y, z_vect.z);

      

where the values ​​are:

Vector x_vect = {1.0f, 0.0f, 0.0f};
Vector y_vect = {0.0f, 1.0f, 0.0f};
Vector z_vect = {0.0f, 0.0f, 1.0f};

      

You can download the full source code for this little app from here: http://ablage.stabentheiner.de/2013-01-21_openglimportmodel.zip

The problem is, if you rotate, say Y axis first, and then try to play with the X slider, it won't rotate the axis it is supposed to do (see C4D animation for comparison). I think this is because I do an X-turn first and then a Y-turn. Switching lines of code doesn't solve the problem, but pushes it towards a different axis.

+3


source to share


2 answers


This is because when you call glRotatef()

, the rotation of the object will rotate. Take a look at this beautiful death:
awesome d6

Imagine an x-axis going through 4 and 3 (opposite sides on d6 always add up to 7), a z-axis going through 6 and 1, and a y-axis going through 2 and 5.
If you write this code:

glRotatef(90,1,0,0);
glRotatef(90,0,1,0);

      



The cube will first rotate 90 degrees around the x-axis, making the 6 side up. However, the axis remains relative to the matrix, so the next call will rotate the matrix around the axis through 2 and 5, effectively rotating the 6 side to the right. When you include these calls and make the matrix rotate around it on the y-axis, the side with six will go right first and up.

Now, with that aside. What you can do is rotate the camera around the object, instead of rotating the object itself. I suggest reading about introducing camera for arcades. This seems like a good start.

+2


source


http://doc-snapshot.qt-project.org/4.8/opengl-grabber.html

http://doc-snapshot.qt-project.org/4.8/opengl-grabber-glwidget-cpp.html



When I was doing rotations in opengl in Qt the grabber example was very helpful. Here's a bit from glwidget.cpp.

void GLWidget::paintGL()
{
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 glPushMatrix();
// Here is the rotation code:
 glRotated(xRot / 16.0, 1.0, 0.0, 0.0);
 glRotated(yRot / 16.0, 0.0, 1.0, 0.0);
 glRotated(zRot / 16.0, 0.0, 0.0, 1.0);

 drawGear(gear1, -3.0, -2.0, 0.0, gear1Rot / 16.0);
 drawGear(gear2, +3.1, -2.0, 0.0, -2.0 * (gear1Rot / 16.0) - 9.0);

 glRotated(+90.0, 1.0, 0.0, 0.0);
 drawGear(gear3, -3.1, -1.8, -2.2, +2.0 * (gear1Rot / 16.0) - 2.0);

 glPopMatrix();
}

      

+1


source







All Articles