TrackColumns.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //-----------------------------------------------------------------------------
  2. // TrackColumns.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using RacingGame.Graphics;
  13. using Model = RacingGame.Graphics.Model;
  14. using RacingGame.Landscapes;
  15. using RacingGame.Shaders;
  16. namespace RacingGame.Tracks
  17. {
  18. /// <summary>
  19. /// Track columns
  20. /// </summary>
  21. class TrackColumns : IDisposable
  22. {
  23. /// <summary>
  24. /// Column vertices, just a simple circle!
  25. /// </summary>
  26. readonly TangentVertex[] BaseColumnVertices =
  27. new TangentVertex[]
  28. {
  29. // 0
  30. new TangentVertex(
  31. new Vector3(1, 0, 0), new Vector2(0.0f / 6.0f, 0.0f),
  32. new Vector3(1, 0, 0), new Vector3(0, 0, -1)),
  33. // 1
  34. new TangentVertex(
  35. new Vector3(0.5f, 0.866025f, 0), new Vector2(1.0f / 6.0f, 0.0f),
  36. new Vector3(0.5f, 0.866025f, 0), new Vector3(0, 0, -1)),
  37. // 2
  38. new TangentVertex(
  39. new Vector3(-0.5f, 0.866025f, 0), new Vector2(2.0f/ 6.0f, 0.0f),
  40. new Vector3(-0.5f, 0.866025f, 0), new Vector3(0, 0, -1)),
  41. // 3
  42. new TangentVertex(
  43. new Vector3(-1, 0, 0), new Vector2(3.0f / 6.0f, 0.0f),
  44. new Vector3(-1, 0, 0), new Vector3(0, 0, -1)),
  45. // 4
  46. new TangentVertex(
  47. new Vector3(-0.5f, -0.866025f, 0), new Vector2(4.0f / 6.0f, 0.0f),
  48. new Vector3(-0.5f, -0.866025f, 0), new Vector3(0, 0, -1)),
  49. // 5
  50. new TangentVertex(
  51. new Vector3(0.5f, -0.866025f, 0), new Vector2(5.0f / 6.0f, 0.0f),
  52. new Vector3(0.5f, -0.866025f, 0), new Vector3(0, 0, -1)),
  53. // 6 (duplicated first to wrap around tex coords)
  54. new TangentVertex(
  55. new Vector3(1, 0, 0), new Vector2(6.0f / 6.0f, 0.0f),
  56. new Vector3(1, 0, 0), new Vector3(0, 0, -1)),
  57. };
  58. /// <summary>
  59. /// Gap between the seperate piles.
  60. /// </summary>
  61. const float ColumnsDistance = 33.0f;
  62. /// <summary>
  63. /// Column rendering height above the landscape ground.
  64. /// </summary>
  65. const float ColumnGroundHeight = 1.0f;
  66. /// <summary>
  67. /// Minimium column height we need, else we skip generation!
  68. /// </summary>
  69. const float MinimumColumnHeight = 2.5f;
  70. /// <summary>
  71. /// Put columns a little bit on the inside of the road,
  72. /// we don't want them to come through the road.
  73. /// </summary>
  74. const float TopColumnSubHeight = 0.55f;
  75. /// <summary>
  76. /// Column position list.
  77. /// </summary>
  78. List<Vector3> columnPositions = new List<Vector3>();
  79. /// <summary>
  80. /// Rail vertices.
  81. /// </summary>
  82. TangentVertex[] columnVertices = null;
  83. /// <summary>
  84. /// Vertex buffer of guard rail.
  85. /// </summary>
  86. VertexBuffer columnVb = null;
  87. /// <summary>
  88. /// Index buffer orf guard rail.
  89. /// </summary>
  90. IndexBuffer columnIb = null;
  91. /// <summary>
  92. /// Create track columns
  93. /// </summary>
  94. /// <param name="points">Points</param>
  95. /// <param name="landscape">Landscape for getting the ground height</param>
  96. public TrackColumns(List<TrackVertex> points, Landscape landscape)
  97. {
  98. if (landscape == null)
  99. return;
  100. float lastColumnsDistance = ColumnsDistance;
  101. List<Matrix> columnPointSpacesTop = new List<Matrix>();
  102. List<Matrix> columnPointSpacesBottom = new List<Matrix>();
  103. for (int num = 0; num < points.Count; num++)
  104. {
  105. // Distance of the current position to the next position
  106. float distance = Vector3.Distance(
  107. points[(num + 1) % points.Count].pos, points[num].pos);
  108. // Uniform calculation of the distance for the columns,
  109. // so it doesn't matter if there is a gap of 2 or 200 m
  110. // Have we reach or go over the ColumnsDistance?
  111. if (lastColumnsDistance - distance <= 0)
  112. {
  113. // Catmull interpolation, instead the linear interpolation, for a
  114. // better position calculation, especially in curves
  115. Vector3 p1 = points[num - 1 < 0 ? points.Count - 1 : num - 1].pos;
  116. Vector3 p2 = points[num].pos;
  117. Vector3 p3 = points[(num + 1) % points.Count].pos;
  118. Vector3 p4 = points[(num + 2) % points.Count].pos;
  119. Vector3 holderPoint = Vector3.CatmullRom(p1, p2, p3, p4,
  120. lastColumnsDistance / distance);
  121. // Just find out how much this point is pointing up
  122. float draft = Vector3.Dot(points[num].up, new Vector3(0, 0, 1));
  123. // And don't add if height is too small!
  124. float columnHeight = holderPoint.Z -
  125. landscape.GetMapHeight(holderPoint.X, holderPoint.Y);
  126. // Store the position for this holder
  127. if (draft > 0.3f &&//< 0 MaxColumnGenerationAngel &&
  128. columnHeight > MinimumColumnHeight)
  129. {
  130. columnPositions.Add(holderPoint);
  131. // The unit vectors for our local point space
  132. Vector3 right = points[num].right;
  133. Vector3 dir = points[num].dir;
  134. Vector3 up = points[num].up;
  135. // Create the coordinate system for the current point by the 3
  136. // unit vectors.
  137. Matrix pointSpace = Matrix.Identity;
  138. pointSpace.M11 = right.X;
  139. pointSpace.M12 = right.Y;
  140. pointSpace.M13 = right.Z;
  141. pointSpace.M21 = dir.X;
  142. pointSpace.M22 = dir.Y;
  143. pointSpace.M23 = dir.Z;
  144. pointSpace.M31 = up.X;
  145. pointSpace.M32 = up.Y;
  146. pointSpace.M33 = up.Z;
  147. // Remember point space
  148. columnPointSpacesTop.Add(pointSpace);
  149. // Same for bottom, but don't use up vector (let it stay default)
  150. pointSpace = Matrix.Identity;
  151. Vector3 upVector = new Vector3(0, 0, 1);
  152. // Rebuild right vector (to make it 90 degree to our up vector)
  153. Vector3 rightVector = Vector3.Cross(dir, upVector);
  154. pointSpace.M11 = rightVector.X;
  155. pointSpace.M12 = rightVector.Y;
  156. pointSpace.M13 = rightVector.Z;
  157. pointSpace.M21 = dir.X;
  158. pointSpace.M22 = dir.Y;
  159. pointSpace.M23 = dir.Z;
  160. columnPointSpacesBottom.Add(pointSpace);
  161. }
  162. // We have just set a pile, the next pile will be set after
  163. // reaching the next holder gap.
  164. lastColumnsDistance += ColumnsDistance;
  165. }
  166. // The distance we have to cover until the next position.
  167. // We subtract our current distance from the remaining gap distance,
  168. // which will then be checked in the next loop.
  169. lastColumnsDistance -= distance;
  170. }
  171. columnVertices = new TangentVertex[
  172. columnPositions.Count * BaseColumnVertices.Length * 2];
  173. // Go through all columns
  174. for (int num = 0; num < columnPositions.Count; num++)
  175. {
  176. Vector3 pos = columnPositions[num];
  177. // Find out the current landscape height here
  178. Vector3 bottomPos = new Vector3(pos.X, pos.Y,
  179. landscape.GetMapHeight(pos.X, pos.Y) +
  180. ColumnGroundHeight);
  181. Vector3 topPos = new Vector3(pos.X, pos.Y,
  182. pos.Z - TopColumnSubHeight);
  183. // Calculate top v tex coord for this column
  184. float topTexV =
  185. Vector3.Distance(topPos, bottomPos) / (MathHelper.Pi * 2);
  186. // Use the BaseColumnVertices twice, once for the bottom and then for the
  187. // top part of our generated column.
  188. for (int topBottom = 0; topBottom < 2; topBottom++)
  189. {
  190. // Go to all BaseColumnVertices
  191. for (int i = 0; i < BaseColumnVertices.Length; i++)
  192. {
  193. int vertIndex = num * BaseColumnVertices.Length * 2 +
  194. topBottom * BaseColumnVertices.Length + i;
  195. // For the top positions, modify them them to fit directly
  196. // on the bottom side of our road. Same for bottom, but don't
  197. // modify the z value
  198. Matrix transformMatrix = topBottom == 0 ?
  199. columnPointSpacesBottom[num] : columnPointSpacesTop[num];
  200. // We don't have to transform the vertices much, just adjust
  201. // the z value and the v tex coord.
  202. columnVertices[vertIndex] =
  203. new TangentVertex(
  204. (topBottom == 0 ? bottomPos : topPos) +
  205. Vector3.Transform(BaseColumnVertices[i].pos,
  206. transformMatrix),
  207. BaseColumnVertices[i].U, topBottom == 0 ? 0 : topTexV,
  208. Vector3.Transform(BaseColumnVertices[i].normal,
  209. transformMatrix),
  210. Vector3.Transform(-BaseColumnVertices[i].tangent,
  211. transformMatrix));
  212. }
  213. }
  214. // Also add the bottom position to the list of holders we want
  215. // to render later.
  216. if (landscape != null &&
  217. // This is not really required, we can easily optimize this out.
  218. BaseGame.HighDetail)
  219. landscape.AddObjectToRender(
  220. "RoadColumnSegment",
  221. new Vector3(bottomPos.X, bottomPos.Y,
  222. bottomPos.Z - ColumnGroundHeight));
  223. }
  224. // Create the vertex buffer from our vertices.
  225. // fix
  226. //columnVb = new VertexBuffer(
  227. // BaseGame.Device,
  228. // typeof(TangentVertex),
  229. // columnVertices.Length,
  230. // ResourceUsage.WriteOnly,
  231. // ResourceManagementMode.Automatic);
  232. columnVb = new VertexBuffer(
  233. BaseGame.Device,
  234. typeof(TangentVertex),
  235. columnVertices.Length,
  236. BufferUsage.WriteOnly);
  237. columnVb.SetData(columnVertices);
  238. // Count of quads (polygons) we have for each column
  239. int quadPolysPerColumn = BaseColumnVertices.Length - 1;
  240. int[] indices =
  241. new int[(2 * 3 * quadPolysPerColumn) * columnPositions.Count];
  242. // Current vertex index
  243. int vertexIndex = 0;
  244. // Helper variable, current index of the indices list
  245. int indicesIndex = 0;
  246. for (int num = 0; num < columnPositions.Count; num++)
  247. {
  248. // Set all quads of the column
  249. for (int j = 0; j < quadPolysPerColumn; j++)
  250. {
  251. indicesIndex = 3 * 2 * (num * quadPolysPerColumn + j);
  252. // 1. Polygon
  253. indices[indicesIndex] = vertexIndex + j;
  254. indices[indicesIndex + 1] =
  255. vertexIndex + 1 + BaseColumnVertices.Length + j;
  256. indices[indicesIndex + 2] = vertexIndex + 1 + j;
  257. // 2. Polygon
  258. indices[indicesIndex + 3] = indices[indicesIndex + 1];
  259. indices[indicesIndex + 4] = indices[indicesIndex];
  260. indices[indicesIndex + 5] =
  261. vertexIndex + BaseColumnVertices.Length + j;
  262. }
  263. // Go to next column
  264. vertexIndex += BaseColumnVertices.Length * 2;
  265. }
  266. // Create the index buffer from our indices.
  267. // fix
  268. //columnIb = new IndexBuffer(
  269. // BaseGame.Device,
  270. // typeof(int),
  271. // indices.Length,
  272. // ResourceUsage.WriteOnly,
  273. // ResourceManagementMode.Automatic);
  274. columnIb = new IndexBuffer(
  275. BaseGame.Device,
  276. typeof(int),
  277. indices.Length,
  278. BufferUsage.WriteOnly);
  279. columnIb.SetData(indices);
  280. }
  281. public void Dispose()
  282. {
  283. columnVb.Dispose();
  284. columnIb.Dispose();
  285. }
  286. /// <summary>
  287. /// Render
  288. /// </summary>
  289. public void Render(Material columnMaterial)
  290. {
  291. // We use tangent vertices for everything here
  292. // Restore the world matrix
  293. BaseGame.WorldMatrix = Matrix.Identity;
  294. // Render all columns
  295. ShaderEffect.normalMapping.Render(
  296. columnMaterial,
  297. "Specular20",
  298. new BaseGame.RenderHandler(RenderColumnVertices));
  299. /*now done in landscape object rendering
  300. // And also render all the holders
  301. foreach (Vector3 pos in columnHolderPositions)
  302. {
  303. holderModel.Render(pos);
  304. }
  305. */
  306. // Restore the world matrix
  307. BaseGame.WorldMatrix = Matrix.Identity;
  308. }
  309. /// <summary>
  310. /// Render column vertices
  311. /// </summary>
  312. private void RenderColumnVertices()
  313. {
  314. if (columnVertices == null)
  315. return;
  316. BaseGame.Device.SetVertexBuffer(columnVb);
  317. BaseGame.Device.Indices = columnIb;
  318. BaseGame.Device.DrawIndexedPrimitives(
  319. PrimitiveType.TriangleList,
  320. 0, 0, (BaseColumnVertices.Length - 1) *
  321. columnPositions.Count * 2);
  322. }
  323. }
  324. }