Jump to content
  • Advertisement

Merx

Member
  • Content Count

    7
  • Joined

  • Last visited

Community Reputation

135 Neutral

About Merx

  • Rank
    Newbie
  1. ok, found the problems with my code. The reason why my shadows disapeared under certain angles was a really embarrasing typo: float minX = transf.z; The second problem, that shadows got cut off was just a simple logic problem while creating the lightViewMatrix, where glm::mat4 lvMatrix = glm::lookAt(glm::vec3(0.0f), -glm::normalize(lightPos), glm::vec3(0.0f, 1.0f, 0.0f)); had to be glm::vec4 frustumCenter = points[0]; for (unsigned int i = 1; i < 8; i++) { frustumCenter += points[i]; } frustumCenter /= 8; glm::mat4 lvMatrix = glm::lookAt(glm::vec3(frustumCenter) + glm::normalize(lightPos), glm::vec3(frustumCenter), glm::vec3(0.0f, 1.0f, 0.0f)); If anyone who has a similar problem happens to run across this, this is my complete code: //function where you get the lightViewProjectionMatrix; std::vector<glm::vec4> corners; corners.reserve(8); getFrustumWorldSpace(corners); lightVPMatrix = getDirLightVPMatrix(corners); void Game::getFrustumWorldSpace(std::vector<glm::vec4>& points) { glm::mat4 inverseProjectionMatrix = glm::inverse(projection * view); for (unsigned int x = 0; x < 2; x++) for (unsigned int y = 0; y < 2; y++) for (unsigned int z = 0; z < 2; z++) { glm::vec4 projClipSpacePosition(x*2.0f - 1.0f, y*2.0f - 1.0f, z*2.0f - 1.0f, 1.0f); glm::vec4 projWorldSpacePosition = inverseProjectionMatrix * projClipSpacePosition; points.push_back(projWorldSpacePosition / projWorldSpacePosition.w); } } glm::mat4 Game::getDirLightVPMatrix(std::vector<glm::vec4> points) { glm::vec4 frustumCenter = points[0]; for (unsigned int i = 1; i < 8; i++) { frustumCenter += points[i]; } frustumCenter /= 8; glm::mat4 lvMatrix = glm::lookAt(glm::vec3(frustumCenter) + glm::normalize(lightPos), glm::vec3(frustumCenter), glm::vec3(0.0f, 1.0f, 0.0f)); glm::vec4 transf = lvMatrix * points[0]; float minZ = transf.z; float maxZ = transf.z; float minX = transf.x; float maxX = transf.x; float minY = transf.y; float maxY = transf.y; for (unsigned int i = 1; i < 8; i++) { transf = lvMatrix * points[i]; if (transf.z > maxZ) maxZ = transf.z; if (transf.z < minZ) minZ = transf.z; if (transf.x > maxX) maxX = transf.x; if (transf.x < minX) minX = transf.x; if (transf.y > maxY) maxY = transf.y; if (transf.y < minY) minY = transf.y; } glm::mat4 lpMatrix = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, minZ, maxZ); const float scaleX = 2.0f / (maxX - minX); const float scaleY = 2.0f / (maxY - minY); const float offsetX = -0.5f * (minX + maxX) * scaleX; const float offsetY = -0.5f * (minY + maxY) * scaleY; glm::mat4 cropMatrix(1.0f); cropMatrix[0][0] = scaleX; cropMatrix[1][1] = scaleY; cropMatrix[3][0] = offsetX; cropMatrix[3][1] = offsetY; return cropMatrix * lpMatrix * lvMatrix; }
  2. thnx, but it was another issue, the shadows moving where i was talking about was just a typo on my part :P
  3. This is what i made of that code glm::mat4 Game::getDirLightVPMatrix() { glm::mat4 lvMatrix = glm::lookAt(-glm::normalize(lightDir), glm::vec3(0.0f), glm::vec3(1.0f)); glm::mat4 CameraViewToLight = glm::inverse(view) * lvMatrix; const float ScaleXInv = 1.0f / projection[0][0]; const float ScaleYInv = 1.0f / projection[1][1]; std::vector<glm::vec3> CornersVS; CornersVS.reserve(8); const float NearX = ScaleXInv * near; const float NearY = ScaleYInv * near; CornersVS.push_back(glm::vec3(-NearX, +NearY, near)); CornersVS.push_back(glm::vec3(+NearX, +NearY, near)); CornersVS.push_back(glm::vec3(+NearX, -NearY, near)); CornersVS.push_back(glm::vec3(-NearX, -NearY, near)); const float FarX = ScaleXInv * far; const float FarY = ScaleYInv * far; CornersVS.push_back(glm::vec3(-FarX, +FarY, far)); CornersVS.push_back(glm::vec3(+FarX, +FarY, far)); CornersVS.push_back(glm::vec3(+FarX, -FarY, far)); CornersVS.push_back(glm::vec3(-FarX, -FarY, far)); glm::vec4 transf = CameraViewToLight * glm::vec4(CornersVS[0], 1.0); float minZ = transf.z; float maxZ = transf.z; float minX = transf.x; float maxX = transf.x; float minY = transf.y; float maxY = transf.y; for (unsigned int i = 1; i < 8; i++) { transf = CameraViewToLight * glm::vec4(CornersVS[i], 1.0f); if (transf.z > maxZ) maxZ = transf.z; if (transf.z < minZ) minZ = transf.z; if (transf.x > maxX) maxX = transf.x; if (transf.x < minX) minX = transf.x; if (transf.y > maxY) maxY = transf.y; if (transf.y < minY) minY = transf.y; } glm::mat4 lpMatrix = glm::ortho(minX, maxX, minY, maxY, minZ, maxZ); return lpMatrix * lvMatrix; } it does work, and it does generate a different shadow map, but it suffers from very similar problem
  4. its not that the shadow casters are offscreen, ill show some screenshots with the problem, as well with the shadow map generated. These seem to me kinda... off.   Everything going allright...   Shadows getting cut off   Shadows completely gone besides the character   The vertex shader i use to generate the shadow (no fragment because shadow maps ) The lightSpaceMatrix is the matrix generated in the prev shown getDirLightVPMatrix function #version 330 core layout (location = 0) in vec3 position; uniform mat4 lightSpaceMatrix; uniform mat4 model; void main() { gl_Position = lightSpaceMatrix * model * vec4(position, 1.0f); }
  5. I almost got it working, the shadows are rendered fine, the only problem  is that under certain angles shadows get cut off or completely disapear. Obviously something is going wrong, but i don't know what. Here is my code:   Getting world positions of frustum corners: void Game::getFrustumWorldSpace(std::vector<glm::vec4>& points) { glm::mat4 inverseProjectionMatrix = glm::inverse(projection * view); for (unsigned int x = 0; x < 2; x++) for (unsigned int y = 0; y < 2; y++) for (unsigned int z = 0; z < 2; z++) { glm::vec4 projClipSpacePosition(x*2.0f - 1.0f, y*2.0f - 1.0f, z*2.0f - 1.0f, 1.0f); glm::vec4 projWorldSpacePosition = inverseProjectionMatrix * projClipSpacePosition; points.push_back(projWorldSpacePosition / projWorldSpacePosition.w); } } Generating the lightVPMatrix: glm::mat4 Game::getDirLightVPMatrix(std::vector<glm::vec4> points) { glm::mat4 lvMatrix = glm::lookAt(glm::normalize(lightDir), glm::vec3(0.0f), glm::vec3(1.0f)); glm::vec4 transf = lvMatrix * points[0]; float minZ = transf.z; float maxZ = transf.z; float minX = transf.z; float maxX = transf.x; float minY = transf.y; float maxY = transf.y; for (unsigned int i = 1; i < 8; i++) { transf = lvMatrix * points[i]; if (transf.z > maxZ) maxZ = transf.z; if (transf.z < minZ) minZ = transf.z; if (transf.x > maxX) maxX = transf.x; if (transf.x < minX) minX = transf.x; if (transf.y > maxY) maxY = transf.y; if (transf.y < minY) minY = transf.y; } glm::mat4 lpMatrix = glm::ortho(-1.0f, 1.0f, -1.0f, 1.0f, minZ, maxZ); const float scaleX = 2.0f / (maxX - minX); const float scaleY = 2.0f / (maxY - minY); const float offsetX = -0.5f * (minX + maxX) * scaleX; const float offsetY = -0.5f * (minY + maxY) * scaleY; glm::mat4 cropMatrix(1.0f); cropMatrix[0][0] = scaleX; cropMatrix[1][1] = scaleY; cropMatrix[3][0] = offsetX; cropMatrix[3][1] = offsetY; return cropMatrix * lpMatrix * lvMatrix; } If someone could help my find the error i made that would be greatly appreciated!
  6. I'm probably asking for too much here, but would anyone be able to write pseudo code for me? I would've liked to post my current progress here, but my project files are on my desktop, which i won't have access to until after the weekend. Doesn't have to be anything fancy (obviously) like "pancaking" or csm, or anything of the sort. I'm fairly sure i can get that to work myself once i properly understand this. If anyone could do that for me that would be amazing!
  7. Im trying to optimize my shadow maps by making my directional shadow maps fit in the view frustum, however this is causing me some problems. I tried several things, but all didn't work, or didn't work properly.   Assuming i only have the following information: vec3 describing direction of the directional light perspective camera with values (fov, width, height, near plane, far plane) and transform matrix   How can i use this information to create the correct light space matrix to generate and sample my shadow maps?   Any help would be greatly appreciated!
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!