LWJGL: Camera distance from image plane?
- by Rogem
Let me paste some code before I ask the question...
public static void createWindow(int[] args) {
try {
Display.setFullscreen(false);
DisplayMode d[] = Display.getAvailableDisplayModes();
for (int i = 0; i < d.length; i++) {
if (d[i].getWidth() == args[0] && d[i].getHeight() == args[1]
&& d[i].getBitsPerPixel() == 32) {
displayMode = d[i];
break;
}
}
Display.setDisplayMode(displayMode);
Display.create();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
public static void initGL() {
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1.0);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45.0f, (float) displayMode.getWidth()
/ (float) displayMode.getHeight(), 0.1f, 100.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
}
So, with the camera and screen setup out of the way, I can now ask the actual question: How do I know what the camera distance is from the image plane? I also would like to know what the angle between the image plane's center normal and a line drawn from the middle of one of the edges to the camera position is.
This will be used to consequently draw a vector from the camera's position through the player's click-coordinates to determine the world coordinates they clicked (or could've clicked).
Also, when I set the camera coordinates, do I set the coordinates of the camera or do I set the coordinates of the image plane?
Thank you for your help.
EDIT: So, I managed to solve how to calculate the distance of the camera... Here's the relevant code...
private static float getScreenFOV(int dim) {
if (dim == 0) {
float dist = (float) Math.tan((Math.PI / 2 - Math.toRadians(FOV_Y))/2) * 0.5f;
float FOV_X = 2 * (float) Math.atan(getScreenRatio() * 0.5f / dist);
return FOV_X;
} else if (dim == 1) {
return FOV_Y;
}
return 0;
}
FOV_Y is the Field of View that one defines in gluPerspective (float fovy in javadoc). This seems to be (and would logically be) for the height of the screen. Now I just need to figure out how to calculate that vector.