/// <summary> /// </summary> /// <param name="graphics">Needed for setting AspectRatio</param> public Camera(GraphicsDevice graphics, GameAssets a_gameAssets, ModelClasses.Map a_map) { m_cameraMinDist = 10; m_cameraMaxDist = 40; m_originalFocusPos = Vector3.Zero; //original values are 0, -10, 40 if you change and forget ^_^ m_originalCameraPos = new Vector3(0, -10, 40f); //m_pointLight = new Vector3(0, 0, 40); m_cameraPos = m_originalCameraPos; m_focusPos = m_originalFocusPos; //Currently just as identity matrix m_world = Matrix.CreateTranslation(Vector3.Zero); //set camera position //Currently above the world looking down SetViewMatrix(); //45 degree view field m_projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), graphics.Viewport.AspectRatio, 1, 300); m_shaderEffect = new ShaderEffect(a_gameAssets, ref m_projection, a_map); m_BoundingBoxEffect = new BasicEffect(graphics); m_BoundingBoxEffect.LightingEnabled = false; m_BoundingBoxEffect.TextureEnabled = false; m_BoundingBoxEffect.VertexColorEnabled = true; m_BoundingBoxEffect.Projection = m_projection; }
public BoundingBox?CreateBBSelection(GraphicsDevice a_gd, Plane a_plane, ModelClasses.Map a_map, Camera a_camera) { //The old position, where you first started dragging mouse Vector3?f_oldPos = GetWorldPosition(a_gd, a_plane, a_map, a_camera, m_mouse.m_oldXpos, m_mouse.m_oldYpos); if (f_oldPos.HasValue) { Vector3?f_newPos; //If mouse is inside the HUD it sets the new pos just outside the hud if (m_mouse.m_mouseState.Y > HUD.m_area.Top) { f_newPos = GetWorldPosition(a_gd, a_plane, a_map, a_camera, m_mouse.m_mouseState.X, HUD.m_area.Top); }//Otherwise it sets the current mouse pos as the other point, to create the BB between oldPos & newPos else { f_newPos = GetWorldPosition(a_gd, a_plane, a_map, a_camera, m_mouse.m_mouseState.X, m_mouse.m_mouseState.Y); } if (f_newPos.HasValue) { //If the mouse cursor is to the left of where you started if (f_newPos.Value.X < f_oldPos.Value.X) { //If the mouse cursor is below the position where you started if (f_newPos.Value.Y < f_oldPos.Value.Y) { return(new BoundingBox(new Vector3(f_newPos.Value.X, f_newPos.Value.Y, f_newPos.Value.Z - m_mouseBBoffset), new Vector3(f_oldPos.Value.X, f_oldPos.Value.Y, f_oldPos.Value.Z + m_mouseBBoffset))); }//IF the mouse cursor is above where you started else { return(new BoundingBox(new Vector3(f_newPos.Value.X, f_oldPos.Value.Y, f_newPos.Value.Z - m_mouseBBoffset), new Vector3(f_oldPos.Value.X, f_newPos.Value.Y, f_oldPos.Value.Z + m_mouseBBoffset))); } }//IF mouse cursor is to the right of where you started else { //If mouse cursor is below where you started if (f_newPos.Value.Y < f_oldPos.Value.Y) { return(new BoundingBox(new Vector3(f_oldPos.Value.X, f_newPos.Value.Y, f_oldPos.Value.Z - m_mouseBBoffset), new Vector3(f_newPos.Value.X, f_oldPos.Value.Y, f_newPos.Value.Z + m_mouseBBoffset))); }//If mouse cursor is above of where you started else { return(new BoundingBox(new Vector3(f_oldPos.Value.X, f_oldPos.Value.Y, f_oldPos.Value.Z - m_mouseBBoffset), new Vector3(f_newPos.Value.X, f_newPos.Value.Y, f_newPos.Value.Z + m_mouseBBoffset))); } } } } return(null); }
public ShaderEffect(GameAssets a_gameAssets, ref Matrix a_proj, ModelClasses.Map a_map) { m_effect = a_gameAssets.m_effect; m_effect.CurrentTechnique = m_effect.Techniques["tech_heightMap"]; m_effect.Parameters["LampColor"].SetValue(new Vector3(0.8f, 0.8f, 0.8f)); m_effect.Parameters["World"].SetValue(Matrix.CreateTranslation(a_map.m_mapWidth / 2, a_map.m_mapDepth / 2, 0)); //* Matrix.CreateRotationX(MathHelper.ToRadians(-90))); m_effect.Parameters["Projection"].SetValue(a_proj); m_effect.Parameters["heightMap"].SetValue(a_gameAssets.m_heightMap); m_effect.Parameters["sandMap"].SetValue(a_gameAssets.m_sandMap); m_effect.Parameters["grassMap"].SetValue(a_gameAssets.m_grassMap); m_effect.Parameters["rockMap"].SetValue(a_gameAssets.m_rockMap); m_effect.Parameters["snowMap"].SetValue(a_gameAssets.m_snowMap); m_effect.Parameters["maxHeight"].SetValue(ModelClasses.Map.m_maxHeight); m_effect.Parameters["textureSize"].SetValue(a_map.m_mapWidth); m_effect.Parameters["texelSize"].SetValue((float)1 / a_map.m_mapWidth); m_effect.Parameters["tileSize"].SetValue(ModelClasses.Map.m_tileSizeDivided); }
public void LoadHeightMap(GraphicsDevice a_graphics, ModelClasses.Map a_map) { float[] vec4Buffer = new float[a_map.m_mapWidth * a_map.m_mapDepth]; int i = 0; for (int y = 0; y < a_map.m_mapDepth; y++) { for (int x = 0; x < a_map.m_mapWidth; x++) { vec4Buffer[i] = (float)a_map.m_tiles[x, y].m_height / ModelClasses.Map.m_maxHeight; i++; } } m_heightMap = new Texture2D(a_graphics, a_map.m_mapWidth, a_map.m_mapDepth, false, SurfaceFormat.Single); m_heightMap.SetData(vec4Buffer); }
/// <summary> /// Returns world position by raytracing from nearplane to farplane and intersecting at a plane somewhere along the path /// </summary> /// <param name="a_device">Needed for unproject functions</param> /// <param name="a_plane">The plane that the ray intersects</param> /// <param name="a_camera">Needed to get all the matrixes</param> public Vector3?GetWorldPosition(GraphicsDevice a_device, Plane a_plane, ModelClasses.Map a_map, Camera a_camera, int a_mx, int a_my) { //If the mouse pos is inside the HUD it will return null if (a_my < HUD.m_area.Top) { Ray f_ray = GetWorldRay(a_device, a_camera, a_mx, a_my); foreach (ModelClasses.Tile tile in a_map.m_tiles) { m_intersection = f_ray.Intersects(tile.m_boundBox); if (m_intersection.HasValue) { //Uses formula Point = Raypos + (Raydir * distanceToPoint) to calculate the position Vector3 f_wpos = f_ray.Position + f_ray.Direction * m_intersection.Value; return(f_wpos); } } //This will only happen if it fails to intersect with the map tiles, like you click outside map Plane plane = a_plane; plane.D -= 2; m_intersection = f_ray.Intersects(plane); if (m_intersection.HasValue) { Vector3 f_wpos = f_ray.Position + f_ray.Direction * m_intersection.Value; int x = (int)f_wpos.X; int y = (int)f_wpos.Y; //If the position is outside of map left side, it will get 0 if (f_wpos.X < 0) { f_wpos.X = 0; x = 0; }//If position is outside map right side it will be max width of map else if (f_wpos.X >= a_map.m_mapWidth) { f_wpos.X = a_map.m_mapWidth - 0.01f; x = a_map.m_Xlength - 1; } //If the position is outside map (bottom) it will be set to minimum if (f_wpos.Y < 0) { f_wpos.Y = 0; y = 0; }//If outside on the top it will be set as maximum top else if (f_wpos.Y >= a_map.m_mapDepth) { f_wpos.Y = a_map.m_mapDepth - 0.01f; y = a_map.m_Ylength - 1; } f_wpos.Z = a_map.m_tiles[x, y].m_boundBox.Max.Z; return(f_wpos); } } return(null); }