static void computeLightSpace(glm::mat4 view, glm::mat4 proj, glm::vec3 lightDir, glm::mat4 out[3]){ glm::mat4 inv = glm::inverse(proj*view); glm::vec4 corners[8] = { {-1,-1,-1,1},{1,-1,-1,1},{1,1,-1,1},{-1,1,-1,1}, {-1,-1, 1,1},{1,-1, 1,1},{1,1, 1,1},{-1,1, 1,1} }; for(int i=0;i<3;i++){ glm::mat4 lightView = glm::lookAt(-lightDir*100.0f,glm::vec3(0),glm::vec3(0,1,0)); glm::vec3 mn(FLT_MAX), mx(-FLT_MAX); for(auto &c: corners){ glm::vec4 wc = inv * c; wc/=wc.w; glm::vec4 lc = lightView * wc; mn = glm::min(mn,glm::vec3(lc)); mx = glm::max(mx,glm::vec3(lc)); } out[i] = glm::ortho(mn.x,mx.x,mn.y,mx.y,-mx.z-50.0f,-mn.z+50.0f) * lightView; } } void VoxelGame::render(glm::mat4 const& view, glm::mat4 const& proj){ computeLightSpace(view,proj,lightDir,lightSpaceMat); // 1) depth passes for(int i=0;i