Sign in to follow this  
bushimports

Using gluUnProject to get focus on Vertices in 3D

Recommended Posts

I have a program where I can move objects around in 3D but it drags the object reagardless of wheather the object is selected or not. What I want to do is have a way to first get focus(and highlight/change the color) with the passive mouse on whatever it is I want to select, then if the object is focused and the chosen mouse button goes down call the fuction to do the dragging. I can easily do everything in 2d without any special functions,but I would like to do everything in 3d. Actually I am not sure if gluProject/gluUnProject are what I need to use for this or not,and I am not sure I have done the functions for Project and UnProject properly. Below is the code I have right now
GLdouble Vertex::Project(GLdouble x,GLdouble y){
   GLdouble Modelview[16];
   GLdouble Projection[16];
   GLint Viewport[4];
   GLdouble z;
   glGetDoublev(GL_PROJECTION_MATRIX,Projection);
   glGetDoublev(GL_MODELVIEW_MATRIX,Modelview);
   glGetIntegerv(GL_VIEWPORT,Viewport);
   glReadPixels(x,Viewport[3]-y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&z);
   gluProject(vx,vy,vz,Modelview,Projection,Viewport,&x,&y,&z);
   return (x,y);
}

GLdouble Vertex::UnProject(GLdouble x,GLdouble y){
   GLdouble Modelview[16];
   GLdouble Projection[16];
   GLint Viewport[4];   
   GLdouble z;
   glGetDoublev(GL_MODELVIEW_MATRIX,Modelview);
   glGetDoublev(GL_PROJECTION_MATRIX,Projection);
   glGetIntegerv(GL_VIEWPORT,Viewport);
   glReadPixels(x,Viewport[3]-y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&z);
   gluUnProject(x,Viewport[3]-y,z,Modelview,Projection,Viewport,&vx,&vy,&vz);
   return (vx,vy);
}
              

void  Vertex::Mouse(int Button,int state,int x,int y){
float mousey = glutGet(GLUT_SCREEN_HEIGHT) -y;           

if((UnProject(x,y))&& Button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && Focused == true){
  offset[0] = vx - x;    // save where user clicked for dragging
  offset[1] = vy - mousey;
  ActMouse(x,y);
  ISelected = true; 
  glutSetCursor(GLUT_CURSOR_NONE);    
}
else if(Button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && Focused == false){
if(glutGetModifiers() != GLUT_ACTIVE_SHIFT){
ISelected = false;
glutSetCursor(GLUT_CURSOR_LEFT_ARROW); 
} 
}
glutPostRedisplay();
}                                               

void  Vertex::ActMouse(int x,int y){  
float mousey = glutGet(GLUT_SCREEN_HEIGHT) -y;    
if(ISelected == true){
UnProject(x,y);               
cout << "I am Selected " << ID << "\n";
SetPosition(offset[0]+x,offset[1]+mousey,vz); 
}
 glutPostRedisplay(); 
}

void  Vertex::PassMouse(int x,int y){    
float mousey = glutGet(GLUT_SCREEN_HEIGHT) -y;       
 float rad = 1.5;
 if((Project(x,y))&& (x >= vx-rad && (x <= vx + rad ))&&(y >= vy-rad && (y <= vy + rad))){
 Focused = true;
 }
 else{
 Focused = false;
 }
glutPostRedisplay();
} 

Any help here would be greatly appreciated,maybe some example code with a good explaination.Much Thanks, Jody Bush        



[Edited by - bushimports on April 30, 2010 7:08:01 PM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this