0

I'm trying to left click and drag any 3d Object and if i let go of it, it should stay in its new position, how would i achieve this? The 3d object is loaded from the draw function that i have in a header file.

Someone said i should be using glutMouseFunc or glutMotionFunc.

void MouseClickCallbackFunction(int button, int state, int x, int y)
{

    if (state == GLUT_DOWN) {

    if (button == GLUT_LEFT)
    {
        std::cout << "Left " << x << " " << y <<std::endl;
        leftClick.x = x;
        leftClick.y = y;
    }
    else if (button == GLUT_RIGHT_BUTTON) {

        std::cout << "Right " << x << " " << y << std::endl;
        rightClick.x = x;
        rightClick.y = y;

    }
}
    theGame->mouseClicked(button, state, x, y);
    glutPostRedisplay();

}

/* function MouseMotionCallbackFunction()
 * Description:
 *   - this is called when the mouse is clicked and moves
 */
void MouseMotionCallbackFunction(int x, int y)
{
    theGame->mouseMoved(x, y);
    glutPostRedisplay();
}

int main(int argc, char **argv)
{
    /* initialize the window and OpenGL properly */
    glutInit(&argc, argv);
    glutInitContextVersion(4, 2);
    glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
    glutCreateWindow("OpenGL Framework");

    glewExperimental = true;
    if (glewInit() != GLEW_OK)
    {
        std::cout << "GLEW could not be initialized. \n";
        system("pause");
        return 0;
    }

    //glewInit();

    std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;

    /* set up our function callbacks */
    glutDisplayFunc(DisplayCallbackFunction);
    glutKeyboardFunc(KeyboardCallbackFunction);
    glutKeyboardUpFunc(KeyboardUpCallbackFunction);
    glutMouseFunc(MouseClickCallbackFunction);
    glutMotionFunc(MouseMotionCallbackFunction);
    glutTimerFunc(1, TimerCallbackFunction, 0);

    /* init the game */
    theGame = new Game();
    theGame->initializeGame();

    /* start the game */
    glutMainLoop();
    return 0;
         }

Here is the Draw fucntion with the object

void Game::draw()
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    PassThrough.Bind();
    PassThrough.SendUniformMat4("uModel", MonkeyTransform.data, false);
    PassThrough.SendUniformMat4("uView", CameraTransform.GetInverse().data, false);
    PassThrough.SendUniformMat4("uProj", CameraProjection.data, false);

    PassThrough.SendUniform("uTex", 0);
    PassThrough.SendUniform("LightPosition", CameraTransform.GetInverse() * vec4(4.0f,0.0f,0.0f,1.0f));
    PassThrough.SendUniform("LightAmbient", vec3(0.15f, 0.15f, 0.15f));
    PassThrough.SendUniform("LightDiffuse", vec3(0.7f,0.1f,0.2f));
    PassThrough.SendUniform("LightSpecular", vec3(0.8f,0.1f,0.1f));
    PassThrough.SendUniform("LightSpecularExponent", 50.0f);
    PassThrough.SendUniform("Attenuation_Constant", 1.0f);
    PassThrough.SendUniform("Attenuation_Linear", 0.1f);
    PassThrough.SendUniform("Attenuation_Quadratic", 0.01f);

    glBindVertexArray(Monkey.VAO);
    glDrawArrays(GL_TRIANGLES, 0, Monkey.GetNumVerticies());
    glBindVertexArray(0); 

    PassThrough.unBind();
    glutSwapBuffers();
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Allz.24
  • 1
  • 6
  • take a look at this [Compute objects moving with arrows and mouse](https://stackoverflow.com/a/50908533/2521214) – Spektre Nov 15 '18 at 06:11

1 Answers1

0

The concept is called mouse picking. A method to achieve this is ray casting. Essentially what you want to do is map the mouse coordinates to a region in your scene. In your case you want to check if the position is within the monkey. Then it is as simple as handling the mouse up, down, and move events.

Mouse down: check if object is being picked. If it is, great- maybe highlight it or something. Store ref to object picked; store position of mouse at beginning of pick (pos)

Mouse move: is mouse down? Is picked object ref? Update object position based on delta between pos and coord in mouse move event

Mouse up: clear picked obj ref, position

This link may be of interest http://antongerdelan.net/opengl/raycasting.html

Klathzazt
  • 2,415
  • 19
  • 27