Picking Using A Ray

Hi

The opengl.org page:

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

describes a method for picking an object using a pick-ray.

I coded the described method but can’t get it to work; see code below.

To help see that the pick-ray is correct I added the method drawRay() which is called in display(). However, I don’t see any ray lines rendered and suspect I’ve misinterpreted the opengl.org description of object picking using a pick-ray.

Thanks.

Graham

===

public Ray3D mouseRayWorldCoordinates(GLAutoDrawable drawable)
{
    // [http://www.java-tips.org/other-api-tips/jogl/how-to-use-gluunproject-in-jogl.html](http://www.java-tips.org/other-api-tips/jogl/how-to-use-gluunproject-in-jogl.html)
    GL2 gl2 = drawable.getGL().getGL2();
    
    int viewport[] = new int[4];
    float mvmatrix[] = new float[16];
    float projmatrix[] = new float[16];
    int realy = 0;// GL y coord pos
    float wcoordNear[] = new float[4];// wx, wy, wz;// returned xyz coords
    float wcoordFar[] = new float[4];
    
    int x = xCoord;
    int y = yCoord;
    gl2.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
   
    gl2.glGetIntegerv(GL2.GL_VIEWPORT, viewport, 0);
    gl2.glGetFloatv(GL2.GL_MODELVIEW_MATRIX, mvmatrix, 0);
    gl2.glGetFloatv(GL2.GL_PROJECTION_MATRIX, projmatrix, 0);
    realy = viewport[3] - (int)y; /* note viewport[3] is height of window in pixels */
    
    System.out.println("Coordinates at cursor are (" + x + ", " + realy);
    boolean ok = glu.gluUnProject((float) x, (float)realy, 0.0f, //
        mvmatrix, 0,
        projmatrix, 0, 
        viewport, 0, 
        wcoordNear, 0);
    System.out.println("World coords at z=0.0 are ( " //
                       + wcoordNear[0] + ", " + wcoordNear[1] + ", " + wcoordNear[2]
                       + "); ok: " + ok);
    ok = glu.gluUnProject((float) x, (float) realy, 1.0f, //
        mvmatrix, 0,
        projmatrix, 0,
        viewport, 0, 
        wcoordFar, 0);
    System.out.println("World coords at z=1.0 are (" //
                       + wcoordFar[0] + ", " + wcoordFar[1] + ", " + wcoordFar[2]
                       + "); ok: " + ok);
    
    // direction vector is far point - near point
    Vector3D dirVector = new Vector3D(wcoordFar[0]-wcoordNear[0],wcoordFar[1]-wcoordNear[1],wcoordFar[2]-wcoordNear[2]);
    dirVector.normalise();
    Point3D viewerLocation = GLUtilities.cameraLocation(gl2);
    Ray3D mouseRay = new Ray3D(viewerLocation,dirVector);
    
    return mouseRay;
}


// method to draw a test ray
private void drawRay(Ray3D ray, GLAutoDrawable drawable)
{
    GL2 gl2 = drawable.getGL().getGL2();
    Vector3D rayDirection   = ray.getDirection();
    Point3D  rayStart       = ray.getOrigin();
    Point3D  rayEnd         = rayStart.pointAlongVector(rayDirection,100,1e-06);
    
    gl2.glColor3f( 1.0f, 0.0f, 0.0f );
    
    gl2.glBegin(GL2.GL_LINES);    // start drawing the line
    gl2.glVertex3d(rayStart.getX(),rayStart.getY(),rayStart.getZ());
    gl2.glVertex3d(rayEnd.getX(),rayEnd.getY(),rayEnd.getZ());
    gl2.glEnd();                 // finished drawing the line

}