Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Using
- using System;
- using System.Collections.Generic;
- using System.Windows.Forms;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using System.Collections.Generic;
- using PolitieNL.Game.Managers;
- using Pathfinding;
- #endregion
- namespace PolitieNL.Game.Levels
- {
- class Level
- {
- #region Members
- /// <summary>
- /// Test model
- /// </summary>
- private Model _level;
- private Model _props;
- KeyboardState currentKeyboardState;
- GamePadState currentGamePadState;
- private Matrix[] _transforms;
- private Matrix[] _porpsTransforms;
- Map _mMap;
- private Effect _eShadowMapEffect;
- SpriteBatch spriteBatch;
- TriangleProcessor _pickingProcessor;
- // The shadow map render target
- RenderTarget2D _shadowRenderTarget;
- const int ShadowMapWidthHeight = 2048;
- // ViewProjection matrix from the lights perspective
- Matrix _lightViewProjection;
- // Transform matrices
- Matrix _world;
- Matrix _view;
- Matrix _projection;
- private Matrix _lightView;
- private float rotate;
- // Light direction
- Vector3 lightPos = new Vector3(10,10,10);
- Vector3 lightDir = new Vector3(-0.3333333f, 0.6666667f, 0.6666667f);
- // Starting position and direction of our camera
- Vector3 cameraPosition = new Vector3(690.0f, 400.0f, 271);
- Vector3 cameraForward = new Vector3(0, -0.4472136f, -0.8944272f);
- BoundingFrustum cameraFrustum = new BoundingFrustum(Matrix.Identity);
- #endregion
- #region Functions
- /// <summary>
- /// Initialize level
- /// </summary>
- /// <param name="content">contentmanager for loading models</param>
- /// <param name="projMat">projection matrix</param>
- public void Initialize(ContentManager content, PolitieNL.Game.CameraClass.Camera cam , GraphicsDevice device)
- {
- spriteBatch = new SpriteBatch(device);
- //Load test model
- _level = content.Load<Model>("Models/PNTS_Building");
- _props = content.Load<Model>("Models/PNTS_Props");
- //_eShadowMapEffect = content.Load<Effect>("Effect/DrawModel");
- _view = cam.ViewMatrix;
- _projection = cam.ProjectionMatrix;
- //Managers.ModelHandler.InitModel(_level, cam.ProjectionMatrix, out _transforms);
- //Managers.ModelHandler.InitModel(_props, cam.ProjectionMatrix, out _porpsTransforms);
- var transMat = new Matrix[_level.Bones.Count];
- _pickingProcessor = new TriangleProcessor();
- var lst = new List<string>();
- _pickingProcessor.ExtractData(_level, ref transMat, new Vector3(0, 0, 0), Matrix.Identity, false, lst);
- // Create floating point render target
- _shadowRenderTarget = new RenderTarget2D(device,
- ShadowMapWidthHeight,
- ShadowMapWidthHeight,
- true,
- SurfaceFormat.Single,
- DepthFormat.Depth24);
- _mMap = new Map();
- _mMap.LoadContent(content, cam);
- }
- public void Update(GameTime gameTime)
- {
- //HandleInput(gameTime);
- //UpdateCamera(gameTime);
- }
- /// <summary>
- /// Draws the level
- /// </summary>
- public void Draw(Matrix viewMat, Matrix projMat, GraphicsDevice device)
- {
- // Create the new view matrix
- _view = viewMat;
- _projection = projMat;
- // Set the new frustum value
- cameraFrustum.Matrix = viewMat * projMat;
- // Update the lights ViewProjection matrix based on the
- // current camera frustum
- _lightViewProjection = CreateLightViewProjectionMatrix();
- device.BlendState = BlendState.Opaque;
- device.DepthStencilState = DepthStencilState.Default;
- // Render the scene to the shadow map
- CreateShadowMap(device);
- // Draw the scene using the shadow map
- DrawWithShadowMap(device);
- // Display the shadow map to the screen
- DrawShadowMapToScreen(device);
- //Managers.ModelHandler.DrawModel(_level, viewMat, Matrix.Identity, _transforms);
- //Managers.ModelHandler.DrawModel(_props, viewMat, Matrix.Identity, _transforms);
- //if (m_map.IsMapthere()) m_map.Draw(spriteBatch, viewMat, transforms, projMat);
- }
- public float UpdatePicking(ref Ray ray)
- {
- for (int i = 0; i < _pickingProcessor.indexList.Count; ++i)
- {
- Vector3 vertex1, vertex2, vertex3;
- vertex1 = vertex2 = vertex3 = Vector3.Zero;
- float? intersection = null;
- _pickingProcessor.RayIntersectsTriangle(ref ray,
- _pickingProcessor.vertexList[_pickingProcessor.indexList[i].I0],
- _pickingProcessor.vertexList[_pickingProcessor.indexList[i].I1],
- _pickingProcessor.vertexList[_pickingProcessor.indexList[i].I2],
- out intersection);
- if (intersection != null)
- return (float)intersection;
- }
- return 0;
- }
- public Map GetMap() { return _mMap; }
- #endregion
- #region Shadowmap methods
- /// <summary>
- /// Creates the WorldViewProjection matrix from the perspective of the
- /// light using the cameras bounding frustum to determine what is visible
- /// in the scene.
- /// </summary>
- /// <returns>The WorldViewProjection for the light</returns>
- Matrix CreateLightViewProjectionMatrix()
- {
- #region algOne
- // Matrix with that will rotate in points the direction of the light
- Matrix lightRotation = Matrix.CreateLookAt(Vector3.Zero,
- -lightDir,
- Vector3.Up);
- // Get the corners of the frustum
- Vector3[] frustumCorners = cameraFrustum.GetCorners();
- // Transform the positions of the corners into the direction of the light
- for (int i = 0; i < frustumCorners.Length; i++)
- {
- frustumCorners[i] = Vector3.Transform(frustumCorners[i], lightRotation);
- }
- // Find the smallest box around the points
- BoundingBox lightBox = BoundingBox.CreateFromPoints(frustumCorners);
- Vector3 boxSize = lightBox.Max - lightBox.Min;
- Vector3 halfBoxSize = boxSize * 0.5f;
- // The position of the light should be in the center of the back
- // pannel of the box.
- Vector3 lightPosition = lightBox.Min + halfBoxSize;
- lightPosition.Z = lightBox.Min.Z;
- // We need the position back in world coordinates so we transform
- // the light position by the inverse of the lights rotation
- lightPosition = Vector3.Transform(lightPosition,
- Matrix.Invert(lightRotation));
- // Create the view matrix for the light
- Matrix lightView = Matrix.CreateLookAt(lightPosition,
- lightPosition - lightDir,
- Vector3.Up);
- // Create the projection matrix for the light
- // The projection is orthographic since we are using a directional light
- Matrix lightProjection = Matrix.CreateOrthographic(boxSize.X, boxSize.Y,
- -boxSize.Z, boxSize.Z);
- _lightView = lightView;
- return lightView * lightProjection;
- #endregion
- }
- /// <summary>
- /// Renders the scene to the floating point render target then
- /// sets the texture for use when drawing the scene.
- /// </summary>
- void CreateShadowMap(GraphicsDevice device)
- {
- // Set our render target to our floating point render target
- device.SetRenderTarget(_shadowRenderTarget);
- // Clear the render target to white or all 1's
- // We set the clear to white since that represents the
- // furthest the object could be away
- device.Clear(Color.White);
- device.DepthStencilState = DepthStencilState.Default;
- device.BlendState = BlendState.Opaque;
- // Draw any occluders in our case that is just the dude model
- _world = Matrix.Identity;
- // Set the models world matrix so it will rotate
- //_world = Matrix.CreateRotationY(MathHelper.ToRadians(rotate));
- // Draw the dude model
- DrawModel(_props, true);
- // Set render target back to the back buffer
- device.SetRenderTarget(null);
- }
- /// <summary>
- /// Renders the scene using the shadow map to darken the shadow areas
- /// </summary>
- void DrawWithShadowMap(GraphicsDevice device)
- {
- device.Clear(Color.Green);
- device.DepthStencilState = DepthStencilState.Default;
- device.BlendState = BlendState.Opaque;
- device.SamplerStates[1] = SamplerState.PointClamp;
- // Draw the grid
- _world = Matrix.Identity;
- DrawModel(_level, false);
- _world = Matrix.Identity;
- // Draw the Props model
- //_world = Matrix.CreateRotationY(MathHelper.ToRadians(rotate));
- DrawModel(_props, false);
- }
- /// <summary>
- /// Helper function to draw a model
- /// </summary>
- /// <param name="model">The model to draw</param>
- ///<param name="createShadowMap">Draw to Create or not</param>
- void DrawModel(Model model, bool createShadowMap)
- {
- string techniqueName = createShadowMap ? "CreateShadowMap" : "DrawWithShadowMap";
- _transforms = new Matrix[model.Bones.Count];
- model.CopyAbsoluteBoneTransformsTo(_transforms);
- // Loop over meshs in the model
- foreach (ModelMesh mesh in model.Meshes)
- {
- foreach (Effect effect in mesh.Effects)
- {
- // Set the currest values for the effect
- effect.CurrentTechnique = effect.Techniques[techniqueName];
- effect.Parameters["World"].SetValue(_transforms[mesh.ParentBone.Index] * _world);
- effect.Parameters["View"].SetValue(_view);
- effect.Parameters["Projection"].SetValue(_projection);
- // effect.Parameters["LightPos"].SetValue(lightPos);
- // effect.Parameters["mLightView"].SetValue(_lightView);
- effect.Parameters["LightDirection"].SetValue(lightDir);
- effect.Parameters["LightViewProj"].SetValue(_lightViewProjection);
- if (!createShadowMap)
- effect.Parameters["ShadowMap"].SetValue(_shadowRenderTarget);
- }
- //mesh.Effects = _eShadowMapEffect;
- // Loop over effects in the mesh
- // Draw the mesh
- mesh.Draw();
- }
- }
- /// <summary>
- /// Render the shadow map texture to the screen
- /// </summary>
- void DrawShadowMapToScreen(GraphicsDevice device)
- {
- spriteBatch.Begin(0, BlendState.Opaque, SamplerState.PointClamp, null, null);
- spriteBatch.Draw(_shadowRenderTarget, new Rectangle(0, 0, 128, 128), Color.White);
- spriteBatch.End();
- device.Textures[0] = null;
- device.SamplerStates[0] = SamplerState.LinearWrap;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement