Results 1 to 4 of 4

Thread: Cannot pick object using ray casting method

  1. #1
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Cannot pick object using ray casting method

    Hi all,
    I tried to solve the riddle myself but still cant solve it. Basically i am trying to pick object by casting ray into the scene. I have read a couple of threads in this forum like this one http://www.opengl.org/discussion_boards/...true#Post279322 but still it aint working for me.
    I m using opengl3.3 core profile. I define my own UnProject function as follows,
    Code :
    //invMVP is inverse of MVP matrix
    //win[x,y] range from -[win_width/2,win_height/2] to [win_width/2,win_height/2] 
    winz is obtained using
    glReadPixels( x, height-y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
     
    void UnProject(float winx, float winy, float winz,  float* objXYZ) {
       GLint viewport[4];
       glGetIntegerv(GL_VIEWPORT, viewport);
     
       M3DVector3f tmp;
     
       tmp[0] = ((2*winx-viewport[0])/float(viewport[2])) -1;
       tmp[1] = ((2*winy-viewport[1])/float(viewport[3])) -1;
       tmp[2] = 2*winz-1;
     
       m3dTransformVector3(objXYZ,tmp,invMVP);
    }
    After these calls, i generate a ray from the cam position (viewRay.o) in the ray direction. So here is the whole thing can anyone tell me if I am missing something.
    Code :
    int window_x = x - width/2.0;
    int window_y = (height - y) - height/2.0;
    glReadPixels( x, height-y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
    UnProject(window_x,window_y,winZ,objPt);
    viewRay.d.x = objPt[0]-viewRay.o.x;
    viewRay.d.y = objPt[1]-viewRay.o.y;
    viewRay.d.z = objPt[2]-viewRay.o.z;

    Note that i have checked my winZ values and they are correct.
    Regards,
    Mobeen

  2. #2
    Senior Member Regular Contributor
    Join Date
    Dec 2007
    Posts
    258

    Re: Cannot pick object using ray casting method

    colour picking for the win ..
    You are making life hard for yourself. Inverse projection isn't really that accurate anyway.

  3. #3
    Junior Member Newbie
    Join Date
    Mar 2011
    Posts
    5

    Re: Cannot pick object using ray casting method

    hi

    you can use this page, I think it's very helpful:

    http://www.opengl.org/resources/faq/.../selection.htm

  4. #4
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: Cannot pick object using ray casting method

    Hi all,
    I managed to solve the problem. I was not dividing the os coordinates (after multiplication with the inverse MVP) by os.w now it is perfectly fine. I found this wiki page http://www.opengl.org/wiki/GluProjec...UnProject_code to be helpful. Here is my UnProject code
    Code :
    void UnProject(float winx, float winy, float winz, GLint* viewport, float* objXYZ) {
       M3DVector4f tmp;
     
       tmp[0] = (2.0f*((winx-viewport[0])/float(viewport[2]))) -1;
       tmp[1] = (2.0f*((winy-viewport[1])/float(viewport[3]))) -1;
       tmp[2] = (2.0f*winz)-1.0;
       tmp[3] = 1.0;
       m3dTransformVector4(objXYZ,tmp,invMVP);
       objXYZ[3]=1.0/objXYZ[3];
       objXYZ[0]*=objXYZ[3];
       objXYZ[1]*=objXYZ[3];
       objXYZ[2]*=objXYZ[3];   
    }
    Regards,
    Mobeen

Similar Threads

  1. Selection and pick object
    By dandosan in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 06-19-2013, 11:54 AM
  2. Problem to pick a line and arc object in opengl
    By kundi ropte in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 04-14-2011, 11:49 PM
  3. select/pick object
    By Irena in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 04-08-2010, 07:14 PM
  4. HELP! How to Pick object on the screen?
    By jojozhong in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 10-09-2000, 03:58 AM
  5. Hi, I need help on the pick object with mouse on the screen.
    By jojozhong in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 10-05-2000, 08:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean