1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
| std::vector<float> shadowCascadeLevels{ cameraFarPlane / 50.0f, cameraFarPlane / 25.0f, cameraFarPlane / 10.0f, cameraFarPlane / 2.0f };
glGenFramebuffers(1, &lightFBO); glGenTextures(1, &lightDepthMaps); glBindTexture(GL_TEXTURE_2D_ARRAY, lightDepthMaps); glTexImage3D( GL_TEXTURE_2D_ARRAY, 0, GL_DEPTH_COMPONENT32F, depthMapResolution, depthMapResolution, int(shadowCascadeLevels.size()) + 1, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
const auto lightMatrices = getLightSpaceMatrices(); glBindBuffer(GL_UNIFORM_BUFFER, matricesUBO); for (size_t i = 0; i < lightMatrices.size(); ++i) { glBufferSubData(GL_UNIFORM_BUFFER, i * sizeof(glm::mat4x4), sizeof(glm::mat4x4), &lightMatrices[i]); } glBindBuffer(GL_UNIFORM_BUFFER, 0);
simpleDepthShader.use();
std::vector<glm::vec4> getFrustumCornersWorldSpace(const glm::mat4& projview) { const auto inv = glm::inverse(projview);
std::vector<glm::vec4> frustumCorners; for (unsigned int x = 0; x < 2; ++x) { for (unsigned int y = 0; y < 2; ++y) { for (unsigned int z = 0; z < 2; ++z) { const glm::vec4 pt = inv * glm::vec4(2.0f * x - 1.0f, 2.0f * y - 1.0f, 2.0f * z - 1.0f, 1.0f); frustumCorners.push_back(pt / pt.w); } } }
return frustumCorners; }
std::vector<glm::vec4> getFrustumCornersWorldSpace(const glm::mat4& proj, const glm::mat4& view) { return getFrustumCornersWorldSpace(proj * view); }
glm::mat4 getLightSpaceMatrix(const float nearPlane, const float farPlane) { const auto proj = glm::perspective( glm::radians(camera.Zoom), (float)fb_width / (float)fb_height, nearPlane, farPlane); const auto corners = getFrustumCornersWorldSpace(proj, camera.GetViewMatrix());
glm::vec3 center = glm::vec3(0, 0, 0); for (const auto& v : corners) { center += glm::vec3(v); } center /= corners.size();
const auto lightView = glm::lookAt(center + lightDir, center, glm::vec3(0.0f, 1.0f, 0.0f));
float minX = std::numeric_limits<float>::max(); float maxX = std::numeric_limits<float>::lowest(); float minY = std::numeric_limits<float>::max(); float maxY = std::numeric_limits<float>::lowest(); float minZ = std::numeric_limits<float>::max(); float maxZ = std::numeric_limits<float>::lowest(); for (const auto& v : corners) { const auto trf = lightView * v; minX = std::min(minX, trf.x); maxX = std::max(maxX, trf.x); minY = std::min(minY, trf.y); maxY = std::max(maxY, trf.y); minZ = std::min(minZ, trf.z); maxZ = std::max(maxZ, trf.z); }
constexpr float zMult = 10.0f; if (minZ < 0) { minZ *= zMult; } else { minZ /= zMult; } if (maxZ < 0) { maxZ /= zMult; } else { maxZ *= zMult; }
const glm::mat4 lightProjection = glm::ortho(minX, maxX, minY, maxY, minZ, maxZ); return lightProjection * lightView; }
std::vector<glm::mat4> getLightSpaceMatrices() { std::vector<glm::mat4> ret; for (size_t i = 0; i < shadowCascadeLevels.size() + 1; ++i) { if (i == 0) { ret.push_back(getLightSpaceMatrix(cameraNearPlane, shadowCascadeLevels[i])); } else if (i < shadowCascadeLevels.size()) { ret.push_back(getLightSpaceMatrix(shadowCascadeLevels[i - 1], shadowCascadeLevels[i])); } else { ret.push_back(getLightSpaceMatrix(shadowCascadeLevels[i - 1], cameraFarPlane)); } } return ret; }
|