GameMesh.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GameMesh.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 Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Content;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using RobotGameData.Render;
  16. using RobotGameData.Resource;
  17. using RobotGameData.Helper;
  18. #endregion
  19. namespace RobotGameData.GameObject
  20. {
  21. public enum RenderingSpace
  22. {
  23. /// <summary>
  24. /// 3D world space
  25. /// </summary>
  26. World = 0,
  27. /// <summary>
  28. /// 2D screen space
  29. /// </summary>
  30. Screen,
  31. }
  32. /// <summary>
  33. /// This mesh is drawn on the 3D world.
  34. /// It contains a vertex buffer and an index buffer.
  35. /// </summary>
  36. public class GameMesh : GameSceneNode
  37. {
  38. #region Fields
  39. // If it's true, will be disabled VertexBuffer and IndexBuffer
  40. public bool userPrimitive = true;
  41. public VertexPositionColorTexture[] vertexData = null;
  42. public short[] indexData = null;
  43. public VertexBuffer vertexBuffer = null;
  44. public IndexBuffer indexBuffer = null;
  45. Texture2D textureResource = null;
  46. int updateVertexCount = 0;
  47. int primitiveCount = 0;
  48. bool alphaTestEnable = false;
  49. bool alphaBlendEnable = false;
  50. CompareFunction alphaFunction = CompareFunction.Always;
  51. Blend sourceBlend = Blend.One;
  52. Blend destinationBlend = Blend.Zero;
  53. BlendFunction blendFunction = BlendFunction.Add;
  54. int referenceAlpha = 0;
  55. bool depthBufferEnable = true;
  56. bool depthBufferWriteEnable = true;
  57. CompareFunction depthBufferFunction = CompareFunction.LessEqual;
  58. CullMode cullMode = CullMode.CullCounterClockwiseFace;
  59. static VertexDeclaration vertexDeclaration = null;
  60. static BasicEffect basicEffect = null;
  61. #endregion
  62. #region Properties
  63. public int UpdateVertexCount
  64. {
  65. get { return updateVertexCount; }
  66. set { updateVertexCount = value; }
  67. }
  68. public int PrimitiveCount
  69. {
  70. get { return this.primitiveCount; }
  71. set { this.primitiveCount = value; }
  72. }
  73. public bool AlphaTestEnable
  74. {
  75. get { return alphaTestEnable; }
  76. set { alphaTestEnable = value; }
  77. }
  78. public bool AlphaBlendEnable
  79. {
  80. get { return alphaBlendEnable; }
  81. set { alphaBlendEnable = value; }
  82. }
  83. public int ReferenceAlpha
  84. {
  85. get { return referenceAlpha; }
  86. set { referenceAlpha = value; }
  87. }
  88. public CompareFunction AlphaFunction
  89. {
  90. get { return alphaFunction; }
  91. set { alphaFunction = value; }
  92. }
  93. public bool DepthBufferEnable
  94. {
  95. get { return depthBufferEnable; }
  96. set { depthBufferEnable = value; }
  97. }
  98. public bool DepthBufferWriteEnable
  99. {
  100. get { return depthBufferWriteEnable; }
  101. set { depthBufferWriteEnable = value; }
  102. }
  103. public CompareFunction DepthBufferFunction
  104. {
  105. get { return depthBufferFunction; }
  106. set { depthBufferFunction = value; }
  107. }
  108. public Blend SourceBlend
  109. {
  110. get { return sourceBlend; }
  111. set { sourceBlend = value; }
  112. }
  113. public Blend DestinationBlend
  114. {
  115. get { return destinationBlend; }
  116. set { destinationBlend = value; }
  117. }
  118. public BlendFunction BlendFunction
  119. {
  120. get { return blendFunction; }
  121. set { blendFunction = value; }
  122. }
  123. public CullMode CullMode
  124. {
  125. get { return cullMode; }
  126. set { cullMode = value; }
  127. }
  128. #endregion
  129. protected override void Dispose(bool disposing)
  130. {
  131. if (this.vertexBuffer != null)
  132. {
  133. this.vertexBuffer.Dispose();
  134. this.vertexBuffer = null;
  135. }
  136. if (this.indexBuffer != null)
  137. {
  138. this.indexBuffer.Dispose();
  139. this.indexBuffer = null;
  140. }
  141. if (vertexDeclaration != null)
  142. {
  143. vertexDeclaration.Dispose();
  144. vertexDeclaration = null;
  145. }
  146. if (basicEffect != null)
  147. {
  148. basicEffect.Dispose();
  149. basicEffect = null;
  150. }
  151. base.Dispose(disposing);
  152. }
  153. protected override void UnloadContent()
  154. {
  155. base.UnloadContent();
  156. }
  157. /// <summary>
  158. /// the mesh is drawn by using the vertexData.
  159. /// When the userPrimitive member is set to true,
  160. /// it is drawn without using the vertex buffer and the index buffer.
  161. /// </summary>
  162. /// <param name="renderTracer"></param>
  163. protected override void OnDraw(RenderTracer renderTracer)
  164. {
  165. GraphicsDevice device = renderTracer.Device;
  166. RenderState renderState = device.RenderState;
  167. basicEffect.Texture = this.textureResource;
  168. basicEffect.World = this.TransformedMatrix;
  169. basicEffect.View = renderTracer.View;
  170. basicEffect.Projection = renderTracer.Projection;
  171. basicEffect.LightingEnabled = false;
  172. device.VertexDeclaration = vertexDeclaration;
  173. device.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
  174. device.SamplerStates[0].AddressV = TextureAddressMode.Wrap;
  175. device.SamplerStates[0].MinFilter = TextureFilter.Linear;
  176. device.SamplerStates[0].MagFilter = TextureFilter.Linear;
  177. device.SamplerStates[0].MipFilter = TextureFilter.Point;
  178. renderState.AlphaTestEnable = alphaTestEnable;
  179. renderState.AlphaBlendEnable = alphaBlendEnable;
  180. renderState.AlphaFunction = alphaFunction;
  181. renderState.SourceBlend = sourceBlend;
  182. renderState.DestinationBlend = destinationBlend;
  183. renderState.BlendFunction = blendFunction;
  184. renderState.ReferenceAlpha = referenceAlpha;
  185. renderState.DepthBufferEnable = depthBufferEnable;
  186. renderState.DepthBufferWriteEnable = depthBufferWriteEnable;
  187. renderState.DepthBufferFunction = depthBufferFunction;
  188. renderState.CullMode = cullMode;
  189. basicEffect.Begin();
  190. for (int i = 0; i < basicEffect.CurrentTechnique.Passes.Count; i++)
  191. {
  192. EffectPass pass = basicEffect.CurrentTechnique.Passes[i];
  193. pass.Begin();
  194. if (userPrimitive )
  195. {
  196. // Use index?
  197. if (indexData != null)
  198. {
  199. // only use vertex and index data
  200. device.DrawUserIndexedPrimitives<VertexPositionColorTexture>(
  201. PrimitiveType.TriangleList,
  202. vertexData,
  203. 0,
  204. vertexData.Length,
  205. indexData,
  206. 0,
  207. this.primitiveCount);
  208. }
  209. else
  210. {
  211. device.DrawUserPrimitives<VertexPositionColorTexture>(
  212. PrimitiveType.TriangleList,
  213. vertexData,
  214. 0,
  215. this.primitiveCount);
  216. }
  217. }
  218. else
  219. {
  220. // Use vertex buffer
  221. device.Vertices[0].SetSource(vertexBuffer,
  222. 0,
  223. VertexPositionColorTexture.SizeInBytes);
  224. // Use index?
  225. if (indexBuffer != null)
  226. {
  227. // Use index buffer
  228. device.Indices = indexBuffer;
  229. device.DrawIndexedPrimitives(PrimitiveType.TriangleList,
  230. 0,
  231. 0,
  232. updateVertexCount,
  233. 0,
  234. this.primitiveCount);
  235. }
  236. else
  237. {
  238. device.DrawPrimitives(PrimitiveType.TriangleList,
  239. 0,
  240. this.primitiveCount);
  241. }
  242. }
  243. pass.End();
  244. }
  245. basicEffect.End();
  246. device.RenderState.DepthBufferWriteEnable = true;
  247. }
  248. /// <summary>
  249. /// create members for creating mesh data.
  250. /// </summary>
  251. /// <param name="vertexCount">vertex count</param>
  252. /// <param name="indexCount">index count</param>
  253. /// <param name="fileName">texture file name</param>
  254. public void Create(int vertexCount, int indexCount, string fileName)
  255. {
  256. // Load texture
  257. GameResourceTexture2D resource =
  258. FrameworkCore.ResourceManager.LoadTexture(fileName);
  259. Create(vertexCount, indexCount, resource.Texture2D);
  260. }
  261. /// <summary>
  262. /// create members for creating mesh data.
  263. /// </summary>
  264. /// <param name="vertexCount">vertex count</param>
  265. /// <param name="indexCount">index count</param>
  266. /// <param name="texture">texture resource</param>
  267. public void Create(int vertexCount, int indexCount, Texture2D texture)
  268. {
  269. GraphicsDevice device = FrameworkCore.Game.GraphicsDevice;
  270. this.updateVertexCount = vertexCount;
  271. if( indexCount > 0)
  272. this.primitiveCount = vertexCount / 2;
  273. else
  274. this.primitiveCount = vertexCount / 3;
  275. this.textureResource = texture;
  276. if (basicEffect == null)
  277. {
  278. basicEffect = new BasicEffect(device, null);
  279. basicEffect.LightingEnabled = false;
  280. basicEffect.VertexColorEnabled = true;
  281. basicEffect.TextureEnabled = true;
  282. }
  283. if (vertexDeclaration == null)
  284. {
  285. vertexDeclaration = new VertexDeclaration(device,
  286. VertexPositionColorTexture.VertexElements);
  287. }
  288. // Create vertexBuffer
  289. if( vertexCount > 0)
  290. {
  291. vertexData = new VertexPositionColorTexture[vertexCount];
  292. for (int i = 0; i < vertexCount; i++)
  293. {
  294. vertexData[i].Color.PackedValue = 0xFFFFFFFF;
  295. }
  296. if (userPrimitive == false)
  297. {
  298. vertexBuffer = new DynamicVertexBuffer(device,
  299. VertexPositionColorTexture.SizeInBytes * vertexCount,
  300. BufferUsage.WriteOnly);
  301. vertexBuffer.SetData(vertexData);
  302. }
  303. }
  304. // Create indexBuffer
  305. if( indexCount > 0)
  306. {
  307. indexData = new short[indexCount];
  308. if (userPrimitive == false)
  309. {
  310. indexBuffer = new DynamicIndexBuffer(device,
  311. sizeof(short) * indexCount,
  312. BufferUsage.WriteOnly,
  313. IndexElementSize.SixteenBits);
  314. indexBuffer.SetData(indexData);
  315. }
  316. }
  317. }
  318. /// <summary>
  319. /// configures a position to the vertex component data.
  320. /// </summary>
  321. /// <param name="index">an index of the vertex component data</param>
  322. /// <param name="position">a position vector</param>
  323. public void SetPositionData(int index, Vector3 position)
  324. {
  325. vertexData[index].Position = position;
  326. }
  327. /// <summary>
  328. /// configure positions to the vertex component data.
  329. /// </summary>
  330. /// <param name="position">array of position vector</param>
  331. public void SetPositionData(Vector3[] position)
  332. {
  333. for(int i = 0; i < position.Length; i++)
  334. {
  335. SetPositionData(i, position[i]);
  336. }
  337. }
  338. /// <summary>
  339. /// configures color to the vertex component data.
  340. /// </summary>
  341. /// <param name="index">an index of the vertex component data</param>
  342. /// <param name="color">packed color value</param>
  343. public void SetColorData(int index, uint color)
  344. {
  345. vertexData[index].Color.PackedValue = color;
  346. }
  347. /// <summary>
  348. /// configures color to the vertex component data.
  349. /// </summary>
  350. /// <param name="index">an index of the vertex component data</param>
  351. /// <param name="color">color</param>
  352. public void SetColorData(int index, Color color)
  353. {
  354. vertexData[index].Color = color;
  355. }
  356. /// <summary>
  357. /// configure colors to the vertex component data.
  358. /// </summary>
  359. /// <param name="color">array of packed color value</param>
  360. public void SetColorData(uint[] color)
  361. {
  362. for (int i = 0; i < color.Length; i++)
  363. {
  364. SetColorData(i, color[i]);
  365. }
  366. }
  367. /// <summary>
  368. /// configure colors to the vertex component data.
  369. /// </summary>
  370. /// <param name="color">array of color</param>
  371. public void SetColorData(Color[] color)
  372. {
  373. for (int i = 0; i < color.Length; i++)
  374. {
  375. SetColorData(i, color[i]);
  376. }
  377. }
  378. /// <summary>
  379. /// configures texture coordinates to the vertex component data.
  380. /// </summary>
  381. /// <param name="index">an index of the vertex component data</param>
  382. /// <param name="texturecoord">texture coordinates</param>
  383. public void SetTextureCoordData(int index, Vector2 texturecoord)
  384. {
  385. vertexData[index].TextureCoordinate = texturecoord;
  386. }
  387. /// <summary>
  388. /// configures texture coordinates to the vertex component data.
  389. /// </summary>
  390. /// <param name="texturecoord">array of texture coordinates</param>
  391. public void SetTextureCoordData(Vector2[] texturecoord)
  392. {
  393. for (int i = 0; i < texturecoord.Length; i++)
  394. {
  395. SetTextureCoordData(i, texturecoord[i]);
  396. }
  397. }
  398. /// <summary>
  399. /// configures vertex index to the vertex component data.
  400. /// </summary>
  401. /// <param name="index">an index of the vertex component data</param>
  402. /// <param name="val">index value</param>
  403. public void SetIndexData(int index, short val)
  404. {
  405. indexData[index] = val;
  406. }
  407. /// <summary>
  408. /// configures vertex indicies to the vertex component data.
  409. /// </summary>
  410. /// <param name="val">array of value</param>
  411. public void SetIndexData(short[] val)
  412. {
  413. for (int i = 0; i < val.Length; i++)
  414. {
  415. SetIndexData(i, val[i]);
  416. }
  417. }
  418. /// <summary>
  419. /// binds the vertex component data to the vertex buffer.
  420. /// </summary>
  421. public void BindVertexBuffer()
  422. {
  423. // Set vertex buffer
  424. vertexBuffer.SetData<VertexPositionColorTexture>(vertexData);
  425. }
  426. /// <summary>
  427. /// binds the index values to the index buffer.
  428. /// </summary>
  429. public void BindIndexBuffer()
  430. {
  431. // Set index buffer
  432. indexBuffer.SetData<short>(indexData);
  433. }
  434. }
  435. }