}
/** Generate the shadow map. */
private static void generateShadowMap() {
float lightToSceneDistance, nearPlane, fieldOfView;
FloatBuffer lightModelView = BufferUtils.createFloatBuffer(16);
FloatBuffer lightProjection = BufferUtils.createFloatBuffer(16);
Matrix4f lightProjectionTemp = new Matrix4f();
Matrix4f lightModelViewTemp = new Matrix4f();
float sceneBoundingRadius = 95.0F;
lightToSceneDistance = (float) Math.sqrt(lightPosition.get(0) * lightPosition.get(0) + lightPosition.get(1) *
lightPosition.get(1) + lightPosition.get(2) * lightPosition.get(2));
nearPlane = lightToSceneDistance - sceneBoundingRadius;
fieldOfView = (float) Math.toDegrees(2.0F * Math.atan(sceneBoundingRadius / lightToSceneDistance));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fieldOfView, 1.0F, nearPlane, nearPlane + (2.0F * sceneBoundingRadius));
glGetFloat(GL_PROJECTION_MATRIX, lightProjection);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(lightPosition.get(0), lightPosition.get(1), lightPosition.get(2), 0.0F, 0.0F, 0.0F, 0.0F, 1.0F, 0.0F);
glGetFloat(GL_MODELVIEW_MATRIX, lightModelView);
glViewport(0, 0, shadowWidth, shadowHeight);
if (useFBO) {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameBuffer);
}
glClear(GL_DEPTH_BUFFER_BIT);
// Set rendering states to the minimum required, for speed.
glShadeModel(GL_FLAT);
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_NORMALIZE);
glColorMask(false, false, false, false);
glEnable(GL_POLYGON_OFFSET_FILL);
renderObjects(false);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, shadowWidth, shadowHeight, 0);
// Unbind the framebuffer if we are using them.
if (useFBO) {
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
}
// Setup the rendering states.
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
glColorMask(true, true, true, true);
glDisable(GL_POLYGON_OFFSET_FILL);
lightProjectionTemp.load(lightProjection);
lightModelViewTemp.load(lightModelView);
lightProjection.flip();
lightModelView.flip();
Matrix4f tempMatrix = new Matrix4f();
tempMatrix.setIdentity();
tempMatrix.translate(new Vector3f(0.5F, 0.5F, 0.5F));