Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Notes: -
- public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
- {
- base.Draw(gameTime, spriteBatch);
- //Set the rendertarget to draw the Blinn-Phong shaded scene to
- GraphicsDevice.SetRenderTarget(renderTarget);
- GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
- Material material = new Material();
- material.DiffuseColor = Color.Red;
- material.AmbientColor = Color.Red;
- material.AmbientIntensity = 0.2f;
- material.SpecularColor = Color.White;
- material.SpecularIntensity = 2.0f;
- material.SpecularPower = 25.0f;
- //Draw all the models
- foreach (ModelData m in models)
- {
- ModelMesh mesh = m.Model.Meshes[0];
- Effect e = mesh.Effects[0];
- e.CurrentTechnique = e.Techniques[m.Technique];
- material.SetEffectParameters(e);
- this.camera.SetEffectParameters(e);
- e.Parameters["World"].SetValue(world * m.Transform);
- e.Parameters["WorldInverseTransposed"].SetValue(Matrix.Transpose(Matrix.Invert(world * m.Transform)));
- e.Parameters["CameraEye"].SetValue(new Vector4(this.camera.Eye, 0));
- // TODO: LightSource Color + Intensity
- e.Parameters["LightSources"].SetValue(lightPositions);
- mesh.Draw();
- }
- //Restore the rendertarget to the backbuffer and clear it.
- GraphicsDevice.SetRenderTarget(null);
- GraphicsDevice.Clear(Color.Black);
- //Pass the standard variable to the spriteBatch vertex shader.
- //The vertex shader isn't used (for as far as I can tell at least) but because the standard
- //vertex shader for spriteBatch is compiled in 2_0 and the God Ray shader in 3_0 I had to implement it
- //manually in order to compile it in 3_0.
- Matrix projection = Matrix.CreateOrthographicOffCenter(0,
- GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0, 1);
- Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
- postProcessing.Parameters["MatrixTransform"].SetValue(halfPixelOffset * projection);
- //Setup all the required data for the shader
- postProcessing.CurrentTechnique = postProcessing.Techniques["Technique1"];
- Vector3 lightPosition = Vector3.Transform(lightPositions[0], world * camera.ViewMatrix * camera.ProjectionMatrix);
- postProcessing.Parameters["lightPosition"].SetValue(new Vector2(lightPosition.X, lightPosition.Y));
- postProcessing.Parameters["Tex"].SetValue(renderTarget);
- postProcessing.Parameters["exposure"].SetValue(0.5f);
- postProcessing.Parameters["decay"].SetValue(0.5f);
- postProcessing.Parameters["weight"].SetValue(0.5f);
- postProcessing.Parameters["density"].SetValue(0.5f);
- //Draw the renderTarget to the screen at the size of the viewport so it fits the screen
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, SamplerState.PointWrap, DepthStencilState.Default, RasterizerState.CullNone, postProcessing);
- spriteBatch.Draw(renderTarget, new Rectangle(spriteBatch.GraphicsDevice.Viewport.X, spriteBatch.GraphicsDevice.Viewport.Y, spriteBatch.GraphicsDevice.Viewport.Width, spriteBatch.GraphicsDevice.Viewport.Height), Color.White);
- spriteBatch.End();
- //Restore some of the changes spriteBatch.Begin() made to the graphics device so the 3D render won't break
- GraphicsDevice.BlendState = BlendState.Opaque;
- GraphicsDevice.DepthStencilState = DepthStencilState.Default;
- GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement