Exemplo n.º 1
0
        private void RenderFrame()
        {
            //Set the defaul render target and the depth stencil buffers
            device.SetRenderTarget(device.BackBuffer, device.BackDepthBuffer);

            //Specify the ViewPort
            device.ViewPort = new ViewPort(0, 0, Width, Height);

            //Clear the render target and depth stencil buffers
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, new Color4(0, 0, 0, 0), 1, 0);


            //Set the primitive type and vertex buffer
            device.PrimitiveTopology = IAPrimitive.TriangleList;
            device.SetVertexBuffer(0, vertexBuffer, 0);

            //Set the shader program to use
            device.Program = shaderProgram;

            //Pass the tranformation values to the shader code
            input.WorldViewProj = world * view * projection;

            //Draw the geometry
            device.Draw(3, 0);

            //Present the render target buffer to the screen
            device.Present();
        }
Exemplo n.º 2
0
        private void MensureLuminance(GraphicDevice device, IShaderStage stage)
        {
            effect.Technique = HdrEffect.SampleAvgLum;

            RenderTexture2D dest   = _rtToneMap[Tonemaptextures - 1];
            RenderTexture2D source = _rtHdrScene;

            dest.SetTarget(device);
            source.SetTexture(0, device);

            stage.SetSampler(0, _linearSampler);

            device.Clear(ClearFlags.Target, Color4.Black, 1, 0);
            _quadRender.DrawQuad(effect);

            effect.Technique = HdrEffect.ResampleAvgLum;
            stage.SetSampler(0, _pointSampler);

            for (int i = Tonemaptextures - 2; i > 0; i--)
            {
                dest   = _rtToneMap[i];
                source = _rtToneMap[i + 1];

                dest.SetTarget(device);
                source.SetTexture(0, device);
                device.Clear(ClearFlags.Target, Color4.Black, 1, 0);
                _quadRender.DrawQuad(effect);
            }

            effect.Technique = HdrEffect.ResampleAvgLumExp;
            source           = _rtToneMap[1];
            dest             = _rtToneMap[0];

            dest.SetTarget(device);
            source.SetTexture(0, device);

            device.Clear(ClearFlags.Target, Color4.Black, 1, 0);
            _quadRender.DrawQuad(effect);
        }
Exemplo n.º 3
0
        private void RenderFrame()
        {
            //Set the render target and the depth stencil buffers
            //for rendering to the display just set the device default
            //BackBuffer and BackDepthBuffer
            device.SetRenderTarget(device.BackBuffer, device.BackDepthBuffer);

            //Set the ViewPort to used by the device during the viewport tranformation
            device.ViewPort = new ViewPort(0, 0, Width, Height);

            //Clear the render target and depth stencil buffers
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, new Color4(0, 0, 0, 0), 1, 0);

            //Set the primitive type
            device.PrimitiveTopology = IAPrimitive.TriangleList;

            //Bind the vertex buffer to slot 0 at offset 0
            device.SetVertexBuffer(0, vertexBuffer, 0);

            //Set the index buffer
            device.SetIndexBuffer(indexBuffer);


            //Send the transformation matrices to the vertex shader
            input.World      = Matrix.RotationY(-(float)Environment.TickCount / 5000.0f);
            input.View       = view;
            input.Projection = projection;

            //Send the light info and other values to the pixel shader
            input.CameraPosition    = cameraPosition;
            input.ReflectionRatio   = 0.05f;
            input.SpecularRatio     = 0.15f;
            input.SpecularStyleLerp = 0.15f;
            input.SpecularPower     = 8;
            input.DirectionalLight  = new DirectionalLight
            {
                Color     = Color3.White,
                Direction = new Euler(45, 0, 0).ToDirection()
            };

            //Bind a texture with a sampler state. As a convetion the SamplerState
            //in the shader must have the same name as the texture with 's' as prefix
            //for example in the shader the sampler state is declared
            //SamplerState sDiffuseTexture;
            input.DiffuseTexture = diffuseTexture.ToSampler(diffuseSampler);

            device.GetShaderStage <PixelShader>().SetResource(0, diffuseTexture);
            device.GetShaderStage <PixelShader>().SetSampler(0, diffuseSampler);

            //Bind textures with default sampler state (linear filtering and wrap TextureAddressMode).
            //these statements have the same behavior that calling nightTexture.ToSampler()
            input.NightTexture     = nightTexture;
            input.NormalMapTexture = normalMapTexture;
            input.ReflectionMask   = reflectionMask;


            //Set the shader program
            device.Program = shaderProgram;

            //Draw the geometry using the indices count, the start index an the vertex base offset
            device.DrawIndexed((int)indexBuffer.SizeInBytes / indexBuffer.Stride, 0, 0);

            //Present the render target buffer to the display.
            device.Present();
        }
Exemplo n.º 4
0
        private unsafe void RenderStar(GraphicDevice device)
        {
            RenderTexture2D source;
            RenderTexture2D dest;

            var glare = _state.Glare;

            //renderMgr.Fill(rtStarFinal, 0);
            // Avoid rendering the star if it's not being used in the current glare
            if (glare == null || glare.glareLuminance <= 0.0f || glare.starLuminance <= 0.0f)
            {
                _rtStarFinal.SetTarget(device);
                device.Clear(ClearFlags.Target, Color4.Black, 1, 0);

                return;
            }

            fixed(Vector4 *_ptSampleOffsets = _sampleOffsetsStar)
            {
                Vector2 *ptSampleOffsets = (Vector2 *)_ptSampleOffsets;

                fixed(Vector4 *ptSampleWeights = _sampleWeightsStar)
                {
                    //#region StarSourceToBloomSource (DownSampler2x2)

                    //effect.Technique = HDREffect.DownSampler2x2;
                    //dest = rtBrightPassFilter;
                    //source = rtStartSource;

                    //dest.SetTarget(device);
                    //source.SetTexture(0, device);
                    //device.Clear(ClearFlags.Target, Color4.Black, 1, 0);
                    //quadRender.DrawQuad(effect);
                    //#endregion

                    #region BrighPassToStarSource

                    effect.Technique = HdrEffect.GaussBlur5X5;
                    source           = _rtBrightPassFilter;
                    dest             = _rtStartSource;

                    dest.SetTarget(device);
                    source.SetTexture(0, device);

                    FillSampleOffsets_GaussBlur5x5(source.Width, source.Height, ptSampleOffsets, ptSampleWeights, 1);

                    _map.SampleOffsets = _sampleOffsetsStar;
                    _map.SampleWeights = _sampleWeightsStar;

                    _quadRender.DrawQuad(effect);

                    #endregion

                    //float fTanFoV = (float)Math.Atan(Numerics.PI / 8.0f);
                    const float    fTanFoV   = 0.37419668052268495F;
                    const int      maxPasses = 3;
                    const int      samples   = 8;
                    int            i; // Loop variables
                    StarDefinition starDef = glare.StarDef;

                    device.PS.SetSampler(0, _linearSampler);

                    float srcW = _rtStartSource.Width;
                    float srcH = _rtStartSource.Height;


                    for (int p = 0; p < maxPasses; p++)
                    {
                        float ratio = (float)(p + 1) / (float)maxPasses;
                        for (int s = 0; s < samples; s++)
                        {
                            Vector4 chromaticAberrColor = Numerics.Lerp(StarDefinition.ChromaticAberrationColor[s], _sColorWhite, ratio);
                            _sAaColor[p, s] = Numerics.Lerp(_sColorWhite, chromaticAberrColor, glare.chromaticAberration);
                        }
                    }

                    _time += starDef.rotation ? Engine.ElapsedTime : 0;
                    float radOffset = glare.starInclination + starDef.inclination + _time;

                    Vector2 stepUv;
                    int     lines = starDef.StarLines.Length;

                    float attnPowScaleInicial = (fTanFoV + 0.1f) * 1.0f * (160.0f + 120.0f) / (srcW + srcH) * 1.2f;

                    for (int d = 0; d < lines; d++)
                    {
                        Starline starLine = starDef.StarLines[d];
                        float    rad      = radOffset + starLine.Inclination;
                        float    sn       = (float)Math.Sin(rad);
                        float    cs       = (float)Math.Cos(rad);
                        stepUv.X = sn / srcW * starLine.SampleLength;
                        stepUv.Y = cs / srcH * starLine.SampleLength;

                        float attnPowScale = attnPowScaleInicial;

                        #region PASSES
                        // 1 direction expansion loop
                        source = _rtStartSource;
                        dest   = _rtStarPasses[0];
                        for (int p = 0; p < starLine.Passes; p++)
                        {
                            if (p == starLine.Passes - 1)
                            {
                                source = dest;
                                dest   = _rtStarLines[d];
                            }
                            // Sampling configration for each stage
                            for (i = 0; i < samples; i++)
                            {
                                float lum = (float)Math.Pow(starLine.Attenuation, attnPowScale * i);

                                ptSampleWeights[i] = _sAaColor[starLine.Passes - 1 - p, i] * lum * (p + 1.0f) * 0.5f;

                                // Offset of sampling coordinate
                                ptSampleOffsets[i].X = stepUv.X * i;
                                ptSampleOffsets[i].Y = stepUv.Y * i;
                                if (Math.Abs(ptSampleOffsets[i].X) >= 0.9f || Math.Abs(ptSampleOffsets[i].Y) >= 0.9f)
                                {
                                    ptSampleOffsets[i] = Vector2.Zero;
                                    ptSampleWeights[i] = Vector4.Zero;
                                }
                            }

                            effect.Technique = HdrEffect.Star;
                            dest.SetTarget(device);
                            source.SetTexture(0, device);

                            _map.SampleOffsets = _sampleOffsetsStar;
                            _map.SampleWeights = _sampleWeightsStar;

                            _quadRender.DrawQuad(effect);

                            // Setup next expansion
                            stepUv       *= samples;
                            attnPowScale *= samples;

                            // Set the work drawn just before to next texture source.
                            if (p == 0)
                            {
                                source = dest;
                                dest   = _rtStarPasses[1];
                            }
                            else if (p != starLine.Passes - 1)
                            {
                                var rtTemp = source;
                                source = dest;
                                dest   = source;
                            }
                        }

                        #endregion PASSES
                    }

                    dest = _rtStarFinal;
                    dest.SetTarget(device);

                    float invLines = 1.0f / (float)lines;
                    for (i = 0; i < lines; i++)
                    {
                        _rtStarLines[i].SetTexture(i, device);
                        device.PS.SetSampler(i, _linearSampler);
                        ptSampleWeights[i] = new Vector4(invLines, invLines, invLines, invLines);
                    }

                    effect.Technique   = HdrEffect.MergeTexture;
                    _map.NbTextures    = lines;
                    _map.SampleWeights = _sampleWeightsStar;

                    _quadRender.DrawQuad(effect);

                    //for (i = 0; i < lines; i++)
                    //{
                    //    device.PSStage.SetSampler(i, pointSampler);
                    //}
                }
            }
        }