GameQuad.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GameQuad.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using RobotGameData;
  16. using RobotGameData.Render;
  17. #endregion
  18. namespace RobotGameData.GameObject
  19. {
  20. /// <summary>
  21. /// Since this sprite contains its own up vector and normal vector,
  22. /// it does not get affected by view matrix.
  23. /// </summary>
  24. public class GameQuad : GameSceneNode
  25. {
  26. #region Fields
  27. Vector3 upperLeft;
  28. Vector3 lowerLeft;
  29. Vector3 upperRight;
  30. Vector3 lowerRight;
  31. Vector3 normal;
  32. Vector3 planeUp;
  33. Vector3 left;
  34. VertexPositionNormalTexture[] vertices;
  35. int[] indices;
  36. VertexDeclaration vertexDecl;
  37. BasicEffect effect;
  38. IGraphicsDeviceService graphics;
  39. bool alphaTestEnable = false;
  40. bool alphaBlendEnable = false;
  41. CompareFunction alphaFunction = CompareFunction.Always;
  42. Blend sourceBlend = Blend.One;
  43. Blend destinationBlend = Blend.Zero;
  44. BlendFunction blendFunction = BlendFunction.Add;
  45. int referenceAlpha = 0;
  46. bool depthBufferEnable = true;
  47. bool depthBufferWriteEnable = true;
  48. CompareFunction depthBufferFunction = CompareFunction.LessEqual;
  49. CullMode cullMode = CullMode.CullCounterClockwiseFace;
  50. #endregion
  51. #region Properties
  52. /// <summary>
  53. /// Gets or sets the world matrix.
  54. /// </summary>
  55. public Matrix World
  56. {
  57. get { return effect.World; }
  58. set { effect.World = value; }
  59. }
  60. /// <summary>
  61. /// Gets or sets the view matrix.
  62. /// </summary>
  63. public Matrix View
  64. {
  65. get { return effect.View; }
  66. set { effect.View = value; }
  67. }
  68. /// <summary>
  69. /// Gets or sets the projection matrix.
  70. /// </summary>
  71. public Matrix Projection
  72. {
  73. get { return effect.Projection; }
  74. set { effect.Projection = value; }
  75. }
  76. /// <summary>
  77. /// Gets or sets the texture.
  78. /// </summary>
  79. public Texture2D Texture
  80. {
  81. get { return effect.Texture; }
  82. set
  83. {
  84. effect.Texture = value;
  85. if (effect.Texture == null)
  86. effect.TextureEnabled = false;
  87. else
  88. effect.TextureEnabled = true;
  89. }
  90. }
  91. /// <summary>
  92. /// Gets or sets the lighting enable.
  93. /// </summary>
  94. public bool LightingEnabled
  95. {
  96. get { return effect.LightingEnabled; }
  97. set { effect.LightingEnabled = value; }
  98. }
  99. /// <summary>
  100. /// Gets or sets the vertex color enable.
  101. /// </summary>
  102. public bool VertexColorEnabled
  103. {
  104. get { return effect.VertexColorEnabled; }
  105. set { effect.VertexColorEnabled = value; }
  106. }
  107. /// <summary>
  108. /// Gets or sets the alpha of the mesh.
  109. /// </summary>
  110. public float Alpha
  111. {
  112. get { return effect.Alpha; }
  113. set { effect.Alpha = value; }
  114. }
  115. public bool AlphaTestEnable
  116. {
  117. get { return alphaTestEnable; }
  118. set { alphaTestEnable = value; }
  119. }
  120. public bool AlphaBlendEnable
  121. {
  122. get { return alphaBlendEnable; }
  123. set { alphaBlendEnable = value; }
  124. }
  125. public int ReferenceAlpha
  126. {
  127. get { return referenceAlpha; }
  128. set { referenceAlpha = value; }
  129. }
  130. public CompareFunction AlphaFunction
  131. {
  132. get { return alphaFunction; }
  133. set { alphaFunction = value; }
  134. }
  135. public bool DepthBufferEnable
  136. {
  137. get { return depthBufferEnable; }
  138. set { depthBufferEnable = value; }
  139. }
  140. public bool DepthBufferWriteEnable
  141. {
  142. get { return depthBufferWriteEnable; }
  143. set { depthBufferWriteEnable = value; }
  144. }
  145. public CompareFunction DepthBufferFunction
  146. {
  147. get { return depthBufferFunction; }
  148. set { depthBufferFunction = value; }
  149. }
  150. public Blend SourceBlend
  151. {
  152. get { return sourceBlend; }
  153. set { sourceBlend = value; }
  154. }
  155. public Blend DestinationBlend
  156. {
  157. get { return destinationBlend; }
  158. set { destinationBlend = value; }
  159. }
  160. public BlendFunction BlendFunction
  161. {
  162. get { return blendFunction; }
  163. set { blendFunction = value; }
  164. }
  165. public CullMode CullMode
  166. {
  167. get { return cullMode; }
  168. set { cullMode = value; }
  169. }
  170. #endregion
  171. /// <summary>
  172. /// Constructor.
  173. /// </summary>
  174. /// <param name="origin">origin position</param>
  175. /// <param name="normal">normal vector</param>
  176. /// <param name="up">up vector</param>
  177. /// <param name="width">width size</param>
  178. /// <param name="height">height size</param>
  179. public GameQuad(Vector3 origin, Vector3 normal, Vector3 up,
  180. float width, float height)
  181. {
  182. this.graphics =
  183. (IGraphicsDeviceService)FrameworkCore.Game.Services.GetService(
  184. typeof(IGraphicsDeviceService));
  185. this.effect = new BasicEffect(graphics.GraphicsDevice, null);
  186. this.vertices = new VertexPositionNormalTexture[4];
  187. this.indices = new int[6];
  188. this.normal = normal;
  189. this.planeUp = up;
  190. // Calculate the quad corners
  191. this.left = Vector3.Cross(normal, this.planeUp);
  192. Vector3 uppercenter = (this.planeUp * height / 2) + origin;
  193. this.upperLeft = uppercenter + (this.left * width / 2);
  194. this.upperRight = uppercenter - (this.left * width / 2);
  195. this.lowerLeft = this.upperLeft - (this.planeUp * height);
  196. this.lowerRight = this.upperRight - (this.planeUp * height);
  197. vertexDecl = new VertexDeclaration(graphics.GraphicsDevice,
  198. VertexPositionNormalTexture.VertexElements);
  199. FillVertices();
  200. }
  201. /// <summary>
  202. /// fills vertex data using members.
  203. /// </summary>
  204. private void FillVertices()
  205. {
  206. // Fill in texture coordinates to display full texture
  207. // on quad
  208. Vector2 textureUpperLeft = new Vector2( 0.0f, 0.0f );
  209. Vector2 textureUpperRight = new Vector2( 1.0f, 0.0f );
  210. Vector2 textureLowerLeft = new Vector2( 0.0f, 1.0f );
  211. Vector2 textureLowerRight = new Vector2( 1.0f, 1.0f );
  212. // Provide a normal for each vertex
  213. for (int i = 0; i < this.vertices.Length; i++)
  214. {
  215. this.vertices[i].Normal = this.normal;
  216. }
  217. // Set the position and texture coordinate for each
  218. // vertex
  219. this.vertices[0].Position = this.lowerLeft;
  220. this.vertices[0].TextureCoordinate = textureLowerLeft;
  221. this.vertices[1].Position = this.upperLeft;
  222. this.vertices[1].TextureCoordinate = textureUpperLeft;
  223. this.vertices[2].Position = this.lowerRight;
  224. this.vertices[2].TextureCoordinate = textureLowerRight;
  225. this.vertices[3].Position = this.upperRight;
  226. this.vertices[3].TextureCoordinate = textureUpperRight;
  227. // Set the index buffer for each vertex, using
  228. // clockwise winding
  229. this.indices[0] = 0;
  230. this.indices[1] = 1;
  231. this.indices[2] = 2;
  232. this.indices[3] = 2;
  233. this.indices[4] = 1;
  234. this.indices[5] = 3;
  235. }
  236. protected override void Dispose(bool disposing)
  237. {
  238. if (vertexDecl != null)
  239. {
  240. vertexDecl.Dispose();
  241. vertexDecl = null;
  242. }
  243. if (effect != null)
  244. {
  245. effect.Dispose();
  246. effect = null;
  247. }
  248. base.Dispose(disposing);
  249. }
  250. /// <summary>
  251. /// draws a 3D quad.
  252. /// </summary>
  253. /// <param name="renderTracer"></param>
  254. protected override void OnDraw(RenderTracer renderTracer)
  255. {
  256. World = this.TransformedMatrix;
  257. View = renderTracer.View;
  258. Projection = renderTracer.Projection;
  259. renderTracer.Device.VertexDeclaration = vertexDecl;
  260. renderTracer.Device.RenderState.AlphaTestEnable = alphaTestEnable;
  261. renderTracer.Device.RenderState.AlphaBlendEnable = alphaBlendEnable;
  262. renderTracer.Device.RenderState.AlphaFunction = alphaFunction;
  263. renderTracer.Device.RenderState.SourceBlend = sourceBlend;
  264. renderTracer.Device.RenderState.DestinationBlend = destinationBlend;
  265. renderTracer.Device.RenderState.BlendFunction = blendFunction;
  266. renderTracer.Device.RenderState.ReferenceAlpha = referenceAlpha;
  267. renderTracer.Device.RenderState.DepthBufferEnable = depthBufferEnable;
  268. renderTracer.Device.RenderState.DepthBufferWriteEnable =
  269. depthBufferWriteEnable;
  270. renderTracer.Device.RenderState.DepthBufferFunction = depthBufferFunction;
  271. renderTracer.Device.RenderState.CullMode = cullMode;
  272. GraphicsDevice device = renderTracer.Device;
  273. effect.Begin();
  274. foreach (EffectPass pass in effect.CurrentTechnique.Passes)
  275. {
  276. pass.Begin();
  277. device.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(
  278. PrimitiveType.TriangleList, this.vertices, 0, 4, this.indices, 0, 2);
  279. pass.End();
  280. }
  281. effect.End();
  282. }
  283. }
  284. }