PDA

View Full Version : moving selected object with mouse



sumaia
09-13-2015, 08:06 AM
hello ,
i am very new in openGL, i have faced many problems while i was drawing and selecting and moving objects in 3D
first for selection , i faced a problem then it was solved correctly using processHits way (selection mode ) ,then i moved to the camera , and also it was done correctly , then one thing is left ,
moving the selected object , whatever the camera position is ...

let's suppose we have a vertex v, its current position is :
v.x,v.y,v.z

we select the vertex and we want to move it (of course i am not talking about vertex suppose it is a sphere and v is its center )
now we we move the mouse by dy, and dx, (dy =newmouseposition.y -oldmouseposition.y) and (dx =newmouseposition.x -oldmouseposition.x)
i want to move the object on plane XY
so if i did it this way :

v.x=v.x+dx ;
v.y=v.y+dy;
that is not totally okay cuz after moving a camera with some movements may be the x axis will be on vertical and y for horizantal or the become in an opposite way
i moved to raycast way ,but ait didnt work :


glm::vec3 myglwidget::raycast(int mouse_x, int mouse_y)
{

GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );

winX = (float)mouse_x;
winY = (float)viewport[3] - (float)mouse_y;

glReadPixels( mouse_x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

return glm::vec3(posX, posY, posZ);


}

so what is the solution for this kind of movement ?

Steven Katic
09-18-2015, 12:37 AM
hi,
it sounds like u are dealing with problems related to trying to perform transformations along a plane where the viewing plane gets near or on the perpendicular angle to it. I am Assuming you code is working as expected(i.e. bug free). it can get really awkward at this angle: so typically a user would re-orient the camera to a more reasonable angle to manipulated from.

Take a look at http://www.codeproject.com/Articles/35139/Interactive-Techniques-in-Three-dimensional-Scenes#thespecialcase

Although planes are explicitly used as a contrived concept in that link, your use of x, y implies the use of a plane. Does that sound reasonable?
it might help. Do you have any of the usual visual cues: axis widget/grid etc.. that help the brain stay "orientated" to what you see in the view port?

The above is a guess.

Steven Katic
09-18-2015, 05:19 AM
I think the major problem though is the disorientation you may experience especially at a position where the viewing plane(i.e. your camera) is rolled. Here's an example:

2104
hopefully you can see what's happening here:
The camera has been rolled 90 deg around the z axis(ok..it's y axis in opengl) oh...and slightly pitched 11.5 degrees up(but it actually a y axis yaw(because of the roll!) confusing? you bet. So now if you continue with your x, y code, left/right and up/down mouse movements are switched, not in 3d space, but only on the viewing plane (to the users detriment). So you are asking for a solution. There really isn't one. Even if you built in more visual cues than the ones illustrated to help the user stay mentally orientated to the 3d space, its rarely a useful way to move objects (making cinematic tracking videos where rolling becomes a special effect could be a exception.. others?). Otherwise, it is typical to maintain an orbiting camera around the objects you are moving in a more intuitive manner as seen in modelling packages like maya/max etc. What would you use it for? do you have something in mind?

Ahhh... the image no good, too small, is not effective. its a screen shot from a little app that interactively illustrates this kind of stuff in real time. send me an email and i'll reply by sending the zipped up app to you if you wish.