Exemplo n.º 1
0
        /* More or less universal functions... */
        public static void Initialize(GLHelper glHelper)
        {
            Rendering.glHelper = glHelper;

            glHelper.Textures.AddTexture(EmptyTextureName, Properties.Resources.Empty);
            glHelper.Shaders.AddProgramWithShaders(DefaultShaderName,
                                                   File.ReadAllText("Data\\Default.vert"),
                                                   File.ReadAllText("Data\\Default.frag"));

            glHelper.Shaders.AddProgramWithShaders(LightMarkerShaderName,
                                                   File.ReadAllText("Data\\LightMarker.vert"),
                                                   File.ReadAllText("Data\\LightMarker.frag"),
                                                   File.ReadAllText("Data\\LightMarker.geom"));

            glHelper.Shaders.AddProgramWithShaders(LightLineShaderName,
                                                   File.ReadAllText("Data\\LightMarker.vert"),
                                                   File.ReadAllText("Data\\LightMarker.frag"),
                                                   File.ReadAllText("Data\\LightLine.geom"));

            glHelper.Buffers.AddVertices(AxisMarkerObjectName, new GLVertex[]
            {
                new GLVertex(new Vector3(-300.0f, 0.0f, 0.0f), Vector3.Zero, OpenTK.Graphics.Color4.Black, Vector2.Zero),
                new GLVertex(new Vector3(300.0f, 0.0f, 0.0f), Vector3.Zero, OpenTK.Graphics.Color4.Black, Vector2.Zero),
                new GLVertex(new Vector3(0.0f, 300.0f, 0.0f), Vector3.Zero, OpenTK.Graphics.Color4.Black, Vector2.Zero),
                new GLVertex(new Vector3(0.0f, -300.0f, 0.0f), Vector3.Zero, OpenTK.Graphics.Color4.Black, Vector2.Zero)
            });
            glHelper.Buffers.AddIndices(AxisMarkerObjectName, new uint[] { 0, 1, 2, 3 }, PrimitiveType.Lines);

            glHelper.Buffers.AddVertices(LightMarkerObjectName, new GLVertex[]
            {
                new GLVertex(Vector3.Zero, Vector3.Zero, OpenTK.Graphics.Color4.White, Vector2.Zero)
            });
            glHelper.Buffers.AddIndices(LightMarkerObjectName, new uint[] { 0 }, PrimitiveType.Points);

            Lights[0]                    = new LightStruct();
            Lights[0].Enabled            = true;
            Lights[0].Position           = Vector4.Zero;
            Lights[0].Intensities        = new Vector3(0.75f, 0.75f, 0.75f);
            Lights[0].AmbientCoefficient = 0.5f;

            Lights[1]                    = new LightStruct();
            Lights[1].Enabled            = true;
            Lights[1].Intensities        = new Vector3(0.4f, 0.7f, 1.0f);
            Lights[1].AmbientCoefficient = 0.05f;

            Lights[2]                    = new LightStruct();
            Lights[2].Enabled            = true;
            Lights[2].Intensities        = new Vector3(1.0f, 0.7f, 0.4f);
            Lights[2].AmbientCoefficient = 0.05f;

            Lights[3]                    = new LightStruct();
            Lights[3].Enabled            = true;
            Lights[3].Intensities        = new Vector3(0.4f, 0.1f, 0.7f);
            Lights[3].AmbientCoefficient = 0.05f;

            Lights[4]                    = new LightStruct();
            Lights[4].Enabled            = true;
            Lights[4].Intensities        = new Vector3(0.7f, 1.0f, 0.4f);
            Lights[4].AmbientCoefficient = 0.05f;
        }
    // Gets all the lights in the scene and pass them to the buffer
    void ProcessLights()
    {
        Light[] lights = FindObjectsOfType <Light>();
        m_lights = new LightStruct[lights.Length];
        for (int i = 0; i < lights.Length; i++)
        {
            m_lights[i].dir       = lights[i].transform.forward;
            m_lights[i].color     = lights[i].color;
            m_lights[i].intensity = lights[i].intensity;

            switch (lights[i].type)
            {
            case LightType.Point:
                m_lights[i].angle = 360f;
                m_lights[i].range = lights[i].range;
                m_lights[i].pos   = lights[i].transform.position;
                break;

            case LightType.Spot:
                m_lights[i].angle = lights[i].spotAngle;
                m_lights[i].range = lights[i].range;
                m_lights[i].pos   = lights[i].transform.position;
                break;

            default: // Directional lights
                // NOTE: For now, area lights are not supported, so they are treated as directional ones
                m_lights[i].angle = 360f;
                m_lights[i].range = float.PositiveInfinity;
                m_lights[i].pos   = Vector3.positiveInfinity;
                break;
            }
        }

        m_lightsBuffer = new ComputeBuffer(m_lights.Length, LightStruct.GetBytes());
        m_lightsBuffer.SetData(m_lights);
    }
        /// <summary>
        /// Sets the parameters.
        /// </summary>
        /// <param name="cached">if set to <c>true</c> [cached].</param>
        public override void SetParameters(bool cached)
        {
            base.SetParameters(cached);

            if (!cached)
            {
                Camera camera = this.renderManager.CurrentDrawingCamera;

                this.shaderParameters.CameraPosition    = camera.Position;
                this.shaderParameters.ReferenceAlpha    = this.ReferenceAlpha;
                this.shaderParameters.DiffuseColor      = this.diffuseColor;
                this.shaderParameters.Alpha             = this.Alpha;
                this.shaderParameters.AmbientColor      = this.ambientColor;
                this.shaderParameters.SpecularPower     = this.SpecularPower;
                this.shaderParameters.SpecularIntensity = this.SpecularIntensity;
                this.shaderParameters.TextureOffset     = this.TexcoordOffset;

                if (this.LightingEnabled)
                {
                    if (this.nearbyLight != null)
                    {
                        // Common parameters
                        this.Light.Color     = this.nearbyLight.Color.ToVector3();
                        this.Light.Intensity = this.nearbyLight.Intensity;

                        if (this.nearbyLight is DirectionalLightProperties)
                        {
                            DirectionalLightProperties directional = this.nearbyLight as DirectionalLightProperties;
                            this.Light.Direction = directional.Direction;
                        }
                        else if (this.nearbyLight is PointLightProperties)
                        {
                            PointLightProperties point = this.nearbyLight as PointLightProperties;
                            this.Light.Position   = point.Position;
                            this.Light.LightRange = point.LightRange;
                        }
                        else
                        {
                            SpotLightProperties spot = this.nearbyLight as SpotLightProperties;
                            this.Light.Direction  = spot.Direction;
                            this.Light.LightRange = spot.LightRange;
                            this.Light.Position   = spot.Position;
                            this.Light.ConeAngle  = (float)Math.Cos(spot.LightConeAngle / 2);
                        }

                        this.shaderParameters.Light = this.Light;
                    }
                }

                this.Parameters = this.shaderParameters;

                if (this.diffuse != null)
                {
                    this.graphicsDevice.SetTexture(this.diffuse, 0);
                }

                if (this.ambient != null)
                {
                    this.graphicsDevice.SetTexture(this.ambient, 1);
                }
            }
        }