user14026433
user14026433

Reputation:

Orbit a cube using mouse click and mouse drag (Opengl)

I want to move around a 3D cube holding the left mouse click + mouse drag. holding the left-click and dragging the mouse should allow the user to orbit horizontally and vertically. Currently, my code allows the 3D cube to appear, but it doesn't orbit the object when I click and drag. Here is my code:

void UMouseClick(int button, int state, int x, int y)
{
    if((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)){
         cout << "Left Mouse Button Clicked!" << endl;
         currentClickLeft = true; // this is my global variable
}

if((button == GLUT_LEFT_BUTTON) && (state == GLUT_UP)){
         cout << "Left Mouse Button Released!" << endl;
         currentClickLeft = false;
}

void UMouseMove(int x, int y)
{
   if(currentClickLeft == true && mouseDtected) {
        lastMouseX = x;
        lastMouseY = y;
        mouseDetected = false;

        mouseXOffset = x - lastMouseX;
        mouseYOffset = lastMouseY - y;

        lastMouseX = x;
        lastMouseY = y;

        mouseXOffset *= sensitivity;
        mouseYOffset *= sensitivity;

        yaw += mouseXOffset;
        pitch += mouseYOffset;

        front.x = 10.0f * cos(yaw);
        front.y = 10.0f * sin(pitch);
        front.z = sin(yaw) * cos(pitch) * 10.f;
}

What exactly is the problem here?

Also, I am new to this programming language, so if I haven't provided something please let me know.

Upvotes: 1

Views: 308

Answers (1)

Spektre
Spektre

Reputation: 51893

I would feel safer to have:

if ((currentClickLeft == true)&&(mouseDtected))

But the real problem is that you have:

lastMouseX = x;
lastMouseY = y;

before:

mouseXOffset = x - lastMouseX;
mouseYOffset = lastMouseY - y;

So your offsets are always zero !!! After this you do it again so my bet is that you just forgot to remove it.

Upvotes: 1

Related Questions