private final int[] viewport = new int[4];
private final double[] modelview = new double[16];
private final double[] projection = new double[16];
private final double p0_X[] = new double [1];
private final double p0_Y[] = new double [1];
private final double p0_Z[] = new double [1];
private final double p1_X[] = new double [1];
private final double p1_Y[] = new double [1];
private final double p1_Z[] = new double [1];
private int height;
private int x;
private int y;
private final Ray rayo = new Ray();
//---------------------------------------------------------------------
// EL origen del rayo esta dado por la posición de la cámara
// gluLookAt(....)
//--------------------------------------------------------------------
private void generarDireccionRayo(GL gl,GLU glu){
gl.glGetDoublev( GL.GL_MODELVIEW_MATRIX, modelview );
gl.glGetDoublev( GL.GL_PROJECTION_MATRIX, projection );
gl.glGetIntegerv( GL.GL_VIEWPORT, viewport );
// height, es la altura de la ventana
int val = glu.gluUnProject(x,height-y,0,
modelview, projection, viewport,
p0_X , p0_Y , p0_Z);
if(val == GL.GL_FALSE)
throw new RuntimeException("Error en gluUnproject()");
val = glu.gluUnProject(x,height-y,1,
modelview, projection, viewport,
p1_X , p1_Y , p1_Z);
if(val == GL.GL_FALSE)
throw new RuntimeException("Error en gluUnproject()");
rayo.direction.x = (float)(p1_X[0] - p0_X[0]);
rayo.direction.y = (float)(p1_Y[0] - p0_Y[0]);
rayo.direction.z = (float)(p1_Z[0] - p0_Z[0]);
rayo.direction.normalize();
}
public void mouseClicked(MouseEvent e) {
x = e.getX();
y = e.getY();
}