Beispiel #1
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;
        }
Beispiel #2
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);
        }