Game1.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Content;
  6. using Microsoft.Xna.Framework.Graphics;
  7. using Microsoft.Xna.Framework.Input;
  8. namespace Colored3DCube
  9. {
  10. public class Game1 : Game
  11. {
  12. GraphicsDeviceManager graphics;
  13. KeyboardState currentKeys;
  14. BasicEffect basicEffect;
  15. Matrix worldMatrix, viewMatrix, projectionMatrix;
  16. public Game1 ()
  17. {
  18. graphics = new GraphicsDeviceManager (this);
  19. Content.RootDirectory = "Content";
  20. }
  21. protected override void Initialize ()
  22. {
  23. base.Initialize ();
  24. }
  25. protected override void LoadContent ()
  26. {
  27. // setup our graphics scene matrices
  28. worldMatrix = Matrix.Identity;
  29. viewMatrix = Matrix.CreateLookAt (new Vector3 (0, 0, 5), Vector3.Zero, Vector3.Up);
  30. projectionMatrix = Matrix.CreatePerspectiveFieldOfView (MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 1, 10);
  31. // Setup our basic effect
  32. basicEffect = new BasicEffect (GraphicsDevice);
  33. basicEffect.World = worldMatrix;
  34. basicEffect.View = viewMatrix;
  35. basicEffect.Projection = projectionMatrix;
  36. basicEffect.VertexColorEnabled = true;
  37. CreateCubeVertexBuffer ();
  38. CreateCubeIndexBuffer ();
  39. }
  40. protected override void UnloadContent ()
  41. {
  42. }
  43. protected override void Update (GameTime gameTime)
  44. {
  45. currentKeys = Keyboard.GetState ();
  46. //Press Esc To Exit
  47. if (currentKeys.IsKeyDown (Keys.Escape))
  48. this.Exit ();
  49. //Press Directional Keys to rotate cube
  50. if (currentKeys.IsKeyDown (Keys.Up))
  51. worldMatrix *= Matrix.CreateRotationX (-0.05f);
  52. if (currentKeys.IsKeyDown (Keys.Down))
  53. worldMatrix *= Matrix.CreateRotationX (0.05f);
  54. if (currentKeys.IsKeyDown (Keys.Left))
  55. worldMatrix *= Matrix.CreateRotationY (-0.05f);
  56. if (currentKeys.IsKeyDown (Keys.Right))
  57. worldMatrix *= Matrix.CreateRotationY (0.05f);
  58. base.Update (gameTime);
  59. }
  60. protected override void Draw (GameTime gameTime)
  61. {
  62. GraphicsDevice.Clear (Color.CornflowerBlue);
  63. GraphicsDevice.SetVertexBuffer (vertices);
  64. GraphicsDevice.Indices = indices;
  65. //RasterizerState rasterizerState1 = new RasterizerState ();
  66. //rasterizerState1.CullMode = CullMode.None;
  67. //graphics.GraphicsDevice.RasterizerState = rasterizerState1;
  68. basicEffect.World = worldMatrix;
  69. foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes) {
  70. pass.Apply ();
  71. GraphicsDevice.DrawIndexedPrimitives (PrimitiveType.TriangleList, 0, 0,
  72. number_of_vertices, 0, number_of_indices / 3);
  73. }
  74. base.Draw (gameTime);
  75. }
  76. const int number_of_vertices = 8;
  77. const int number_of_indices = 36;
  78. VertexBuffer vertices;
  79. void CreateCubeVertexBuffer ()
  80. {
  81. VertexPositionColor[] cubeVertices = new VertexPositionColor[number_of_vertices];
  82. cubeVertices [0].Position = new Vector3 (-1, -1, -1);
  83. cubeVertices [1].Position = new Vector3 (-1, -1, 1);
  84. cubeVertices [2].Position = new Vector3 (1, -1, 1);
  85. cubeVertices [3].Position = new Vector3 (1, -1, -1);
  86. cubeVertices [4].Position = new Vector3 (-1, 1, -1);
  87. cubeVertices [5].Position = new Vector3 (-1, 1, 1);
  88. cubeVertices [6].Position = new Vector3 (1, 1, 1);
  89. cubeVertices [7].Position = new Vector3 (1, 1, -1);
  90. cubeVertices [0].Color = Color.Black;
  91. cubeVertices [1].Color = Color.Red;
  92. cubeVertices [2].Color = Color.Yellow;
  93. cubeVertices [3].Color = Color.Green;
  94. cubeVertices [4].Color = Color.Blue;
  95. cubeVertices [5].Color = Color.Magenta;
  96. cubeVertices [6].Color = Color.White;
  97. cubeVertices [7].Color = Color.Cyan;
  98. vertices = new VertexBuffer (GraphicsDevice, VertexPositionColor.VertexDeclaration, number_of_vertices, BufferUsage.WriteOnly);
  99. vertices.SetData<VertexPositionColor> (cubeVertices);
  100. }
  101. IndexBuffer indices;
  102. void CreateCubeIndexBuffer ()
  103. {
  104. UInt16[] cubeIndices = new UInt16[number_of_indices];
  105. //bottom face
  106. cubeIndices [0] = 0;
  107. cubeIndices [1] = 2;
  108. cubeIndices [2] = 3;
  109. cubeIndices [3] = 0;
  110. cubeIndices [4] = 1;
  111. cubeIndices [5] = 2;
  112. //top face
  113. cubeIndices [6] = 4;
  114. cubeIndices [7] = 6;
  115. cubeIndices [8] = 5;
  116. cubeIndices [9] = 4;
  117. cubeIndices [10] = 7;
  118. cubeIndices [11] = 6;
  119. //front face
  120. cubeIndices [12] = 5;
  121. cubeIndices [13] = 2;
  122. cubeIndices [14] = 1;
  123. cubeIndices [15] = 5;
  124. cubeIndices [16] = 6;
  125. cubeIndices [17] = 2;
  126. //back face
  127. cubeIndices [18] = 0;
  128. cubeIndices [19] = 7;
  129. cubeIndices [20] = 4;
  130. cubeIndices [21] = 0;
  131. cubeIndices [22] = 3;
  132. cubeIndices [23] = 7;
  133. //left face
  134. cubeIndices [24] = 0;
  135. cubeIndices [25] = 4;
  136. cubeIndices [26] = 1;
  137. cubeIndices [27] = 1;
  138. cubeIndices [28] = 4;
  139. cubeIndices [29] = 5;
  140. //right face
  141. cubeIndices [30] = 2;
  142. cubeIndices [31] = 6;
  143. cubeIndices [32] = 3;
  144. cubeIndices [33] = 3;
  145. cubeIndices [34] = 6;
  146. cubeIndices [35] = 7;
  147. indices = new IndexBuffer (GraphicsDevice, IndexElementSize.SixteenBits, number_of_indices, BufferUsage.WriteOnly);
  148. indices.SetData<UInt16> (cubeIndices);
  149. }
  150. }
  151. }