예제 #1
0
        /// <summary>
        /// The Phong Shader uses the Phong Reflection model to generate colors.
        /// https://en.wikipedia.org/wiki/Phong_reflection_model
        /// </summary>
        /// <param name="Material">Surface material.</param>
        /// <param name="Ambient">Ambient intensity.</param>
        /// <param name="LightSources">Light sources.</param>
        public PhongShader(PhongMaterial Material, PhongIntensity Ambient,
                           params PhongLightSource[] LightSources)
        {
            this.ambientReflectionConstantFront  = Material.AmbientReflectionConstantFront;
            this.diffuseReflectionConstantFront  = Material.DiffuseReflectionConstantFront;
            this.specularReflectionConstantFront = Material.SpecularReflectionConstantFront;
            this.shininessFront = Material.ShininessFront;
            this.ambientReflectionConstantBack  = Material.AmbientReflectionConstantBack;
            this.diffuseReflectionConstantBack  = -Material.DiffuseReflectionConstantBack;
            this.specularReflectionConstantBack = Material.SpecularReflectionConstantBack;
            this.shininessBack = Material.ShininessBack;
            this.distance      = 0;

            this.sources   = LightSources;
            this.nrSources = LightSources.Length;
            if (this.nrSources == 1)
            {
                this.source         = LightSources[0];
                this.sourcePosition = this.source.TransformedPosition;

                PhongIntensity I = this.source.Diffuse;

                this.sourceDiffuseRed   = I.Red;
                this.sourceDiffuseGreen = I.Green;
                this.sourceDiffuseBlue  = I.Blue;
                this.sourceDiffuseAlpha = I.Alpha;

                I = this.source.Specular;

                this.sourceSpecularRed   = I.Red;
                this.sourceSpecularGreen = I.Green;
                this.sourceSpecularBlue  = I.Blue;
                this.sourceSpecularAlpha = I.Alpha;
            }

            this.ambientRedFront   = this.ambientReflectionConstantFront * Ambient.Red;
            this.ambientGreenFront = this.ambientReflectionConstantFront * Ambient.Green;
            this.ambientBlueFront  = this.ambientReflectionConstantFront * Ambient.Blue;
            this.ambientAlphaFront = this.ambientReflectionConstantFront * Ambient.Alpha;
            this.ambientRedBack    = this.ambientReflectionConstantBack * Ambient.Red;
            this.ambientGreenBack  = this.ambientReflectionConstantBack * Ambient.Green;
            this.ambientBlueBack   = this.ambientReflectionConstantBack * Ambient.Blue;
            this.ambientAlphaBack  = this.ambientReflectionConstantBack * Ambient.Alpha;
        }
예제 #2
0
        /// <summary>
        /// The Phong Shader uses the Phong Reflection model to generate colors.
        /// https://en.wikipedia.org/wiki/Phong_reflection_model
        /// </summary>
        /// <param name="Material">Surface material.</param>
        /// <param name="Ambient">Ambient intensity.</param>
        /// <param name="LightSources">Light sources.</param>
        public PhongShader(PhongMaterial Material, PhongIntensity Ambient,
                           params PhongLightSource[] LightSources)
        {
            this.material = Material;
            this.ambient  = Ambient;
            this.sources  = LightSources;

            this.ambientReflectionConstant     = Material.AmbientReflectionConstant;
            this.diffuseReflectionConstant     = Material.DiffuseReflectionConstant;
            this.specularReflectionConstant    = Material.SpecularReflectionConstant;
            this.hasSpecularReflectionConstant = this.specularReflectionConstant != 0;
            this.shininess = Material.Shininess;

            this.nrSources    = LightSources.Length;
            this.singleSource = this.nrSources == 1;

            if (this.singleSource)
            {
                this.source         = LightSources[0];
                this.sourcePosition = this.source.Position;

                PhongIntensity I = this.source.Diffuse;

                this.sourceDiffuseRed   = I.Red;
                this.sourceDiffuseGreen = I.Green;
                this.sourceDiffuseBlue  = I.Blue;
                this.sourceDiffuseAlpha = I.Alpha;

                I = this.source.Specular;

                this.sourceSpecularRed   = I.Red;
                this.sourceSpecularGreen = I.Green;
                this.sourceSpecularBlue  = I.Blue;
                this.sourceSpecularAlpha = I.Alpha;
            }

            this.ambientRed   = this.ambientReflectionConstant * Ambient.Red;
            this.ambientGreen = this.ambientReflectionConstant * Ambient.Green;
            this.ambientBlue  = this.ambientReflectionConstant * Ambient.Blue;
            this.ambientAlpha = this.ambientReflectionConstant * Ambient.Alpha;

            if (this.ambientAlpha < 255 ||
                this.sourceDiffuseAlpha < 255 ||
                this.sourceSpecularAlpha < 255)
            {
                this.opaque = false;
            }
            else
            {
                this.opaque = true;

                foreach (PhongLightSource Source in this.sources)
                {
                    if (!Source.Opaque)
                    {
                        this.opaque = false;
                        break;
                    }
                }
            }
        }