Advertisement Jump to content
Sign in to follow this  

OpenGL Moving items withthe mouse (OpenGL (gluUnProject related))

This topic is 4237 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings! This is my first post on this forum and I am a beginner in OpenGL. I have gotten the basics (I think) and can put some shapes on the screen and position them programmatically. I now want to be able to click on an item (say a sphere) on the screen and move the item around the screen using the mouse. I have so far managed to get my head around and the code working which selects which items has been clicked. I am having a hard time with mapping the screen coordinates to object coordinates. At the moment the following is the code I am using to try and achieve this.

GLdouble modelMatrix[16];
GLdouble projMatrix[16];
int viewport[4];
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);
GLdouble objX, objY, objZ;

gluUnProject(m_MouseNew.x, viewport[3] - m_MouseNew.y, 1, modelMatrix, projMatrix, viewport, &objX, &objY, &objZ);

//posX is used later to transform the sphere when it gets drawn
m_p3DObjects[m_nSelection]->posX = objX;

Unfortunately, this does not work as expected. When farther the mouse gets from the centre of the screen, the further the sphere moves away from the centre of the screen. The difference becomes greater for more off-centre positions. What am I doing wrong? Does anybody have example code of a drag-move code for OpenGL? Thanks in advance, Aristotel

Share this post

Link to post
Share on other sites
Hmm thats quite a question there

Well i could not tell you what is going on with your code :p

What i can try and do is give you an idea. from what i understand the object follows the mouse. if this is correct then peraphs trying something like this idea.

hide mouse

Get Current Mouse postion
Check differnce from center of screen
Set mouse postion back to center of screen

use differnce to move object selected


When object unselected
Show mouse and disable move object func and or calls.

just mabye a thought not sure if it will help.

as i sead just an idea :)

Share this post

Link to post
Share on other sites
Hi there,

No the sphere drawn does not follow the mouse, it only follows the mouse's direction, but not distance from the centre of the screen. Assume the sphere is in the centre of the screen. I click on if with the mouse, holding the button down.If I drag the mouse say 5 pixels away from the centre of the screen, the sphere moves in the same direction, but 10 pixels(example). If I move the move 10 pixels, the sphere will move like 30 pixels. If I move the mouse 20 pixels, the sphere might move 60 pixels off-centre.

So the direction seems to follow correctly, but not the distance...where the perfect alignment occurs at the centre of the screen. Did that make any more sense?

Thanks once again

Share this post

Link to post
Share on other sites
Ahh i c well for the most part im not sure why that happens as im sure you are not but i might be able to help you out still.

but ill need to know what compiler you are useing for debugging purposes!

Share this post

Link to post
Share on other sites
Ok so heres what you can do my compileres newer but there bolth vc micrsoft so.

Put a break line where your function is that calculats the move of the mouse or where ever your getting the data that effects the spheres movment. you can just right click on the codeing window and should be able to add one no problem.

now i don't know if you know how to use the debugger or not but by inserting a break point and then debugging your program will run up to that spot and then you can slowly step through the code . -- Introduction to Debugging

its a nice atricle on debugging.

well ill try an continue anyways but if you dont get any of what im saying read the article above ok.

as you step through your code you general have two options step over and step in

step over will just jump over function and still run them you just wont have to watch it. step in will take you into a function and you will go through it step by step.

ok so that sead.

what im explaing this to you for is in my compilere although it should be more or less the same. i hit alt 5 and i get a small window at the bottum that shows me all of my local variables and there values. if you can track down where your values are getting higher then they should it will be easyer to fix the problem :)

well hopefuly that made some kind of sence.

Share this post

Link to post
Share on other sites
PS on another note what and where did you learn how to load in objects i havent found anthing decnet that isnt a milk shake 3d model loader id like a 3ds object loader or something like that. so if you know anything please let me know!

Hmm peraphs i should have done that earlyer like look up the specific type of file i was trying to load ... funny how that search came back with much more of what i was looking for............

Share this post

Link to post
Share on other sites
I will soon be working on a similar problem. Right now in my engine there is a local coordinate system that shows up for my selected model with XYZ control points. I click and drag these to move the models.

Right now I have a hack version that just moves in the selected axis by how ever much the mouse moves in the X screen direction. But eventually I would like to be perfectly accurate like you are trying to achieve (currently my results behave like yours).

Begin reasonable thinking:
What you'll want to do is get a vector in world space from your screen space, so this will require two unprojects.

gluUnProject( oldMouse.x, viewport[3] - oldMouse.y, objectpick.depth,
modelMatrix, projMatrix, viewport,
&oldWorldX, &oldWorldX, &oldWorldX);

gluUnProject( newMouse.x, viewport[3] - newMouse.y, objectpick.depth,
modelMatrix, projMatrix, viewport,
&newWorldX, &newWorldX, &newWorldX);

worldMoveVectorX = newWorldX - oldWorldX;
worldMoveVectorY = newWorldY - oldWorldY;
worldMoveVectorZ = newWorldZ - oldWorldZ;

worldMoveVector should now contain a vector in world space that correlates to the screen vector created by a mouse move.

Assuming the above works correctly, adding this vector to your objects position will move it with the mouse in screen space.

My twist on it is that I want to be able to move in object space. So next (this is where I become increasingly unclear) I think I'd need to project the world space vector onto the selected object move axis vector. And the resultant magnitude is how far in the selected axis I'd need to move to correspond to the mouse movement.

Hope that helped some, and let us know when you find a good solution.

P.S. Jouei, I got mine from

edit: I changed the Z component of unproject to objectpick.z, I believe this is where you problem may stem. If you are using a perspective matrix. The deeper into the scene your selected object is, the greater the corresponding world vector will be for any given screen vector. By generating the vector at the object's depth, this should be more accurate.

P.P.S, what the hell? So slow it's painful.

Share this post

Link to post
Share on other sites
Yeah i have been having trouble finding a tutorial that ether a works or b is just poorly writen

Gametutorials is nice but it is no longer free.

i wish it was :( but i do not have 70$ to by a cd full of code goodies.

ps i need more caffine getting tired in my search..

Share this post

Link to post
Share on other sites
Hi guys, thanks for your replies...

The project I was working on was at the office, and uses MFC,but now I am home whee I only have Visual Studio Express 2005 which doesn't support MFC. I will try and recreate the same project in a VS 2005 Express project, that does not need MFC to set up the windowing etc.

Regarding Debuging, no worries, I have been a software engineers for a few years now, but mostly trying to learn a bit of OpenGL..thats the only thing I am having issues with.

Regarding loading objects, I didn't..I only used a cone and a sphere.

Over the weekend I will set up a VS2005 project and try out honayboyz suggestion as that seems to be what I was missing out on. I will let you all know how it goes and at that point I can share the VS2005 project with you all.

Hope to let you on the progress soon..

Thanks again!


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!