dragging OBJECTS under the mouse

Hi
at the moment I can move individual objects but they move faster than the mouse pointer -what I would like now is to be able to move the objects under the mouse function - I have a selection buffer test-function which at the moment only output a message box stating the object that is selected or none if it is clicked elsewhere - how could I modify this function to select and drag objects

#define BUFSIZE 512
//pick objects
void CMyView: ickObjects( int x, int y)
{
GLuint selectBuf[BUFSIZE];
GLint hits, i,j, k;
GLint viewport[4];
int namez, z1,z2;

char linez[255], text[255], temp[255];


glGetIntegerv(GL_VIEWPORT,viewport);
glSelectBuffer(BUFSIZE,selectBuf);
glMatrixMode(GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
(void) glRenderMode(GL_SELECT);

//create a selection/picking region
gluPickMatrix((GLdouble) x, (GLdouble)(viewport[3] - y), 1.0, 1.0, viewport);
gluPerspective(70.0, m_AspectRatio, 1.0, 300.0) ;
Render(GL_SELECT);
hits = glRenderMode(GL_RENDER);

glMatrixMode(GL_PROJECTION);
glPopMatrix ();

glMatrixMode(GL_MODELVIEW);


sprintf (text, "Total No. of hits (codas under mouse): %d

", hits);
j=0;
for(i=0; i<hits; i++)
{
namez = selectBuf [j]; j++;
z1 = selectBuf [j]; j++;
z2 = selectBuf [j]; j++;
sprintf (temp, "Number of CODAS: %d
Min z:%d
Max z: %d
", namez, z1, z2);
for(k=0; k<namez; k++)
{
sprintf(linez, "CODA No:%d

", selectBuf[j]);
j++;
}
strcat(text, temp);
strcat(text, linez);
}
strcat(text, "

Object Names:
BOX1 - 1
BOX2 - 2
BOX3 - 3");
MessageBox( text, “Hits!”, MB_OK);

hits = glRenderMode(GL_RENDER);


glMatrixMode(GL_MODELVIEW);

}

cheers

I’m doing that by testing how long the distance between to pixels is. For that, I’m using gluUnproject for each of the two pixels. With that I get the distance between two pixel coordinates transformed to object coordinates.

[This message has been edited by Kilam Malik (edited 12-06-2000).]

Any code samples or examples of how you implemented it

Movement in pixels is dx,dy:

Point3D p1,p2;

PickPlane(Point2D(0,0),p1);
PickPlane(Point2D(dx,dy),p2);

moveX-=p2.x()-p1.x();
moveY+=p2.y()-p1.y();

void Pane3D::PickPlane(Point2D pIn,Point3D &pOut)
{
GLdouble x,y;
GLdouble ox,oy,oz;

x=pIn.x();
y=drawSizeY-pIn.y();

GLdouble emat[] ={1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1};
gluUnProject(x,y,1.0,emat,projMatrix,viewport,&ox,&oy,&oz);

pOut.x(ox);
pOut.y(oy);
pOut.z(oz);
}

I use the glOrtographic view. Maybe you have to play around with the third value of gluUnproject (here 1.0) which is the depth value of the object.

Good Luck!

could you explain the code a bit? thanx

any other way of doing this or any demos?

Ok, now with some comments:

// PickPlane gives you the 3D object space
// point for a 2D point on the screen.
void Pane3D::PickPlane(Point2D pIn,Point3D &pOut)
{
GLdouble x,y;
GLdouble ox,oy,oz;

// Conversion to OpenGL coordinate system:
x=pIn.x();
y=drawSizeY-pIn.y();

GLdouble emat[] ={1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1};

// Projection from the screen coordinates
// back in object space. The 1.0 as third
// parameter is the z-value. You should read
// it out of the backbuffer if you use
// gluPerspective view.
gluUnProject(x,y,1.0,emat,projMatrix,viewport,&ox,&oy,&oz);

pOut.x(ox);
pOut.y(oy);
pOut.z(oz);
}

Point3D p1,p2;

// Calculate the 3D points p1 and p2 for
// two points on the screen:
PickPlane(Point2D(0,0),p1);
PickPlane(Point2D(dx,dy),p2);

// Calculate the distance in the object space
// by substraction of the two 3d points
// returned before.
moveX-=p2.x()-p1.x();
moveY+=p2.y()-p1.y();

Kilam.

thanx for the comment i’ll try it out!

what are point2D and point3D defined as - also when using gluProject/gluUnProject don’t you need to use render mode?

thanx

Point2D and Point3D are user defined classes which I use as new types. You can also use x, y and z distinct.

Kilam.

what would the types of pIn and & pOut be?

could you talk me through the steps how you set up your selection buffer?

thanx

Originally posted by fox:
[b]what would the types of pIn and & pOut be?

could you talk me through the steps how you set up your selection buffer?

thanx[/b]

pIn and pOut are C++ classes which represent a point or vector. Think of it as a struct with the members x, y and z (double each).
You can implement the same without classes like
void Pane3D::PickPlane(double pInX, doubel pInY, double pInZ, double &pOutX, double &pOutY, double &pOutZ)

To the selection buffer: I don’t use it. This is an alternative method. With this you can drag the object directly under the mouse. I’m not sure if this is possible with the selection buffer instead. But I’m doing it this way and it works.

Kilam.

what is drawSizeY

[This message has been edited by fox (edited 12-20-2000).]

help!!