Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: rotate cube with mouse - opengl

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2007
    Posts
    1

    rotate cube with mouse - opengl

    Hi there,

    I have the following code and when ran a cube is shown and using the mouse and holding the left mouse button you can rotate it. The problem is that you can rotate the cube whether you click on the cube or the background.

    What i would like to do is rotate the cube only when the mouse pointer is clicked onto the cube. Reading up i have read that i would need to implement picking/selection techniques.

    Can anyone help me as i am haveing some trouble? I would then like to be able to move(drag) the cube with the mouse around the screen.

    Kind Regards
    Nick

    Code :
     
    #include <gl/glut.h>
     
    bool fullscreen = false;
    bool mouseDown = false;
     
    float xrot = 0.0f;
    float yrot = 0.0f;
     
    float xdiff = 0.0f;
    float ydiff = 0.0f;
     
    void drawBox()
    {
    glBegin(GL_QUADS);
     
    glColor3f(1.0f, 0.0f, 0.0f);
    // FRONT
    glVertex3f(-0.5f, -0.5f, 0.5f);
    glVertex3f( 0.5f, -0.5f, 0.5f);
    glVertex3f( 0.5f, 0.5f, 0.5f);
    glVertex3f(-0.5f, 0.5f, 0.5f);
    // BACK
    glVertex3f(-0.5f, -0.5f, -0.5f);
    glVertex3f(-0.5f, 0.5f, -0.5f);
    glVertex3f( 0.5f, 0.5f, -0.5f);
    glVertex3f( 0.5f, -0.5f, -0.5f);
     
    glColor3f(0.0f, 1.0f, 0.0f);
    // LEFT
    glVertex3f(-0.5f, -0.5f, 0.5f);
    glVertex3f(-0.5f, 0.5f, 0.5f);
    glVertex3f(-0.5f, 0.5f, -0.5f);
    glVertex3f(-0.5f, -0.5f, -0.5f);
    // RIGHT
    glVertex3f( 0.5f, -0.5f, -0.5f);
    glVertex3f( 0.5f, 0.5f, -0.5f);
    glVertex3f( 0.5f, 0.5f, 0.5f);
    glVertex3f( 0.5f, -0.5f, 0.5f);
     
    glColor3f(0.0f, 0.0f, 1.0f);
    // TOP
    glVertex3f(-0.5f, 0.5f, 0.5f);
    glVertex3f( 0.5f, 0.5f, 0.5f);
    glVertex3f( 0.5f, 0.5f, -0.5f);
    glVertex3f(-0.5f, 0.5f, -0.5f);
    // BOTTOM
    glVertex3f(-0.5f, -0.5f, 0.5f);
    glVertex3f(-0.5f, -0.5f, -0.5f);
    glVertex3f( 0.5f, -0.5f, -0.5f);
    glVertex3f( 0.5f, -0.5f, 0.5f);
    glEnd();
    }
     
    bool init()
    {
    glClearColor(0.93f, 0.93f, 0.93f, 0.0f);
     
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glClearDepth(1.0f);
     
    return true;
    }
     
    void display()
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
     
    gluLookAt(
    0.0f, 0.0f, 3.0f,
    0.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f);
     
    glRotatef(xrot, 1.0f, 0.0f, 0.0f);
    glRotatef(yrot, 0.0f, 1.0f, 0.0f);
     
    drawBox();
     
    glFlush();
    glutSwapBuffers();
    }
     
    void resize(int w, int h)
    {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
     
    glViewport(0, 0, w, h);
     
    gluPerspective(45.0f, 1.0f * w / h, 1.0f, 100.0f);
     
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }
     
    void idle()
    {
    if (!mouseDown)
    {
    xrot += 0.3f;
    yrot += 0.4f;
    }
     
    glutPostRedisplay();
    }
     
    void keyboard(unsigned char key, int x, int y)
    {
    switch(key)
    {
    case 27 : 
    exit(1); break;
    }
    }
     
    void specialKeyboard(int key, int x, int y)
    {
    if (key == GLUT_KEY_F1)
    {
    fullscreen = !fullscreen;
     
    if (fullscreen)
    glutFullScreen();
    else
    {
    glutReshapeWindow(500, 500);
    glutPositionWindow(50, 50);
    }
    }
    }
     
    void mouse(int button, int state, int x, int y)
    {
    if (button == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN)
    {
    mouseDown = true;
     
    xdiff = x - yrot;
    ydiff = -y + xrot;
    }
    else
    mouseDown = false;
    }
     
    void mouseMotion(int x, int y)
    {
    if (mouseDown)
    {
    yrot = x - xdiff;
    xrot = y + ydiff;
     
    glutPostRedisplay();
    }
    }
     
    int main(int argc, char *argv[])
    {
    glutInit(&amp;argc, argv);
     
    glutInitWindowPosition(50, 50);
    glutInitWindowSize(500, 500);
     
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
     
    glutCreateWindow("13 - Solid Shapes");
     
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutSpecialFunc(specialKeyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(mouseMotion);
    glutReshapeFunc(resize);
    //glutIdleFunc(idle);
     
    if (!init())
    return 1;
     
    glutMainLoop();
     
    return 0;
    }

  2. #2
    Intern Newbie
    Join Date
    Jul 2001
    Posts
    37

    Re: rotate cube with mouse - opengl


  3. #3
    Junior Member Newbie
    Join Date
    Sep 2010
    Posts
    14

    Re: rotate cube with mouse - opengl

    Thanks for the sharing your code.

    One question If i may ask, what is the purpose of the idle function:

    void idle()
    {
    if (!mouseDown)
    {
    xrot += 0.3f;
    yrot += 0.4f;
    }

    Isn't the cube rotation is already implemented in the mouseMotion function?

    Thanks again

  4. #4
    Intern Newbie
    Join Date
    Aug 2009
    Posts
    37

    Re: rotate cube with mouse - opengl

    ALi
    It looks like idle() tests if one of the buttons on the mouse is being held down, if it is NOT (held down) then idle() will put a slow rotation on the object in both the positive x and y directions. If that's not what you need, you could always rewrite this idle() function to reset your cube to the original state, when the window first opened. Just add your cube's original coordinates, move it to that position, and then zero out all the rotation values.
    Focus, results in clarity of vision.

  5. #5
    Junior Member Newbie
    Join Date
    Apr 2011
    Posts
    1

    Re: rotate cube with mouse - opengl

    Hello,
    I also have the same question as Nick has (in the first post).
    I want to rotate or move a cube only when we click somewhere ON it, and not if we click in the background.
    Can someone please help me with this. Any pointers would be really really appreciated.
    Thanks and Regards,
    Vineet

  6. #6
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,716

    Re: rotate cube with mouse - opengl

    Let me make sure I understand this.

    You felt the need to unearth a post from 6 months ago, which itself was a reply to a post from 3 years before it. All to ask a question that the second post in the thread answers.

  7. #7
    Junior Member Newbie
    Join Date
    Apr 2011
    Posts
    13

    Re: rotate cube with mouse - opengl

    Actually, what you want is a picking test. If you want to know whether an location is inside an object, you have to cope with picking mode, name stack. Have a look at functions: glRenderMode, glInitNames, glSelectBuffer, glPushName, glPopName, glLoadName...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •