Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void UpdateOcclusion(Matrix View, Matrix Projection)
- {
- Matrix infiniteView = View;
- infiniteView.Translation = Vector3.Zero;
- Viewport viewport = graphicsDevice.Viewport;
- Vector3 projectedPosition = viewport.Project(-LightDirection, Projection,
- infiniteView, Matrix.Identity);
- if ((projectedPosition.Z < 0) || (projectedPosition.Z > 1))
- {
- lightBehindCamera = true;
- return;
- }
- lightPosition = new Vector2(projectedPosition.X, projectedPosition.Y);
- lightBehindCamera = false;
- if (occlusionQueryActive)
- {
- // If the previous query has not yet completed, wait until it does.
- if (!occlusionQuery.IsComplete)
- return;
- // Use the occlusion query pixel count to work
- // out what percentage of the sun is visible.
- const float queryArea = querySize * querySize;
- occlusionAlpha = Math.Min(occlusionQuery.PixelCount / queryArea, 1);
- Console.WriteLine(occlusionAlpha);
- }
- // Set renderstates for drawing the occlusion query geometry. We want depth
- // tests enabled, but depth writes disabled, and we disable color writes
- // to prevent this query polygon actually showing up on the screen.
- graphicsDevice.BlendState = ColorWriteDisable;
- graphicsDevice.DepthStencilState = DepthStencilState.DepthRead;
- // Set up our BasicEffect to center on the current 2D light position.
- basicEffect.World = Matrix.CreateTranslation(lightPosition.X,
- lightPosition.Y, 0);
- basicEffect.Projection = Matrix.CreateOrthographicOffCenter(0,
- viewport.Width,
- viewport.Height,
- 0, 0, 1);
- basicEffect.CurrentTechnique.Passes[0].Apply();
- // Issue the occlusion query.
- occlusionQuery.Begin();
- graphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleStrip, queryVertices, 0, 2);
- occlusionQuery.End();
- occlusionQueryActive = true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement