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 3 of 3

Thread: to have the box move not having to hold the mouse button down

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2002
    Location
    Windsor
    Posts
    6

    to have the box move not having to hold the mouse button down

    // This is my program.
    // when I click and hold the middle button down. I want to box to rotate itself with out having to hold it down. I m trying to use idle fucntion.
    Please give me pointers on how I should make it work? below is my program.
    thx

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <GL/glut.h>

    GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
    GLfloat eyeX=0.0, eyeY=0.0, eyeZ=10.0;
    GLfloat dx=0.0, dy=0.0, dz=0.0;
    GLfloat ty, tx, tz;
    int angle;
    int spinning=0;
    void myInit(int *, char **);
    void mouseManager(int, int, int, int);
    void displayFunc(void);
    void idle(void);


    int main(int argc, char **argv){

    myInit(&argc, argv);

    glutDisplayFunc(displayFunc);
    glutMouseFunc(mouseManager);
    glutIdleFunc(idle);
    glutMainLoop(); //infinite loop
    }


    void displayFunc(void){
    int i;
    static done=0;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(40.0, // field of view
    640.0/480.0, // Aspect ratio
    1.0, // Near Z
    16.0 // Far Z
    );
    /*
    glMatrixMode(GL_PROJECTION); // virtual camera
    glLoadIdentity();
    glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2);
    */

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(eyeX, eyeY, eyeZ, // Eye is at (0, 0, 10)
    0.0, 0.0, 0.0, // center is at (0,0,0)
    0.0, 1.0, 0.0); // up is Y direction


    glTranslatef( tx, -ty, tz);
    glRotatef(xAngle, 1.0, 0.0, 0.0);
    glRotatef(yAngle, 0.0, 1.0, 0.0);
    glRotatef(zAngle, 0.0, 0.0, 1.0);

    if(done)
    glCallList(1);
    else{
    done=1;
    glNewList(1, GL_COMPILE_AND_EXECUTE);
    glBegin(GL_QUADS);



    glColor3f(0.75, 0.75, 0.5);


    //left

    glVertex3f(1.0, -1.0, 1.0);
    glVertex3f(1.0, -1.0, -1.0);
    glVertex3f(1.0, 1.0, -1.0);
    glVertex3f(1.0, 1.0, 1.0);



    glColor3f(1.0, 0.9, 0.0);

    //right side
    glVertex3f(-1.0, -1.0, 1.0);
    glVertex3f(-1.0, -1.0, -1.0);
    glVertex3f(-1.0, 1.0, -1.0);
    glVertex3f(-1.0, 1.0, 1.0);




    //front
    glColor3f(1.0, 0.0, 0.0);
    glVertex3f(-1.0, 1.0, 1.0);
    glVertex3f(1.0, 1.0, 1.0);
    glVertex3f(1.0, -1.0, 1.0);
    glVertex3f(-1.0, -1.0, 1.0);



    //back
    glColor3f(0.0, 1.0, 0.0);
    // glTranslatef( tx, 0.0, 0.0);
    glVertex3f(-1.0, 1.0, -1.0);
    glVertex3f(1.0, 1.0, -1.0);
    glVertex3f(1.0, -1.0, -1.0);
    glVertex3f(-1.0, -1.0, -1.0);


    //top
    glColor3f(0.0, 0.0, 1.0);

    glVertex3f(-1.0, 1.0, 1.0);
    glVertex3f(1.0, 1.0, 1.0);
    glVertex3f(1.0, 1.0, -1.0);
    glVertex3f(-1.0, 1.0, -1.0);


    //bottom
    glColor3f(0.5, 0.5, 0.5);

    glVertex3f(-1.0, -1.0, 1.0);
    glVertex3f(1.0, -1.0, 1.0);
    glVertex3f(1.0, -1.0, -1.0);
    glVertex3f(-1.0, -1.0, -1.0);



    glEnd();
    glEndList();
    }
    /*
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(-1.0, -1.0, -12);
    glRotatef(2*xAngle, 1.0, 0.0, 0.0);
    glRotatef(2*yAngle, 0.0, 1.0, 0.0);
    glRotatef(2*zAngle, 0.0, 0.0, 1.0);
    */
    glCallList(1);

    glutSwapBuffers();
    }

    void mouseManager(int button, int state, int x, int y){
    float alpha;
    alpha=-(angle*3.1415926)/180;
    switch(button){
    case GLUT_LEFT_BUTTON:

    if(state==GLUT_DOWN){
    // xAngle+=2.0;
    // dx=0.4;
    // spinning=1;
    // moveX+=10.0;

    angle=0; tx=0; ty=0; tz=0; eyeY=0, eyeZ=10;

    }
    else{
    dx=0.0;
    spinning=0;
    }
    break;

    case GLUT_RIGHT_BUTTON:
    if(state==GLUT_DOWN)
    { //dy=0.4;
    // yAngle+=2.0;
    spinning=1;
    // moveX-=10.0;

    }
    else{
    dy=0.0;
    spinning=0;
    }
    break;

    case GLUT_MIDDLE_BUTTON:
    if(state==GLUT_DOWN){
    dz=0.4;

    // zAngle+=2.0;
    spinning=1;
    }
    else{
    dz=0.0;

    spinning=0;
    }

    break;
    defaut:
    ;
    }

    displayFunc();
    }

    void myInit(int *argc, char **argv){
    glutInit(argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(100, 150);
    glutCreateWindow(argv[0]);

    glClearColor(1.0,1.0,1.0,0.0); // Set clear color to white
    angle=0;
    ty=0;
    tx=0;
    tz=0;

    glEnable(GL_DEPTH_TEST);
    }
    void idle(void){
    float alpha;
    alpha=-(angle*1.1)/180;
    if(spinning==1){
    xAngle+=dx;
    yAngle+=dy;
    zAngle+=dz;
    // tx=tx + cos(alpha);
    // tz=tz + sin(alpha);

    /*
    light0_position[0]-=dl;
    light0_position[1]-=dl;
    if(light0_position[0] <-10){
    light0_position[0]=10;
    light0_position[1]=10;
    }
    */
    };
    glutPostRedisplay();
    }

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: to have the box move not having to hold the mouse button down

    I prefer to use the glutTimerFunc over the glutIdleFunc. One reason is that you have better control on the rate of updating the animation. You can set the glutimerfunc every X milliseconds.

    I think your problem is in your mouse function and you change the state of your object. You have to remember that the mouse function is called every time the state of the mouse changes. In your code from the way it looks, as soon as you release the mouse button your turns the rotation off.
    You may want to add a flip-flop type state, this will change the state each time the button is pressed. first press start's rotation, second press stops rotation.

    Example code to add:

    if ((spinning = 1) && (state == GLUT_DOWN)
    {
    spinning = 0; Stop rotation
    break;
    }

    if ((spinning = 0) && (state == GLUT_DOWN)
    {
    spinning = 1;
    break;
    }


    Your code with error pointed out:

    case GLUT_MIDDLE_BUTTON:
    if(state==GLUT_DOWN){
    dz=0.4;

    // zAngle+=2.0;
    spinning=1;
    }
    else{
    dz=0.0;

    spinning=0; // turns off spinning when button is released.
    }

    break;


    [QUOTE]Originally posted by OpenGL_Student:
    [B]// This is my program.
    // when I click and hold the middle button down. I want to box to rotate itself with out having to hold it down. I m trying to use idle fucntion.
    Please give me pointers on how I should make it work? below is my program.
    thx


    [This message has been edited by nexusone (edited 03-08-2002).]

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2002
    Location
    Windsor
    Posts
    6

    Re: to have the box move not having to hold the mouse button down

    Oh I see I will work on it and see how it goes. Thank you very much! I will get back to you on how I am with it!

Posting Permissions

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