Exemple #1
0
        public void scatterPhoton(ShadingState state, Color power)
        {
            Color diffuse, specular;

            // make sure we are on the right side of the material
            state.faceforward();
            diffuse  = getDiffuse(state);
            specular = getSpecular(state);
            state.storePhoton(state.getRay().getDirection(), power, diffuse);
            float  d   = diffuse.getAverage();
            float  r   = specular.getAverage();
            double rnd = state.getRandom(0, 0, 1);

            if (rnd < d)
            {
                // photon is scattered
                power.mul(diffuse).mul(1.0f / d);
                OrthoNormalBasis onb = state.getBasis();
                double           u   = 2 * Math.PI * rnd / d;
                double           v   = state.getRandom(0, 1, 1);
                float            s   = (float)Math.Sqrt(v);
                float            s1  = (float)Math.Sqrt(1.0 - v);
                Vector3          w   = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1);
                w = onb.transform(w, new Vector3());
                state.traceDiffusePhoton(new Ray(state.getPoint(), w), power);
            }
            else if (rnd < d + r)
            {
                if (glossyness == 0)
                {
                    float cos = -Vector3.dot(state.getNormal(), state.getRay().getDirection());
                    power.mul(diffuse).mul(1.0f / d);
                    // photon is reflected
                    float   dn  = 2 * cos;
                    Vector3 dir = new Vector3();
                    dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
                    dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
                    dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;
                    state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power);
                }
                else
                {
                    float dn = 2.0f * state.getCosND();
                    // reflected direction
                    Vector3 refDir = new Vector3();
                    refDir.x = (dn * state.getNormal().x) + state.getRay().dx;
                    refDir.y = (dn * state.getNormal().y) + state.getRay().dy;
                    refDir.z = (dn * state.getNormal().z) + state.getRay().dz;
                    power.mul(spec).mul(1.0f / r);
                    OrthoNormalBasis onb = state.getBasis();
                    double           u   = 2 * Math.PI * (rnd - r) / r;
                    double           v   = state.getRandom(0, 1, 1);
                    float            s   = (float)Math.Pow(v, 1 / ((1.0f / glossyness) + 1));
                    float            s1  = (float)Math.Sqrt(1 - s * s);
                    Vector3          w   = new Vector3((float)Math.Cos(u) * s1, (float)Math.Sin(u) * s1, s);
                    w = onb.transform(w, new Vector3());
                    state.traceReflectionPhoton(new Ray(state.getPoint(), w), power);
                }
            }
        }
 public void getSamples(ShadingState state)
 {
     if (Vector3.dot(dir, state.getGeoNormal()) < 0 && Vector3.dot(dir, state.getNormal()) < 0)
     {
         // project point onto source plane
         float x = state.getPoint().x - src.x;
         float y = state.getPoint().y - src.y;
         float z = state.getPoint().z - src.z;
         float t = ((x * dir.x) + (y * dir.y) + (z * dir.z));
         if (t >= 0.0)
         {
             x -= (t * dir.x);
             y -= (t * dir.y);
             z -= (t * dir.z);
             if (((x * x) + (y * y) + (z * z)) <= r2)
             {
                 Point3 p = new Point3();
                 p.x = src.x + x;
                 p.y = src.y + y;
                 p.z = src.z + z;
                 LightSample dest = new LightSample();
                 dest.setShadowRay(new Ray(state.getPoint(), p));
                 dest.setRadiance(radiance, radiance);
                 dest.traceShadow(state);
                 state.addSample(dest);
             }
         }
     }
 }
Exemple #3
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            state.getRay().getPoint(state.getPoint());
            Instance parent = state.getInstance();
            Point3 localPoint = parent.transformWorldToObject(state.getPoint());
            state.getNormal().set(localPoint.x, localPoint.y, localPoint.z);
            state.getNormal().normalize();

            float phi = (float)Math.Atan2(state.getNormal().y, state.getNormal().x);
            if (phi < 0)
                phi += (float)(2 * Math.PI);
            float theta = (float)Math.Acos(state.getNormal().z);
            state.getUV().y = theta / (float)Math.PI;
            state.getUV().x = phi / (float)(2 * Math.PI);
            Vector3 v = new Vector3();
            v.x = -2 * (float)Math.PI * state.getNormal().y;
            v.y = 2 * (float)Math.PI * state.getNormal().x;
            v.z = 0;
            state.setShader(parent.getShader(0));
            state.setModifier(parent.getModifier(0));
            // into world space
            Vector3 worldNormal = parent.transformNormalObjectToWorld(state.getNormal());
            v = parent.transformVectorObjectToWorld(v);
            state.getNormal().set(worldNormal);
            state.getNormal().normalize();
            state.getGeoNormal().set(state.getNormal());
            // compute basis in world space
            state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), v));
        }
Exemple #4
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            state.getRay().getPoint(state.getPoint());
            Instance parent = state.getInstance();
            // get local point
            Point3 p = state.transformWorldToObject(state.getPoint());
            // compute local normal
            float deriv = p.x * p.x + p.y * p.y + p.z * p.z - ri2 - ro2;

            state.getNormal().set(p.x * deriv, p.y * deriv, p.z * deriv + 2 * ro2 * p.z);
            state.getNormal().normalize();

            double phi   = Math.Asin(MathUtils.clamp(p.z / ri, -1, 1));
            double theta = Math.Atan2(p.y, p.x);

            if (theta < 0)
            {
                theta += 2 * Math.PI;
            }
            state.getUV().x = (float)(theta / (2 * Math.PI));
            state.getUV().y = (float)((phi + Math.PI / 2) / Math.PI);
            state.setShader(parent.getShader(0));
            state.setModifier(parent.getModifier(0));
            // into world space
            Vector3 worldNormal = state.transformNormalObjectToWorld(state.getNormal());

            state.getNormal().set(worldNormal);
            state.getNormal().normalize();
            state.getGeoNormal().set(state.getNormal());
            // make basis in world space
            state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
        }
Exemple #5
0
 public void getSamples(ShadingState state)
 {
     if (Vector3.dot(dir, state.getGeoNormal()) < 0 && Vector3.dot(dir, state.getNormal()) < 0)
     {
         // project point onto source plane
         float x = state.getPoint().x - src.x;
         float y = state.getPoint().y - src.y;
         float z = state.getPoint().z - src.z;
         float t = ((x * dir.x) + (y * dir.y) + (z * dir.z));
         if (t >= 0.0)
         {
             x -= (t * dir.x);
             y -= (t * dir.y);
             z -= (t * dir.z);
             if (((x * x) + (y * y) + (z * z)) <= r2)
             {
                 Point3 p = new Point3();
                 p.x = src.x + x;
                 p.y = src.y + y;
                 p.z = src.z + z;
                 LightSample dest = new LightSample();
                 dest.setShadowRay(new Ray(state.getPoint(), p));
                 dest.setRadiance(radiance, radiance);
                 dest.traceShadow(state);
                 state.addSample(dest);
             }
         }
     }
 }
Exemple #6
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            state.getRay().getPoint(state.getPoint());
            Instance parent     = state.getInstance();
            Point3   localPoint = state.transformWorldToObject(state.getPoint());

            state.getNormal().set(localPoint.x, localPoint.y, 0);
            state.getNormal().normalize();

            float phi = (float)Math.Atan2(state.getNormal().y, state.getNormal().x);

            if (phi < 0)
            {
                phi += (float)(2.0 * Math.PI);
            }
            state.getUV().x = phi / (float)(2 * Math.PI);
            state.getUV().y = (localPoint.z + 1) * 0.5f;
            state.setShader(parent.getShader(0));
            state.setModifier(parent.getModifier(0));
            // into world space
            Vector3 worldNormal = state.transformNormalObjectToWorld(state.getNormal());
            Vector3 v           = state.transformVectorObjectToWorld(new Vector3(0, 0, 1));

            state.getNormal().set(worldNormal);
            state.getNormal().normalize();
            state.getGeoNormal().set(state.getNormal());
            // compute basis in world space
            state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), v));
        }
Exemple #7
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);
                }
            }
        }
Exemple #8
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);
                    }
                }
            }
        }
Exemple #9
0
 public Color getRadiance(ShadingState state)
 {
     Point3[] p = new Point3[3];
     if (!state.getTrianglePoints(p))
         return getFillColor(state);
     // transform points into camera space
     Point3 center = state.getPoint();
     Matrix4 w2c = state.getWorldToCamera();
     center = w2c.transformP(center);
     for (int i = 0; i < 3; i++)
         p[i] = w2c.transformP(state.getInstance().transformObjectToWorld(p[i]));
     float cn = 1.0f / (float)Math.Sqrt(center.x * center.x + center.y * center.y + center.z * center.z);
     for (int i = 0, i2 = 2; i < 3; i2 = i, i++)
     {
         // compute orthogonal projection of the shading point onto each
         // triangle edge as in:
         // http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
         float t = (center.x - p[i].x) * (p[i2].x - p[i].x);
         t += (center.y - p[i].y) * (p[i2].y - p[i].y);
         t += (center.z - p[i].z) * (p[i2].z - p[i].z);
         t /= p[i].distanceToSquared(p[i2]);
         float projx = (1 - t) * p[i].x + t * p[i2].x;
         float projy = (1 - t) * p[i].y + t * p[i2].y;
         float projz = (1 - t) * p[i].z + t * p[i2].z;
         float n = 1.0f / (float)Math.Sqrt(projx * projx + projy * projy + projz * projz);
         // check angular width
         float dot = projx * center.x + projy * center.y + projz * center.z;
         if (dot * n * cn >= cosWidth)
             return getLineColor(state);
     }
     return getFillColor(state);
 }
Exemple #10
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            Instance i = state.getInstance();

            state.getRay().getPoint(state.getPoint());
            Ray     r = state.getRay();
            IShader s = i.getShader(0);

            state.setShader(s != null ? s : this);
            int primID = state.getPrimitiveID();
            int hair   = primID / numSegments;
            int line   = primID % numSegments;
            int vRoot  = hair * 3 * (numSegments + 1);
            int v0     = vRoot + line * 3;

            // tangent vector
            Vector3 v = getTangent(line, v0, state.getV());

            v = state.transformVectorObjectToWorld(v);
            state.setBasis(OrthoNormalBasis.makeFromWV(v, new Vector3(-r.dx, -r.dy, -r.dz)));
            state.getBasis().swapVW();
            // normal
            state.getNormal().set(0, 0, 1);
            state.getBasis().transform(state.getNormal());
            state.getGeoNormal().set(state.getNormal());

            state.getUV().set(0, (line + state.getV()) / numSegments);
        }
        public Color getIrradiance(ShadingState state, Color diffuseReflectance)
        {
            OrthoNormalBasis onb    = state.getBasis();
            Vector3          w      = new Vector3();
            Color            result = Color.black();

            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)(2 * Math.PI * xi);
                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(maxDist);
                result.add(Color.blend(bright, dark, state.traceShadow(r)));
            }
            return(result.mul((float)Math.PI / samples));
        }
Exemple #12
0
        public void ScatterPhoton(ShadingState state, Color power)
        {
            Color diffuse;

            // make sure we are on the right side of the material
            if (Vector3.dot(state.getNormal(), state.getRay().getDirection()) > 0.0)
            {
                state.getNormal().negate();
                state.getGeoNormal().negate();
            }
            diffuse = Color.GRAY;
            state.storePhoton(state.getRay().getDirection(), power, diffuse);
            float  avg = diffuse.getAverage();
            double rnd = state.getRandom(0, 0, 1);

            if (rnd < avg)
            {
                // photon is scattered
                power.mul(diffuse).mul(1.0f / avg);
                OrthoNormalBasis onb = state.getBasis();
                double           u   = 2 * Math.PI * rnd / avg;
                double           v   = state.getRandom(0, 1, 1);
                float            s   = (float)Math.Sqrt(v);
                float            s1  = (float)Math.Sqrt(1.0 - v);
                Vector3          w   = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1);
                w = onb.transform(w, new Vector3());
                state.traceDiffusePhoton(new Ray(state.getPoint(), w), power);
            }
        }
Exemple #13
0
        public Color GetRadiance(ShadingState state)
        {
            // make sure we are on the right side of the material
            state.faceforward();
            // direct lighting
            state.initLightSamples();
            state.initCausticSamples();
            Color d = getDiffuse(state);
            Color lr = state.diffuse(d);
            if (!state.includeSpecular)
                return lr;
            if (glossyness == 0)
            {
                float cos = state.getCosND();
                float dn = 2 * cos;
                Vector3 refDir = new Vector3();
                refDir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
                refDir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
                refDir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;
                Ray refRay = new Ray(state.getPoint(), refDir);
                // compute Fresnel term
                cos = 1 - cos;
                float cos2 = cos * cos;
                float cos5 = cos2 * cos2 * cos;

                Color spec = getSpecular(state);
                Color ret = Color.white();
                ret.sub(spec);
                ret.mul(cos5);
                ret.add(spec);
                return lr.add(ret.mul(state.traceReflection(refRay, 0)));
            }
            else
                return lr.add(state.specularPhong(getSpecular(state), 2 / glossyness, numSamples));
        }
Exemple #14
0
        public Color getRadiance(ShadingState state)
        {
            if (!state.includeSpecular)
            {
                return(Color.BLACK);
            }
            state.faceforward();
            float   cos    = state.getCosND();
            float   dn     = 2 * cos;
            Vector3 refDir = new Vector3();

            refDir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
            refDir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
            refDir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;
            Ray refRay = new Ray(state.getPoint(), refDir);

            // compute Fresnel term
            cos = 1 - cos;
            float cos2 = cos * cos;
            float cos5 = cos2 * cos2 * cos;
            Color ret  = Color.white();

            ret.sub(color);
            ret.mul(cos5);
            ret.add(color);
            return(ret.mul(state.traceReflection(refRay, 0)));
        }
Exemple #15
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);
     }
 }
Exemple #16
0
 public void scatterPhoton(ShadingState state, Color power)
 {
     Color diffuse;
     // make sure we are on the right side of the material
     if (Vector3.dot(state.getNormal(), state.getRay().getDirection()) > 0.0)
     {
         state.getNormal().negate();
         state.getGeoNormal().negate();
     }
     diffuse = getDiffuse(state);
     state.storePhoton(state.getRay().getDirection(), power, diffuse);
     float avg = diffuse.getAverage();
     double rnd = state.getRandom(0, 0, 1);
     if (rnd < avg)
     {
         // photon is scattered
         power.mul(diffuse).mul(1.0f / avg);
         OrthoNormalBasis onb = state.getBasis();
         double u = 2 * Math.PI * rnd / avg;
         double v = state.getRandom(0, 1, 1);
         float s = (float)Math.Sqrt(v);
         float s1 = (float)Math.Sqrt(1.0 - v);
         Vector3 w = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1);
         w = onb.transform(w, new Vector3());
         state.traceDiffusePhoton(new Ray(state.getPoint(), w), power);
     }
 }
 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;
 }
Exemple #18
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);
            }
        }
Exemple #19
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            state.getRay().getPoint(state.getPoint());
            Instance parent      = state.getInstance();
            Vector3  worldNormal = state.transformNormalObjectToWorld(normal);

            state.getNormal().set(worldNormal);
            state.getGeoNormal().set(worldNormal);
            state.setShader(parent.getShader(0));
            state.setModifier(parent.getModifier(0));
            Point3 p = state.transformWorldToObject(state.getPoint());
            float  hu, hv;

            switch (k)
            {
            case 0:
            {
                hu = p.y;
                hv = p.z;
                break;
            }

            case 1:
            {
                hu = p.z;
                hv = p.x;
                break;
            }

            case 2:
            {
                hu = p.x;
                hv = p.y;
                break;
            }

            default:
                hu = hv = 0;
                break;
            }
            state.getUV().x = hu * bnu + hv * bnv + bnd;
            state.getUV().y = hu * cnu + hv * cnv + cnd;
            state.setBasis(OrthoNormalBasis.makeFromW(normal));
        }
Exemple #20
0
 public void prepareShadingState(ShadingState state)
 {
     state.init();
     state.getRay().getPoint(state.getPoint());
     Instance parent = state.getInstance();
     Point3 n = parent.transformWorldToObject(state.getPoint());
     state.getNormal().set(n.x * (2 * n.x * n.x - 1), n.y * (2 * n.y * n.y - 1), n.z * (2 * n.z * n.z - 1));
     state.getNormal().normalize();
     state.setShader(parent.getShader(0));
     state.setModifier(parent.getModifier(0));
     // into world space
     Vector3 worldNormal = parent.transformNormalObjectToWorld(state.getNormal());
     state.getNormal().set(worldNormal);
     state.getNormal().normalize();
     state.getGeoNormal().set(state.getNormal());
     // create basis in world space
     state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
 }
Exemple #21
0
        public Color GetRadiance(ShadingState state)
        {
            if (!state.includeSpecular)
                return Color.BLACK;
            Vector3 reflDir = new Vector3();
            Vector3 refrDir = new Vector3();
            state.faceforward();
            float cos = state.getCosND();
            bool inside = state.isBehind();
            float neta = inside ? eta : 1.0f / eta;

            float dn = 2 * cos;
            reflDir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
            reflDir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
            reflDir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;

            // refracted ray
            float arg = 1 - (neta * neta * (1 - (cos * cos)));
            bool tir = arg < 0;
            if (tir)
                refrDir.x = refrDir.y = refrDir.z = 0;
            else
            {
                float nK = (neta * cos) - (float)Math.Sqrt(arg);
                refrDir.x = (neta * state.getRay().dx) + (nK * state.getNormal().x);
                refrDir.y = (neta * state.getRay().dy) + (nK * state.getNormal().y);
                refrDir.z = (neta * state.getRay().dz) + (nK * state.getNormal().z);
            }

            // compute Fresnel terms
            float cosTheta1 = Vector3.dot(state.getNormal(), reflDir);
            float cosTheta2 = -Vector3.dot(state.getNormal(), refrDir);

            float pPara = (cosTheta1 - eta * cosTheta2) / (cosTheta1 + eta * cosTheta2);
            float pPerp = (eta * cosTheta1 - cosTheta2) / (eta * cosTheta1 + cosTheta2);
            float kr = 0.5f * (pPara * pPara + pPerp * pPerp);
            float kt = 1 - kr;

            Color absorption = null;
            if (inside && absorptionDistance > 0)
            {
                // this ray is inside the object and leaving it
                // compute attenuation that occured along the ray
                absorption = Color.mul(-state.getRay().getMax() / absorptionDistance, absorptionColor.copy().opposite()).exp();
                if (absorption.isBlack())
                    return Color.BLACK; // nothing goes through
            }
            // refracted ray
            Color ret = Color.black();
            if (!tir)
            {
                ret.madd(kt, state.traceRefraction(new Ray(state.getPoint(), refrDir), 0)).mul(color);
            }
            if (!inside || tir)
                ret.add(Color.mul(kr, state.traceReflection(new Ray(state.getPoint(), reflDir), 0)).mul(color));
            return absorption != null ? ret.mul(absorption) : ret;
        }
Exemple #22
0
        public Color GetRadiance(ShadingState state)
        {
            int   side = state.getPrimitiveID();
            Color kd   = null;

            switch (side)
            {
            case 0:
                kd = left;
                break;

            case 1:
                kd = right;
                break;

            case 3:
                kd = back;
                break;

            case 4:
                kd = bottom;
                break;

            case 5:
                float lx = state.getPoint().x;
                float ly = state.getPoint().y;
                if (lx >= lxmin && lx < lxmax && ly >= lymin && ly < lymax && state.getRay().dz > 0)
                {
                    return(state.includeLights ? radiance : Color.BLACK);
                }
                kd = top;
                break;

            default:
                Debug.Assert(false);
                break;
            }
            // make sure we are on the right side of the material
            state.faceforward();
            // setup lighting
            state.initLightSamples();
            state.initCausticSamples();
            return(state.diffuse(kd));
        }
Exemple #23
0
        public void scatterPhoton(ShadingState state, Color power)
        {
            // make sure we are on the right side of the material
            state.faceforward();
            Color d = getDiffuse(state);

            state.storePhoton(state.getRay().getDirection(), power, d);
            float  avgD = d.getAverage();
            float  avgS = spec.getAverage();
            double rnd  = state.getRandom(0, 0, 1);

            if (rnd < avgD)
            {
                // photon is scattered diffusely
                power.mul(d).mul(1.0f / avgD);
                OrthoNormalBasis onb = state.getBasis();
                double           u   = 2 * Math.PI * rnd / avgD;
                double           v   = state.getRandom(0, 1, 1);
                float            s   = (float)Math.Sqrt(v);
                float            s1  = (float)Math.Sqrt(1.0f - v);
                Vector3          w   = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1);
                w = onb.transform(w, new Vector3());
                state.traceDiffusePhoton(new Ray(state.getPoint(), w), power);
            }
            else if (rnd < avgD + avgS)
            {
                // photon is scattered specularly
                float dn = 2.0f * state.getCosND();
                // reflected direction
                Vector3 refDir = new Vector3();
                refDir.x = (dn * state.getNormal().x) + state.getRay().dx;
                refDir.y = (dn * state.getNormal().y) + state.getRay().dy;
                refDir.z = (dn * state.getNormal().z) + state.getRay().dz;
                power.mul(spec).mul(1.0f / avgS);
                OrthoNormalBasis onb = state.getBasis();
                double           u   = 2 * Math.PI * (rnd - avgD) / avgS;
                double           v   = state.getRandom(0, 1, 1);
                float            s   = (float)Math.Pow(v, 1 / (this.power + 1));
                float            s1  = (float)Math.Sqrt(1 - s * s);
                Vector3          w   = new Vector3((float)Math.Cos(u) * s1, (float)Math.Sin(u) * s1, s);
                w = onb.transform(w, new Vector3());
                state.traceReflectionPhoton(new Ray(state.getPoint(), w), power);
            }
        }
Exemple #24
0
        public void getSamples(ShadingState state)
        {
            if (storedPhotons == 0)
            {
                return;
            }
            NearestPhotons np = new NearestPhotons(state.getPoint(), gatherNum, gatherRadius * gatherRadius);

            locatePhotons(np);
            if (np.found < 8)
            {
                return;
            }
            Point3  ppos     = new Point3();
            Vector3 pdir     = new Vector3();
            Vector3 pvec     = new Vector3();
            float   invArea  = 1.0f / ((float)Math.PI * np.dist2[0]);
            float   maxNDist = np.dist2[0] * 0.05f;
            float   f2r2     = 1.0f / (filterValue * filterValue * np.dist2[0]);
            float   fInv     = 1.0f / (1.0f - 2.0f / (3.0f * filterValue));

            for (int i = 1; i <= np.found; i++)
            {
                Photon phot = np.index[i];
                Vector3.decode(phot.dir, pdir);
                float cos = -Vector3.dot(pdir, state.getNormal());
                if (cos > 0.001)
                {
                    ppos.set(phot.x, phot.y, phot.z);
                    Point3.sub(ppos, state.getPoint(), pvec);
                    float pcos = Vector3.dot(pvec, state.getNormal());
                    if ((pcos < maxNDist) && (pcos > -maxNDist))
                    {
                        LightSample sample = new LightSample();
                        sample.setShadowRay(new Ray(state.getPoint(), pdir.negate()));
                        sample.setRadiance(new Color().setRGBE(np.index[i].power).mul(invArea / cos), Color.BLACK);
                        sample.getDiffuseRadiance().mul((1.0f - (float)Math.Sqrt(np.dist2[i] * f2r2)) * fInv);
                        state.addSample(sample);
                    }
                }
            }
        }
Exemple #25
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            state.getRay().getPoint(state.getPoint());
            Instance parent = state.getInstance();
            Point3   n      = state.transformWorldToObject(state.getPoint());

            state.getNormal().set(n.x * (2 * n.x * n.x - 1), n.y * (2 * n.y * n.y - 1), n.z * (2 * n.z * n.z - 1));
            state.getNormal().normalize();
            state.setShader(parent.getShader(0));
            state.setModifier(parent.getModifier(0));
            // into world space
            Vector3 worldNormal = state.transformNormalObjectToWorld(state.getNormal());

            state.getNormal().set(worldNormal);
            state.getNormal().normalize();
            state.getGeoNormal().set(state.getNormal());
            // create basis in world space
            state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
        }
Exemple #26
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            state.getRay().getPoint(state.getPoint());
            Instance parent     = state.getInstance();
            Point3   localPoint = state.transformWorldToObject(state.getPoint());

            float cx = state.getU();
            float cy = state.getV();
            float cz = state.getW();

            state.getNormal().set(localPoint.x - cx, localPoint.y - cy, localPoint.z - cz);
            state.getNormal().normalize();

            float phi = (float)Math.Atan2(state.getNormal().y, state.getNormal().x);

            if (phi < 0)
            {
                phi += (float)(2.0 * Math.PI);
            }
            float theta = (float)Math.Acos(state.getNormal().z);

            state.getUV().y = theta / (float)Math.PI;
            state.getUV().x = phi / (float)(2 * Math.PI);
            Vector3 v       = new Vector3();

            v.x = -2 * (float)Math.PI * state.getNormal().y;
            v.y = 2 * (float)Math.PI * state.getNormal().x;
            v.z = 0;
            state.setShader(parent.getShader(0));
            state.setModifier(parent.getModifier(0));
            // into world space
            Vector3 worldNormal = state.transformNormalObjectToWorld(state.getNormal());

            v = state.transformVectorObjectToWorld(v);
            state.getNormal().set(worldNormal);
            state.getNormal().normalize();
            state.getGeoNormal().set(state.getNormal());
            // compute basis in world space
            state.setBasis(OrthoNormalBasis.makeFromWV(state.getNormal(), v));
        }
 public Color getGlobalRadiance(ShadingState state)
 {
     if (globalPhotonMap == null)
     {
         if (state.getShader() != null)
             return state.getShader().getRadiance(state);
         else
             return Color.BLACK;
     }
     else
         return globalPhotonMap.getRadiance(state.getPoint(), state.getNormal());
 }
Exemple #28
0
        public void store(ShadingState state, Vector3 dir, Color power, Color diffuse)
        {
            Photon p = new Photon(state.getPoint(), state.getNormal(), dir, power, diffuse);

            lock (lockObj)
            {
                storedPhotons++;
                photonList.Add(p);
                bounds.include(new Point3(p.x, p.y, p.z));
                maxPower = Math.Max(maxPower, power.getMax());
            }
        }
Exemple #29
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            state.getRay().getPoint(state.getPoint());
            Point3 localPoint = state.transformWorldToObject(state.getPoint());

            localPoint.x -= particles[3 * state.getPrimitiveID() + 0];
            localPoint.y -= particles[3 * state.getPrimitiveID() + 1];
            localPoint.z -= particles[3 * state.getPrimitiveID() + 2];

            state.getNormal().set(localPoint.x, localPoint.y, localPoint.z);
            state.getNormal().normalize();

            state.setShader(state.getInstance().getShader(0));
            state.setModifier(state.getInstance().getModifier(0));
            // into object space
            Vector3 worldNormal = state.transformNormalObjectToWorld(state.getNormal());
            state.getNormal().set(worldNormal);
            state.getNormal().normalize();
            state.getGeoNormal().set(state.getNormal());
            state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
        }
Exemple #30
0
            public void store(ShadingState state, Vector3 dir, Color power, Color diffuse)
            {
                state.faceforward();
                PointLight vpl = new PointLight();

                vpl.p     = state.getPoint();
                vpl.n     = state.getNormal();
                vpl.power = power;
                lock (lockObj)
                {
                    virtualLights.Add(vpl);
                }
            }
        public void scatterPhoton(ShadingState state, Color power)
        {
            Color diffuse;

            // make sure we are on the right side of the material
            state.faceforward();
            diffuse = getDiffuse(state);
            state.storePhoton(state.getRay().getDirection(), power, diffuse);
            float  d   = diffuse.getAverage();
            float  r   = d * refl;
            double rnd = state.getRandom(0, 0, 1);

            if (rnd < d)
            {
                // photon is scattered
                power.mul(diffuse).mul(1.0f / d);
                OrthoNormalBasis onb = state.getBasis();
                double           u   = 2 * Math.PI * rnd / d;
                double           v   = state.getRandom(0, 1, 1);
                float            s   = (float)Math.Sqrt(v);
                float            s1  = (float)Math.Sqrt(1.0 - v);
                Vector3          w   = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1);
                w = onb.transform(w, new Vector3());
                state.traceDiffusePhoton(new Ray(state.getPoint(), w), power);
            }
            else if (rnd < d + r)
            {
                float cos = -Vector3.dot(state.getNormal(), state.getRay().getDirection());
                power.mul(diffuse).mul(1.0f / d);
                // photon is reflected
                float   dn  = 2 * cos;
                Vector3 dir = new Vector3();
                dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
                dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
                dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;
                state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power);
            }
        }
Exemple #32
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            state.getRay().getPoint(state.getPoint());
            Point3 localPoint = state.transformWorldToObject(state.getPoint());

            localPoint.x -= particles[3 * state.getPrimitiveID() + 0];
            localPoint.y -= particles[3 * state.getPrimitiveID() + 1];
            localPoint.z -= particles[3 * state.getPrimitiveID() + 2];

            state.getNormal().set(localPoint.x, localPoint.y, localPoint.z);
            state.getNormal().normalize();

            state.setShader(state.getInstance().getShader(0));
            state.setModifier(state.getInstance().getModifier(0));
            // into object space
            Vector3 worldNormal = state.transformNormalObjectToWorld(state.getNormal());

            state.getNormal().set(worldNormal);
            state.getNormal().normalize();
            state.getGeoNormal().set(state.getNormal());
            state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
        }
Exemple #33
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());
         }
     }
 }
Exemple #34
0
 public void getSamples(ShadingState state)
 {
     if (storedPhotons == 0)
         return;
     NearestPhotons np = new NearestPhotons(state.getPoint(), gatherNum, gatherRadius * gatherRadius);
     locatePhotons(np);
     if (np.found < 8)
         return;
     Point3 ppos = new Point3();
     Vector3 pdir = new Vector3();
     Vector3 pvec = new Vector3();
     float invArea = 1.0f / ((float)Math.PI * np.dist2[0]);
     float maxNDist = np.dist2[0] * 0.05f;
     float f2r2 = 1.0f / (filterValue * filterValue * np.dist2[0]);
     float fInv = 1.0f / (1.0f - 2.0f / (3.0f * filterValue));
     for (int i = 1; i <= np.found; i++)
     {
         Photon phot = np.index[i];
         Vector3.decode(phot.dir, pdir);
         float cos = -Vector3.dot(pdir, state.getNormal());
         if (cos > 0.001)
         {
             ppos.set(phot.x, phot.y, phot.z);
             Point3.sub(ppos, state.getPoint(), pvec);
             float pcos = Vector3.dot(pvec, state.getNormal());
             if ((pcos < maxNDist) && (pcos > -maxNDist))
             {
                 LightSample sample = new LightSample();
                 sample.setShadowRay(new Ray(state.getPoint(), pdir.negate()));
                 sample.setRadiance(new Color().setRGBE(np.index[i].power).mul(invArea / cos), Color.BLACK);
                 sample.getDiffuseRadiance().mul((1.0f - (float)Math.Sqrt(np.dist2[i] * f2r2)) * fInv);
                 state.addSample(sample);
             }
         }
     }
 }
Exemple #35
0
 public void ScatterPhoton(ShadingState state, Color power)
 {
     float avg = color.getAverage();
     double rnd = state.getRandom(0, 0, 1);
     if (rnd >= avg)
         return;
     state.faceforward();
     float cos = state.getCosND();
     power.mul(color).mul(1.0f / avg);
     // photon is reflected
     float dn = 2 * cos;
     Vector3 dir = new Vector3();
     dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
     dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
     dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;
     state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power);
 }
Exemple #36
0
 public Color getGlobalRadiance(ShadingState state)
 {
     if (globalPhotonMap == null)
     {
         if (state.getShader() != null)
         {
             return(state.getShader().GetRadiance(state));
         }
         else
         {
             return(Color.BLACK);
         }
     }
     else
     {
         return(globalPhotonMap.getRadiance(state.getPoint(), state.getNormal()));
     }
 }
Exemple #37
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            state.getRay().getPoint(state.getPoint());
            int n = state.getPrimitiveID();

            /*
             *
             *
             * switch (n)
             * {
             *  case 0:
             *      state.getNormal().set(new Vector3(1, 0, 0));
             *      break;
             *  case 1:
             *      state.getNormal().set(new Vector3(-1, 0, 0));
             *      break;
             *  case 2:
             *      state.getNormal().set(new Vector3(0, 1, 0));
             *      break;
             *  case 3:
             *      state.getNormal().set(new Vector3(0, -1, 0));
             *      break;
             *  case 4:
             *      state.getNormal().set(new Vector3(0, 0, 1));
             *      break;
             *  case 5:
             *      state.getNormal().set(new Vector3(0, 0, -1));
             *      break;
             *  default:
             *      state.getNormal().set(new Vector3(0, 0, 0));
             *      break;
             * }
             *
             */

            state.getNormal().set(normalVectors[n]);

            state.getGeoNormal().set(state.getNormal());
            state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
            state.setShader(state.getInstance().getShader(0));
            state.setModifier(state.getInstance().getModifier(0));
        }
Exemple #38
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            state.getRay().getPoint(state.getPoint());
            Instance parent = state.getInstance();
            Vector3  normal;

            switch (state.getPrimitiveID())
            {
            case 0:
                normal = new Vector3(-1, 0, 0);
                break;

            case 1:
                normal = new Vector3(1, 0, 0);
                break;

            case 2:
                normal = new Vector3(0, -1, 0);
                break;

            case 3:
                normal = new Vector3(0, 1, 0);
                break;

            case 4:
                normal = new Vector3(0, 0, -1);
                break;

            case 5:
                normal = new Vector3(0, 0, 1);
                break;

            default:
                normal = new Vector3(0, 0, 0);
                break;
            }
            state.getNormal().set(state.transformNormalObjectToWorld(normal));
            state.getGeoNormal().set(state.getNormal());
            state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
            state.setShader(parent.getShader(0));
            state.setModifier(parent.getModifier(0));
        }
Exemple #39
0
 public void scatterPhoton(ShadingState state, Color power)
 {
     // make sure we are on the right side of the material
     state.faceforward();
     Color d = getDiffuse(state);
     state.storePhoton(state.getRay().getDirection(), power, d);
     float avgD = d.getAverage();
     float avgS = spec.getAverage();
     double rnd = state.getRandom(0, 0, 1);
     if (rnd < avgD)
     {
         // photon is scattered diffusely
         power.mul(d).mul(1.0f / avgD);
         OrthoNormalBasis onb = state.getBasis();
         double u = 2 * Math.PI * rnd / avgD;
         double v = state.getRandom(0, 1, 1);
         float s = (float)Math.Sqrt(v);
         float s1 = (float)Math.Sqrt(1.0f - v);
         Vector3 w = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1);
         w = onb.transform(w, new Vector3());
         state.traceDiffusePhoton(new Ray(state.getPoint(), w), power);
     }
     else if (rnd < avgD + avgS)
     {
         // photon is scattered specularly
         float dn = 2.0f * state.getCosND();
         // reflected direction
         Vector3 refDir = new Vector3();
         refDir.x = (dn * state.getNormal().x) + state.getRay().dx;
         refDir.y = (dn * state.getNormal().y) + state.getRay().dy;
         refDir.z = (dn * state.getNormal().z) + state.getRay().dz;
         power.mul(spec).mul(1.0f / avgS);
         OrthoNormalBasis onb = state.getBasis();
         double u = 2 * Math.PI * (rnd - avgD) / avgS;
         double v = state.getRandom(0, 1, 1);
         float s = (float)Math.Pow(v, 1 / (this.power + 1));
         float s1 = (float)Math.Sqrt(1 - s * s);
         Vector3 w = new Vector3((float)Math.Cos(u) * s1, (float)Math.Sin(u) * s1, s);
         w = onb.transform(w, new Vector3());
         state.traceReflectionPhoton(new Ray(state.getPoint(), w), power);
     }
 }
Exemple #40
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            state.getRay().getPoint(state.getPoint());
            int n = state.getPrimitiveID();

            switch (n)
            {
            case 0:
                state.getNormal().set(new Vector3(1, 0, 0));
                break;

            case 1:
                state.getNormal().set(new Vector3(-1, 0, 0));
                break;

            case 2:
                state.getNormal().set(new Vector3(0, 1, 0));
                break;

            case 3:
                state.getNormal().set(new Vector3(0, -1, 0));
                break;

            case 4:
                state.getNormal().set(new Vector3(0, 0, 1));
                break;

            case 5:
                state.getNormal().set(new Vector3(0, 0, -1));
                break;

            default:
                state.getNormal().set(new Vector3(0, 0, 0));
                break;
            }
            state.getGeoNormal().set(state.getNormal());
            state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
            state.setShader(state.getInstance().getShader(0));
            state.setModifier(state.getInstance().getModifier(0));
        }
Exemple #41
0
        public void modify(ShadingState state)
        {
            Point3 p = state.transformWorldToObject(state.getPoint());
            p.x *= size;
            p.y *= size;
            p.z *= size;
            Vector3 normal = state.transformNormalWorldToObject(state.getNormal());
            double f0 = f(p.x, p.y, p.z);
            double fx = f(p.x + .0001, p.y, p.z);
            double fy = f(p.x, p.y + .0001, p.z);
            double fz = f(p.x, p.y, p.z + .0001);

            normal.x -= scale * (float)((fx - f0) / .0001);
            normal.y -= scale * (float)((fy - f0) / .0001);
            normal.z -= scale * (float)((fz - f0) / .0001);
            normal.normalize();

            state.getNormal().set(state.transformNormalObjectToWorld(normal));
            state.getNormal().normalize();
            state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
        }
Exemple #42
0
        public Color GetRadiance(ShadingState state)
        {
            Point3[] p = new Point3[3];
            if (!state.getTrianglePoints(p))
            {
                return(getFillColor(state));
            }
            // transform points into camera space
            Point3  center = state.getPoint();
            Matrix4 w2c    = state.getWorldToCamera();

            center = w2c.transformP(center);
            for (int i = 0; i < 3; i++)
            {
                p[i] = w2c.transformP(state.transformObjectToWorld(p[i]));
            }
            float cn = 1.0f / (float)Math.Sqrt(center.x * center.x + center.y * center.y + center.z * center.z);

            for (int i = 0, i2 = 2; i < 3; i2 = i, i++)
            {
                // compute orthogonal projection of the shading point onto each
                // triangle edge as in:
                // http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
                float t = (center.x - p[i].x) * (p[i2].x - p[i].x);
                t += (center.y - p[i].y) * (p[i2].y - p[i].y);
                t += (center.z - p[i].z) * (p[i2].z - p[i].z);
                t /= p[i].distanceToSquared(p[i2]);
                float projx = (1 - t) * p[i].x + t * p[i2].x;
                float projy = (1 - t) * p[i].y + t * p[i2].y;
                float projz = (1 - t) * p[i].z + t * p[i2].z;
                float n     = 1.0f / (float)Math.Sqrt(projx * projx + projy * projy + projz * projz);
                // check angular width
                float dot = projx * center.x + projy * center.y + projz * center.z;
                if (dot * n * cn >= cosWidth)
                {
                    return(getLineColor(state));
                }
            }
            return(getFillColor(state));
        }
Exemple #43
0
        public Color GetRadiance(ShadingState state)
        {
            // make sure we are on the right side of the material
            state.faceforward();
            // direct lighting
            state.initLightSamples();
            state.initCausticSamples();
            Color d  = getDiffuse(state);
            Color lr = state.diffuse(d);

            if (!state.includeSpecular)
            {
                return(lr);
            }
            if (glossyness == 0)
            {
                float   cos    = state.getCosND();
                float   dn     = 2 * cos;
                Vector3 refDir = new Vector3();
                refDir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
                refDir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
                refDir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;
                Ray refRay = new Ray(state.getPoint(), refDir);
                // compute Fresnel term
                cos = 1 - cos;
                float cos2 = cos * cos;
                float cos5 = cos2 * cos2 * cos;

                Color spec = getSpecular(state);
                Color ret  = Color.white();
                ret.sub(spec);
                ret.mul(cos5);
                ret.add(spec);
                return(lr.add(ret.mul(state.traceReflection(refRay, 0))));
            }
            else
            {
                return(lr.add(state.specularPhong(getSpecular(state), 2 / glossyness, numSamples)));
            }
        }
Exemple #44
0
        public void prepareShadingState(ShadingState state)
        {
            state.init();
            state.getRay().getPoint(state.getPoint());
            Instance parent = state.getInstance();
            float    u      = state.getU();
            float    v      = state.getV();

            float[] bu  = bernstein(u);
            float[] bdu = bernsteinDeriv(u);
            float[] bv  = bernstein(v);
            float[] bdv = bernsteinDeriv(v);
            getPatchPoint(u, v, patches[state.getPrimitiveID()], bu, bv, bdu, bdv, new Point3(), state.getNormal());
            state.getNormal().set(parent.transformNormalObjectToWorld(state.getNormal()));
            state.getNormal().normalize();
            state.getGeoNormal().set(state.getNormal());
            state.getUV().set(u, v);
            state.setShader(parent.getShader(0));
            state.setModifier(parent.getModifier(0));
            // FIXME: use actual derivatives to create basis
            state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
        }
Exemple #45
0
        public void scatterPhoton(ShadingState state, Color power)
        {
            float  avg = color.getAverage();
            double rnd = state.getRandom(0, 0, 1);

            if (rnd >= avg)
            {
                return;
            }
            state.faceforward();
            float cos = state.getCosND();

            power.mul(color).mul(1.0f / avg);
            // photon is reflected
            float   dn  = 2 * cos;
            Vector3 dir = new Vector3();

            dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
            dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
            dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;
            state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power);
        }
Exemple #46
0
 public Color getGlobalRadiance(ShadingState state)
 {
     Point3 p = state.getPoint();
     Vector3 n = state.getNormal();
     int set = (int)(state.getRandom(0, 1, 1) * numSets);
     float maxAvgPow = 0;
     float minDist = 1;
     Color pow = null;
     foreach (PointLight vpl in virtualLights[set])
     {
         maxAvgPow = Math.Max(maxAvgPow, vpl.power.getAverage());
         if (Vector3.dot(n, vpl.n) > 0.9f)
         {
             float d = vpl.p.distanceToSquared(p);
             if (d < minDist)
             {
                 pow = vpl.power;
                 minDist = d;
             }
         }
     }
     return pow == null ? Color.BLACK : pow.copy().mul(1.0f / maxAvgPow);
 }
Exemple #47
0
        public Color GetRadiance(ShadingState state)
        {
            if (!state.includeSpecular)
                return Color.BLACK;
            state.faceforward();
            float cos = state.getCosND();
            float dn = 2 * cos;
            Vector3 refDir = new Vector3();
            refDir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
            refDir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
            refDir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;
            Ray refRay = new Ray(state.getPoint(), refDir);

            // compute Fresnel term
            cos = 1 - cos;
            float cos2 = cos * cos;
            float cos5 = cos2 * cos2 * cos;
            Color ret = Color.white();
            ret.sub(color);
            ret.mul(cos5);
            ret.add(color);
            return ret.mul(state.traceReflection(refRay, 0));
        }
 public Color getIrradiance(ShadingState state, Color diffuseReflectance)
 {
     OrthoNormalBasis onb = state.getBasis();
     Vector3 w = new Vector3();
     Color result = Color.black();
     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)(2 * Math.PI * xi);
         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(maxDist);
         result.add(Color.blend(bright, dark, state.traceShadow(r)));
     }
     return result.mul((float)Math.PI / samples);
 }
Exemple #49
0
 public void ScatterPhoton(ShadingState state, Color power)
 {
     Color diffuse, specular;
     // make sure we are on the right side of the material
     state.faceforward();
     diffuse = getDiffuse(state);
     specular = getSpecular(state);
     state.storePhoton(state.getRay().getDirection(), power, diffuse);
     float d = diffuse.getAverage();
     float r = specular.getAverage();
     double rnd = state.getRandom(0, 0, 1);
     if (rnd < d)
     {
         // photon is scattered
         power.mul(diffuse).mul(1.0f / d);
         OrthoNormalBasis onb = state.getBasis();
         double u = 2 * Math.PI * rnd / d;
         double v = state.getRandom(0, 1, 1);
         float s = (float)Math.Sqrt(v);
         float s1 = (float)Math.Sqrt(1.0 - v);
         Vector3 w = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1);
         w = onb.transform(w, new Vector3());
         state.traceDiffusePhoton(new Ray(state.getPoint(), w), power);
     }
     else if (rnd < d + r)
     {
         if (glossyness == 0)
         {
             float cos = -Vector3.dot(state.getNormal(), state.getRay().getDirection());
             power.mul(diffuse).mul(1.0f / d);
             // photon is reflected
             float dn = 2 * cos;
             Vector3 dir = new Vector3();
             dir.x = (dn * state.getNormal().x) + state.getRay().getDirection().x;
             dir.y = (dn * state.getNormal().y) + state.getRay().getDirection().y;
             dir.z = (dn * state.getNormal().z) + state.getRay().getDirection().z;
             state.traceReflectionPhoton(new Ray(state.getPoint(), dir), power);
         }
         else
         {
             float dn = 2.0f * state.getCosND();
             // reflected direction
             Vector3 refDir = new Vector3();
             refDir.x = (dn * state.getNormal().x) + state.getRay().dx;
             refDir.y = (dn * state.getNormal().y) + state.getRay().dy;
             refDir.z = (dn * state.getNormal().z) + state.getRay().dz;
             power.mul(spec).mul(1.0f / r);
             OrthoNormalBasis onb = state.getBasis();
             double u = 2 * Math.PI * (rnd - r) / r;
             double v = state.getRandom(0, 1, 1);
             float s = (float)Math.Pow(v, 1 / ((1.0f / glossyness) + 1));
             float s1 = (float)Math.Sqrt(1 - s * s);
             Vector3 w = new Vector3((float)Math.Cos(u) * s1, (float)Math.Sin(u) * s1, s);
             w = onb.transform(w, new Vector3());
             state.traceReflectionPhoton(new Ray(state.getPoint(), w), power);
         }
     }
 }
 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;
 }
            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);
                    }
                }
            }
Exemple #52
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());
         }
     }
 }
Exemple #53
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);
                    }
                }
            }
        }
Exemple #54
0
 public void prepareShadingState(ShadingState state)
 {
     state.init();
     state.getRay().getPoint(state.getPoint());
     int n = state.getPrimitiveID();
     switch (n)
     {
         case 0:
             state.getNormal().set(new Vector3(1, 0, 0));
             break;
         case 1:
             state.getNormal().set(new Vector3(-1, 0, 0));
             break;
         case 2:
             state.getNormal().set(new Vector3(0, 1, 0));
             break;
         case 3:
             state.getNormal().set(new Vector3(0, -1, 0));
             break;
         case 4:
             state.getNormal().set(new Vector3(0, 0, 1));
             break;
         case 5:
             state.getNormal().set(new Vector3(0, 0, -1));
             break;
         default:
             state.getNormal().set(new Vector3(0, 0, 0));
             break;
     }
     state.getGeoNormal().set(state.getNormal());
     state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
     state.setShader(state.getInstance().getShader(0));
     state.setModifier(state.getInstance().getModifier(0));
 }
Exemple #55
0
 public void prepareShadingState(ShadingState state)
 {
     state.init();
     state.getRay().getPoint(state.getPoint());
     Instance parent = state.getInstance();
     Vector3 normal;
     switch (state.getPrimitiveID())
     {
         case 0:
             normal = new Vector3(-1, 0, 0);
             break;
         case 1:
             normal = new Vector3(1, 0, 0);
             break;
         case 2:
             normal = new Vector3(0, -1, 0);
             break;
         case 3:
             normal = new Vector3(0, 1, 0);
             break;
         case 4:
             normal = new Vector3(0, 0, -1);
             break;
         case 5:
             normal = new Vector3(0, 0, 1);
             break;
         default:
             normal = new Vector3(0, 0, 0);
             break;
     }
     state.getNormal().set(state.transformNormalObjectToWorld(normal));
     state.getGeoNormal().set(state.getNormal());
     state.setBasis(OrthoNormalBasis.makeFromW(state.getNormal()));
     state.setShader(parent.getShader(0));
     state.setModifier(parent.getModifier(0));
 }
        public void scatterPhoton(ShadingState state, Color power)
        {
            // make sure we are on the right side of the material
            state.faceforward();
            Color d = getDiffuse(state);
            state.storePhoton(state.getRay().getDirection(), power, d);
            float avgD = d.getAverage();
            float avgS = rhoS.getAverage();
            double rnd = state.getRandom(0, 0, 1);
            if (rnd < avgD)
            {
                // photon is scattered diffusely
                power.mul(d).mul(1.0f / avgD);
                OrthoNormalBasis onb = state.getBasis();
                double u = 2 * Math.PI * rnd / avgD;
                double v = state.getRandom(0, 1, 1);
                float s = (float)Math.Sqrt(v);
                float s1 = (float)Math.Sqrt(1.0f - v);
                Vector3 w = new Vector3((float)Math.Cos(u) * s, (float)Math.Sin(u) * s, s1);
                w = onb.transform(w, new Vector3());
                state.traceDiffusePhoton(new Ray(state.getPoint(), w), power);
            }
            else if (rnd < avgD + avgS)
            {
                // photon is scattered specularly
                power.mul(rhoS).mul(1 / avgS);
                OrthoNormalBasis basis = state.getBasis();
                Vector3 inv = state.getRay().getDirection().negate(new Vector3());
                double r1 = rnd / avgS;
                double r2 = state.getRandom(0, 1, 1);

                float alphaRatio = alphaY / alphaX;
                float phi = 0;
                if (r1 < 0.25)
                {
                    double val = 4 * r1;
                    phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val));
                }
                else if (r1 < 0.5)
                {
                    double val = 1 - 4 * (0.5 - r1);
                    phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val));
                    phi = (float)Math.PI - phi;
                }
                else if (r1 < 0.75)
                {
                    double val = 4 * (r1 - 0.5);
                    phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val));
                    phi += (float)Math.PI;
                }
                else
                {
                    double val = 1 - 4 * (1 - r1);
                    phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val));
                    phi = 2 * (float)Math.PI - phi;
                }

                float cosPhi = (float)Math.Cos(phi);
                float sinPhi = (float)Math.Sin(phi);

                float denom = (cosPhi * cosPhi) / (alphaX * alphaX) + (sinPhi * sinPhi) / (alphaY * alphaY);
                float theta = (float)Math.Atan(Math.Sqrt(-Math.Log(1 - r2) / denom));

                float sinTheta = (float)Math.Sin(theta);
                float cosTheta = (float)Math.Cos(theta);

                Vector3 h = new Vector3();
                h.x = sinTheta * cosPhi;
                h.y = sinTheta * sinPhi;
                h.z = cosTheta;
                basis.transform(h);

                Vector3 o = new Vector3();
                float ih = Vector3.dot(h, inv);
                o.x = 2 * ih * h.x - inv.x;
                o.y = 2 * ih * h.y - inv.y;
                o.z = 2 * ih * h.z - inv.z;

                Ray r = new Ray(state.getPoint(), o);
                state.traceReflectionPhoton(r, power);
            }
        }
Exemple #57
0
 public void store(ShadingState state, Vector3 dir, Color power, Color diffuse)
 {
     // don't store on the wrong side of a surface
     if (Vector3.dot(state.getNormal(), dir) > 0)
         return;
     Point3 pt = state.getPoint();
     // outside grid bounds ?
     if (!bounds.contains(pt))
         return;
     Vector3 ext = bounds.getExtents();
     int ix = (int)(((pt.x - bounds.getMinimum().x) * nx) / ext.x);
     int iy = (int)(((pt.y - bounds.getMinimum().y) * ny) / ext.y);
     int iz = (int)(((pt.z - bounds.getMinimum().z) * nz) / ext.z);
     ix = MathUtils.clamp(ix, 0, nx - 1);
     iy = MathUtils.clamp(iy, 0, ny - 1);
     iz = MathUtils.clamp(iz, 0, nz - 1);
     int id = ix + iy * nx + iz * nx * ny;
     lock (lockObj)
     {
         int hid = id % cellHash.Length;
         PhotonGroup g = cellHash[hid];
         PhotonGroup last = null;
         bool hasID = false;
         while (g != null)
         {
             if (g.id == id)
             {
                 hasID = true;
                 if (Vector3.dot(state.getNormal(), g.normal) > NORMAL_THRESHOLD)
                     break;
             }
             last = g;
             g = g.next;
         }
         if (g == null)
         {
             g = new PhotonGroup(id, state.getNormal());
             if (last == null)
                 cellHash[hid] = g;
             else
                 last.next = g;
             if (!hasID)
             {
                 hashSize++; // we have not seen this ID before
                 // resize hash if we have grown too large
                 if (hashSize > cellHash.Length)
                     growPhotonHash();
             }
         }
         g.count++;
         g.flux.add(power);
         g.diffuse.add(diffuse);
         numStoredPhotons++;
     }
 }
Exemple #58
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);
            }
        }
Exemple #59
0
 public void store(ShadingState state, Vector3 dir, Color power, Color diffuse)
 {
     Photon p = new Photon(state.getPoint(), state.getNormal(), dir, power, diffuse);
     lock (lockObj)
     {
         storedPhotons++;
         photonList.Add(p);
         bounds.include(new Point3(p.x, p.y, p.z));
         maxPower = Math.Max(maxPower, power.getMax());
     }
 }
        public Color getRadiance(ShadingState state)
        {
            // make sure we are on the right side of the material
            state.faceforward();
            OrthoNormalBasis onb = state.getBasis();
            // direct lighting and caustics
            state.initLightSamples();
            state.initCausticSamples();
            Color lr = Color.black();
            // compute specular contribution
            if (state.includeSpecular)
            {
                Vector3 inv = state.getRay().getDirection().negate(new Vector3());
                foreach (LightSample sample in state)
                {
                    float cosNL = sample.dot(state.getNormal());
                    float fr = brdf(inv, sample.getShadowRay().getDirection(), onb);
                    lr.madd(cosNL * fr, sample.getSpecularRadiance());
                }

                // indirect lighting - specular
                if (numRays > 0)
                {
                    int n = state.getDepth() == 0 ? numRays : 1;
                    for (int i = 0; i < n; i++)
                    {
                        // specular indirect lighting
                        double r1 = state.getRandom(i, 0, n);
                        double r2 = state.getRandom(i, 1, n);

                        float alphaRatio = alphaY / alphaX;
                        float phi = 0;
                        if (r1 < 0.25)
                        {
                            double val = 4 * r1;
                            phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val));
                        }
                        else if (r1 < 0.5)
                        {
                            double val = 1 - 4 * (0.5 - r1);
                            phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val));
                            phi = (float)Math.PI - phi;
                        }
                        else if (r1 < 0.75)
                        {
                            double val = 4 * (r1 - 0.5);
                            phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val));
                            phi += (float)Math.PI;
                        }
                        else
                        {
                            double val = 1 - 4 * (1 - r1);
                            phi = (float)Math.Atan(alphaRatio * Math.Tan(Math.PI / 2 * val));
                            phi = 2 * (float)Math.PI - phi;
                        }

                        float cosPhi = (float)Math.Cos(phi);
                        float sinPhi = (float)Math.Sin(phi);

                        float denom = (cosPhi * cosPhi) / (alphaX * alphaX) + (sinPhi * sinPhi) / (alphaY * alphaY);
                        float theta = (float)Math.Atan(Math.Sqrt(-Math.Log(1 - r2) / denom));

                        float sinTheta = (float)Math.Sin(theta);
                        float cosTheta = (float)Math.Cos(theta);

                        Vector3 h = new Vector3();
                        h.x = sinTheta * cosPhi;
                        h.y = sinTheta * sinPhi;
                        h.z = cosTheta;
                        onb.transform(h);

                        Vector3 o = new Vector3();
                        float ih = Vector3.dot(h, inv);
                        o.x = 2 * ih * h.x - inv.x;
                        o.y = 2 * ih * h.y - inv.y;
                        o.z = 2 * ih * h.z - inv.z;

                        float no = onb.untransformZ(o);
                        float ni = onb.untransformZ(inv);
                        float w = ih * cosTheta * cosTheta * cosTheta * (float)Math.Sqrt(Math.Abs(no / ni));

                        Ray r = new Ray(state.getPoint(), o);
                        lr.madd(w / n, state.traceGlossy(r, i));
                    }
                }
                lr.mul(rhoS);
            }
            // add diffuse contribution
            lr.add(state.diffuse(getDiffuse(state)));
            return lr;
        }