/// <summary> /// Render a point light /// </summary> /// <param name="camera"></param> /// <param name="pointLight"></param> public void RenderPointLight(DeferredLightingCamera camera, DeferredPointLight pointLight) { if (deferredPointLightEffect == null) { deferredPointLightEffect = Game.Content.Load <Effect>("Shaders/DeferredRender/DeferredPointLight"); } if (pointLightVolumeModel == null) { pointLightVolumeModel = Game.Content.Load <Model>("Models/PointLightVolume"); } //set the G-Buffer parameters deferredPointLightEffect.Parameters["normalMap"].SetValue(camera.NormalBuffer); deferredPointLightEffect.Parameters["depthMap"].SetValue(camera.DepthBuffer); deferredPointLightEffect.Parameters["sgrMap"].SetValue(camera.SpecularGlowReflectionMap); //compute the light world matrix //scale according to light radius, and translate it to light position Matrix sphereWorldMatrix = Matrix.CreateScale(pointLight.Radius) * Matrix.CreateTranslation(pointLight.Transform.Position); //light position deferredPointLightEffect.Parameters["lightPosition"].SetValue(pointLight.Transform.Position); //set the color, radius and Intensity deferredPointLightEffect.Parameters["Color"].SetValue(pointLight.Color.ToVector3()); deferredPointLightEffect.Parameters["lightRadius"].SetValue(pointLight.Radius); deferredPointLightEffect.Parameters["lightIntensity"].SetValue(pointLight.Intensity); //parameters for specular computations deferredPointLightEffect.Parameters["cameraPosition"].SetValue(camera.Transform.Position); deferredPointLightEffect.Parameters["InvertViewProjection"].SetValue(Matrix.Invert(camera.View * camera.Projection)); Matrix[] transforms = new Matrix[pointLightVolumeModel.Bones.Count]; pointLightVolumeModel.CopyAbsoluteBoneTransformsTo(transforms); Matrix meshWorld; int mmpc; for (int m = 0; m < pointLightVolumeModel.Meshes.Count; m++) { if (pointLightVolumeModel.Meshes[m].ParentBone != null) { meshWorld = transforms[pointLightVolumeModel.Meshes[m].ParentBone.Index] * sphereWorldMatrix; } else { meshWorld = transforms[0] * sphereWorldMatrix; } mmpc = pointLightVolumeModel.Meshes[m].MeshParts.Count; for (int mp = 0; mp < mmpc; mp++) { deferredPointLightEffect.Parameters["wvp"].SetValue(meshWorld * camera.View * camera.Projection); int pCnt = deferredPointLightEffect.CurrentTechnique.Passes.Count; for (int p = 0; p < pCnt; p++) { deferredPointLightEffect.CurrentTechnique.Passes[p].Apply(); GraphicsDevice.SetVertexBuffer(pointLightVolumeModel.Meshes[m].MeshParts[mp].VertexBuffer); GraphicsDevice.Indices = pointLightVolumeModel.Meshes[m].MeshParts[mp].IndexBuffer; GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, pointLightVolumeModel.Meshes[m].MeshParts[mp].VertexOffset, 0, pointLightVolumeModel.Meshes[m].MeshParts[mp].PrimitiveCount); } } } }
public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.GraphicsProfile = GraphicsProfile.HiDef; graphics.PreferredBackBufferWidth = 1920; graphics.PreferredBackBufferHeight = 1080; IsMouseVisible = true; kbm = new KeyboardStateManager(this); lightingManager = new DeferredLightingManager(this); camera = new DeferredLightingCamera(this, .5f, 20000); camera.Position = new Vector3(0, 1, 0); camera.ClearColor = Color.Black; Components.Add(camera); Services.AddService(typeof(Base3DCamera), camera); BaseSkyBox skyBox = new BaseSkyBox(this, "Textures/SkyBox/HMcubemap"); skyBox.effectAsset = "Shaders/DeferredRender/SkyBoxShader"; Components.Add(skyBox); Plane plane = new Plane(this, "Shaders/DeferredRender/DeferredModelRender"); plane.ColorAsset = "Textures/cobbles"; plane.BumpAsset = "Textures/cobblesNormal"; plane.OcclusionAsset = "Textures/cobbleOcclusion"; plane.SpecularAsset = "Textures/cobblesSpecular"; plane.Scale = Vector3.One * 2; plane.UVMultiplier = Vector2.One * 5; plane.Position = new Vector3(0, -1, -20); Components.Add(plane); Base3DObject bunny = new Base3DObject(this, "Models/bunny", "Shaders/DeferredRender/DeferredModelRender"); bunny.Position = new Vector3(0, -1f, -20); Components.Add(bunny); Cube cube = new Cube(this, "Shaders/DeferredRender/DeferredModelRender"); cube.ColorAsset = "Textures/xnauguk"; cube.Position = new Vector3(-3, -.5f, -20); Components.Add(cube); Sphere sphere = new Sphere(this, "Shaders/DeferredRender/DeferredModelRender"); sphere.Scale = Vector3.One * 2; sphere.Position = new Vector3(3, -0, -20); sphere.ColorAsset = "Textures/brick"; sphere.BumpAsset = "Textures/brickNormal"; sphere.OcclusionAsset = "Textures/brickOcclusion"; sphere.SpecularAsset = "Textures/brickSpecular"; sphere.UVMultiplier = Vector2.One * 2; Components.Add(sphere); Cube sandBlock = new Cube(this, "Shaders/DeferredRender/DeferredModelRender"); sandBlock.ColorAsset = "Textures/sand"; sandBlock.BumpAsset = "Textures/sandNormal"; sandBlock.Position = new Vector3(-3, 1.5f, -20); sandBlock.Rotate(Vector3.Up + Vector3.Forward + Vector3.Left, 15); Components.Add(sandBlock); DeferredDirectionalLight directionalLight = new DeferredDirectionalLight(this, new Vector3(10, 10, 10), Color.AliceBlue, 1, true); directionalLight.Transform.LookAt(Vector3.Zero, 1, Vector3.Forward); Components.Add(directionalLight); lightingManager.AddLight(directionalLight); DeferredPointLight pointLight = new DeferredPointLight(this, new Vector3(0, 1, -20), Color.Red, 10, 1, false); Components.Add(pointLight); lightingManager.AddLight(pointLight); coneLight = new DeferredConeLight(this, new Vector3(-10, 5, -25), Color.Gold, 1, MathHelper.ToRadians(45), 5, true); coneLight.Transform.LookAt(new Vector3(0, 0, -20), 1, Vector3.Forward); Components.Add(coneLight); lightingManager.AddLight(coneLight); DeferredConeLight coneLight2 = new DeferredConeLight(this, new Vector3(0, 20, -20), Color.Green, 1, MathHelper.ToRadians(25), 50, true); coneLight2.Transform.LookAt(new Vector3(0, 0, -20), 1, Vector3.Forward); Components.Add(coneLight2); lightingManager.AddLight(coneLight2); // Shadow casters and receivers.. lightingManager.AddShadowCaster(bunny); lightingManager.AddShadowCaster(cube); lightingManager.AddShadowCaster(sphere); lightingManager.AddShadowCaster(sandBlock); }