opengl - Rotating around a sphere using vectors and NOT glulookat

Tag: opengl , rotation , linear-algebra Author: a648916040 Date: 2013-05-02

I'm having an issue with drawing a model and rotating it using the mouse, I'm pretty sure there's a problem with the mathematics but not sure . The object just rotates in a weird way. I want the object to start rotating each click from its current spot and not reset because of the vectors are now changed and the calculation starts all over again.

void DrawHandler::drawModel(Model * model){
unsigned int l_index;
glMatrixMode(GL_MODELVIEW); // Modeling transformation
Point tempCross;
float tempInner= innerProduct(model->getBeginRotate(),model->getCurrRotate());
float tempNormA =normProduct(model->getBeginRotate());
float tempNormB=normProduct(model->getCurrRotate());
glRotatef(acos (tempInner/(tempNormA*tempNormB)) * 180.0 / M_PI,tempCross.getX(),tempCross.getY(),tempCross.getZ());

for (l_index=0;l_index < model->getTrianglesDequeSize() ;l_index++)

    Triangle t = model->getTriangleByPosition(l_index);
    Vertex a1 = model->getVertexByPosition(t.getA());
    Vertex a2 = model->getVertexByPosition(t.getB());
    Vertex a3 = model->getVertexByPosition(t.getC());
    glVertex3f( a1.getX(),a1.getY(),a1.getZ()); 
    glVertex3f( a2.getX(),a2.getY(),a2.getZ());
    glVertex3f( a3.getX(),a3.getY(),a3.getZ());



This is the mouse function which saves the beginning vector of the rotating formula

    void Controller::mouse(int btn, int state, int x, int y)
    if (btn==GLUT_LEFT_BUTTON){
                    (-float(x)*x - y*y + SPHERERADIUS*SPHERERADIUS < 0)? 0:float(sqrt(-float(x)*x - y*y + SPHERERADIUS*SPHERERADIUS))));

and finally the following function which holds the current vector. (the beginning vector is where the mouse was clicked at and the curr vector is where the mouse position at the moment )

void Controller::getMousePosition(int x,int y){
            (-float(x)*x - y*y + SPHERERADIUS*SPHERERADIUS < 0)? 0:float(sqrt(-float(x)*x - y*y + SPHERERADIUS*SPHERERADIUS))));

where sphereradius is the sphere radius O_O of 70 degress

is any calculation wrong ? cant seem to find the problem... thanks

Other Answer1

Why so complicated? Either you change the view matrix or you change the model matrix of your focused object. If you choose to change the model matrix and your object is centered in (0,0,0) of the world coordinate system, computing the rotation around a sphere illusion is trivial - you just rotate into the opposite direction. If you want to change the view matrix (which is actually done when you change the position of the camera) you have to approximate the surface points on the chosen sphere. Therefore, you could introduce two parameters specifying two angles. Everytime you click move your mouse, you update the params and compute the new locations on the sphere. There are some useful equations in [].

Other Answer2

Without knowing what library (or libraries) you're using your code is rather difficult to read. It seems you're setting up your camera at (0, 0, -250), looking towards the origin, then rotating around the origin by the angle between two vectors, model->getCurrRotate() and model->getBeginRotate().

The problem seems to be that in "mouse down" events you explicitly set BeginRotate to the point on the sphere under the mouse, then in "mouse move" events you set CurrRotate to the point under the mouse, so every time you click somewhere else, you lose the previous state of rotation because BeginRotate and CurrRotate are simply overwritten.

Combining multiple rotations around arbitrary different axes is not a trivially simple task. The proper way to do it is to use quaternions. You may find this primer on quaternions and other 3D math concepts useful.

You might also want a more robust algorithm for converting screen coordinates to model coordinates on the sphere. The one you are using is assuming the sphere appears 70 pixels in radius on the screen and that the projection matrix is orthographic.