PDA

View Full Version : Move object with mouse



roband915
01-25-2011, 05:58 AM
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

mobeen
01-25-2011, 06:05 AM
Could u post what u did?
Nobody will code it from scratch for u.

roband915
01-25-2011, 06:33 AM
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.


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.



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();
}