예제 #1
0
 public Color getIrradiance(ShadingState state, Color diffuseReflectance)
 {
     if (samples <= 0)
         return Color.BLACK;
     // compute new sample
     Color irr = Color.black();
     OrthoNormalBasis onb = state.getBasis();
     Vector3 w = new Vector3();
     int n = state.getDiffuseDepth() == 0 ? samples : 1;
     for (int i = 0; i < n; i++)
     {
         float xi = (float)state.getRandom(i, 0, n);
         float xj = (float)state.getRandom(i, 1, n);
         float phi = (float)(xi * 2 * Math.PI);
         float cosPhi = (float)Math.Cos(phi);
         float sinPhi = (float)Math.Sin(phi);
         float sinTheta = (float)Math.Sqrt(xj);
         float cosTheta = (float)Math.Sqrt(1.0f - xj);
         w.x = cosPhi * sinTheta;
         w.y = sinPhi * sinTheta;
         w.z = cosTheta;
         onb.transform(w);
         ShadingState temp = state.traceFinalGather(new Ray(state.getPoint(), w), i);
         if (temp != null)
         {
             temp.getInstance().prepareShadingState(temp);
             if (temp.getShader() != null)
                 irr.add(temp.getShader().getRadiance(temp));
         }
     }
     irr.mul((float)Math.PI / n);
     return irr;
 }
예제 #2
0
        public void getSamples(ShadingState state)
        {
            if (samples == null)
            {
                int n = state.getDiffuseDepth() > 0 ? 1 : numSamples;
                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 dir = getDirection(su, sv);
                    basis.transform(dir);
                    if (Vector3.dot(dir, state.getGeoNormal()) > 0)
                    {
                        LightSample dest = new LightSample();
                        dest.setShadowRay(new Ray(state.getPoint(), dir));
                        dest.getShadowRay().setMax(float.MaxValue);
                        Color radiance = texture.getPixel(su, sv);
                        dest.setRadiance(radiance, radiance);
                        dest.getDiffuseRadiance().mul(invP);
                        dest.getSpecularRadiance().mul(invP);
                        dest.traceShadow(state);
                        state.addSample(dest);
                    }
                }
            }
            else
            {
                for (int i = 0; i < numSamples; i++)
                {
                    if (Vector3.dot(samples[i], state.getGeoNormal()) > 0 && Vector3.dot(samples[i], state.getNormal()) > 0)
                    {
                        LightSample dest = new LightSample();
                        dest.setShadowRay(new Ray(state.getPoint(), samples[i]));
                        dest.getShadowRay().setMax(float.MaxValue);
                        dest.setRadiance(colors[i], colors[i]);
                        dest.traceShadow(state);
                        state.addSample(dest);
                    }
                }
            }
        }
예제 #3
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);
                }
            }
        }
예제 #4
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);
                    }
                }
            }
        }
예제 #5
0
 public void store(ShadingState state, Vector3 dir, Color power, Color diffuse)
 {
     if (((state.getDiffuseDepth() == 0) && (state.getReflectionDepth() > 0 || state.getRefractionDepth() > 0)))
     {
         // this is a caustic photon
         Photon p = new Photon(state.getPoint(), dir, power);
         lock (lockObj)
         {
             storedPhotons++;
             photonList.Add(p);
             bounds.include(new Point3(p.x, p.y, p.z));
             maxPower = Math.Max(maxPower, power.getMax());
         }
     }
 }
예제 #6
0
        public Color getIrradiance(ShadingState state, Color diffuseReflectance)
        {
            if (samples <= 0)
            {
                return(Color.BLACK);
            }
            // compute new sample
            Color            irr = Color.black();
            OrthoNormalBasis onb = state.getBasis();
            Vector3          w   = new Vector3();
            int n = state.getDiffuseDepth() == 0 ? samples : 1;

            for (int i = 0; i < n; i++)
            {
                float xi       = (float)state.getRandom(i, 0, n);
                float xj       = (float)state.getRandom(i, 1, n);
                float phi      = (float)(xi * 2 * Math.PI);
                float cosPhi   = (float)Math.Cos(phi);
                float sinPhi   = (float)Math.Sin(phi);
                float sinTheta = (float)Math.Sqrt(xj);
                float cosTheta = (float)Math.Sqrt(1.0f - xj);
                w.x = cosPhi * sinTheta;
                w.y = sinPhi * sinTheta;
                w.z = cosTheta;
                onb.transform(w);
                ShadingState temp = state.traceFinalGather(new Ray(state.getPoint(), w), i);
                if (temp != null)
                {
                    temp.getInstance().prepareShadingState(temp);
                    if (temp.getShader() != null)
                    {
                        irr.add(temp.getShader().getRadiance(temp));
                    }
                }
            }
            irr.mul((float)Math.PI / n);
            return(irr);
        }
예제 #7
0
 public Color traceRefraction(ShadingState previous, Ray r, int i)
 {
     // limit path depth and disable caustic paths
     if (previous.getRefractionDepth() >= maxRefractionDepth || previous.getDiffuseDepth() > 0)
         return Color.BLACK;
     IntersectionState istate = previous.getIntersectionState();
     istate.numRefractionRays++;
     scene.trace(r, istate);
     return istate.hit() ? shadeHit(ShadingState.createRefractionBounceState(previous, r, i)) : Color.BLACK;
 }
예제 #8
0
 public void traceDiffusePhoton(ShadingState previous, Ray r, Color power)
 {
     if (previous.getDiffuseDepth() >= maxDiffuseDepth)
         return;
     IntersectionState istate = previous.getIntersectionState();
     scene.trace(r, istate);
     if (previous.getIntersectionState().hit())
     {
         // create a new shading context
         ShadingState state = ShadingState.createDiffuseBounceState(previous, r, 0);
         shadePhoton(state, power);
     }
 }
예제 #9
0
 public ShadingState traceFinalGather(ShadingState previous, Ray r, int i)
 {
     if (previous.getDiffuseDepth() >= maxDiffuseDepth)
         return null;
     IntersectionState istate = previous.getIntersectionState();
     scene.trace(r, istate);
     return istate.hit() ? ShadingState.createFinalGatherState(previous, r, i) : null;
 }
예제 #10
0
        public void getSamples(ShadingState state)
        {
            if (getNumSamples() <= 0)
            {
                return;
            }
            Vector3 wc = Point3.sub(center, state.getPoint(), new Vector3());
            float   l2 = wc.LengthSquared();

            if (l2 <= r2)
            {
                return; // inside the sphere?
            }
            // top of the sphere as viewed from the current shading point
            float topX = wc.x + state.getNormal().x *radius;
            float topY = wc.y + state.getNormal().y *radius;
            float topZ = wc.z + state.getNormal().z *radius;

            if (state.getNormal().dot(topX, topY, topZ) <= 0)
            {
                return; // top of the sphere is below the horizon
            }
            float            cosThetaMax = (float)Math.Sqrt(Math.Max(0, 1 - r2 / Vector3.dot(wc, wc)));
            OrthoNormalBasis basis       = OrthoNormalBasis.makeFromW(wc);
            int   samples = state.getDiffuseDepth() > 0 ? 1 : getNumSamples();
            float scale   = (float)(2 * Math.PI * (1 - cosThetaMax));
            Color c       = Color.mul(scale / samples, radiance);

            for (int i = 0; i < samples; i++)
            {
                // random offset on unit square
                double randX = state.getRandom(i, 0, samples);
                double randY = state.getRandom(i, 1, samples);

                // cone sampling
                double  cosTheta = (1 - randX) * cosThetaMax + randX;
                double  sinTheta = Math.Sqrt(1 - cosTheta * cosTheta);
                double  phi      = randY * 2 * Math.PI;
                Vector3 dir      = new Vector3((float)(Math.Cos(phi) * sinTheta), (float)(Math.Sin(phi) * sinTheta), (float)cosTheta);
                basis.transform(dir);

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

                float    ocx = state.getPoint().x - center.x;
                float    ocy = state.getPoint().y - center.y;
                float    ocz = state.getPoint().z - center.z;
                float    qa  = Vector3.dot(dir, dir);
                float    qb  = 2 * ((dir.x * ocx) + (dir.y * ocy) + (dir.z * ocz));
                float    qc  = ((ocx * ocx) + (ocy * ocy) + (ocz * ocz)) - r2;
                double[] t   = Solvers.solveQuadric(qa, qb, qc);
                if (t == null)
                {
                    continue;
                }
                LightSample dest = new LightSample();
                // compute shadow ray to the sampled point
                dest.setShadowRay(new Ray(state.getPoint(), dir));
                // FIXME: arbitrary bias, should handle as in other places
                dest.getShadowRay().setMax((float)t[0] - 1e-3f);
                // prepare sample
                dest.setRadiance(c, c);
                dest.traceShadow(state);
                state.addSample(dest);
            }
        }
예제 #11
0
 public Color getIrradiance(ShadingState state, Color diffuseReflectance)
 {
     // no gi engine, or we have already exceeded number of available bounces
     if (giEngine == null || state.getDiffuseDepth() >= maxDiffuseDepth)
         return Color.BLACK;
     return giEngine.getIrradiance(state, diffuseReflectance);
 }
예제 #12
0
            public void getSamples(ShadingState state)
            {
                if (meshlight.numSamples == 0)
                {
                    return;
                }
                Vector3 n = state.getNormal();
                Point3  p = state.getPoint();
                // vector towards each vertex of the light source
                Vector3 p0 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 0]), p, new Vector3());

                // cull triangle if it is facing the wrong way
                if (Vector3.dot(p0, ng) >= 0)
                {
                    return;
                }
                Vector3 p1 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 1]), p, new Vector3());
                Vector3 p2 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 2]), p, new Vector3());

                // if all three vertices are below the hemisphere, stop
                if (Vector3.dot(p0, n) <= 0 && Vector3.dot(p1, n) <= 0 && Vector3.dot(p2, n) <= 0)
                {
                    return;
                }
                p0.normalize();
                p1.normalize();
                p2.normalize();
                float   dot = Vector3.dot(p2, p0);
                Vector3 h   = new Vector3();

                h.x = p2.x - dot * p0.x;
                h.y = p2.y - dot * p0.y;
                h.z = p2.z - dot * p0.z;
                float hlen = h.Length();

                if (hlen > 1e-6f)
                {
                    h.div(hlen);
                }
                else
                {
                    return;
                }
                Vector3 n0   = Vector3.cross(p0, p1, new Vector3());
                float   len0 = n0.Length();

                if (len0 > 1e-6f)
                {
                    n0.div(len0);
                }
                else
                {
                    return;
                }
                Vector3 n1   = Vector3.cross(p1, p2, new Vector3());
                float   len1 = n1.Length();

                if (len1 > 1e-6f)
                {
                    n1.div(len1);
                }
                else
                {
                    return;
                }
                Vector3 n2   = Vector3.cross(p2, p0, new Vector3());
                float   len2 = n2.Length();

                if (len2 > 1e-6f)
                {
                    n2.div(len2);
                }
                else
                {
                    return;
                }

                float cosAlpha = MathUtils.clamp(-Vector3.dot(n2, n0), -1.0f, 1.0f);
                float cosBeta  = MathUtils.clamp(-Vector3.dot(n0, n1), -1.0f, 1.0f);
                float cosGamma = MathUtils.clamp(-Vector3.dot(n1, n2), -1.0f, 1.0f);

                float alpha = (float)Math.Acos(cosAlpha);
                float beta  = (float)Math.Acos(cosBeta);
                float gamma = (float)Math.Acos(cosGamma);

                float area = alpha + beta + gamma - (float)Math.PI;

                float cosC    = MathUtils.clamp(Vector3.dot(p0, p1), -1.0f, 1.0f);
                float salpha  = (float)Math.Sin(alpha);
                float product = salpha * cosC;

                // use lower sampling depth for diffuse bounces
                int   samples = state.getDiffuseDepth() > 0 ? 1 : meshlight.numSamples;
                Color c       = Color.mul(area / samples, meshlight.radiance);

                for (int i = 0; i < samples; i++)
                {
                    // random offset on unit square
                    double randX = state.getRandom(i, 0, samples);
                    double randY = state.getRandom(i, 1, samples);

                    float phi    = (float)randX * area - alpha + (float)Math.PI;
                    float sinPhi = (float)Math.Sin(phi);
                    float cosPhi = (float)Math.Cos(phi);

                    float u = cosPhi + cosAlpha;
                    float v = sinPhi - product;

                    float q  = (-v + cosAlpha * (cosPhi * -v + sinPhi * u)) / (salpha * (sinPhi * -v - cosPhi * u));
                    float q1 = 1.0f - q * q;
                    if (q1 < 0.0f)
                    {
                        q1 = 0.0f;
                    }

                    float sqrtq1 = (float)Math.Sqrt(q1);
                    float ncx    = q * p0.x + sqrtq1 * h.x;
                    float ncy    = q * p0.y + sqrtq1 * h.y;
                    float ncz    = q * p0.z + sqrtq1 * h.z;
                    dot = p1.dot(ncx, ncy, ncz);
                    float z  = 1.0f - (float)randY * (1.0f - dot);
                    float z1 = 1.0f - z * z;
                    if (z1 < 0.0f)
                    {
                        z1 = 0.0f;
                    }
                    Vector3 nd = new Vector3();
                    nd.x = ncx - dot * p1.x;
                    nd.y = ncy - dot * p1.y;
                    nd.z = ncz - dot * p1.z;
                    nd.normalize();
                    float   sqrtz1 = (float)Math.Sqrt(z1);
                    Vector3 result = new Vector3();
                    result.x = z * p1.x + sqrtz1 * nd.x;
                    result.y = z * p1.y + sqrtz1 * nd.y;
                    result.z = z * p1.z + sqrtz1 * nd.z;

                    // make sure the sample is in the right hemisphere - facing in
                    // the right direction
                    if (Vector3.dot(result, n) > 0 && Vector3.dot(result, state.getGeoNormal()) > 0 && Vector3.dot(result, ng) < 0)
                    {
                        // compute intersection with triangle (if any)
                        Ray shadowRay = new Ray(state.getPoint(), result);
                        if (!intersectTriangleKensler(shadowRay))
                        {
                            continue;
                        }
                        LightSample dest = new LightSample();
                        dest.setShadowRay(shadowRay);
                        // prepare sample
                        dest.setRadiance(c, c);
                        dest.traceShadow(state);
                        state.addSample(dest);
                    }
                }
            }
예제 #13
0
 public void store(ShadingState state, Vector3 dir, Color power, Color diffuse)
 {
     if (((state.getDiffuseDepth() == 0) && (state.getReflectionDepth() > 0 || state.getRefractionDepth() > 0)))
     {
         // this is a caustic photon
         Photon p = new Photon(state.getPoint(), dir, power);
         lock (lockObj)
         {
             storedPhotons++;
             photonList.Add(p);
             bounds.include(new Point3(p.x, p.y, p.z));
             maxPower = Math.Max(maxPower, power.getMax());
         }
     }
 }
예제 #14
0
 public Color getIrradiance(ShadingState state, Color diffuseReflectance)
 {
     if (samples <= 0)
         return Color.BLACK;
     if (state.getDiffuseDepth() > 0)
     {
         // do simple path tracing for additional bounces (single ray)
         float xi = (float)state.getRandom(0, 0, 1);
         float xj = (float)state.getRandom(0, 1, 1);
         float phi = (float)(xi * 2 * Math.PI);
         float cosPhi = (float)Math.Cos(phi);
         float sinPhi = (float)Math.Sin(phi);
         float sinTheta = (float)Math.Sqrt(xj);
         float cosTheta = (float)Math.Sqrt(1.0f - xj);
         Vector3 w = new Vector3();
         w.x = cosPhi * sinTheta;
         w.y = sinPhi * sinTheta;
         w.z = cosTheta;
         OrthoNormalBasis onb = state.getBasis();
         onb.transform(w);
         Ray r = new Ray(state.getPoint(), w);
         ShadingState temp = state.traceFinalGather(r, 0);
         return temp != null ? getGlobalRadiance(temp).copy().mul((float)Math.PI) : Color.BLACK;
     }
     //rwl.readLock().lockwoot();//fixme
     Color irr;
     lock(lockObj)
         irr = getIrradiance(state.getPoint(), state.getNormal());
     //rwl.readLock().unlock();
     if (irr == null)
     {
         // compute new sample
         irr = Color.black();
         OrthoNormalBasis onb = state.getBasis();
         float invR = 0;
         float minR = float.PositiveInfinity;
         Vector3 w = new Vector3();
         for (int i = 0; i < samples; i++)
         {
             float xi = (float)state.getRandom(i, 0, samples);
             float xj = (float)state.getRandom(i, 1, samples);
             float phi = (float)(xi * 2 * Math.PI);
             float cosPhi = (float)Math.Cos(phi);
             float sinPhi = (float)Math.Sin(phi);
             float sinTheta = (float)Math.Sqrt(xj);
             float cosTheta = (float)Math.Sqrt(1.0f - xj);
             w.x = cosPhi * sinTheta;
             w.y = sinPhi * sinTheta;
             w.z = cosTheta;
             onb.transform(w);
             Ray r = new Ray(state.getPoint(), w);
             ShadingState temp = state.traceFinalGather(r, i);
             if (temp != null)
             {
                 minR = Math.Min(r.getMax(), minR);
                 invR += 1.0f / r.getMax();
                 temp.getInstance().prepareShadingState(temp);
                 irr.add(getGlobalRadiance(temp));
             }
         }
         irr.mul((float)Math.PI / samples);
         invR = samples / invR;
         //rwl.writeLock().lockwoot();//fixme
         lock(lockObj)
             insert(state.getPoint(), state.getNormal(), invR, irr);
         //rwl.writeLock().unlock();
         // view irr-cache points
         // irr = Color.YELLOW.copy().mul(1e6f);
     }
     return irr;
 }
예제 #15
0
        public Color getIrradiance(ShadingState state, Color diffuseReflectance)
        {
            float   b   = (float)Math.PI * c / diffuseReflectance.getMax();
            Color   irr = Color.black();
            Point3  p   = state.getPoint();
            Vector3 n   = state.getNormal();
            int     set = (int)(state.getRandom(0, 1, 1) * numSets);

            foreach (PointLight vpl in virtualLights[set])
            {
                Ray   r      = new Ray(p, vpl.p);
                float dotNlD = -(r.dx * vpl.n.x + r.dy * vpl.n.y + r.dz * vpl.n.z);
                float dotND  = r.dx * n.x + r.dy * n.y + r.dz * n.z;
                if (dotNlD > 0 && dotND > 0)
                {
                    float r2      = r.getMax() * r.getMax();
                    Color opacity = state.traceShadow(r);
                    Color power   = Color.blend(vpl.power, Color.BLACK, opacity);
                    float g       = (dotND * dotNlD) / r2;
                    irr.madd(0.25f * Math.Min(g, b), power);
                }
            }
            // bias compensation
            int nb = (state.getDiffuseDepth() == 0 || numBias <= 0) ? numBias : 1;

            if (nb <= 0)
            {
                return(irr);
            }
            OrthoNormalBasis onb   = state.getBasis();
            Vector3          w     = new Vector3();
            float            scale = (float)Math.PI / nb;

            for (int i = 0; i < nb; i++)
            {
                float xi       = (float)state.getRandom(i, 0, nb);
                float xj       = (float)state.getRandom(i, 1, nb);
                float phi      = (float)(xi * 2 * Math.PI);
                float cosPhi   = (float)Math.Cos(phi);
                float sinPhi   = (float)Math.Sin(phi);
                float sinTheta = (float)Math.Sqrt(xj);
                float cosTheta = (float)Math.Sqrt(1.0f - xj);
                w.x = cosPhi * sinTheta;
                w.y = sinPhi * sinTheta;
                w.z = cosTheta;
                onb.transform(w);
                Ray r = new Ray(state.getPoint(), w);
                r.setMax((float)Math.Sqrt(cosTheta / b));
                ShadingState temp = state.traceFinalGather(r, i);
                if (temp != null)
                {
                    temp.getInstance().prepareShadingState(temp);
                    if (temp.getShader() != null)
                    {
                        float dist      = temp.getRay().getMax();
                        float r2        = dist * dist;
                        float cosThetaY = -Vector3.dot(w, temp.getNormal());
                        if (cosThetaY > 0)
                        {
                            float g = (cosTheta * cosThetaY) / r2;
                            // was this path accounted for yet?
                            if (g > b)
                            {
                                irr.madd(scale * (g - b) / g, temp.getShader().GetRadiance(temp));
                            }
                        }
                    }
                }
            }
            return(irr);
        }
예제 #16
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
                    double randX = state.getRandom(i, 0, n);
                    double randY = state.getRandom(i, 1, n);

                    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);
                    }
                }
            }
        }
예제 #17
0
            public void getSamples(ShadingState state)
            {
                if (meshlight.numSamples == 0)
                    return;
                Vector3 n = state.getNormal();
                Point3 p = state.getPoint();
                // vector towards each vertex of the light source
                Vector3 p0 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 0]), p, new Vector3());
                // cull triangle if it is facing the wrong way
                if (Vector3.dot(p0, ng) >= 0)
                    return;
                Vector3 p1 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 1]), p, new Vector3());
                Vector3 p2 = Point3.sub(meshlight.getPoint(meshlight.triangles[tri3 + 2]), p, new Vector3());
                // if all three vertices are below the hemisphere, stop
                if (Vector3.dot(p0, n) <= 0 && Vector3.dot(p1, n) <= 0 && Vector3.dot(p2, n) <= 0)
                    return;
                p0.normalize();
                p1.normalize();
                p2.normalize();
                float dot = Vector3.dot(p2, p0);
                Vector3 h = new Vector3();
                h.x = p2.x - dot * p0.x;
                h.y = p2.y - dot * p0.y;
                h.z = p2.z - dot * p0.z;
                float hlen = h.Length();
                if (hlen > 1e-6f)
                    h.div(hlen);
                else
                    return;
                Vector3 n0 = Vector3.cross(p0, p1, new Vector3());
                float len0 = n0.Length();
                if (len0 > 1e-6f)
                    n0.div(len0);
                else
                    return;
                Vector3 n1 = Vector3.cross(p1, p2, new Vector3());
                float len1 = n1.Length();
                if (len1 > 1e-6f)
                    n1.div(len1);
                else
                    return;
                Vector3 n2 = Vector3.cross(p2, p0, new Vector3());
                float len2 = n2.Length();
                if (len2 > 1e-6f)
                    n2.div(len2);
                else
                    return;

                float cosAlpha = MathUtils.clamp(-Vector3.dot(n2, n0), -1.0f, 1.0f);
                float cosBeta = MathUtils.clamp(-Vector3.dot(n0, n1), -1.0f, 1.0f);
                float cosGamma = MathUtils.clamp(-Vector3.dot(n1, n2), -1.0f, 1.0f);

                float alpha = (float)Math.Acos(cosAlpha);
                float beta = (float)Math.Acos(cosBeta);
                float gamma = (float)Math.Acos(cosGamma);

                float area = alpha + beta + gamma - (float)Math.PI;

                float cosC = MathUtils.clamp(Vector3.dot(p0, p1), -1.0f, 1.0f);
                float salpha = (float)Math.Sin(alpha);
                float product = salpha * cosC;

                // use lower sampling depth for diffuse bounces
                int samples = state.getDiffuseDepth() > 0 ? 1 : meshlight.numSamples;
                Color c = Color.mul(area / samples, meshlight.radiance);
                for (int i = 0; i < samples; i++)
                {
                    // random offset on unit square
                    double randX = state.getRandom(i, 0, samples);
                    double randY = state.getRandom(i, 1, samples);

                    float phi = (float)randX * area - alpha + (float)Math.PI;
                    float sinPhi = (float)Math.Sin(phi);
                    float cosPhi = (float)Math.Cos(phi);

                    float u = cosPhi + cosAlpha;
                    float v = sinPhi - product;

                    float q = (-v + cosAlpha * (cosPhi * -v + sinPhi * u)) / (salpha * (sinPhi * -v - cosPhi * u));
                    float q1 = 1.0f - q * q;
                    if (q1 < 0.0f)
                        q1 = 0.0f;

                    float sqrtq1 = (float)Math.Sqrt(q1);
                    float ncx = q * p0.x + sqrtq1 * h.x;
                    float ncy = q * p0.y + sqrtq1 * h.y;
                    float ncz = q * p0.z + sqrtq1 * h.z;
                    dot = p1.dot(ncx, ncy, ncz);
                    float z = 1.0f - (float)randY * (1.0f - dot);
                    float z1 = 1.0f - z * z;
                    if (z1 < 0.0f)
                        z1 = 0.0f;
                    Vector3 nd = new Vector3();
                    nd.x = ncx - dot * p1.x;
                    nd.y = ncy - dot * p1.y;
                    nd.z = ncz - dot * p1.z;
                    nd.normalize();
                    float sqrtz1 = (float)Math.Sqrt(z1);
                    Vector3 result = new Vector3();
                    result.x = z * p1.x + sqrtz1 * nd.x;
                    result.y = z * p1.y + sqrtz1 * nd.y;
                    result.z = z * p1.z + sqrtz1 * nd.z;

                    // make sure the sample is in the right hemisphere - facing in
                    // the right direction
                    if (Vector3.dot(result, n) > 0 && Vector3.dot(result, state.getGeoNormal()) > 0 && Vector3.dot(result, ng) < 0)
                    {
                        // compute intersection with triangle (if any)
                        Ray shadowRay = new Ray(state.getPoint(), result);
                        if (!intersectTriangleKensler(shadowRay))
                            continue;
                        LightSample dest = new LightSample();
                        dest.setShadowRay(shadowRay);
                        // prepare sample
                        dest.setRadiance(c, c);
                        dest.traceShadow(state);
                        state.addSample(dest);
                    }
                }
            }
예제 #18
0
        public void getSamples(ShadingState state)
        {
            if (getNumSamples() <= 0)
                return;
            Vector3 wc = Point3.sub(center, state.getPoint(), new Vector3());
            float l2 = wc.LengthSquared();
            if (l2 <= r2)
                return; // inside the sphere?
            // top of the sphere as viewed from the current shading point
            float topX = wc.x + state.getNormal().x * radius;
            float topY = wc.y + state.getNormal().y * radius;
            float topZ = wc.z + state.getNormal().z * radius;
            if (state.getNormal().dot(topX, topY, topZ) <= 0)
                return; // top of the sphere is below the horizon
            float cosThetaMax = (float)Math.Sqrt(Math.Max(0, 1 - r2 / Vector3.dot(wc, wc)));
            OrthoNormalBasis basis = OrthoNormalBasis.makeFromW(wc);
            int samples = state.getDiffuseDepth() > 0 ? 1 : getNumSamples();
            float scale = (float)(2 * Math.PI * (1 - cosThetaMax));
            Color c = Color.mul(scale / samples, radiance);
            for (int i = 0; i < samples; i++)
            {
                // random offset on unit square
                double randX = state.getRandom(i, 0, samples);
                double randY = state.getRandom(i, 1, samples);

                // cone sampling
                double cosTheta = (1 - randX) * cosThetaMax + randX;
                double sinTheta = Math.Sqrt(1 - cosTheta * cosTheta);
                double phi = randY * 2 * Math.PI;
                Vector3 dir = new Vector3((float)(Math.Cos(phi) * sinTheta), (float)(Math.Sin(phi) * sinTheta), (float)cosTheta);
                basis.transform(dir);

                // check that the direction of the sample is the same as the
                // normal
                float cosNx = Vector3.dot(dir, state.getNormal());
                if (cosNx <= 0)
                    continue;

                float ocx = state.getPoint().x - center.x;
                float ocy = state.getPoint().y - center.y;
                float ocz = state.getPoint().z - center.z;
                float qa = Vector3.dot(dir, dir);
                float qb = 2 * ((dir.x * ocx) + (dir.y * ocy) + (dir.z * ocz));
                float qc = ((ocx * ocx) + (ocy * ocy) + (ocz * ocz)) - r2;
                double[] t = Solvers.solveQuadric(qa, qb, qc);
                if (t == null)
                    continue;
                LightSample dest = new LightSample();
                // compute shadow ray to the sampled point
                dest.setShadowRay(new Ray(state.getPoint(), dir));
                // FIXME: arbitrary bias, should handle as in other places
                dest.getShadowRay().setMax((float)t[0] - 1e-3f);
                // prepare sample
                dest.setRadiance(c, c);
                dest.traceShadow(state);
                state.addSample(dest);
            }
        }
예제 #19
0
        public Color getIrradiance(ShadingState state, Color diffuseReflectance)
        {
            if (samples <= 0)
            {
                return(Color.BLACK);
            }
            if (state.getDiffuseDepth() > 0)
            {
                // do simple path tracing for additional bounces (single ray)
                float   xi       = (float)state.getRandom(0, 0, 1);
                float   xj       = (float)state.getRandom(0, 1, 1);
                float   phi      = (float)(xi * 2 * Math.PI);
                float   cosPhi   = (float)Math.Cos(phi);
                float   sinPhi   = (float)Math.Sin(phi);
                float   sinTheta = (float)Math.Sqrt(xj);
                float   cosTheta = (float)Math.Sqrt(1.0f - xj);
                Vector3 w        = new Vector3();
                w.x = cosPhi * sinTheta;
                w.y = sinPhi * sinTheta;
                w.z = cosTheta;
                OrthoNormalBasis onb = state.getBasis();
                onb.transform(w);
                Ray          r    = new Ray(state.getPoint(), w);
                ShadingState temp = state.traceFinalGather(r, 0);
                return(temp != null?getGlobalRadiance(temp).copy().mul((float)Math.PI) : Color.BLACK);
            }
            //rwl.readLock().lockwoot();//fixme
            Color irr;

            lock (lockObj)
                irr = getIrradiance(state.getPoint(), state.getNormal());
            //rwl.readLock().unlock();
            if (irr == null)
            {
                // compute new sample
                irr = Color.black();
                OrthoNormalBasis onb  = state.getBasis();
                float            invR = 0;
                float            minR = float.PositiveInfinity;
                Vector3          w    = new Vector3();
                for (int i = 0; i < samples; i++)
                {
                    float xi       = (float)state.getRandom(i, 0, samples);
                    float xj       = (float)state.getRandom(i, 1, samples);
                    float phi      = (float)(xi * 2 * Math.PI);
                    float cosPhi   = (float)Math.Cos(phi);
                    float sinPhi   = (float)Math.Sin(phi);
                    float sinTheta = (float)Math.Sqrt(xj);
                    float cosTheta = (float)Math.Sqrt(1.0f - xj);
                    w.x = cosPhi * sinTheta;
                    w.y = sinPhi * sinTheta;
                    w.z = cosTheta;
                    onb.transform(w);
                    Ray          r    = new Ray(state.getPoint(), w);
                    ShadingState temp = state.traceFinalGather(r, i);
                    if (temp != null)
                    {
                        minR  = Math.Min(r.getMax(), minR);
                        invR += 1.0f / r.getMax();
                        temp.getInstance().prepareShadingState(temp);
                        irr.add(getGlobalRadiance(temp));
                    }
                }
                irr.mul((float)Math.PI / samples);
                invR = samples / invR;
                //rwl.writeLock().lockwoot();//fixme
                lock (lockObj)
                    insert(state.getPoint(), state.getNormal(), invR, irr);
                //rwl.writeLock().unlock();
                // view irr-cache points
                // irr = Color.YELLOW.copy().mul(1e6f);
            }
            return(irr);
        }
예제 #20
0
 public Color getIrradiance(ShadingState state, Color diffuseReflectance)
 {
     float b = (float)Math.PI * c / diffuseReflectance.getMax();
     Color irr = Color.black();
     Point3 p = state.getPoint();
     Vector3 n = state.getNormal();
     int set = (int)(state.getRandom(0, 1, 1) * numSets);
     foreach (PointLight vpl in virtualLights[set])
     {
         Ray r = new Ray(p, vpl.p);
         float dotNlD = -(r.dx * vpl.n.x + r.dy * vpl.n.y + r.dz * vpl.n.z);
         float dotND = r.dx * n.x + r.dy * n.y + r.dz * n.z;
         if (dotNlD > 0 && dotND > 0)
         {
             float r2 = r.getMax() * r.getMax();
             Color opacity = state.traceShadow(r);
             Color power = Color.blend(vpl.power, Color.BLACK, opacity);
             float g = (dotND * dotNlD) / r2;
             irr.madd(0.25f * Math.Min(g, b), power);
         }
     }
     // bias compensation
     int nb = (state.getDiffuseDepth() == 0 || numBias <= 0) ? numBias : 1;
     if (nb <= 0)
         return irr;
     OrthoNormalBasis onb = state.getBasis();
     Vector3 w = new Vector3();
     float scale = (float)Math.PI / nb;
     for (int i = 0; i < nb; i++)
     {
         float xi = (float)state.getRandom(i, 0, nb);
         float xj = (float)state.getRandom(i, 1, nb);
         float phi = (float)(xi * 2 * Math.PI);
         float cosPhi = (float)Math.Cos(phi);
         float sinPhi = (float)Math.Sin(phi);
         float sinTheta = (float)Math.Sqrt(xj);
         float cosTheta = (float)Math.Sqrt(1.0f - xj);
         w.x = cosPhi * sinTheta;
         w.y = sinPhi * sinTheta;
         w.z = cosTheta;
         onb.transform(w);
         Ray r = new Ray(state.getPoint(), w);
         r.setMax((float)Math.Sqrt(cosTheta / b));
         ShadingState temp = state.traceFinalGather(r, i);
         if (temp != null)
         {
             temp.getInstance().prepareShadingState(temp);
             if (temp.getShader() != null)
             {
                 float dist = temp.getRay().getMax();
                 float r2 = dist * dist;
                 float cosThetaY = -Vector3.dot(w, temp.getNormal());
                 if (cosThetaY > 0)
                 {
                     float g = (cosTheta * cosThetaY) / r2;
                     // was this path accounted for yet?
                     if (g > b)
                         irr.madd(scale * (g - b) / g, temp.getShader().getRadiance(temp));
                 }
             }
         }
     }
     return irr;
 }