gmseed
11-08-2011, 03:22 AM
Hi
A couple of questions:
1) If the camera is set at (0,0,0) and the negative z-axis points into the screen and the zNear and zFar planes are set to 1 and 1,000 using gluPerspective() then how can an object be viewed as we are looking down the -z axis with the near/far planes behind the viewer?
2) What is the relationship between the near and far z values set with glDepthRange() and those using gluPerspective()? I'm using the method described by opengl.org to develop a picking-ray using gluUnProject() with z=0 for the near plane and z=1 for the far-plane. Do I have to map the values returned by gluUnProject() to the near-far z clip planes set by gluPerspective()?
public Ray3D mouseRayWorldCoordinates()
{
GL2 gl2 = gl;
// mouse coordinates
int mouseX = xCoord;
int mouseY = yCoord;
int viewport[] = GLUtilities.viewportArray(gl2);
float mvMatrix[] = GLUtilities.modelViewMatrixAsFloatArray(gl2);
float projMatrix[] = GLUtilities.projectionMatrixAsFloatArray(gl2);
int glY = viewport[3] - (int)mouseY - 1; // GL y coord pos - note viewport[3] is height of window in pixels
// world near - the gluUnProject z value is the screen depth value which goes from 0.0 -> 1.0
System.out.println("Coordinates at cursor are (" + mouseX + ", " + mouseY);
float wcoordNear[] = new float[4];
boolean ok = glu.gluUnProject((float) mouseX, (float)glY, 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);
// world far
float wcoordFar[] = new float[4];
ok = glu.gluUnProject((float) mouseX, (float) glY, 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);
//Ray3D mouseRay = new Ray3D(new Point3D(wcoordNear[0],wcoordNear[1],wcoordNear[2]),dirVector);
return mouseRay;
}
Thanks
Graham
A couple of questions:
1) If the camera is set at (0,0,0) and the negative z-axis points into the screen and the zNear and zFar planes are set to 1 and 1,000 using gluPerspective() then how can an object be viewed as we are looking down the -z axis with the near/far planes behind the viewer?
2) What is the relationship between the near and far z values set with glDepthRange() and those using gluPerspective()? I'm using the method described by opengl.org to develop a picking-ray using gluUnProject() with z=0 for the near plane and z=1 for the far-plane. Do I have to map the values returned by gluUnProject() to the near-far z clip planes set by gluPerspective()?
public Ray3D mouseRayWorldCoordinates()
{
GL2 gl2 = gl;
// mouse coordinates
int mouseX = xCoord;
int mouseY = yCoord;
int viewport[] = GLUtilities.viewportArray(gl2);
float mvMatrix[] = GLUtilities.modelViewMatrixAsFloatArray(gl2);
float projMatrix[] = GLUtilities.projectionMatrixAsFloatArray(gl2);
int glY = viewport[3] - (int)mouseY - 1; // GL y coord pos - note viewport[3] is height of window in pixels
// world near - the gluUnProject z value is the screen depth value which goes from 0.0 -> 1.0
System.out.println("Coordinates at cursor are (" + mouseX + ", " + mouseY);
float wcoordNear[] = new float[4];
boolean ok = glu.gluUnProject((float) mouseX, (float)glY, 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);
// world far
float wcoordFar[] = new float[4];
ok = glu.gluUnProject((float) mouseX, (float) glY, 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);
//Ray3D mouseRay = new Ray3D(new Point3D(wcoordNear[0],wcoordNear[1],wcoordNear[2]),dirVector);
return mouseRay;
}
Thanks
Graham