Results 1 to 3 of 3

Thread: Move object with mouse

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    2

    Move object with mouse

    Ive been searching the forum but with no luck I am afraid. I have a fairly simple question I would guess. I use the library "Shapelib" to load a shape-file and all I want to do is to be able to move the shapefile with the mouse. I click and hold the left-button and then when i move the mouse the shape should follow.
    Is there someone who can give me a point in the right direction on this one?
    My idea was to use glTranslatef(x,y,0.0) and then calculate how many pixels the mouse moved. But it didn't work that well.

    /Robert

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Move object with mouse

    Could u post what u did?
    Nobody will code it from scratch for u.
    Regards,
    Mobeen

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2011
    Posts
    2

    Re: Move object with mouse

    Absolutely. Mostly I was just looking to get some theoretical tips or hints on how to approach the problem and Im not sure if this helps in any way but maybe you get the idea.

    I thought I could just translate the coords here. It kind of works but its not very nice to look at. Its flickering and most of the time the shape-file just dissapears.
    Code :
    void Widget::draw()
    {
        glMatrixMode(GL_MODELVIEW);
        glTranslatef(offsetX,offsetY,0.0);
     
        glPushMatrix();
        //Loading shape-file
        .
        .
        .
        glEnd();
     
        glPopMatrix();
        glFlush();
    }

    Hopefully I got all the code that matters below.

    Code :
    void Widget::initializeGL()
    {
       glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //White
       glShadeModel(GL_FLAT);
       glEnable (GL_LINE_SMOOTH);
       glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
       glEnable (GL_BLEND);
       glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
       QGLWidget::setAutoBufferSwap(true);
       glDisable(GL_DEPTH_TEST);
       glPointSize(3);
    }
     
     
    void Widget::paintGL()
    {
         glClear(GL_COLOR_BUFFER_BIT);
         glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //White
         glLoadIdentity(); //Clear the matrix.
         draw();
    }
     
    void Widget::resizeGL(int width, int height)
    {
    	if(height<=0) height=1;
    	   glViewport (0, 0, (GLsizei) width, (GLsizei) height);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity ();
    	glOrtho(sBoundingBox.minX, sBoundingBox.maxX,
                    sBoundingBox.minY,sBoundingBox.maxY,-1,1);
    	glMatrixMode(GL_MODELVIEW);
    }
     
    void Widget::mouseMoveEvent(QMouseEvent *event)
    {
       //1st position
       startX = Mouse.x;
       startY = Mouse.y;
     
       // I convert the coordinates from mouse here 
       line2 = convCoords(event->x(),event->y());
     
       //2nd position
       Mouse.x = line2[0]; //Longitude
       Mouse.y = line2[1]; //Latitude
     
       offsetX = startX - Mouse.x;
       offsetY = startY - Mouse.y;
     
       update();
    }

Similar Threads

  1. How can I move the object along with the mouse?
    By mehmedean in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 01-11-2010, 03:15 AM
  2. move by mouse
    By zwe|sam in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 06-13-2003, 07:58 AM
  3. move object with a mouse
    By adityask in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-14-2002, 02:22 AM
  4. move object with a mouse
    By adityask in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 03-13-2002, 12:01 PM
  5. Render a menu bar & move an object using a mouse
    By Pik in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-09-2001, 08:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean