Thread: I'm lost with mouse motion OpenGL

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    11

    I'm lost with mouse motion OpenGL

    I want to be able to click on the teapot I have drawn in a room. So far, I can't get it to work. Anyone have any tips? This is killing me!

    Code:
    glPushMatrix();
    			glColor3fv(BLUE);
    			glTranslatef(5.0, 0.4, -5.0);
    			glScaled(0.3, 0.2, 0.3);
    			//recalcModelPos();
    			glutSolidTeapot(0.80);
    			//glutSwapBuffers();
    		glPopMatrix();
    Code:
    void mouse(int button, int state, int x, int y)
    {
    	if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    	{
    		mousemotion = 1;
    		mousex = x;
    		mousey = y;
    	}
    	if(button == GLUT_LEFT_BUTTON && state == GLUT_UP)
    	{
    		mousemotion = 0;
    	}
    }
    
    void motion(int x, int y)
    {
    	if(mousemotion)
    	{
    		xangle = xangle - (y - mousey);
    		yangle = yangle - (x - mousex);
    		mousex = x;
    		mousey = y;
    		glutPostRedisplay();
    	}
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    How are you applying xangle and yangle here?

    I'm presuming you can already use the mouse to, eg, move the view or a specific object.

    If you want to be able to click on a specific object and do something (I've never tried this) I think you are in for a bit of a challenge: you will have to figure out how to translate screen coordinates into 3D world coordinates.

    Not to sure where to start with that. I'm sure if you post to the "official forum"

    OpenGL coding: beginners - OpenGL.org Discussion and Help Forums

    someone else HAS done this before . I always use the relative change of the mouse position to affect coordinates, which I think is what you are trying to do here.

    In the meantime, one obvious thing you could do is throw a printf in (or use the debugger) to track the mouse x/y coords and figure out where the teapot is. A bit hackish since it won't help if you move things around, but it may provide some clues as to the real solution, which I guess must involve the "frustrum" (apt name) or something.

    If all you are looking to do is rotate the view around with the mouse, I can show you how to do that. It involves working out a ratio of how far you want to move, eg.
    Code:
    View.x += ((float)X - (float)MouseX )/4;
    Here "X" is the current position and "MouseX" was the previous position in the glut mouse move callback (ie, X was one of the parameters supplied by glut). A bigger denominator will give you greater precision, but less range back and forth, since the mouse pointer cannot go outside the bounds of the window. This is also totally dependent on how you have set up the perspective; I always use a glViewport with the height and width set the same as the window dimensions in pixels.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  2. opengl mouse
    By bluehead in forum Game Programming
    Replies: 4
    Last Post: 04-02-2005, 05:43 PM
  3. opengl question. mouse location vs gluperspective
    By revelation437 in forum Game Programming
    Replies: 1
    Last Post: 10-19-2004, 07:18 PM
  4. Mouse 'control' prob in OpenGL
    By gazsux in forum Game Programming
    Replies: 5
    Last Post: 04-17-2003, 10:00 AM
  5. Mouse in OpenGL?
    By TheGr8one in forum Game Programming
    Replies: 0
    Last Post: 11-01-2001, 07:19 PM