Advertisement
Guest User

Untitled

a guest
Nov 9th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public void UpdateOcclusion(Matrix View, Matrix Projection)
  2.         {
  3.             Matrix infiniteView = View;
  4.  
  5.             infiniteView.Translation = Vector3.Zero;
  6.  
  7.             Viewport viewport = graphicsDevice.Viewport;
  8.  
  9.             Vector3 projectedPosition = viewport.Project(-LightDirection, Projection,
  10.                                                          infiniteView, Matrix.Identity);
  11.  
  12.             if ((projectedPosition.Z < 0) || (projectedPosition.Z > 1))
  13.             {
  14.                 lightBehindCamera = true;
  15.                 return;
  16.             }
  17.  
  18.             lightPosition = new Vector2(projectedPosition.X, projectedPosition.Y);
  19.             lightBehindCamera = false;
  20.  
  21.             if (occlusionQueryActive)
  22.             {
  23.                 // If the previous query has not yet completed, wait until it does.
  24.                 if (!occlusionQuery.IsComplete)
  25.                     return;
  26.  
  27.                 // Use the occlusion query pixel count to work
  28.                 // out what percentage of the sun is visible.
  29.                 const float queryArea = querySize * querySize;
  30.  
  31.                 occlusionAlpha = Math.Min(occlusionQuery.PixelCount / queryArea, 1);
  32.                 Console.WriteLine(occlusionAlpha);
  33.             }
  34.  
  35.             // Set renderstates for drawing the occlusion query geometry. We want depth
  36.             // tests enabled, but depth writes disabled, and we disable color writes
  37.             // to prevent this query polygon actually showing up on the screen.
  38.             graphicsDevice.BlendState = ColorWriteDisable;
  39.             graphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
  40.  
  41.             // Set up our BasicEffect to center on the current 2D light position.
  42.             basicEffect.World = Matrix.CreateTranslation(lightPosition.X,
  43.                                                          lightPosition.Y, 0);
  44.  
  45.             basicEffect.Projection = Matrix.CreateOrthographicOffCenter(0,
  46.                                                                         viewport.Width,
  47.                                                                         viewport.Height,
  48.                                                                         0, 0, 1);
  49.  
  50.             basicEffect.CurrentTechnique.Passes[0].Apply();
  51.  
  52.             // Issue the occlusion query.
  53.             occlusionQuery.Begin();
  54.  
  55.             graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, queryVertices, 0, 2);
  56.  
  57.             occlusionQuery.End();
  58.  
  59.             occlusionQueryActive = true;
  60.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement