PDA

View Full Version : Moving selected objects in OpenGL



rakesh_thp
07-06-2009, 04:47 AM
Hi,
I am trying to develop a CAD like application using OpenGL and wxWidgets... I have reached at a stage where i can draw a circle, square, load an image, polygon etc.. am also able to zoom and pan..

as a further step, i want to move object, scale it and rotate.. to do this i have written a code to select and pick object that lies under mouse pointer.. Now what do i have to do to move the picked object..?? I tried by finding the difference of old and new points and recalculating the points of the object..


C.x += dx;
C.y+= dy;

where dx and dy are differences of old and new x, and y values resp..

This is showing some movement. but not as desired.. it isnt moving along with the mouse movement.. For a small mouse movement, it shows a huge displacement and also very fast... so what can be done to make a selected object move along wiith the mouse pointer...??

Also i'm not able to zoom with reference to the mouse pointer.. it zooms with reference to the origin (0,0,0) only.. please help me..

Thanks in advance..

Regards
Rakesh Patil

rakesh_thp
07-07-2009, 04:24 AM
I guess my way of explaining problem is not clear.. DO i need to explaing again..??

Salbris
07-07-2009, 09:54 AM
Are you using the glutMotionFunc ?

What you need to do is check whenever the mouse is moved AND if the player is holding the button you use to move objects. You also need to check if the user has already clicked on an object to move and is then holding the mouse to move it.

Here's the steps:
1. User mouse clicks down on object
2. Call function with mouse down
3. User keeps mouse held down and moves mouse
4. Call function with mouse motion
5. User stops holding mouse down
6. Call function with mouse up


Does that help?

SeanAustin
07-07-2009, 12:51 PM
void reshape(int w, int h)
{
glViewport(0, 0, (GLint) w, (GLint) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if ( h==0 )
gluPerspective(45, (GLdouble)w, 1.0, 2000.0);
else
gluPerspective(45, (GLdouble)w/ (GLdouble)h, 1.0, 2000.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void mouse ( int button, int state, int x, int y )
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
moving = 1;
startx = x;
starty = y;
}
if ( button == GLUT_LEFT_BUTTON && state == GLUT_UP )
moving = 0;

if ( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN )
{
moving = 2;
starty = y;
}
if ( button == GLUT_RIGHT_BUTTON && state == GLUT_UP )
moving = 0;
}


void motion ( int x, int y )
{
if ( moving == 1 )
{
angle = angle + ( x - startx );
angle2 = angle2 + ( y - starty );
startx = x;
starty = y;
glutPostRedisplay ( );
}
if ( moving == 2 )
{
zoom = zoom + ( y - starty );
starty = y;
glutPostRedisplay ( );
}
}



And make sure you call
glutMouseFunc ( mouse );
glutMotionFunc ( motion );

after init() in your main loop


And needed globals

float angle, angle2, cloud_rot;
float zoom = -25.50;

int moving, startx, starty;

rakesh_thp
07-08-2009, 02:18 AM
@ Salbris
am not using glutMotionFunc..

Infact am using VC++ 2008 along with wxWidgets and OpenGL.. So I got MouseEvents through which i can manage various mouse events..

@SeanAustin
Thanks for the trouble but what does that code does for right down event.?? zooming..?? If so does it gives me the desired effect.?? Secondly I'm using gluOrtho instead of gluPerspective.. So would der be any change in that case.?? Is 'glustPostRedisplay function similar to refreshing the canvas..?? what are angle and angle2..?? what should be their initial values..?? can they be used for rotating.??

THanks again for both of you guys..

Kind Regards
Rakesh Patil

Salbris
07-10-2009, 01:15 PM
After reading you first post again it seems you might just have the speed set too high. You said: "For little mouse movement there is a huge displacement and very fast."

There could be two problems here:

1. The speed that the mouse moves the object is too fast.

2. Or your not updating the position of the object every tick.