ShadowMapMasterRenderer.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package eu.tankernn.gameEngine.renderEngine.shadows;
  2. import java.util.List;
  3. import java.util.Map;
  4. import org.lwjgl.opengl.GL11;
  5. import org.lwjgl.util.vector.Matrix4f;
  6. import org.lwjgl.util.vector.Vector2f;
  7. import org.lwjgl.util.vector.Vector3f;
  8. import eu.tankernn.gameEngine.entities.Camera;
  9. import eu.tankernn.gameEngine.entities.Entity;
  10. import eu.tankernn.gameEngine.entities.Light;
  11. import eu.tankernn.gameEngine.models.TexturedModel;
  12. /**
  13. * This class is in charge of using all of the classes in the shadows package to
  14. * carry out the shadow render pass, i.e. rendering the scene to the shadow map
  15. * texture. This is the only class in the shadows package which needs to be
  16. * referenced from outside the shadows package.
  17. *
  18. * @author Karl
  19. *
  20. */
  21. public class ShadowMapMasterRenderer {
  22. public static final int SHADOW_MAP_SIZE = 4096;
  23. private ShadowFrameBuffer shadowFbo;
  24. private ShadowShader shader;
  25. private ShadowBox shadowBox;
  26. private Matrix4f projectionMatrix = new Matrix4f();
  27. private Matrix4f lightViewMatrix = new Matrix4f();
  28. private Matrix4f projectionViewMatrix = new Matrix4f();
  29. private Matrix4f offset = createOffset();
  30. private ShadowMapEntityRenderer entityRenderer;
  31. /**
  32. * Creates instances of the important objects needed for rendering the scene
  33. * to the shadow map. This includes the {@link ShadowBox} which calculates
  34. * the position and size of the "view cuboid", the simple renderer and
  35. * shader program that are used to render objects to the shadow map, and the
  36. * {@link ShadowFrameBuffer} to which the scene is rendered. The size of the
  37. * shadow map is determined here.
  38. *
  39. * @param camera
  40. * - the camera being used in the scene.
  41. */
  42. public ShadowMapMasterRenderer(Camera camera) {
  43. shader = new ShadowShader();
  44. shadowBox = new ShadowBox(lightViewMatrix, camera);
  45. shadowFbo = new ShadowFrameBuffer(SHADOW_MAP_SIZE, SHADOW_MAP_SIZE);
  46. entityRenderer = new ShadowMapEntityRenderer(shader, projectionViewMatrix);
  47. }
  48. /**
  49. * Carries out the shadow render pass. This renders the entities to the
  50. * shadow map. First the shadow box is updated to calculate the size and
  51. * position of the "view cuboid". The light direction is assumed to be
  52. * "-lightPosition" which will be fairly accurate assuming that the light is
  53. * very far from the scene. It then prepares to render, renders the entities
  54. * to the shadow map, and finishes rendering.
  55. *
  56. * @param entities
  57. * - the lists of entities to be rendered. Each list is
  58. * associated with the {@link TexturedModel} that all of the
  59. * entities in that list use.
  60. * @param sun
  61. * - the light acting as the sun in the scene.
  62. */
  63. public void render(Map<TexturedModel, List<Entity>> entities, Light sun) {
  64. shadowBox.update();
  65. Vector3f sunPosition = sun.getPosition();
  66. Vector3f lightDirection = new Vector3f(-sunPosition.x, -sunPosition.y, -sunPosition.z);
  67. prepare(lightDirection, shadowBox);
  68. entityRenderer.render(entities);
  69. finish();
  70. }
  71. /**
  72. * This biased projection-view matrix is used to convert fragments into
  73. * "shadow map space" when rendering the main render pass. It converts a
  74. * world space position into a 2D coordinate on the shadow map. This is
  75. * needed for the second part of shadow mapping.
  76. *
  77. * @return The to-shadow-map-space matrix.
  78. */
  79. public Matrix4f getToShadowMapSpaceMatrix() {
  80. return Matrix4f.mul(offset, projectionViewMatrix, null);
  81. }
  82. /**
  83. * Clean up the shader and FBO on closing.
  84. */
  85. public void cleanUp() {
  86. shader.cleanUp();
  87. shadowFbo.cleanUp();
  88. }
  89. /**
  90. * @return The ID of the shadow map texture. The ID will always stay the
  91. * same, even when the contents of the shadow map texture change
  92. * each frame.
  93. */
  94. public int getShadowMap() {
  95. return shadowFbo.getShadowMap();
  96. }
  97. /**
  98. * @return The light's "view" matrix.
  99. */
  100. protected Matrix4f getLightSpaceTransform() {
  101. return lightViewMatrix;
  102. }
  103. /**
  104. * Prepare for the shadow render pass. This first updates the dimensions of
  105. * the orthographic "view cuboid" based on the information that was
  106. * calculated in the {@link SHadowBox} class. The light's "view" matrix is
  107. * also calculated based on the light's direction and the center position of
  108. * the "view cuboid" which was also calculated in the {@link ShadowBox}
  109. * class. These two matrices are multiplied together to create the
  110. * projection-view matrix. This matrix determines the size, position, and
  111. * orientation of the "view cuboid" in the world. This method also binds the
  112. * shadows FBO so that everything rendered after this gets rendered to the
  113. * FBO. It also enables depth testing, and clears any data that is in the
  114. * FBOs depth attachment from last frame. The simple shader program is also
  115. * started.
  116. *
  117. * @param lightDirection
  118. * - the direction of the light rays coming from the sun.
  119. * @param box
  120. * - the shadow box, which contains all the info about the
  121. * "view cuboid".
  122. */
  123. private void prepare(Vector3f lightDirection, ShadowBox box) {
  124. updateOrthoProjectionMatrix(box.getWidth(), box.getHeight(), box.getLength());
  125. updateLightViewMatrix(lightDirection, box.getCenter());
  126. Matrix4f.mul(projectionMatrix, lightViewMatrix, projectionViewMatrix);
  127. shadowFbo.bindFrameBuffer();
  128. GL11.glEnable(GL11.GL_DEPTH_TEST);
  129. GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
  130. shader.start();
  131. }
  132. /**
  133. * Finish the shadow render pass. Stops the shader and unbinds the shadow
  134. * FBO, so everything rendered after this point is rendered to the screen,
  135. * rather than to the shadow FBO.
  136. */
  137. private void finish() {
  138. shader.stop();
  139. shadowFbo.unbindFrameBuffer();
  140. }
  141. /**
  142. * Updates the "view" matrix of the light. This creates a view matrix which
  143. * will line up the direction of the "view cuboid" with the direction of the
  144. * light. The light itself has no position, so the "view" matrix is centered
  145. * at the center of the "view cuboid". The created view matrix determines
  146. * where and how the "view cuboid" is positioned in the world. The size of
  147. * the view cuboid, however, is determined by the projection matrix.
  148. *
  149. * @param direction
  150. * - the light direction, and therefore the direction that the
  151. * "view cuboid" should be pointing.
  152. * @param center
  153. * - the center of the "view cuboid" in world space.
  154. */
  155. private void updateLightViewMatrix(Vector3f direction, Vector3f center) {
  156. direction.normalise();
  157. center.negate();
  158. lightViewMatrix.setIdentity();
  159. float pitch = (float) Math.acos(new Vector2f(direction.x, direction.z).length());
  160. Matrix4f.rotate(pitch, new Vector3f(1, 0, 0), lightViewMatrix, lightViewMatrix);
  161. float yaw = (float) Math.toDegrees(((float) Math.atan(direction.x / direction.z)));
  162. yaw = direction.z > 0 ? yaw - 180 : yaw;
  163. Matrix4f.rotate((float) -Math.toRadians(yaw), new Vector3f(0, 1, 0), lightViewMatrix,
  164. lightViewMatrix);
  165. Matrix4f.translate(center, lightViewMatrix, lightViewMatrix);
  166. }
  167. /**
  168. * Creates the orthographic projection matrix. This projection matrix
  169. * basically sets the width, length and height of the "view cuboid", based
  170. * on the values that were calculated in the {@link ShadowBox} class.
  171. *
  172. * @param width
  173. * - shadow box width.
  174. * @param height
  175. * - shadow box height.
  176. * @param length
  177. * - shadow box length.
  178. */
  179. private void updateOrthoProjectionMatrix(float width, float height, float length) {
  180. projectionMatrix.setIdentity();
  181. projectionMatrix.m00 = 2f / width;
  182. projectionMatrix.m11 = 2f / height;
  183. projectionMatrix.m22 = -2f / length;
  184. projectionMatrix.m33 = 1;
  185. }
  186. /**
  187. * Create the offset for part of the conversion to shadow map space. This
  188. * conversion is necessary to convert from one coordinate system to the
  189. * coordinate system that we can use to sample to shadow map.
  190. *
  191. * @return The offset as a matrix (so that it's easy to apply to other matrices).
  192. */
  193. private static Matrix4f createOffset() {
  194. Matrix4f offset = new Matrix4f();
  195. offset.translate(new Vector3f(0.5f, 0.5f, 0.5f));
  196. offset.scale(new Vector3f(0.5f, 0.5f, 0.5f));
  197. return offset;
  198. }
  199. }