Sid133
Sid133

Reputation: 364

glutMotionFunc ''Flipping" Issue

I need some real OpenGL troubleshooting here. Am using OpenGL and Freeglut

I have a proper scene rotation (Kind of like 3pp ) using the combination of glutMotionFunc(); and glRotatef();.

Here is my rotation function:

float xrot = 0, yrot = 0, zrot = 0, lastx, lasty, lastz;


void mouseMovement(int x, int y)
{
    int diffx = x - lastx; 
    int diffy = y - lasty; 
    lastx = x;
    lasty = y;
    xrot += (float)diffy;
    yrot += (float)diffx;
}

My display function:

void display(void)
{
    glClearColor(0.0, 0.0, 0.0, 1.0);                   
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -cRadius);
    glRotatef(xrot, 1.0, 0.0, 0.0);
    glRotatef(yrot, 0.0, 1.0, 0.0);         
    glBegin(GL_LINES);
    ---------
    ----
    glEnd();
    glTranslated(-xpos, 0.0f, zpos);
    glutSwapBuffers();      
}

And finally the main function :

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Window");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMotionFunc(mouseMovement);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

Now when i load, the scene renders fine, rotation is happening smoothly when am clicking and dragging the scene with mouse(LMB).

The only issue is that, during the rotation of scene by dragging, the scene is flipping to a different position before starting to rotate. ie, when i click and drag again, the rotation of scene is not continuing from where I left it during the previous mouse drag event, instead it flips to some random xrot and yrot positions.

Hope am making it clear. If some could try and replicate the same and provide some insight on what could be the issue here, it would be helpful.

Here is my reshape function as well, just in case if anything is missing here as well

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (GLfloat)w / (GLfloat)h, 0.1, 500.0);
    glMatrixMode(GL_MODELVIEW);
}

Upvotes: 1

Views: 202

Answers (1)

Rabbid76
Rabbid76

Reputation: 211166

The issue is caused, because lastx and lasty are uninitialized. They do not state the previous muse position when the mouseMovement callback is called the 1st time, that causes that the 1st rotation is random.
Note, the values of lastx and lasty are read, before they are set:

int diffx = x - lastx; 
int diffy = y - lasty; 
lastx = x;
lasty = y;

You can solve this by implementing the glutMouseFunc callback. Set lastx and lasty in the call back, so the variables are initialized when a mouse button is pressed:

void mouseFunc(int button, int state, int x, int y) {
    lastx = x;
    lasty = y;
}
int main(int argc, char **argv) {
    // [...]

    glutMouseFunc(mouseFunc);

    // [...]
}

Upvotes: 2

Related Questions