private static mat4 GetProjectionMatrix(this TSpotLight light)
        {
            const double angle       = 90.0 / 180.0 * Math.PI; // in radians
            const float  aspectRatio = 1.0f;

            // TODO: how to get a precise projection?
            mat4 projection = glm.perspective((float)angle, aspectRatio, 0.1f, 50);

            return(projection);
        }
Esempio n. 2
0
        /// <summary>
        /// Cast shadow.(Prepare shadow mapping texture)
        /// </summary>
        /// <param name="param"></param>
        public override void Act(ActionParams param)
        {
            Scene scene = this.scene;

            // Render ambient color.
            {
                var arg = new ShadowMappingAmbientEventArgs(param, scene.Camera, scene.AmbientColor);
                RenderAmbientColor(scene.RootNode, arg);
            }

            foreach (var light in scene.Lights)
            {
                if (light is SpotLight || light is DirectionalLight)
                {
                    LightTheScene(light, scene, param);
                }
                else if (light is PointLight)
                {
                    var xLight = new TSpotLight(light.Position, TSpotLightDirection.X, light.Attenuation)
                    {
                        Diffuse = light.Diffuse, Specular = light.Specular,
                    };
                    var nxLight = new TSpotLight(light.Position, TSpotLightDirection.NX, light.Attenuation)
                    {
                        Diffuse = light.Diffuse, Specular = light.Specular,
                    };
                    var yLight = new TSpotLight(light.Position, TSpotLightDirection.Y, light.Attenuation)
                    {
                        Diffuse = light.Diffuse, Specular = light.Specular,
                    };
                    var nyLight = new TSpotLight(light.Position, TSpotLightDirection.NY, light.Attenuation)
                    {
                        Diffuse = light.Diffuse, Specular = light.Specular,
                    };
                    var zLight = new TSpotLight(light.Position, TSpotLightDirection.Z, light.Attenuation)
                    {
                        Diffuse = light.Diffuse, Specular = light.Specular,
                    };
                    var nzLight = new TSpotLight(light.Position, TSpotLightDirection.NZ, light.Attenuation)
                    {
                        Diffuse = light.Diffuse, Specular = light.Specular,
                    };
                    var lights = new TSpotLight[] { xLight, nxLight, yLight, nyLight, zLight, nzLight };
                    foreach (var item in lights)
                    {
                        LightTheScene(item, scene, param);
                    }
                }
                else
                {
                    throw new Exception(string.Format("Unexpected light type:{0}", light.GetType().FullName));
                }
            }
        }
        private static void SetUniforms(this TSpotLight light, ShaderProgram program)
        {
            program.SetUniform("light.position", light.Position);
            program.SetUniform("light.diffuse", light.Diffuse);
            program.SetUniform("light.specular", light.Specular);
            program.SetUniform("light.constant", light.Attenuation.Constant);
            program.SetUniform("light.linear", light.Attenuation.Linear);
            program.SetUniform("light.quadratic", light.Attenuation.Exp);
            program.SetUniform("light.direction", light.Direction.ToVec3());
            //program.SetUniform("light.cutOff", light.CutOff);

            int lightUpRoutine = (int)light.Direction;

            program.SetUniform("lightUpRoutine", lightUpRoutine);
        }
        private static mat4 GetViewMatrix(this TSpotLight light)
        {
            vec3 direction, up;

            switch (light.Direction)
            {
            case TSpotLightDirection.X:
                direction = new vec3(-1, 0, 0);
                up        = new vec3(0, 1, 0);
                break;

            case TSpotLightDirection.NX:
                direction = new vec3(1, 0, 0);
                up        = new vec3(0, 1, 0);
                break;

            case TSpotLightDirection.Y:
                direction = new vec3(0, -1, 0);
                up        = new vec3(0, 0, 1);
                break;

            case TSpotLightDirection.NY:
                direction = new vec3(0, 1, 0);
                up        = new vec3(0, 0, 1);
                break;

            case TSpotLightDirection.Z:
                direction = new vec3(0, 0, -1);
                up        = new vec3(0, 1, 0);
                break;

            case TSpotLightDirection.NZ:
                direction = new vec3(0, 0, 1);
                up        = new vec3(0, 1, 0);
                break;

            default:
                throw new NotDealWithNewEnumItemException(typeof(TSpotLightDirection));
            }

            mat4 view = glm.lookAt(light.Position, light.Position - direction, up);

            return(view);
        }