Advertisement
Guest User

Untitled

a guest
Nov 9th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.28 KB | None | 0 0
  1. public void UpdateOcclusion(Matrix View, Matrix Projection)
  2.         {
  3.             // The sun is infinitely distant, so it should not be affected by the
  4.             // position of the camera. Floating point math doesn't support infinitely
  5.             // distant vectors, but we can get the same result by making a copy of our
  6.             // view matrix, then resetting the view translation to zero. Pretending the
  7.             // camera has not moved position gives the same result as if the camera
  8.             // was moving, but the light was infinitely far away. If our flares came
  9.             // from a local object rather than the sun, we would use the original view
  10.             // matrix here.
  11.             Matrix infiniteView = View;
  12.  
  13.             infiniteView.Translation = Vector3.Zero;
  14.  
  15.             // Project the light position into 2D screen space.
  16.             Viewport viewport = graphicsDevice.Viewport;
  17.  
  18.             Vector3 projectedPosition = viewport.Project(-LightDirection, Projection,
  19.                                                          infiniteView, Matrix.Identity);
  20.  
  21.             // Don't draw any flares if the light is behind the camera.
  22.             if ((projectedPosition.Z < 0) || (projectedPosition.Z > 1))
  23.             {
  24.                 lightBehindCamera = true;
  25.                 return;
  26.             }
  27.  
  28.             lightPosition = new Vector2(projectedPosition.X, projectedPosition.Y);
  29.             lightBehindCamera = false;
  30.  
  31.             if (occlusionQueryActive)
  32.             {
  33.                 // If the previous query has not yet completed, wait until it does.
  34.                 if (!occlusionQuery.IsComplete)
  35.                     return;
  36.  
  37.                 // Use the occlusion query pixel count to work
  38.                 // out what percentage of the sun is visible.
  39.                 const float queryArea = querySize * querySize;
  40.  
  41.                 occlusionAlpha = Math.Min(occlusionQuery.PixelCount / queryArea, 1);
  42.                 Console.WriteLine(occlusionAlpha);
  43.             }
  44.  
  45.             // Set renderstates for drawing the occlusion query geometry. We want depth
  46.             // tests enabled, but depth writes disabled, and we disable color writes
  47.             // to prevent this query polygon actually showing up on the screen.
  48.             graphicsDevice.BlendState = ColorWriteDisable;
  49.             graphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
  50.  
  51.             // Set up our BasicEffect to center on the current 2D light position.
  52.             basicEffect.World = Matrix.CreateTranslation(lightPosition.X,
  53.                                                          lightPosition.Y, 0);
  54.  
  55.             basicEffect.Projection = Matrix.CreateOrthographicOffCenter(0,
  56.                                                                         viewport.Width,
  57.                                                                         viewport.Height,
  58.                                                                         0, 0, 1);
  59.  
  60.             basicEffect.CurrentTechnique.Passes[0].Apply();
  61.  
  62.             // Issue the occlusion query.
  63.             occlusionQuery.Begin();
  64.  
  65.             graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, queryVertices, 0, 2);
  66.  
  67.             occlusionQuery.End();
  68.  
  69.             occlusionQueryActive = true;
  70.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement