Game1.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Game1.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Audio;
  14. using Microsoft.Xna.Framework.Content;
  15. using Microsoft.Xna.Framework.GamerServices;
  16. using Microsoft.Xna.Framework.Graphics;
  17. using Microsoft.Xna.Framework.Input;
  18. using Microsoft.Xna.Framework.Input.Touch;
  19. using Microsoft.Xna.Framework.Media;
  20. using CustomVertex;
  21. namespace UseCustomVertexWindows
  22. {
  23. /// <summary>
  24. /// This is the main type for your game
  25. /// </summary>
  26. public class Game1 : Microsoft.Xna.Framework.Game
  27. {
  28. BasicEffect basicEffect;
  29. Matrix worldMatrix;
  30. Matrix viewMatrix;
  31. Matrix projectionMatrix;
  32. const int number_of_vertices = 36;
  33. CustomVertex1[] cubeVertices;
  34. VertexBuffer vertexBuffer;
  35. GraphicsDeviceManager graphics;
  36. public Game1()
  37. {
  38. graphics = new GraphicsDeviceManager(this);
  39. Content.RootDirectory = "Content";
  40. }
  41. private void CreateVertexBuffer()
  42. {
  43. cubeVertices = new CustomVertex1[number_of_vertices];
  44. InitializeCube();
  45. vertexBuffer = new VertexBuffer(
  46. graphics.GraphicsDevice,
  47. typeof(CustomVertex1),
  48. number_of_vertices,
  49. BufferUsage.None
  50. );
  51. vertexBuffer.SetData<CustomVertex1>(cubeVertices);
  52. graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer);
  53. }
  54. /// <summary>
  55. /// This is called when the game should draw itself.
  56. /// </summary>
  57. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  58. protected override void Draw(GameTime gameTime)
  59. {
  60. graphics.GraphicsDevice.Clear(Color.SteelBlue);
  61. RasterizerState rasterizerState1 = new RasterizerState();
  62. rasterizerState1.CullMode = CullMode.None;
  63. graphics.GraphicsDevice.RasterizerState = rasterizerState1;
  64. foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
  65. {
  66. pass.Apply();
  67. graphics.GraphicsDevice.DrawPrimitives(
  68. PrimitiveType.TriangleList,
  69. 0, // start vertex
  70. 12 // number of primitives to draw
  71. );
  72. }
  73. }
  74. protected override void Initialize()
  75. {
  76. // TODO: Add your initialization logic here
  77. basicEffect = new BasicEffect(GraphicsDevice);
  78. CreateVertexBuffer();
  79. base.Initialize();
  80. }
  81. /// <summary>
  82. /// Initializes the vertices and indices of the 3D model.
  83. /// </summary>
  84. private void InitializeCube()
  85. {
  86. Vector3 LeftTopFront = new Vector3(-1.0f, 1.0f, 1.0f);
  87. Vector3 LeftBottomFront = new Vector3(-1.0f, -1.0f, 1.0f);
  88. Vector3 LeftTopBack = new Vector3(-1.0f, 1.0f, -1.0f);
  89. Vector3 LeftBottomBack = new Vector3(-1.0f, -1.0f, -1.0f);
  90. Vector3 RightTopFront = new Vector3(1.0f, 1.0f, 1.0f);
  91. Vector3 RightBottomFront = new Vector3(1.0f, -1.0f, 1.0f);
  92. Vector3 RightTopBack = new Vector3(1.0f, 1.0f, -1.0f);
  93. Vector3 RightBottomBack = new Vector3(1.0f, -1.0f, -1.0f);
  94. Vector2 textureLeftTop = new Vector2(0.0f, 0.0f);
  95. Vector2 textureLeftBottom = new Vector2(0.0f, 1.0f);
  96. Vector2 textureRightTop = new Vector2(1.0f, 0.0f);
  97. Vector2 textureRightBottom = new Vector2(1.0f, 1.0f);
  98. // Front face.
  99. cubeVertices[0] = new CustomVertex1(LeftTopFront, textureLeftTop);
  100. cubeVertices[1] = new CustomVertex1(LeftBottomFront, textureLeftBottom);
  101. cubeVertices[2] = new CustomVertex1(RightTopFront, textureRightTop);
  102. cubeVertices[3] = new CustomVertex1(LeftBottomFront, textureLeftBottom);
  103. cubeVertices[4] = new CustomVertex1(RightBottomFront, textureRightBottom);
  104. cubeVertices[5] = new CustomVertex1(RightTopFront, textureRightTop);
  105. // Back face.
  106. cubeVertices[6] = new CustomVertex1(LeftTopBack, textureRightTop);
  107. cubeVertices[7] = new CustomVertex1(RightTopBack, textureLeftTop);
  108. cubeVertices[8] = new CustomVertex1(LeftBottomBack, textureRightBottom);
  109. cubeVertices[9] = new CustomVertex1(LeftBottomBack, textureRightBottom);
  110. cubeVertices[10] = new CustomVertex1(RightTopBack, textureLeftTop);
  111. cubeVertices[11] = new CustomVertex1(RightBottomBack, textureLeftBottom);
  112. // Top face.
  113. cubeVertices[12] = new CustomVertex1(LeftTopFront, textureLeftBottom);
  114. cubeVertices[13] = new CustomVertex1(RightTopBack, textureRightTop);
  115. cubeVertices[14] = new CustomVertex1(LeftTopBack, textureLeftTop);
  116. cubeVertices[15] = new CustomVertex1(LeftTopFront, textureLeftBottom);
  117. cubeVertices[16] = new CustomVertex1(RightTopFront, textureRightBottom);
  118. cubeVertices[17] = new CustomVertex1(RightTopBack, textureRightTop);
  119. // Bottom face.
  120. cubeVertices[18] = new CustomVertex1(LeftBottomFront, textureLeftTop);
  121. cubeVertices[19] = new CustomVertex1(LeftBottomBack, textureLeftBottom);
  122. cubeVertices[20] = new CustomVertex1(RightBottomBack, textureRightBottom);
  123. cubeVertices[21] = new CustomVertex1(LeftBottomFront, textureLeftTop);
  124. cubeVertices[22] = new CustomVertex1(RightBottomBack, textureRightBottom);
  125. cubeVertices[23] = new CustomVertex1(RightBottomFront, textureRightTop);
  126. // Left face.
  127. cubeVertices[24] = new CustomVertex1(LeftTopFront, textureRightTop);
  128. cubeVertices[25] = new CustomVertex1(LeftBottomBack, textureLeftBottom);
  129. cubeVertices[26] = new CustomVertex1(LeftBottomFront, textureRightBottom);
  130. cubeVertices[27] = new CustomVertex1(LeftTopBack, textureLeftTop);
  131. cubeVertices[28] = new CustomVertex1(LeftBottomBack, textureLeftBottom);
  132. cubeVertices[29] = new CustomVertex1(LeftTopFront, textureRightTop);
  133. // Right face.
  134. cubeVertices[30] = new CustomVertex1(RightTopFront, textureLeftTop);
  135. cubeVertices[31] = new CustomVertex1(RightBottomFront, textureLeftBottom);
  136. cubeVertices[32] = new CustomVertex1(RightBottomBack, textureRightBottom);
  137. cubeVertices[33] = new CustomVertex1(RightTopBack, textureRightTop);
  138. cubeVertices[34] = new CustomVertex1(RightTopFront, textureLeftTop);
  139. cubeVertices[35] = new CustomVertex1(RightBottomBack, textureRightBottom);
  140. }
  141. /// <summary>
  142. /// Initializes the effect (loading, parameter setting,
  143. /// and technique selection) used for the 3D model.
  144. /// </summary>
  145. private void InitializeEffect()
  146. {
  147. Texture2D texture = Content.Load<Texture2D>("XNA_pow2");
  148. //Texture2D texture = Content.Load<Texture2D>("Glass2");
  149. basicEffect.World = worldMatrix;
  150. basicEffect.View = viewMatrix;
  151. basicEffect.Projection = projectionMatrix;
  152. basicEffect.Texture = texture;
  153. basicEffect.TextureEnabled = true;
  154. }
  155. /// <summary>
  156. /// Initializes the transforms used for the 3D model.
  157. /// </summary>
  158. private void InitializeTransform()
  159. {
  160. float tilt = (float)Math.PI / 8.0f;
  161. worldMatrix = Matrix.CreateRotationX(tilt) * Matrix.CreateRotationY(tilt);
  162. viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, 10),
  163. Vector3.Zero, Vector3.Up);
  164. projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
  165. (float)Math.PI / 4.0f, // 45 degrees, since 2 PI Radians is 360 degrees
  166. (float)GraphicsDevice.Viewport.Width /
  167. (float)GraphicsDevice.Viewport.Height,
  168. 1.0f, 100.0f);
  169. }
  170. /// <summary>
  171. /// LoadContent will be called once per game and is the place to load
  172. /// all of your content.
  173. /// </summary>
  174. protected override void LoadContent()
  175. {
  176. InitializeTransform();
  177. InitializeEffect();
  178. InitializeCube();
  179. }
  180. protected override void UnloadContent()
  181. {
  182. // TODO: Unload any non ContentManager content here
  183. }
  184. /// <summary>
  185. /// Allows the game to run logic such as updating the world,
  186. /// checking for collisions, gathering input, and playing audio.
  187. /// </summary>
  188. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  189. protected override void Update(GameTime gameTime)
  190. {
  191. // Allows the game to exit
  192. if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
  193. ButtonState.Pressed)
  194. this.Exit();
  195. #if WINDOWS || MONOMAC
  196. if (Keyboard.GetState().IsKeyDown(Keys.Escape))
  197. this.Exit();
  198. #endif
  199. // TODO: Add your update logic here
  200. base.Update(gameTime);
  201. }
  202. }
  203. }