- Have a camera for light view (cam)
- Set cam's position: vec3(100, 100, 100)
- Set cam's target as light direction: vec3(1, -1, 0)
- Build projection matrix as:
Code: Select all
const auto fov = 90.f * math::pi<float>() / 180.f;
mat.buildProjectionMatrixOrthoLH(fov, fov, 10, 100);
cam->setProjectionMatrix(mat, true);
- Set material of all nodes to a custom vertex/pixel shader pair to output depth.
- Render the scene to a texture
Vertex shader:
Code: Select all
uniform mat4 worldViewProjMat;
in vec3 in_vertex;
void main() {
vec4 vert = vec4(in_vertex, 1.0);
gl_Position = worldViewProjMat * vert;
}
Code: Select all
auto worldMat = services->getVideoDriver()->getTransform(irr::video::ETS_WORLD);
auto viewMat = services->getVideoDriver()->getTransform(irr::video::ETS_VIEW);
auto projMat = services->getVideoDriver()->getTransform(irr::video::ETS_PROJECTION);
auto worldViewProj = projMat * viewMat * worldMat;
services->setVertexShaderConstant("worldViewProjMat", worldViewProj.pointer(), 16);
Code: Select all
out vec2 out_depth;
void main() {
float depth = gl_FragCoord.z;
out_depth = vec2(depth, depth * depth);
}