UseCustomVertexGame.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //-----------------------------------------------------------------------------
  2. // Game1.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using Microsoft.Xna.Framework;
  11. using Microsoft.Xna.Framework.Audio;
  12. using Microsoft.Xna.Framework.Content;
  13. //using Microsoft.Xna.Framework.GamerServices;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework.Input;
  16. using Microsoft.Xna.Framework.Input.Touch;
  17. using Microsoft.Xna.Framework.Media;
  18. namespace UseCustomVertex
  19. {
  20. /// <summary>
  21. /// This is the main type for your game
  22. /// </summary>
  23. public class UseCustomVertexGame : Game
  24. {
  25. BasicEffect basicEffect;
  26. Matrix worldMatrix;
  27. Matrix viewMatrix;
  28. Matrix projectionMatrix;
  29. const int number_of_vertices = 36;
  30. CustomVertex[] cubeVertices;
  31. VertexBuffer vertexBuffer;
  32. GraphicsDeviceManager graphics;
  33. public UseCustomVertexGame()
  34. {
  35. graphics = new GraphicsDeviceManager(this);
  36. Content.RootDirectory = "Content";
  37. IsMouseVisible = true;
  38. }
  39. private void CreateVertexBuffer()
  40. {
  41. cubeVertices = new CustomVertex[number_of_vertices];
  42. InitializeCube();
  43. vertexBuffer = new VertexBuffer(
  44. graphics.GraphicsDevice,
  45. typeof(CustomVertex),
  46. number_of_vertices,
  47. BufferUsage.None
  48. );
  49. vertexBuffer.SetData<CustomVertex>(cubeVertices);
  50. graphics.GraphicsDevice.SetVertexBuffer(vertexBuffer);
  51. }
  52. /// <summary>
  53. /// This is called when the game should draw itself.
  54. /// </summary>
  55. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  56. protected override void Draw(GameTime gameTime)
  57. {
  58. graphics.GraphicsDevice.Clear(Color.SteelBlue);
  59. RasterizerState rasterizerState1 = new RasterizerState();
  60. rasterizerState1.CullMode = CullMode.None;
  61. graphics.GraphicsDevice.RasterizerState = rasterizerState1;
  62. // Update the effect's world matrix to reflect the current rotation
  63. basicEffect.World = worldMatrix;
  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 CustomVertex(LeftTopFront, textureLeftTop);
  100. cubeVertices[1] = new CustomVertex(LeftBottomFront, textureLeftBottom);
  101. cubeVertices[2] = new CustomVertex(RightTopFront, textureRightTop);
  102. cubeVertices[3] = new CustomVertex(LeftBottomFront, textureLeftBottom);
  103. cubeVertices[4] = new CustomVertex(RightBottomFront, textureRightBottom);
  104. cubeVertices[5] = new CustomVertex(RightTopFront, textureRightTop);
  105. // Back face.
  106. cubeVertices[6] = new CustomVertex(LeftTopBack, textureRightTop);
  107. cubeVertices[7] = new CustomVertex(RightTopBack, textureLeftTop);
  108. cubeVertices[8] = new CustomVertex(LeftBottomBack, textureRightBottom);
  109. cubeVertices[9] = new CustomVertex(LeftBottomBack, textureRightBottom);
  110. cubeVertices[10] = new CustomVertex(RightTopBack, textureLeftTop);
  111. cubeVertices[11] = new CustomVertex(RightBottomBack, textureLeftBottom);
  112. // Top face.
  113. cubeVertices[12] = new CustomVertex(LeftTopFront, textureLeftBottom);
  114. cubeVertices[13] = new CustomVertex(RightTopBack, textureRightTop);
  115. cubeVertices[14] = new CustomVertex(LeftTopBack, textureLeftTop);
  116. cubeVertices[15] = new CustomVertex(LeftTopFront, textureLeftBottom);
  117. cubeVertices[16] = new CustomVertex(RightTopFront, textureRightBottom);
  118. cubeVertices[17] = new CustomVertex(RightTopBack, textureRightTop);
  119. // Bottom face.
  120. cubeVertices[18] = new CustomVertex(LeftBottomFront, textureLeftTop);
  121. cubeVertices[19] = new CustomVertex(LeftBottomBack, textureLeftBottom);
  122. cubeVertices[20] = new CustomVertex(RightBottomBack, textureRightBottom);
  123. cubeVertices[21] = new CustomVertex(LeftBottomFront, textureLeftTop);
  124. cubeVertices[22] = new CustomVertex(RightBottomBack, textureRightBottom);
  125. cubeVertices[23] = new CustomVertex(RightBottomFront, textureRightTop);
  126. // Left face.
  127. cubeVertices[24] = new CustomVertex(LeftTopFront, textureRightTop);
  128. cubeVertices[25] = new CustomVertex(LeftBottomBack, textureLeftBottom);
  129. cubeVertices[26] = new CustomVertex(LeftBottomFront, textureRightBottom);
  130. cubeVertices[27] = new CustomVertex(LeftTopBack, textureLeftTop);
  131. cubeVertices[28] = new CustomVertex(LeftBottomBack, textureLeftBottom);
  132. cubeVertices[29] = new CustomVertex(LeftTopFront, textureRightTop);
  133. // Right face.
  134. cubeVertices[30] = new CustomVertex(RightTopFront, textureLeftTop);
  135. cubeVertices[31] = new CustomVertex(RightBottomFront, textureLeftBottom);
  136. cubeVertices[32] = new CustomVertex(RightBottomBack, textureRightBottom);
  137. cubeVertices[33] = new CustomVertex(RightTopBack, textureRightTop);
  138. cubeVertices[34] = new CustomVertex(RightTopFront, textureLeftTop);
  139. cubeVertices[35] = new CustomVertex(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>("Glass2");
  148. basicEffect.World = worldMatrix;
  149. basicEffect.View = viewMatrix;
  150. basicEffect.Projection = projectionMatrix;
  151. basicEffect.Texture = texture;
  152. basicEffect.TextureEnabled = true;
  153. }
  154. /// <summary>
  155. /// Initializes the transforms used for the 3D model.
  156. /// </summary>
  157. private void InitializeTransform()
  158. {
  159. float tilt = (float)Math.PI / 8.0f;
  160. worldMatrix = Matrix.CreateRotationX(tilt) * Matrix.CreateRotationY(tilt);
  161. viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, 10),
  162. Vector3.Zero, Vector3.Up);
  163. projectionMatrix = Matrix.CreatePerspectiveFieldOfView(
  164. (float)Math.PI / 4.0f, // 45 degrees, since 2 PI Radians is 360 degrees
  165. (float)GraphicsDevice.Viewport.Width /
  166. (float)GraphicsDevice.Viewport.Height,
  167. 1.0f, 100.0f);
  168. }
  169. /// <summary>
  170. /// LoadContent will be called once per game and is the place to load
  171. /// all of your content.
  172. /// </summary>
  173. protected override void LoadContent()
  174. {
  175. InitializeTransform();
  176. InitializeEffect();
  177. InitializeCube();
  178. }
  179. protected override void UnloadContent()
  180. {
  181. // TODO: Unload any non ContentManager content here
  182. }
  183. /// <summary>
  184. /// Allows the game to run logic such as updating the world,
  185. /// checking for collisions, gathering input, and playing audio.
  186. /// </summary>
  187. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  188. private float rotationAngle = 0f;
  189. protected override void Update(GameTime gameTime)
  190. {
  191. // Exit on GamePad B or Back
  192. var gamePadState = GamePad.GetState(PlayerIndex.One);
  193. if (gamePadState.Buttons.Back == ButtonState.Pressed)
  194. this.Exit();
  195. if (Keyboard.GetState().IsKeyDown(Keys.Escape))
  196. #if !___IOS___
  197. this.Exit();
  198. #endif
  199. // Spin the cube
  200. rotationAngle += (float)gameTime.ElapsedGameTime.TotalSeconds;
  201. worldMatrix = Matrix.CreateRotationY(rotationAngle) * Matrix.CreateRotationX(rotationAngle / 2f);
  202. base.Update(gameTime);
  203. }
  204. }
  205. }