Advertisement
Guest User

Gr

a guest
Nov 19th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.24 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Graphics;
  3. using Microsoft.Xna.Framework.Input;
  4.  
  5. namespace _3D_Trening
  6. {
  7. /// <summary>
  8. /// This is the main type for your game.
  9. /// </summary>
  10. public class Game1 : Game
  11. {
  12. GraphicsDeviceManager graphics;
  13. SpriteBatch spriteBatch;
  14.  
  15. Texture2D Background;
  16.  
  17. Matrix ViewMatrix, WorldMatrix, ProjectionMatrix;
  18.  
  19. BasicEffect BasicEffect;
  20. VertexPositionColor[] geometryLines;
  21. VertexPositionColor[] geometryPlanetSun;
  22. VertexPositionColor[] geometryPlanetBlue;
  23. VertexPositionColor[] geometryPlanetRed;
  24. VertexPositionColor[] geometryPlanetGreen;
  25.  
  26. float Anglex = 0.0f, Angley = 0.0f,Anglez= 0.0f;
  27. float near, far;
  28.  
  29. public Game1()
  30. {
  31. graphics = new GraphicsDeviceManager(this);
  32. Content.RootDirectory = "Content";
  33. }
  34.  
  35. /// <summary>
  36. /// Allows the game to perform any initialization it needs to before starting to run.
  37. /// This is where it can query for any required services and load any non-graphic
  38. /// related content. Calling base.Initialize will enumerate through any components
  39. /// and initialize them as well.
  40. /// </summary>
  41. protected override void Initialize()
  42. {
  43. // TODO: Add your initialization logic here
  44. this.Window.AllowUserResizing = true;
  45. this.IsMouseVisible = true;
  46. base.Initialize();
  47. }
  48.  
  49. /// <summary>
  50. /// LoadContent will be called once per game and is the place to load
  51. /// all of your content.
  52. /// </summary>
  53. protected override void LoadContent()
  54. {
  55. // Create a new SpriteBatch, which can be used to draw textures.
  56. spriteBatch = new SpriteBatch(GraphicsDevice);
  57.  
  58. BasicEffect = new BasicEffect(GraphicsDevice);
  59. BasicEffect.VertexColorEnabled = true;
  60.  
  61. geometryLines = new VertexPositionColor[2];
  62. geometryPlanetSun = new VertexPositionColor[24];
  63. geometryPlanetBlue = new VertexPositionColor[24];
  64. geometryPlanetRed = new VertexPositionColor[24];
  65. geometryPlanetGreen = new VertexPositionColor[24];
  66.  
  67. geometryLines[0] = new VertexPositionColor(new Vector3(20, 0, 1), Color.Red);
  68. geometryLines[1] = new VertexPositionColor(new Vector3(-20, 0, 1), Color.Red);
  69.  
  70.  
  71. #region Sun
  72. geometryPlanetSun[0] = new VertexPositionColor(new Vector3(-1, -1, 1), Color.Yellow);
  73. geometryPlanetSun[1] = new VertexPositionColor(new Vector3(-1, 1, 1), Color.Yellow);
  74. geometryPlanetSun[2] = new VertexPositionColor(new Vector3(1, -1, 1), Color.Yellow);
  75.  
  76. geometryPlanetSun[3] = new VertexPositionColor(new Vector3(-1, 1, 1), Color.Yellow);
  77. geometryPlanetSun[4] = new VertexPositionColor(new Vector3(1, 1, 1), Color.Yellow);
  78. geometryPlanetSun[5] = new VertexPositionColor(new Vector3(1, -1, 1), Color.Yellow);
  79.  
  80. geometryPlanetSun[6] = new VertexPositionColor(new Vector3(1, -1, 1), Color.Yellow);
  81. geometryPlanetSun[7] = new VertexPositionColor(new Vector3(1, 1, 1), Color.Yellow);
  82. geometryPlanetSun[8] = new VertexPositionColor(new Vector3(1, 1, -1), Color.Yellow);
  83.  
  84.  
  85. geometryPlanetSun[9] = new VertexPositionColor(new Vector3(1, -1, -1), Color.Yellow);
  86. geometryPlanetSun[10] = new VertexPositionColor(new Vector3(1, -1, 1), Color.Yellow);
  87. geometryPlanetSun[11] = new VertexPositionColor(new Vector3(1, 1, -1), Color.Yellow);
  88.  
  89.  
  90. geometryPlanetSun[13] = new VertexPositionColor(new Vector3(-1, 1, -1), Color.Yellow);
  91. geometryPlanetSun[12] = new VertexPositionColor(new Vector3(1, 1, -1), Color.Yellow);
  92. geometryPlanetSun[14] = new VertexPositionColor(new Vector3(-1, -1, -1), Color.Yellow);
  93.  
  94. geometryPlanetSun[16] = new VertexPositionColor(new Vector3(1, 1, -1), Color.Yellow);
  95. geometryPlanetSun[15] = new VertexPositionColor(new Vector3(1, -1, -1), Color.Yellow);
  96. geometryPlanetSun[17] = new VertexPositionColor(new Vector3(-1, -1, -1), Color.Yellow);
  97.  
  98. geometryPlanetSun[18] = new VertexPositionColor(new Vector3(-1, 1, -1), Color.Yellow);
  99. geometryPlanetSun[19] = new VertexPositionColor(new Vector3(-1, 1, 1), Color.Yellow);
  100. geometryPlanetSun[20] = new VertexPositionColor(new Vector3(-1, -1, 1), Color.Yellow);
  101.  
  102. geometryPlanetSun[22] = new VertexPositionColor(new Vector3(-1, 1, -1), Color.Yellow);
  103. geometryPlanetSun[21] = new VertexPositionColor(new Vector3(-1, -1, -1), Color.Yellow);
  104. geometryPlanetSun[23] = new VertexPositionColor(new Vector3(-1, -1, 1), Color.Yellow);
  105.  
  106. #endregion
  107.  
  108. #region BluePlanet
  109. //- 4 = -1 , -2 = 1
  110.  
  111. geometryPlanetBlue[0] = new VertexPositionColor(new Vector3(-4, -4, -2), Color.Red);
  112. geometryPlanetBlue[1] = new VertexPositionColor(new Vector3(-4, -2, -2), Color.Red);
  113. geometryPlanetBlue[2] = new VertexPositionColor(new Vector3(-2, -4, -2), Color.Red);
  114.  
  115. geometryPlanetBlue[3] = new VertexPositionColor(new Vector3(-4, -2, -2), Color.Red);
  116. geometryPlanetBlue[4] = new VertexPositionColor(new Vector3(-2, -2, -2), Color.Red);
  117. geometryPlanetBlue[5] = new VertexPositionColor(new Vector3(-2, -4, -2), Color.Red);
  118.  
  119.  
  120. geometryPlanetBlue[6] = new VertexPositionColor(new Vector3(-2, -4, -2),Color.Blue);
  121. geometryPlanetBlue[7] = new VertexPositionColor(new Vector3(-2, -2, -2), Color.Blue);
  122. geometryPlanetBlue[8] = new VertexPositionColor(new Vector3(-2, -2, -4), Color.Blue);
  123.  
  124. geometryPlanetBlue[9] = new VertexPositionColor(new Vector3(-2, -4, -4), Color.Blue);
  125. geometryPlanetBlue[10] = new VertexPositionColor(new Vector3(-2, -4, -2), Color.Blue);
  126. geometryPlanetBlue[11] = new VertexPositionColor(new Vector3(-2, -2, -4), Color.Blue);
  127.  
  128.  
  129. geometryPlanetBlue[12] = new VertexPositionColor(new Vector3(-2, -2, -4), Color.Green);
  130. geometryPlanetBlue[13] = new VertexPositionColor(new Vector3(-4, -2, -4), Color.Green);
  131. geometryPlanetBlue[14] = new VertexPositionColor(new Vector3(-4, -4, -4), Color.Green);
  132.  
  133. geometryPlanetBlue[15] = new VertexPositionColor(new Vector3(-2, -4, -4), Color.Green);
  134. geometryPlanetBlue[16] = new VertexPositionColor(new Vector3(-2, -2, -4), Color.Green);
  135. geometryPlanetBlue[17] = new VertexPositionColor(new Vector3(-4, -4, -4), Color.Green);
  136.  
  137. geometryPlanetBlue[18] = new VertexPositionColor(new Vector3(-4, -2, -4), Color.Purple);
  138. geometryPlanetBlue[19] = new VertexPositionColor(new Vector3(-4, -2, -2), Color.Purple);
  139. geometryPlanetBlue[20] = new VertexPositionColor(new Vector3(-4, -4, -2), Color.Purple);
  140.  
  141. geometryPlanetBlue[21] = new VertexPositionColor(new Vector3(-4, -4, -4), Color.Purple);
  142. geometryPlanetBlue[22] = new VertexPositionColor(new Vector3(-4, -2, -4), Color.Purple);
  143. geometryPlanetBlue[23] = new VertexPositionColor(new Vector3(-4, -4, -2), Color.Purple);
  144.  
  145. #endregion
  146.  
  147. #region RedPlanet
  148. // 2 - -1 , 4 - 1
  149.  
  150. geometryPlanetRed[0] = new VertexPositionColor(new Vector3(2, 2, 4), Color.Red);
  151. geometryPlanetRed[1] = new VertexPositionColor(new Vector3(2, 4, 4), Color.Red);
  152. geometryPlanetRed[2] = new VertexPositionColor(new Vector3(4, 2, 4), Color.Red);
  153.  
  154. geometryPlanetRed[3] = new VertexPositionColor(new Vector3(2, 4, 4), Color.Red);
  155. geometryPlanetRed[4] = new VertexPositionColor(new Vector3(4, 4, 4), Color.Red);
  156. geometryPlanetRed[5] = new VertexPositionColor(new Vector3(4, 2, 4), Color.Red);
  157.  
  158. geometryPlanetRed[6] = new VertexPositionColor(new Vector3(4, 2, 4), Color.Blue);
  159. geometryPlanetRed[7] = new VertexPositionColor(new Vector3(4, 4, 4), Color.Blue);
  160. geometryPlanetRed[8] = new VertexPositionColor(new Vector3(4, 4, 2), Color.Blue);
  161.  
  162.  
  163. geometryPlanetRed[9] = new VertexPositionColor(new Vector3(4, 2, 2), Color.Blue);
  164. geometryPlanetRed[10] = new VertexPositionColor(new Vector3(4, 2, 4), Color.Blue);
  165. geometryPlanetRed[11] = new VertexPositionColor(new Vector3(4, 4, 2), Color.Blue);
  166.  
  167. geometryPlanetRed[12] = new VertexPositionColor(new Vector3(4, 4, 2), Color.Green);
  168. geometryPlanetRed[13] = new VertexPositionColor(new Vector3(2, 4, 2), Color.Green);
  169. geometryPlanetRed[14] = new VertexPositionColor(new Vector3(2, 2, 2), Color.Green);
  170.  
  171. geometryPlanetRed[15] = new VertexPositionColor(new Vector3(4, 2, 2), Color.Green);
  172. geometryPlanetRed[16] = new VertexPositionColor(new Vector3(4, 4, 2), Color.Green);
  173. geometryPlanetRed[17] = new VertexPositionColor(new Vector3(2, 2, 2), Color.Green);
  174.  
  175.  
  176. geometryPlanetRed[18] = new VertexPositionColor(new Vector3(2, 4, 2), Color.Purple);
  177. geometryPlanetRed[19] = new VertexPositionColor(new Vector3(2, 4, 4), Color.Purple);
  178. geometryPlanetRed[20] = new VertexPositionColor(new Vector3(2, 2, 4), Color.Purple);
  179.  
  180. geometryPlanetRed[22] = new VertexPositionColor(new Vector3(2, 4, 2), Color.Purple);
  181. geometryPlanetRed[21] = new VertexPositionColor(new Vector3(2, 2, 2), Color.Purple);
  182. geometryPlanetRed[23] = new VertexPositionColor(new Vector3(2, 2, 4), Color.Purple);
  183.  
  184.  
  185. #endregion
  186.  
  187. #region GreenPlanet
  188.  
  189. // 1 - -1 , 3 - 1
  190.  
  191. geometryPlanetGreen[0] = new VertexPositionColor(new Vector3(1, 1, 3), Color.Red);
  192. geometryPlanetGreen[1] = new VertexPositionColor(new Vector3(1, 3, 3), Color.Red);
  193. geometryPlanetGreen[2] = new VertexPositionColor(new Vector3(3, 1, 3), Color.Red);
  194.  
  195. geometryPlanetGreen[3] = new VertexPositionColor(new Vector3(1, 3, 3), Color.Red);
  196. geometryPlanetGreen[4] = new VertexPositionColor(new Vector3(3, 3, 3), Color.Red);
  197. geometryPlanetGreen[5] = new VertexPositionColor(new Vector3(3, 1, 3), Color.Red);
  198.  
  199. geometryPlanetGreen[6] = new VertexPositionColor(new Vector3(3, 1, 3), Color.Blue);
  200. geometryPlanetGreen[7] = new VertexPositionColor(new Vector3(3, 3, 3), Color.Blue);
  201. geometryPlanetGreen[8] = new VertexPositionColor(new Vector3(3, 3, 1), Color.Blue);
  202.  
  203.  
  204. geometryPlanetGreen[9] = new VertexPositionColor(new Vector3(3, 1, 1), Color.Blue);
  205. geometryPlanetGreen[10] = new VertexPositionColor(new Vector3(3, 1, 3), Color.Blue);
  206. geometryPlanetGreen[11] = new VertexPositionColor(new Vector3(3, 3, 1), Color.Blue);
  207.  
  208. geometryPlanetGreen[12] = new VertexPositionColor(new Vector3(3, 3, 1), Color.Green);
  209. geometryPlanetGreen[13] = new VertexPositionColor(new Vector3(1, 3, 1), Color.Green);
  210. geometryPlanetGreen[14] = new VertexPositionColor(new Vector3(1, 1, 1), Color.Green);
  211.  
  212. geometryPlanetGreen[15] = new VertexPositionColor(new Vector3(3, 1, 1), Color.Green);
  213. geometryPlanetGreen[16] = new VertexPositionColor(new Vector3(3, 3, 1), Color.Green);
  214. geometryPlanetGreen[17] = new VertexPositionColor(new Vector3(1, 1, 1), Color.Green);
  215.  
  216.  
  217. geometryPlanetGreen[18] = new VertexPositionColor(new Vector3(1, 3, 1), Color.Purple);
  218. geometryPlanetGreen[19] = new VertexPositionColor(new Vector3(1, 3, 3), Color.Purple);
  219. geometryPlanetGreen[20] = new VertexPositionColor(new Vector3(1, 1, 3), Color.Purple);
  220.  
  221.  
  222.  
  223. #endregion
  224.  
  225. Background = Content.Load<Texture2D>("kosmos");
  226.  
  227. // TODO: use this.Content to load your game content here
  228. }
  229.  
  230. /// <summary>
  231. /// UnloadContent will be called once per game and is the place to unload
  232. /// game-specific content.
  233. /// </summary>
  234. protected override void UnloadContent()
  235. {
  236. // TODO: Unload any non ContentManager content here
  237. }
  238.  
  239. /// <summary>
  240. /// Allows the game to run logic such as updating the world,
  241. /// checking for collisions, gathering input, and playing audio.
  242. /// </summary>
  243. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  244. protected override void Update(GameTime gameTime)
  245. {
  246. var keyboard = Keyboard.GetState(); // zmienna pomocnicza
  247. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  248. Exit();
  249.  
  250. // TODO: Add your update logic here
  251.  
  252. if (keyboard.IsKeyDown(Keys.Right)) Angley += 0.02f;
  253. if (keyboard.IsKeyDown(Keys.Left)) Angley -= 0.02f;
  254. if (keyboard.IsKeyDown(Keys.Up)) Anglex += 0.02f;
  255. if (keyboard.IsKeyDown(Keys.Down)) Anglex -= 0.02f;
  256.  
  257. if (keyboard.IsKeyDown(Keys.Z)) Anglez += 0.2f;
  258. if (keyboard.IsKeyDown(Keys.P)) Anglez -= 0.2f;
  259.  
  260. WorldMatrix = Matrix.Identity;
  261. ViewMatrix = Matrix.CreateLookAt
  262. (
  263. new Vector3(0.0f, 0.0f, 20.0f),
  264. Vector3.Zero, Vector3.Up
  265. );
  266.  
  267. ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(50)
  268. , GraphicsDevice.Viewport.AspectRatio, 0.01f, 1000.0f);
  269.  
  270. ViewMatrix = Matrix.CreateRotationX(Anglex) *
  271. Matrix.CreateRotationY(Angley) * Matrix.CreateRotationZ(Anglez) * ViewMatrix;
  272.  
  273. BasicEffect.View = ViewMatrix;
  274. BasicEffect.World = WorldMatrix;
  275. BasicEffect.Projection = ProjectionMatrix;
  276.  
  277.  
  278. base.Update(gameTime);
  279. }
  280.  
  281. /// <summary>
  282. /// This is called when the game should draw itself.
  283. /// </summary>
  284. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  285. protected override void Draw(GameTime gameTime)
  286. {
  287. GraphicsDevice.Clear(Color.CornflowerBlue);
  288.  
  289. spriteBatch.Begin();
  290. spriteBatch.Draw(Background, GraphicsDevice.Viewport.TitleSafeArea, Color.White);
  291. spriteBatch.End();
  292.  
  293. BasicEffect.CurrentTechnique.Passes[0].Apply();
  294.  
  295. GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, geometryPlanetSun, 0, 8);
  296. GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, geometryPlanetBlue, 0, 8);
  297. GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, geometryPlanetRed, 0, 8);
  298. // GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, geometryPlanetGreen, 0, 8);
  299. GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, geometryLines, 0, 1);
  300.  
  301.  
  302. // TODO: Add your drawing code here
  303.  
  304.  
  305.  
  306. base.Draw(gameTime);
  307. }
  308. }
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement