Guest User

Untitled

a guest
Jul 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 KB | None | 0 0
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Content;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Graphics.PackedVector;
  6. using nulltech.Cameras;
  7. using nulltech.Services;
  8.  
  9. namespace nulltech.Illumination.AO
  10. {
  11.     public abstract class AmbientOcclusion
  12.     {
  13.         private const int RandomTextureSize = 64;
  14.         protected readonly VertexPositionTexture[] Vertices = new VertexPositionTexture[4];
  15.         private readonly Vector3[] _cornersViewSpace = new Vector3[8];
  16.         private readonly Vector3[] _cornersWorldSpace = new Vector3[8];
  17.         private readonly Vector3[] _currentFrustumCorners = new Vector3[4];
  18.         private readonly BoundingFrustum _frustum = new BoundingFrustum(Matrix.Identity);
  19.         protected Effect AOEffect;
  20.         private EffectParameter _cornerParam;
  21.         private EffectParameter _farParam;
  22.         private EffectParameter _inverseViewportParam;
  23.         private EffectParameter _randomTextureParam;
  24.         private EffectParameter _rayParam;
  25.         private Texture2D _randomTexture;
  26.  
  27.         protected AmbientOcclusion(EngineServices services)
  28.         {
  29.             GraphicsDevice = services.GraphicsDevice;
  30.             Content = services.ContentManager;
  31.  
  32.             GenerateQuad();
  33.  
  34.             GenerateRandomTexture();
  35.         }
  36.  
  37.         public ContentManager Content { get; private set; }
  38.  
  39.         public GraphicsDevice GraphicsDevice { get; private set; }
  40.  
  41.         public abstract void Calculate(CameraEntity camera);
  42.  
  43.         public abstract void Draw(CameraEntity camera);
  44.  
  45.         private void GenerateRandomTexture()
  46.         {
  47.             _randomTexture = new Texture2D(GraphicsDevice, RandomTextureSize, RandomTextureSize, false,
  48.                                            SurfaceFormat.HalfVector2);
  49.  
  50.             var rand = new Random();
  51.  
  52.             var samples = new HalfVector2[RandomTextureSize * RandomTextureSize];
  53.  
  54.             for (int i = 0; i < RandomTextureSize * RandomTextureSize; i++)
  55.             {
  56.                 Vector2 sVec = Vector2.Zero;
  57.                 sVec.X = (float)((rand.NextDouble() - .5) * 2.0);
  58.                 sVec.Y = (float)((rand.NextDouble() - .5) * 2.0);
  59.                 sVec.Normalize();
  60.                 samples[i] = new HalfVector2(sVec);
  61.             }
  62.  
  63.             _randomTexture.SetData(samples);
  64.         }
  65.  
  66.         protected void GenerateQuad()
  67.         {
  68.             Vertices[0].Position = new Vector3(-1, 1, 1);
  69.             Vertices[1].Position = new Vector3(1, 1, 1);
  70.             Vertices[2].Position = new Vector3(-1, -1, 1);
  71.             Vertices[3].Position = new Vector3(1, -1, 1);
  72.  
  73.             Vertices[0].TextureCoordinate = new Vector2(0, 0);
  74.             Vertices[1].TextureCoordinate = new Vector2(1, 0);
  75.             Vertices[2].TextureCoordinate = new Vector2(0, 1);
  76.             Vertices[3].TextureCoordinate = new Vector2(1, 1);
  77.         }
  78.  
  79.         protected void GenerateCorners(CameraEntity camera)
  80.         {
  81.             _inverseViewportParam.SetValue(camera.InverseViewport);
  82.  
  83.             _farParam.SetValue(camera.ProjectionController.Far);
  84.  
  85.             _frustum.Matrix = camera.ViewProjection;
  86.             _frustum.GetCorners(_cornersWorldSpace);
  87.  
  88.             Matrix matView = camera.View;
  89.  
  90.             Vector3.Transform(_cornersWorldSpace, ref matView, _cornersViewSpace);
  91.  
  92.             for (int i = 0; i < 4; i++) //take only the 4 farthest points
  93.             {
  94.                 _currentFrustumCorners[i] = _cornersViewSpace[i + 4];
  95.             }
  96.  
  97.             Vector3 temp = _currentFrustumCorners[3];
  98.             _currentFrustumCorners[3] = _currentFrustumCorners[2];
  99.             _currentFrustumCorners[2] = temp;
  100.  
  101.             _cornerParam.SetValue(_currentFrustumCorners);
  102.  
  103.             _rayParam.SetValue(_currentFrustumCorners[1]);
  104.         }
  105.  
  106.         protected virtual void ProcessEffect()
  107.         {
  108.             _inverseViewportParam = AOEffect.Parameters["InverseViewportDimensions"];
  109.             _cornerParam = AOEffect.Parameters["FrustumCorners"];
  110.             _rayParam = AOEffect.Parameters["FrustumRay"];
  111.             _farParam = AOEffect.Parameters["Far"];
  112.             _randomTextureParam = AOEffect.Parameters["RandomTexture"];
  113.  
  114.             _randomTextureParam.SetValue(_randomTexture);
  115.         }
  116.     }
  117. }
Add Comment
Please, Sign In to add comment