/// <summary> /// Add a new point lights to the render. /// </summary> /// <param name="pointLight">Point light to be added.</param> private void AddPointLight(PointLight pointLight) { // Set the G-Buffer parameters _pointLightEffect.Parameters["colorMap"].SetValue(_colorTarget); _pointLightEffect.Parameters["normalMap"].SetValue(_normalTarget); _pointLightEffect.Parameters["depthMap"].SetValue(_depthTarget); // Compute the light world, view and projection matrix _pointLightEffect.Parameters["World"].SetValue( Matrix.CreateScale(pointLight.lightRadius) * Matrix.CreateTranslation(pointLight.lightPosition)); _pointLightEffect.Parameters["View"].SetValue(CameraManager.ActiveCamera.View); _pointLightEffect.Parameters["Projection"].SetValue(CameraManager.ActiveCamera.Projection); _pointLightEffect.Parameters["cameraPosition"].SetValue(CameraManager.ActiveCamera.Position); _pointLightEffect.Parameters["InvertViewProjection"].SetValue( Matrix.Invert(CameraManager.ActiveCamera.View * CameraManager.ActiveCamera.Projection)); // Set the position, color, radius, intensity and half pixel _pointLightEffect.Parameters["lightPosition"].SetValue(pointLight.lightPosition); _pointLightEffect.Parameters["Color"].SetValue(pointLight.color.ToVector3()); _pointLightEffect.Parameters["lightRadius"].SetValue(pointLight.lightRadius); _pointLightEffect.Parameters["lightIntensity"].SetValue(pointLight.LightIntensity); _pointLightEffect.Parameters["halfPixel"].SetValue(_halfPixel); // Calculate the distance between the camera and light center float cameraToCenter = Vector3.Distance(CameraManager.ActiveCamera.Position, pointLight.lightPosition); // If we are inside the light volume, draw the sphere's inside face if (cameraToCenter < pointLight.lightRadius) { EngineManager.GameGraphicsDevice.RasterizerState = RasterizerState.CullClockwise; } else { EngineManager.GameGraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; } EngineManager.GameGraphicsDevice.DepthStencilState = DepthStencilState.None; // Draw the point light effect _pointLightEffect.Techniques[0].Passes[0].Apply(); foreach (ModelMesh mesh in ModelManager.GetModel("Sphere").Model.Meshes) { foreach (ModelMeshPart meshPart in mesh.MeshParts) { EngineManager.GameGraphicsDevice.Indices = meshPart.IndexBuffer; EngineManager.GameGraphicsDevice.SetVertexBuffer(meshPart.VertexBuffer); EngineManager.GameGraphicsDevice.DrawIndexedPrimitives( PrimitiveType.TriangleList, 0, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount); } } EngineManager.GameGraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; EngineManager.GameGraphicsDevice.DepthStencilState = DepthStencilState.Default; }
/// <summary> /// Load all the neccessary elements of the renderer, including render targets, /// point and spot lights. /// </summary> public void LoadContent() { // Calculate the halfPixel value _halfPixel = new Vector2() { X = 0.5f / (float)EngineManager.GameGraphicsDevice.PresentationParameters.BackBufferWidth, Y = 0.5f / (float)EngineManager.GameGraphicsDevice.PresentationParameters.BackBufferHeight }; _quadRenderer.LoadContent(); // Create the Render Targets in which will render the objects PresentationParameters pp = EngineManager.GameGraphicsDevice.PresentationParameters; int width = pp.BackBufferWidth; int height = pp.BackBufferHeight; SurfaceFormat format = pp.BackBufferFormat; _colorTarget = new RenderTarget2D(EngineManager.GameGraphicsDevice, width, height, false, SurfaceFormat.Color, pp.DepthStencilFormat, pp.MultiSampleCount, RenderTargetUsage.PreserveContents); _normalTarget = new RenderTarget2D(EngineManager.GameGraphicsDevice, width, height, false, SurfaceFormat.Color, pp.DepthStencilFormat, pp.MultiSampleCount, RenderTargetUsage.PreserveContents); _depthTarget = new RenderTarget2D(EngineManager.GameGraphicsDevice, width, height, false, SurfaceFormat.Single, pp.DepthStencilFormat, pp.MultiSampleCount, RenderTargetUsage.PreserveContents); _lightsTarget = new RenderTarget2D(EngineManager.GameGraphicsDevice, width, height, false, SurfaceFormat.Color, pp.DepthStencilFormat, pp.MultiSampleCount, RenderTargetUsage.PreserveContents); // Load the effects to be used in the deferred rendering _renderGBuffer = EngineManager.ContentManager.Load <Effect>("Content/Effects/RenderGBuffer"); _directionalLightEffect = EngineManager.ContentManager.Load <Effect>("Content/Effects/DirectionalLight"); _finalCombineEffect = EngineManager.ContentManager.Load <Effect>("Content/Effects/CombineFinal"); _pointLightEffect = EngineManager.ContentManager.Load <Effect>("Content/Effects/PointLight"); // Create the point lights in the scene _pointLights = new PointLight[4]; _pointLights[0] = new PointLight(); _pointLights[0].lightPosition = new Vector3(0, 10, 5); _pointLights[0].color = Color.White; _pointLights[0].lightRadius = 15; _pointLights[0].LightIntensity = 5; _pointLights[1] = new PointLight(); _pointLights[1].lightPosition = new Vector3(10, 10, 5); _pointLights[1].color = Color.White; _pointLights[1].lightRadius = 15; _pointLights[1].LightIntensity = 5; _pointLights[2] = new PointLight(); _pointLights[2].lightPosition = new Vector3(20, 10, 5); _pointLights[2].color = Color.White; _pointLights[2].lightRadius = 15; _pointLights[2].LightIntensity = 5; _pointLights[3] = new PointLight(); _pointLights[3].lightPosition = new Vector3(25, 25, -25); _pointLights[3].color = Color.White; _pointLights[3].lightRadius = 50; _pointLights[3].LightIntensity = 50; // Create the spot lights in the scene _spotLights = new SpotLight[3]; _spotLights[0] = new SpotLight(); _spotLights[0].Position = new Vector3(0, 10, 5); _spotLights[0].Direction = new Vector3(0, -1, -1); _spotLights[0].Strength = 0.7f; _spotLights[0].ConeAngle = 0.5f; _spotLights[0].ConeDelay = 2.0f; _spotLights[1] = new SpotLight(); _spotLights[1].Position = new Vector3(10, 10, 0); _spotLights[1].Direction = new Vector3(10, 0, 0); _spotLights[1].Strength = 0.7f; _spotLights[1].ConeAngle = 0.5f; _spotLights[1].ConeDelay = 2.0f; _spotLights[2] = new SpotLight(); _spotLights[2].Position = new Vector3(20, 10, 0); _spotLights[2].Direction = new Vector3(20, 0, 0); _spotLights[2].Strength = 0.7f; _spotLights[2].ConeAngle = 0.5f; _spotLights[2].ConeDelay = 2.0f; }