示例#1
0
        public void getSamples(ShadingState state)
        {
            if (Vector3.dot(sunDirWorld, state.getGeoNormal()) > 0 && Vector3.dot(sunDirWorld, state.getNormal()) > 0)
            {
                LightSample dest = new LightSample();
                dest.setShadowRay(new Ray(state.getPoint(), sunDirWorld));
                dest.getShadowRay().setMax(float.MaxValue);
                dest.setRadiance(sunColor, sunColor);
                dest.traceShadow(state);
                state.addSample(dest);
            }
            int n = state.getDiffuseDepth() > 0 ? 1 : numSkySamples;

            for (int i = 0; i < n; i++)
            {
                // random offset on unit square, we use the infinite version of
                // getRandom because the light sampling is adaptive
                double randX = state.getRandom(i, 0, n);
                double randY = state.getRandom(i, 1, n);

                int x = 0;
                while (randX >= colHistogram[x] && x < colHistogram.Length - 1)
                {
                    x++;
                }
                float[] rowHistogram = imageHistogram[x];
                int     y            = 0;
                while (randY >= rowHistogram[y] && y < rowHistogram.Length - 1)
                {
                    y++;
                }
                // sample from (x, y)
                float u = (float)((x == 0) ? (randX / colHistogram[0]) : ((randX - colHistogram[x - 1]) / (colHistogram[x] - colHistogram[x - 1])));
                float v = (float)((y == 0) ? (randY / rowHistogram[0]) : ((randY - rowHistogram[y - 1]) / (rowHistogram[y] - rowHistogram[y - 1])));

                float px = ((x == 0) ? colHistogram[0] : (colHistogram[x] - colHistogram[x - 1]));
                float py = ((y == 0) ? rowHistogram[0] : (rowHistogram[y] - rowHistogram[y - 1]));

                float   su       = (x + u) / colHistogram.Length;
                float   sv       = (y + v) / rowHistogram.Length;
                float   invP     = (float)Math.Sin(sv * Math.PI) * jacobian / (n * px * py);
                Vector3 localDir = getDirection(su, sv);
                Vector3 dir      = basis.transform(localDir, new Vector3());
                if (Vector3.dot(dir, state.getGeoNormal()) > 0 && Vector3.dot(dir, state.getNormal()) > 0)
                {
                    LightSample dest = new LightSample();
                    dest.setShadowRay(new Ray(state.getPoint(), dir));
                    dest.getShadowRay().setMax(float.MaxValue);
                    Color radiance = getSkyRGB(localDir);
                    dest.setRadiance(radiance, radiance);
                    dest.getDiffuseRadiance().mul(invP);
                    dest.getSpecularRadiance().mul(invP);
                    dest.traceShadow(state);
                    state.addSample(dest);
                }
            }
        }
示例#2
0
        public void getSamples(ShadingState state)
        {
            if (lightBounds.contains(state.getPoint()) && state.getPoint().z < maxZ)
            {
                int   n = state.getDiffuseDepth() > 0 ? 1 : samples;
                float a = area / n;
                for (int i = 0; i < n; i++)
                {
                    // random offset on unit square, we use the infinite version of
                    // getRandom
                    // because the light sampling is adaptive
                    double randX = state.getRandom(i, 0);
                    double randY = state.getRandom(i, 1);

                    Point3 p = new Point3();
                    p.x = (float)(lxmin * (1 - randX) + lxmax * randX);
                    p.y = (float)(lymin * (1 - randY) + lymax * randY);
                    p.z = maxZ - 0.001f;

                    LightSample dest = new LightSample();
                    // prepare shadow ray to sampled point
                    dest.setShadowRay(new Ray(state.getPoint(), p));

                    // check that the direction of the sample is the same as the
                    // normal
                    float cosNx = dest.dot(state.getNormal());
                    if (cosNx <= 0)
                    {
                        return;
                    }

                    // light source facing point ?
                    // (need to check with light source's normal)
                    float cosNy = dest.getShadowRay().dz;
                    if (cosNy > 0)
                    {
                        // compute geometric attenuation and probability scale
                        // factor
                        float r     = dest.getShadowRay().getMax();
                        float g     = cosNy / (r * r);
                        float scale = g * a;
                        // set sample radiance
                        dest.setRadiance(radiance, radiance);
                        dest.getDiffuseRadiance().mul(scale);
                        dest.getSpecularRadiance().mul(scale);
                        dest.traceShadow(state);
                        state.addSample(dest);
                    }
                }
            }
        }
示例#3
0
        public void getSamples(ShadingState state)
        {
            Vector3 d = Point3.sub(lightPoint, state.getPoint(), new Vector3());

            if (Vector3.dot(d, state.getNormal()) > 0 && Vector3.dot(d, state.getGeoNormal()) > 0)
            {
                LightSample dest = new LightSample();
                // prepare shadow ray
                dest.setShadowRay(new Ray(state.getPoint(), lightPoint));
                float scale = 1.0f / (float)(4 * Math.PI * lightPoint.distanceToSquared(state.getPoint()));
                dest.setRadiance(power, power);
                dest.getDiffuseRadiance().mul(scale);
                dest.getSpecularRadiance().mul(scale);
                dest.traceShadow(state);
                state.addSample(dest);
            }
        }