/// <summary> /// Adds the point light. /// </summary> /// <param name="pointLight">The point light.</param> public static void AddPointLight(Lights.PointLight pointLight) { pointLights.Add(pointLight); }
/// <summary> /// Draws a point light. /// </summary> /// <param name="device">The device.</param> /// <param name="colorRT">The color RT.</param> /// <param name="normalRT">The normal RT.</param> /// <param name="depthRT">The depth RT.</param> /// <param name="camera">The camera.</param> /// <param name="pointLight">The point light.</param> void DrawPointLight(GraphicsDevice device, RenderTarget2D colorRT, RenderTarget2D normalRT, RenderTarget2D depthRT, Camera camera, Lights.PointLight pointLight) { // Set the G-Buffer parameters pointLightEffect.Parameters["colorMap"].SetValue(colorRT); pointLightEffect.Parameters["normalMap"].SetValue(normalRT); pointLightEffect.Parameters["depthMap"].SetValue(depthRT); // Compute the light world matrix // scale according to light radius, and translate it to light position Matrix sphereWorldMatrix = Matrix.CreateScale(pointLight.Range) * Matrix.CreateTranslation(pointLight.Position); pointLightEffect.Parameters["World"].SetValue(sphereWorldMatrix); pointLightEffect.Parameters["View"].SetValue(camera.ViewMatrix); pointLightEffect.Parameters["Projection"].SetValue(camera.ProjectionMatrix); // Light position pointLightEffect.Parameters["lightPosition"].SetValue(pointLight.Position); // Set the color, radius and Intensity pointLightEffect.Parameters["Color"].SetValue(pointLight.Color); pointLightEffect.Parameters["lightRadius"].SetValue(pointLight.Range); pointLightEffect.Parameters["lightIntensity"].SetValue(pointLight.Intensity); // Parameters for specular computations pointLightEffect.Parameters["cameraPosition"].SetValue(camera.Position); pointLightEffect.Parameters["InvertViewProjection"].SetValue(Matrix.Invert(camera.ViewMatrix * camera.ProjectionMatrix)); // Size of a halfpixel, for texture coordinates alignment pointLightEffect.Parameters["halfPixel"].SetValue(halfPixel); // Calculate the distance between the camera and light center float cameraToCenter = Vector3.Distance(camera.Position, pointLight.Position); // If we are inside the light volume, draw the sphere's inside face if (cameraToCenter < pointLight.Range) { device.RasterizerState = RasterizerState.CullClockwise; } else { device.RasterizerState = RasterizerState.CullCounterClockwise; } device.DepthStencilState = DepthStencilState.None; // Apply the Effect pointLightEffect.Techniques[0].Passes[0].Apply(); // Draw the Sphere mesh foreach (ModelMesh mesh in sphereModel.Meshes) { foreach (ModelMeshPart meshPart in mesh.MeshParts) { device.SetVertexBuffer(meshPart.VertexBuffer, meshPart.VertexOffset); device.Indices = meshPart.IndexBuffer; device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount); } } }