View Full Version : Drag an object with mouse

07-04-2006, 03:14 PM
The code below is supposed to drag the object when the mouse is clicked and moved. I've an array called "objects" that store the coordinates of the objects. Basically I just compared the value and find out the object I'm pointing at. But when I run the program, it's always the first object that is chosen, become a very small dot on the screen and finally disappeared. Would you please help me?

int ox, oy, j;
int currObj;

void mouse(int button, int status, int x, int y)
{ int i;

if ((button == GLUT_LEFT_BUTTON) && (status == GLUT_DOWN))
{LMPressed = true;
ox = x;
oy = y;
for (i=0; i< Objnum + 1; i++)
{ if (((ox > objects[i].Objmax.x) &amp;&amp; (ox < objects[i].Objmax.x)) &amp;&amp;
((oy > objects[i].Objmax.y) &amp;&amp; (oy < objects[i].Objmin.y)))
j = i;
LMPressed = false;


void mouseMotion(int x, int y)
if (LMPressed) {
objects[j].Objmin.x = x;
objects[j].Objmin.y = y;
objects[j].Objmax.x = x + 0.4;
objects[j].Objmax.y = y + 0.4;
} I've actually tried to use the "Picking" as well but it doesn't work either, here's the code

void mouse(int button, int status, int x, int y)
{ int i;

if ((button == GLUT_LEFT_BUTTON) &amp;&amp; (status == GLUT_DOWN))
{LMPressed = true;

void processHits(GLint hits, GLuint buffer[], int x, int y)
unsigned int i;
int a;
double z;
GLuint names, *prt;

prt = (GLuint *) buffer;
for ( i = 0; i<hits; i++)
names = *prt;
a = (int) names;
z = (double) *prt/0x7fffffff;

objects[a].Objmin.x = (double) x;
objects[a].Objmin.y = (double) y;
objects[a].Objmin.z = z;
objects[a].Objmax.x = (double) x + 0.4;
objects[a].Objmax.y = (double) y + 0.4;
objects[a].Objmax.z = z +0.4;

void mouseMotion(int x, int y)
GLuint selectBuf[BUFSIZE];
GLint hits;
GLint viewport[4];

if (LMPressed == true)
glGetIntegerv(GL_VIEWPORT, viewport);
glSelectBuffer(BUFSIZE, selectBuf);
(void) glRenderMode(GL_SELECT);


gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y), 0.4, 0.4, viewport);
gluOrtho2D(0.0, 3.0, 0.0, 3.0);
drawObject(GL_SELECT, Objnum);


hits = glRenderMode(GL_RENDER);
processHits(hits, selectBuf, x , y);

07-05-2006, 03:40 AM
if (((ox > objects[i].Objmax.x) &amp;&amp; (ox < objects[i].Objmax.x)) &amp;&amp;
((oy > objects[i].Objmax.y) &amp;&amp; (oy < objects[i].Objmin.y)))you're using objects[i].Objmax.x twice. i think one should be objects[i].Objmin.x

07-05-2006, 04:34 AM
Hi RigidBody,

Thanks for pointing out that mistake,I've now change it to

if (((ox > objects[i].Objmin.x) &amp;&amp; (ox < objects[i].Objmax.x)) &amp;&amp;
((oy > objects[i].Objmin.y) &amp;&amp; (oy < objects[i].Objmax.y))) But the problems still remain. One thing I don't understand is, since I didn't change the value of z coordinate of the object, why the object will become so far away from camera? It's originally a cube and become a dot far away when I drag it! And again, it's always the first object the program has chosen.

Brian Paul
07-06-2006, 04:21 PM
My guess is you're fouling up your modelview/projection matrices somewhere. I'd double-check all the matrix-related calls, including glMatrixMode().

Also, use glGetError() to make sure you're not over/underflowing any matrix stacks.