SHOW:
|
|
- or go back to the newest paste.
| 1 | public void UpdateOcclusion(Matrix View, Matrix Projection) | |
| 2 | {
| |
| 3 | - | // The sun is infinitely distant, so it should not be affected by the |
| 3 | + | |
| 4 | - | // position of the camera. Floating point math doesn't support infinitely |
| 4 | + | |
| 5 | - | // distant vectors, but we can get the same result by making a copy of our |
| 5 | + | |
| 6 | - | // view matrix, then resetting the view translation to zero. Pretending the |
| 6 | + | |
| 7 | - | // camera has not moved position gives the same result as if the camera |
| 7 | + | |
| 8 | - | // was moving, but the light was infinitely far away. If our flares came |
| 8 | + | |
| 9 | - | // from a local object rather than the sun, we would use the original view |
| 9 | + | |
| 10 | - | // matrix here. |
| 10 | + | |
| 11 | ||
| 12 | if ((projectedPosition.Z < 0) || (projectedPosition.Z > 1)) | |
| 13 | {
| |
| 14 | lightBehindCamera = true; | |
| 15 | - | // Project the light position into 2D screen space. |
| 15 | + | |
| 16 | } | |
| 17 | ||
| 18 | lightPosition = new Vector2(projectedPosition.X, projectedPosition.Y); | |
| 19 | lightBehindCamera = false; | |
| 20 | ||
| 21 | - | // Don't draw any flares if the light is behind the camera. |
| 21 | + | |
| 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 | } |