Example #1
0
 public void Dispose()
 {
     RsDebug?.Dispose();
     _rsCw?.Dispose();
     _rsCcw?.Dispose();
     LightRenderer.Dispose();
     LightMapRenderer.Dispose();
     ShadowRenderer.Dispose();
 }
Example #2
0
        public void Load(GraphicsDevice device, GraphicsDeviceManager deviceManager, GameWindow window,
                         Effect fxHull, Effect fxLight, Effect fxShadow, Effect fxTexture)
        {
            Device        = device;
            DeviceManager = deviceManager;
            Window        = window;

            BuildGraphicsResources();

            // Load providers.
            Camera.Load(this);
            Textures.Load(this);

            // Load renderers.
            LightMapRenderer.Load(this, fxTexture);
            ShadowRenderer.Load(this, fxShadow, fxHull);
            LightRenderer.Load(this, fxLight);
        }
Example #3
0
        public void Render()
        {
            // Update hulls internal data structures.
            Hulls.Update();

            // We want to use clamping sampler state throughout the lightmap rendering process.
            // This is required when drawing lights. Since light rendering and alpha clearing is done
            // in a single step, light is rendered with slightly larger quad where tex coords run out of the [0..1] range.
            Device.SamplerStates[0] = SamplerState.LinearClamp;

            // Switch render target to lightmap.
            Device.SetRenderTargets(Textures.LightmapBindings);

            // Clear lightmap color, depth and stencil data.
            Device.Clear(ClearOptions.DepthBuffer | ClearOptions.Stencil | ClearOptions.Target, _ambientColor, 1f, 0);

            // Set per frame shader data.
            ShadowRenderer.PreRender();

            // Generate lightmap. For each light, mask the shadowed areas determined by hulls and render light.
            int lightCount = Lights.Count;

            for (int i = 0; i < lightCount; i++)
            {
                Light light = Lights[i];

                // Continue only if light is enabled and not inside any hull.
                if (!light.Enabled || Hulls.Contains(light))
                {
                    continue;
                }

                // Update light's internal data structures.
                light.Update();

                // Continue only if light is within camera view.
                if (!light.Intersects(Camera))
                {
                    continue;
                }

                // Set scissor rectangle to clip any shadows outside of light's range.
                BoundingRectangle scissor;
                Camera.GetScissorRectangle(light, out scissor);
                Device.SetScissorRectangle(ref scissor);

                // Mask shadowed areas by reducing alpha.
                ShadowRenderer.Render(light);

                // Draw light and clear alpha (reset it to 1 [fully lit] for next light).
                LightRenderer.Render(light);

                // Clear light's dirty flag.
                light.Dirty = false;
            }

            // Switch render target back to default.
            Device.SetRenderTargets(Textures.GetOriginalRenderTargetBindings());

            // Blend original scene and lightmap and present to backbuffer.
            LightMapRenderer.Present();

            // Clear hulls dirty flag.
            Hulls.Dirty = false;
        }