View Full Version : dragging objects

I'm trying to get objects to drag with the mouse - the following doesn't work - can anyone make a suggestion/modification

dragObjects( int x, int y)

{

GLdouble modelMatrix[16];

GLdouble projMatrix[16];

GLint viewport[4];

GLdouble winx1, winy1, winz1;

GLdouble winx2, winy2, winz2;

int real_y;

glGetIntegerv(GL_VIEWPORT,viewport);

glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);

glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);

real_y = viewport[3] - (GLint)y-1;

gluUnProject((GLdouble)x, (GLdouble) real_y, 0.0, modelMatrix, projMatrix,

viewport, &winx1, &winy1, &winz1);

gluUnProject((GLdouble)x, (GLdouble)real_y, 1.0, modelMatrix, projMatrix,

viewport, &winx2, &winy2, &winz2);

}

thanx

Kilam Malik

01-05-2001, 04:16 AM

What you are doing here is getting the coordinates of a line in the object space where you picked. Whats missing here is the dragging itself. I assume, you want to drag the object, that it stays on your mouse pointer. That means, you have two points p1 and p2. At p1 you begin your dragging, at p2 you end your dragging. Then you do the gluunproject on p1 and p2, which gives you po1 and po2. The distance between this two points is the vector you have to move your object.

The z-value, you need for gluunproject can be any as long as you work with orthographic. Otherwise you have to use the z-value where the object is (but in the z-buffer, not in object-space!).

Hope that helps,

Kilam.

could you give an example of point coordinates say I have the following object -

(i'm using perspective view -how will this be different?)

glTranslated(1.0, 0.5, -5.0);

glTranslated(1.0, 0.0, m_ObjZoom1);

glTranslated(0.0, 1.0, m_ObjZoom2);

glRotated (m_xObjRotate, 1.0, 0.0, 0.0);

glRotated (m_yObjRotate, 0.0, 1.0, 0.0);

glTranslated(m_ObjPanX, 0,0);

glTranslated(0, m_ObjPanY,0);

DrawObject( 0.3625, 0.338, 0.273, 0.033,0.032, 0.272,0.337, 0.0575, 0.0325,

0.1225,0.1205);

cheers

Originally posted by fox:

could you give an example of point coordinates say I have the following object -

(i'm using perspective view -how will this be different?)

Yes. As previous mentioned you must obtain a valid z_coord in world-space for glUnproject. You can obtain it by using glreadPixels (but there must be something rendered! othwerwise the z-buffer will be empty) or by interpreting the SELECT-results. (you previous made a selection-rendering?) There you get a minZ and a maxZ.

Use this z-coord or the glUnproject-statement in your sample-code.

hope it helps.

what parameters does glreadPixel take and would be it part of the above function dragobjects?

- yes i had implemented a selection buffer but now i'm thinking of using gluunproject - also what should i set the z-value to? could you clarify further - i've been struggling with this for months!

thanx for any help

chennes

01-08-2001, 06:05 AM

Originally posted by fox:

- yes i had implemented a selection buffer but now i'm thinking of using gluunproject - also what should i set the z-value to?

As mentioned earlier, when you get the array back from the select buffer two of the values returned are the min and max depth values of the hit. You probably want to use the minimum value (I believe it is the second value in the array) with gluUnproject, although I've bever used the unproject stuff myself.

I hope this helps some.

Chris

Kilam Malik

01-08-2001, 07:24 AM

Pseudocode to resolve drag vector:

x1, y1 is where the mouse move starts.

x2, y2 is where the mouse move ends.

First get the z-vector for both. If you use orthogonal projection, you can use any z-value. Use glReadPixels for perspective projection, but flip the y-value before:

GLfloat z1 = 0.0f;

x = x1;

y = windowSizeY - y1;

glReadPixels(x-1, y-1, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z1);

This only works, if the object you want to drag is under your mouse. Otherwise glReadPixels will give you 0.0 (or 1.0?).

Another possibility would be to get the z-value from your object. You sure have the z of the center of your object. Make gluProject with it and you have the desired value.

Then you have z1 and z2.

Then make gluUnproject for x1,y1,z1 and x2,y2,z2. You get xp1,yp1,zp1 and xp2,yp2,zp2. This are the points you have picked, but transformed into your 3d-space. Then substract the two vectors and you have your distance to move the object.

Kilam.

[This message has been edited by Kilam Malik (edited 01-08-2001).]

what do you mean by flipping the y-axis also do i need to use gluProject if I'm already using gluUnProject and how do I obtain the coordinates of an object say if it has the following coordinates

glTranslated(1.0, 0.5, -5.0);

glTranslated(1.0, 0.0, m_Zoom1);

glTranslated(0.0, 1.0, m_Zoom2);

glRotated (m_xRotate, 1.0, 0.0, 0.0);

glRotated (m_yRotate, 0.0, 1.0, 0.0);

glTranslated(m_PanX, 0,0);

glTranslated(0, m_PanY,0);

DrawBox( 0.3625, 0.338, 0.273, 0.033,0.032, 0.272,0.337, 0.0575, 0.0325,0.1225,0.1205);

Kilam Malik

01-09-2001, 06:28 AM

Originally posted by fox:

what do you mean by flipping the y-axis also do i need to use gluProject if I'm already using gluUnProject and how do I obtain the coordinates of an object say if it has the following coordinates

glTranslated(1.0, 0.5, -5.0);

glTranslated(1.0, 0.0, m_Zoom1);

glTranslated(0.0, 1.0, m_Zoom2);

glRotated (m_xRotate, 1.0, 0.0, 0.0);

glRotated (m_yRotate, 0.0, 1.0, 0.0);

glTranslated(m_PanX, 0,0);

glTranslated(0, m_PanY,0);

DrawBox( 0.3625, 0.338, 0.273, 0.033,0.032, 0.272,0.337, 0.0575, 0.0325,0.1225,0.1205);

1) The coordinate system of opengl is flipped on the y-axis related to windows gdi coordinate system. So when you get the mouse coordinate y you have to subtract it from the window size yw: y_real = yw - y;

2) I use gluProject to obtain the coordinates in 3d of an object (if drawn in perspective!). Say your box has the center 0.4, 0.4, 0.4. Then you do a

gluProject(0.4, 0.4, 0.4, modelMat, projMat, viewPort, &wx, &wy, &wz);

Then you get the z-coordinate of your cube in wz.

Kilam.

what would the order of operation be looking at my dragObjects code above - i'm really not sure where to go from here.

I can e-mail some code if any one can help me sort this mouse dragging

cheers

[This message has been edited by fox (edited 01-10-2001).]

[This message has been edited by fox (edited 01-11-2001).]

any one out there - help!! http://www.opengl.org/discussion_boards/ubb/confused.gif

[This message has been edited by fox (edited 01-11-2001).]

Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.