Example #1
0
File: frustum.cpp Project: hglm/sre
void sreFrustum::CalculateShadowCasterVolume(const Vector4D& lightpos, int nu_frustum_planes) {
    nu_frustum_planes = maxf(nu_frustum_planes, SRE_NU_FRUSTUM_PLANES);
    // Note: for beam lights, this might be inaccurate.
    if (lightpos.w == 1.0f && Intersects(lightpos.GetPoint3D(), frustum_world)) {
        // If the point light position is inside the view frustum, we only need to consider the
        // set of visible objects.
        shadow_caster_volume.nu_planes = nu_frustum_planes;
        for (int i = 0; i < nu_frustum_planes; i++)
            shadow_caster_volume.plane[i] = frustum_world.plane[i];
        goto end;
    }
    // Calculate the convex hull enclosing the view frustum and the light source.
    shadow_caster_volume.nu_planes = 0;
    // Calculate the dot products between the frustum planes and the light source.
    float dot[6] DST_ALIGNED(16);
    // The SIMD-accelerated version check for 16-bytes aligned arrays, which is hard to
    // guarantee in this case; otherwise, no SIMD will be used.
#if 1
    dstCalculateDotProductsNx1(nu_frustum_planes, &frustum_world.plane[0],
        lightpos, &dot[0]);
#else
    for (int i = 0; i < nu_frustum_planes; i++)
        dot[i] = Dot(frustum_world.plane[i], lightpos);
#endif
    // For each frustum plane, add it if it is part of the convex hull.
    for (int i = 0; i < nu_frustum_planes; i++)
        if (dot[i] > 0) {
             shadow_caster_volume.plane[shadow_caster_volume.nu_planes] = frustum_world.plane[i];
             shadow_caster_volume.nu_planes++;
        }
//    printf("Shadow caster volume planes: from frustum: %d\n", shadow_caster_volume.nu_planes);
    if (shadow_caster_volume.nu_planes == 0 && lightpos.w == 1.0f) {
        // Special case: the point light source is behind the camera and there is no far plane.
        // As a result, there is no plane from the frustum with a positive dot product. In
        // this case, construct a volume consisting of the lightsource and four planes parallel 
        // to the frustum side planes but starting at the lightsource.
        for (int i = 0; i < 4; i++) {
            // Copy the normal.
            shadow_caster_volume.plane[i] = frustum_world.plane[i];
            // Calculate the distance such that the lightsource is in the plane.
            shadow_caster_volume.plane[i].w = - Dot(frustum_world.plane[i].GetVector3D(),
                lightpos.GetPoint3D());
        }
        shadow_caster_volume.nu_planes = 4;
        goto end;
    }
    // For each pair of adjacent frustum planes, if one has a positive dot product and the other
    // does not, calculate a new plane defined by the edge between those two frustum planes and
    // the position of the light source, making sure the plane's normal direction faces inward.
    // For directional lights, the plane runs parallel to the direction of the light.
    nu_shadow_caster_edges = 0;
    int n;
    if (nu_frustum_planes == 5)
        n = 8;
    else
        n = 12;
    for (int i = 0; i < n; i++)
        if ((dot[adjacent_plane[i].plane0] > 0 && dot[adjacent_plane[i].plane1] <= 0) ||
        (dot[adjacent_plane[i].plane0] <= 0 && dot[adjacent_plane[i].plane1] > 0)) {
//            printf("Setting plane from adjacent planes %d and %d using frustum vertices %d and %d\n",
//                adjacent_plane[i].plane0, adjacent_plane[i].plane1, adjacent_plane[i].vertex0,
//                adjacent_plane[i].vertex1);
            if (lightpos.w == 1.0f)
                shadow_caster_volume.plane[shadow_caster_volume.nu_planes] = dstPlaneFromPoints(
                    frustum_world.hull.vertex[adjacent_plane[i].vertex0],
                    frustum_world.hull.vertex[adjacent_plane[i].vertex1],
                    lightpos.GetPoint3D()
                    );
            else {
                shadow_caster_volume.plane[shadow_caster_volume.nu_planes] = dstPlaneFromPoints(
                    frustum_world.hull.vertex[adjacent_plane[i].vertex0],
                    frustum_world.hull.vertex[adjacent_plane[i].vertex1],
                    frustum_world.hull.vertex[adjacent_plane[i].vertex0] + lightpos.GetVector3D()
                    );
                shadow_caster_edge[nu_shadow_caster_edges][0] = adjacent_plane[i].vertex0;
                shadow_caster_edge[nu_shadow_caster_edges][1] = adjacent_plane[i].vertex1;
                nu_shadow_caster_edges++;
            }
            // Make sure the normal is pointed inward, check by taking the dot product with the frustum
            // "centroid".
            shadow_caster_volume.plane[shadow_caster_volume.nu_planes].OrientPlaneTowardsPoint(
                frustum_world.sphere.center);
            shadow_caster_volume.nu_planes++;
        }
end: ;
#if 0
    printf("Light (%lf, %lf, %lf, %lf), %d planes in shadow caster volume.\n",
        lightpos.x, lightpos.y, lightpos.z, lightpos.w, shadow_caster_volume.nu_planes);
    for (int i = 0; i < shadow_caster_volume.nu_planes; i++)
        printf("Plane %d: (%lf, %lf, %lf, %lf) ", i,
            shadow_caster_volume.plane[i].x, shadow_caster_volume.plane[i].y,
            shadow_caster_volume.plane[i].y, shadow_caster_volume.plane[i].w);
    printf("\n");
#endif
}