Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*_________GAME1______________________________________________________________________________________________________________*/
- #region GAME1
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using GrafikaRuchoma_04a_Kamera_3d.CameraComponent;
- using GrafikaRuchoma_04a_Kamera_3d.CustomClasses;
- namespace GrafikaRuchoma_04a_Kamera_3d
- {
- public class Game1 : Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- KeyboardState prevState;
- public BasicEffect basicEffect;
- public Camera camera;
- Floor floor;
- Cube cube, cubeMoon, mars;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- Window.AllowUserResizing = false;
- graphics.PreferredBackBufferHeight = 720;
- graphics.PreferredBackBufferWidth = 1280;
- graphics.IsFullScreen = false;
- IsMouseVisible = true;
- }
- protected override void Initialize()
- {
- basicEffect = new BasicEffect(GraphicsDevice)
- {
- Alpha = 1,
- VertexColorEnabled = true,
- LightingEnabled = false,
- };
- camera = new Camera(this, new Vector3(0, 10, 0), Vector3.Zero, 5);
- Components.Add(camera);
- floor = new Floor(GraphicsDevice, 50, 50);
- cube = new Cube(this, new Vector3(0, 20, 0), new Vector3(0, 0, 0), new Vector3(4), 4, GraphicsDevice);
- Components.Add(cube);
- cubeMoon = new Cube(this, new Vector3(0, 10, 0), new Vector3(0, 0, 0), new Vector3(2), 6, GraphicsDevice);
- Components.Add(cubeMoon);
- mars = new Cube(this, new Vector3(0, 50, 0), new Vector3(0, 0, 0), new Vector3(2), 3, GraphicsDevice, Color.Red, Color.DarkRed);
- Components.Add(mars);
- base.Initialize();
- }
- protected override void LoadContent()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- }
- protected override void UnloadContent() { }
- protected override void Update(GameTime gameTime)
- {
- var keyboardState = Keyboard.GetState();
- var mouseState = Mouse.GetState();
- var mousePostion = new Point(mouseState.X, mouseState.Y);
- if (keyboardState.IsKeyDown(Keys.Escape))
- {
- Exit();
- }
- if (keyboardState.IsKeyDown(Keys.LeftShift))
- {
- camera.cameraSpeed = 25;
- }
- else if (prevState.IsKeyDown(Keys.LeftShift) && keyboardState.IsKeyUp(Keys.LeftShift))
- {
- camera.cameraSpeed = 5;
- }
- cubeMoon.rotationCenter = cube.GetActualPosition();
- base.Update(gameTime);
- prevState = keyboardState;
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- RasterizerState rasterizerState = new RasterizerState()
- {
- CullMode = CullMode.None
- };
- GraphicsDevice.RasterizerState = rasterizerState;
- var depthStencilState = new DepthStencilState
- {
- DepthBufferEnable = true
- };
- GraphicsDevice.DepthStencilState = depthStencilState;
- floor.Draw(camera, basicEffect);
- cube.Draw();
- cubeMoon.Draw();
- mars.Draw();
- base.Draw(gameTime);
- }
- }
- }
- #endregion
- /*_________CAMERA______________________________________________________________________________________________________________*/
- #region CAMERA
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using GrafikaRuchoma_04a_Kamera_3d.CustomClasses;
- namespace GrafikaRuchoma_04a_Kamera_3d.CameraComponent
- {
- public class Camera : GameComponent
- {
- private KeyboardState prevState;
- private Vector3 cameraPosition;
- public Vector3 cameraRotation;
- public float cameraSpeed { get; set; }
- public Vector3 cameraLookAt;
- private Vector3 mouseRotationBuffer;
- private MouseState currentMouseState;
- private MouseState prevMouseState;
- public BoundingBox ColliderBox{ get { return new BoundingBox(cameraPosition - new Vector3(0.3f), cameraPosition + new Vector3(0.3f)); } }
- public bool ShowCenterLine { get; set; }
- public Vector3 Position
- {
- get { return cameraPosition; }
- set
- {
- cameraPosition = value;
- UpdateLookAt();
- }
- }
- public Vector3 Rotation
- {
- get { return cameraRotation; }
- set
- {
- cameraRotation = value;
- UpdateLookAt();
- }
- }
- public Matrix Projection { get; protected set; }
- public Matrix View {get{return Matrix.CreateLookAt(cameraPosition, cameraLookAt, Vector3.Up); }}
- public Camera(Game game, Vector3 position, Vector3 rotation, float speed) : base(game)
- {
- cameraSpeed = speed;
- Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Game.GraphicsDevice.Viewport.AspectRatio, 0.05f, 1000);
- MoveTo(position, rotation);
- prevMouseState = Mouse.GetState();
- }
- private void MoveTo(Vector3 pos, Vector3 rot)
- {
- Position = pos;
- Rotation = rot;
- }
- private void UpdateLookAt()
- {
- var rotationMartix = Matrix.CreateRotationX(cameraRotation.X) * Matrix.CreateRotationY(cameraRotation.Y);
- var lookAtOffset = Vector3.Transform(Vector3.UnitZ, rotationMartix);
- cameraLookAt = cameraPosition + lookAtOffset;
- }
- private Vector3 PreviewMove(Vector3 amount)
- {
- var rotate = Matrix.CreateRotationY(cameraRotation.Y);
- var movment = new Vector3(amount.X, amount.Y, amount.Z);
- movment = Vector3.Transform(movment, rotate);
- return cameraPosition + movment;
- }
- public Vector3 cameraAxiesPosition
- {
- get
- {
- var rotate = Matrix.CreateRotationX(cameraRotation.X) * Matrix.CreateRotationY(cameraRotation.Y) * Matrix.CreateRotationZ(cameraRotation.Z);
- var movment = new Vector3(-0.3f, -0.15f, 0.5f);
- movment = Vector3.Transform(movment, rotate);
- return cameraPosition + movment;
- }
- }
- private void Move(Vector3 scale)
- {
- MoveTo(PreviewMove(scale), Rotation);
- }
- public override void Update(GameTime gameTime)
- {
- if (Game.IsActive)
- {
- currentMouseState = Mouse.GetState();
- var keyboardState = Keyboard.GetState();
- var dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
- var moveVector = Vector3.Zero;
- if (keyboardState.IsKeyDown(Keys.W))
- moveVector.Z = 1;
- if (keyboardState.IsKeyDown(Keys.S))
- moveVector.Z = -1;
- if (keyboardState.IsKeyDown(Keys.A))
- moveVector.X = 1;
- if (keyboardState.IsKeyDown(Keys.D))
- moveVector.X = -1;
- if (keyboardState.IsKeyDown(Keys.Space))
- moveVector.Y = 1;
- if (keyboardState.IsKeyDown(Keys.C))
- moveVector.Y = -1;
- if (keyboardState.IsKeyDown(Keys.P) && prevState.IsKeyUp(Keys.P))
- ShowCenterLine = !ShowCenterLine;
- if (moveVector != Vector3.Zero)
- {
- moveVector.Normalize();
- moveVector *= dt * cameraSpeed;
- Move(moveVector);
- }
- #region Camera Mouse
- if (currentMouseState != prevMouseState)
- {
- var deltaX = currentMouseState.X - (Game.GraphicsDevice.Viewport.Width / 2);
- var deltaY = currentMouseState.Y - (Game.GraphicsDevice.Viewport.Height / 2);
- mouseRotationBuffer.X -= 0.05F * deltaX * dt;
- mouseRotationBuffer.Y -= 0.05F * deltaY * dt;
- if (mouseRotationBuffer.Y < MathHelper.ToRadians(-75))
- mouseRotationBuffer.Y -= mouseRotationBuffer.Y - MathHelper.ToRadians(-75);
- if (mouseRotationBuffer.Y > MathHelper.ToRadians(75))
- mouseRotationBuffer.Y -= mouseRotationBuffer.Y - MathHelper.ToRadians(75);
- Rotation = new Vector3(
- -MathHelper.Clamp(mouseRotationBuffer.Y, MathHelper.ToRadians(-75f), MathHelper.ToRadians(75f)),
- MathHelper.WrapAngle(mouseRotationBuffer.X),
- 0);
- }
- Mouse.SetPosition(Game.GraphicsDevice.Viewport.Width / 2, Game.GraphicsDevice.Viewport.Height / 2);
- prevMouseState = currentMouseState;
- #endregion
- prevState = keyboardState;
- base.Update(gameTime);
- }
- }
- }
- }
- #endregion
- /*_________LINE______________________________________________________________________________________________________________*/
- #region LINE
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace GrafikaRuchoma_04a_Kamera_3d.CustomClasses
- {
- public class Line : IDisposable
- {
- public Vector3 startPoint;
- public Vector3 endPoint;
- private VertexPositionColor[] vertices;
- public Line(Vector3 startPoint, Vector3 endPoint)
- {
- this.startPoint = startPoint;
- this.endPoint = endPoint;
- }
- public Line() { }
- public void DrawLine(BasicEffect basicEffect, GraphicsDevice graphicsDevice)
- {
- basicEffect.CurrentTechnique.Passes[0].Apply();
- vertices = new[] { new VertexPositionColor(startPoint, Color.White), new VertexPositionColor(endPoint, Color.White) };
- graphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, vertices, 0, 1);
- }
- public void DrawLine(BasicEffect basicEffect, GraphicsDevice graphicsDevice, Vector3 start, Vector3 end)
- {
- basicEffect.CurrentTechnique.Passes[0].Apply();
- vertices = new[] { new VertexPositionColor(start, Color.White), new VertexPositionColor(end, Color.White) };
- graphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, vertices, 0, 1);
- }
- public void DrawLine(BasicEffect basicEffect, GraphicsDevice graphicsDevice, Vector3 start, Vector3 end, Color color)
- {
- basicEffect.CurrentTechnique.Passes[0].Apply();
- vertices = new[] { new VertexPositionColor(start, color), new VertexPositionColor(end, color) };
- graphicsDevice.DrawUserPrimitives(PrimitiveType.LineList, vertices, 0, 1);
- }
- public void Dispose()
- {
- throw new NotImplementedException();
- }
- }
- }
- #endregion
- /*_________FLOOR______________________________________________________________________________________________________________*/
- #region Floor
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using GrafikaRuchoma_04a_Kamera_3d.CameraComponent;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework;
- namespace GrafikaRuchoma_04a_Kamera_3d.CustomClasses
- {
- public class Floor
- {
- private int floorWidth;
- private int floorHeight;
- private VertexBuffer floorBuffer;
- private GraphicsDevice device;
- Color[] floorColors = new Color[2] { Color.Black, Color.White };
- public Floor(GraphicsDevice device, int width, int height)
- {
- this.device = device;
- floorWidth = width;
- floorHeight = height;
- BuildFloorBuffer();
- }
- private void BuildFloorBuffer()
- {
- List<VertexPositionColor> vertexList = new List<VertexPositionColor>();
- int counter = 0;
- for (int x = 0; x < floorWidth; x++)
- {
- counter++;
- for (int z = 0; z < floorHeight; z++)
- {
- counter++;
- foreach (var vertex in FloorTile(x, z, floorColors[counter % 2]))
- {
- vertexList.Add(vertex);
- }
- }
- }
- floorBuffer = new VertexBuffer(device, VertexPositionColor.VertexDeclaration, vertexList.Count, BufferUsage.None);
- floorBuffer.SetData(vertexList.ToArray());
- }
- private List<VertexPositionColor> FloorTile(int xOffset, int zOffset, Color tileColor)
- {
- var vList = new List<VertexPositionColor>()
- {
- new VertexPositionColor(new Vector3(0 + xOffset, 0, 0 + zOffset), tileColor),
- new VertexPositionColor(new Vector3(1 + xOffset, 0, 0 + zOffset), tileColor),
- new VertexPositionColor(new Vector3(0 + xOffset, 0, 1 + zOffset), tileColor),
- new VertexPositionColor(new Vector3(1 + xOffset, 0, 0 + zOffset), tileColor),
- new VertexPositionColor(new Vector3(1 + xOffset, 0, 1 + zOffset), tileColor),
- new VertexPositionColor(new Vector3(0 + xOffset, 0, 1 + zOffset), tileColor),
- };
- return vList;
- }
- public void Draw(Camera camera, BasicEffect effect)
- {
- effect.VertexColorEnabled = true;
- effect.View = camera.View;
- effect.Projection = camera.Projection;
- effect.World = Matrix.Identity;
- foreach (var pass in effect.CurrentTechnique.Passes)
- {
- pass.Apply();
- device.SetVertexBuffer(floorBuffer);
- device.DrawPrimitives(PrimitiveType.TriangleList, 0, floorBuffer.VertexCount / 3);
- }
- }
- }
- }
- #endregion
- /*_________CUBE______________________________________________________________________________________________________________*/
- #region CUBE
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using GrafikaRuchoma_04a_Kamera_3d.CameraComponent;
- namespace GrafikaRuchoma_04a_Kamera_3d.CustomClasses
- {
- public class Cube : GameComponent
- {
- public Vector3 middleLocation;
- VertexPositionColor[] planetVertices;
- GraphicsDevice graphicsDevice;
- float speed;
- Vector3 size;
- Vector3 angle;
- Game1 game;
- public Vector3 rotationCenter;
- Camera camera;
- private Matrix World;
- Vector3 vectorZero = Vector3.Zero;
- public List<Color> Colors;
- public Cube(Game game, Vector3 middleLocation, Vector3 rotationCenter, Vector3 size, float speed, GraphicsDevice graphicsDevice, params Color[] colors)
- : base(game)
- {
- Colors = colors.ToList();
- if (!Colors.Any())
- {
- Colors = new List<Color>() { Color.White, Color.Black, Color.White, Color.Black };
- }
- this.game = game as Game1;
- this.camera = this.game.camera;
- this.middleLocation = middleLocation;
- angle = Vector3.Zero;
- this.size = size;
- this.speed = speed;
- this.rotationCenter = rotationCenter;
- this.graphicsDevice = graphicsDevice;
- this.basicEffect = new BasicEffect(graphicsDevice)
- {
- Alpha = 1,
- VertexColorEnabled = true,
- LightingEnabled = false,
- };
- planetVertices = CubeVerticles();
- }
- public VertexPositionColor[] GetPlanetVertices()
- {
- return this.planetVertices;
- }
- public Vector3 GetActualPosition()
- {
- return Vector3.Transform(middleLocation, World);
- }
- public override void Update(GameTime gameTime)
- {
- angle.X += speed;
- World = Matrix.Identity;
- World *= Matrix.CreateRotationX(MathHelper.ToRadians(angle.X));
- World *= Matrix.CreateRotationY(MathHelper.ToRadians(angle.Y));
- World *= Matrix.CreateRotationZ(MathHelper.ToRadians(angle.Z));
- World *= Matrix.CreateTranslation(rotationCenter);
- basicEffect.World = World;
- basicEffect.View = camera.View;
- basicEffect.Projection = camera.Projection;
- }
- private VertexPositionColor[] CubeVerticles()
- {
- Vector3 topLeftFront = new Vector3(-size.X + middleLocation.X, size.Y + middleLocation.Y, size.Z + middleLocation.Z);
- Vector3 bottomLeftFront = new Vector3(-size.X + middleLocation.X, -size.Y + middleLocation.Y, size.Z + middleLocation.Z);
- Vector3 topRightFront = new Vector3(size.X + middleLocation.X, size.Y + middleLocation.Y, size.Z + middleLocation.Z);
- Vector3 bottomRightFront = new Vector3(size.X + middleLocation.X, -size.Y + middleLocation.Y, size.Z + middleLocation.Z);
- Vector3 topLeftBack = new Vector3(-size.X + middleLocation.X, size.Y + middleLocation.Y, -size.Z + middleLocation.Z);
- Vector3 topRightBack = new Vector3(size.X + middleLocation.X, size.Y + middleLocation.Y, -size.Z + middleLocation.Z);
- Vector3 bottomLeftBack = new Vector3(-size.X + middleLocation.X, -size.Y + middleLocation.Y, -size.Z + middleLocation.Z);
- Vector3 bottomRightBack = new Vector3(size.X + middleLocation.X, -size.Y + middleLocation.Y, -size.Z + middleLocation.Z);
- Random rnd = new Random();
- VertexPositionColor[] verticles = new VertexPositionColor[8]{
- new VertexPositionColor(topLeftFront, Colors[rnd.Next(0,Colors.Count())]),//1
- new VertexPositionColor(bottomLeftFront, Colors[rnd.Next(0,Colors.Count())]),//1
- new VertexPositionColor(topRightFront, Colors[rnd.Next(0,Colors.Count())]),//2
- new VertexPositionColor(bottomRightFront, Colors[rnd.Next(0,Colors.Count())]),//3
- new VertexPositionColor(topLeftBack, Colors[rnd.Next(0,Colors.Count())]),//4
- new VertexPositionColor(topRightBack, Colors[rnd.Next(0,Colors.Count())]),//5
- new VertexPositionColor(bottomLeftBack, Colors[rnd.Next(0,Colors.Count())]),//6
- new VertexPositionColor(bottomRightBack, Colors[rnd.Next(0,Colors.Count())])//7
- };
- List<VertexPositionColor> cubeVertices = new List<VertexPositionColor>();
- //Front
- cubeVertices.Add(verticles[2]); cubeVertices.Add(verticles[3]); cubeVertices.Add(verticles[0]);
- cubeVertices.Add(verticles[3]); cubeVertices.Add(verticles[1]); cubeVertices.Add(verticles[0]);
- //Left
- cubeVertices.Add(verticles[0]); cubeVertices.Add(verticles[1]); cubeVertices.Add(verticles[4]);
- cubeVertices.Add(verticles[1]); cubeVertices.Add(verticles[6]); cubeVertices.Add(verticles[4]);
- //Back
- cubeVertices.Add(verticles[4]); cubeVertices.Add(verticles[6]); cubeVertices.Add(verticles[7]);
- cubeVertices.Add(verticles[5]); cubeVertices.Add(verticles[4]); cubeVertices.Add(verticles[7]);
- //Right
- cubeVertices.Add(verticles[5]); cubeVertices.Add(verticles[7]); cubeVertices.Add(verticles[2]);
- cubeVertices.Add(verticles[7]); cubeVertices.Add(verticles[3]); cubeVertices.Add(verticles[2]);
- //Top
- cubeVertices.Add(verticles[2]); cubeVertices.Add(verticles[0]); cubeVertices.Add(verticles[5]);
- cubeVertices.Add(verticles[5]); cubeVertices.Add(verticles[0]); cubeVertices.Add(verticles[4]);
- //Bottom
- cubeVertices.Add(verticles[1]); cubeVertices.Add(verticles[3]); cubeVertices.Add(verticles[6]);
- cubeVertices.Add(verticles[6]); cubeVertices.Add(verticles[3]); cubeVertices.Add(verticles[7]);
- return cubeVertices.ToArray();
- }
- public void Draw()
- {
- basicEffect.CurrentTechnique.Passes[0].Apply();
- this.graphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, planetVertices, 0, planetVertices.Length / 3);
- }
- public BasicEffect basicEffect { get; set; }
- }
- }
- #endregion
- /*_______________________________________________________________________________________________________________________*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement