PDA

View Full Version : dragging OBJECTS under the mouse



fox
12-06-2000, 02:20 AM
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: http://www.opengl.org/discussion_boards/ubb/tongue.gifickObjects( 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\n\n\n", 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\nMin z:%d\nMax z: %d\n", namez, z1, z2);
for(k=0; k<namez; k++)
{
sprintf(linez, "CODA No:%d\n\n", selectBuf[j]);
j++;
}
strcat(text, temp);
strcat(text, linez);
}
strcat(text, "\n\nObject Names:\nBOX1 - 1\nBOX2 - 2\nBOX3 - 3");
MessageBox( text, "Hits!", MB_OK);

hits = glRenderMode(GL_RENDER);


glMatrixMode(GL_MODELVIEW);
}

cheers

Kilam Malik
12-06-2000, 04:07 AM
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).]

fox
12-06-2000, 06:03 AM
Any code samples or examples of how you implemented it

Kilam Malik
12-06-2000, 06:41 AM
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!

fox
12-07-2000, 01:56 AM
could you explain the code a bit? thanx

fox
12-08-2000, 02:04 AM
any other way of doing this or any demos?

Kilam Malik
12-08-2000, 06:05 AM
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.

fox
12-08-2000, 07:08 AM
thanx for the comment i'll try it out!

fox
12-11-2000, 03:06 AM
what are point2D and point3D defined as - also when using gluProject/gluUnProject don't you need to use render mode?

thanx

Kilam Malik
12-11-2000, 05:27 AM
Point2D and Point3D are user defined classes which I use as new types. You can also use x, y and z distinct.


Kilam.

fox
12-14-2000, 04:40 AM
what would the types of pIn and & pOut be?

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

thanx

Kilam Malik
12-14-2000, 06:29 AM
Originally posted by fox:
what would the types of pIn and & pOut be?

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

thanx

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.

fox
12-14-2000, 08:06 AM
what is drawSizeY



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

fox
12-20-2000, 04:32 AM
help!!