How to rotate around the center of the screen using quaternions in opengl?

I am trying to use a rotation in arcball / trackball, but I have a problem with the center of rotation. I want the center to be the center of my screen, no matter what.

Let me explain what I have done so far.

I created a square (rotation axis: vector_start x vector_end, angle: vector_start * vector_end)

From this quaternion, I created a rotation matrix to use it with glMultMatrixf (matrix) and get the desired rotation.

The problem is that although my model seems to rotate around the arc, as it always rotates around its local origin. How can I make it rotate around the center of my screen no matter where its local origin is?

I believe that the solution to this problem may be to translate the entire rotation axis in the center of the screen, and then apply the rotation, but is this possible? Am I missing something here?

+6
rotation opengl quaternions arcball
source share
2 answers

You must solve this by applying rotation and transformation matrices in the correct order. In your code, you can translate back to the beginning of T (-pos_x, -pos_y, -pos_z), apply your rotation and again translate to the center of the object T (pos_x, pos_y, pos_z). This should work as a whole no matter how your rotation matrix is ​​built.

+1
source share

Here is the code I wrote some time ago for a three-stage launch rocket. I got most of the information from http://www.euclideanspace.com/maths/geometry/rotations

Note: hovering, feed and roll may change for you based on the setting of your coordinate system.

// Assuming the angles are in radians. double p = curPitch * Math.PI/180.0 / 2.0; double y = curYaw * Math.PI/180.0 / 2.0; double r = curRoll * Math.PI/180.0 / 2.0; double sinp = Math.sin(p); double siny = Math.sin(y); double sinr = Math.sin(r); double cosp = Math.cos(p); double cosy = Math.cos(y); double cosr = Math.cos(r); Vector3 axis = new Vector3(); //here the important part: how you get your quaternion vector! axis.x = sinr * cosp * cosy - cosr * sinp * siny; axis.y = cosr * sinp * cosy + sinr * cosp * siny; axis.z = cosr * cosp * siny - sinr * sinp * cosy; //now normalize the vector in case we want to use it again later axis = Vector3.normalizeVector(axis); orientation[1] = axis.x; orientation[2] = axis.y; orientation[3] = axis.z; //w is omega: the angle to rotate about the quaternion double w = cosr * cosp * cosy + sinr * sinp * siny; w = Math.acos(w) * 2.0; orientation[0] = w; gl.glPushMatrix(); //translate object first, then rotate it. gl.glTranslated(curDisplacement[0] + saveDisplacement[0], -curDisplacement[1] + saveDisplacement[2], curDisplacement[2] + saveDisplacement[1]); //this order might be messed up because I screwed up my coordinate system, but the idea is still there gl.glRotated(orientation[0]*180/Math.PI, orientation[2]*180/Math.PI, orientation[3]*180/Math.PI, orientation[1]*180/Math.PI); //place your objects gl.glPopMatrix(); 

Hope this helps!

0
source share

All Articles