MLR_Terrain.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //===========================================================================//
  2. // Copyright (C) Microsoft Corporation. All rights reserved. //
  3. //===========================================================================//
  4. #include "MLRHeaders.hpp"
  5. #if defined(TRACE_ENABLED) && defined(MLR_TRACE)
  6. BitTrace *MLR_Terrain_Clip;
  7. #endif
  8. //#############################################################################
  9. //## MLRTerrain with no color no lighting w/ detail texture, uv's from xyz ###
  10. //#############################################################################
  11. MLR_Terrain::ClassData*
  12. MLR_Terrain::DefaultData = NULL;
  13. DynamicArrayOf<Vector2DScalar>
  14. *MLR_Terrain::clipTexCoords;
  15. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  16. //
  17. void
  18. MLR_Terrain::InitializeClass()
  19. {
  20. Verify(!DefaultData);
  21. Verify(gos_GetCurrentHeap() == StaticHeap);
  22. DefaultData =
  23. new ClassData(
  24. MLR_TerrainClassID,
  25. "MidLevelRenderer::MLR_Terrain",
  26. MLR_I_DeT_TMesh::DefaultData,
  27. (MLRPrimitiveBase::Factory)&Make
  28. );
  29. Register_Object(DefaultData);
  30. clipTexCoords = new DynamicArrayOf<Vector2DScalar> (Limits::Max_Number_Vertices_Per_Mesh);
  31. Register_Object(clipTexCoords);
  32. #if defined(TRACE_ENABLED) && defined(MLR_TRACE)
  33. MLR_Terrain_Clip = new BitTrace("MLR_Terrain_Clip");
  34. Register_Object(MLR_Terrain_Clip);
  35. #endif
  36. }
  37. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. //
  39. void
  40. MLR_Terrain::TerminateClass()
  41. {
  42. Unregister_Object(DefaultData);
  43. delete DefaultData;
  44. DefaultData = NULL;
  45. Unregister_Object(clipTexCoords);
  46. delete clipTexCoords;
  47. #if defined(TRACE_ENABLED) && defined(MLR_TRACE)
  48. Unregister_Object(MLR_Terrain_Clip);
  49. delete MLR_Terrain_Clip;
  50. #endif
  51. }
  52. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  53. //
  54. MLR_Terrain::MLR_Terrain(
  55. ClassData *class_data,
  56. MemoryStream *stream,
  57. int version
  58. ):
  59. MLR_I_DeT_TMesh(class_data, stream, version)
  60. {
  61. Check_Pointer(this);
  62. Check_Pointer(stream);
  63. Verify(gos_GetCurrentHeap() == Heap);
  64. texCoords.SetLength(0);
  65. }
  66. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. //
  68. MLR_Terrain::MLR_Terrain(ClassData *class_data):
  69. MLR_I_DeT_TMesh(class_data)
  70. {
  71. Check_Pointer(this);
  72. Verify(gos_GetCurrentHeap() == Heap);
  73. texCoords.SetLength(0);
  74. }
  75. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  76. //
  77. MLR_Terrain::~MLR_Terrain()
  78. {
  79. Check_Object(this);
  80. }
  81. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  82. //
  83. MLR_Terrain*
  84. MLR_Terrain::Make(
  85. MemoryStream *stream,
  86. int version
  87. )
  88. {
  89. Check_Object(stream);
  90. gos_PushCurrentHeap(Heap);
  91. MLR_Terrain *terrain = new MLR_Terrain(DefaultData, stream, version);
  92. gos_PopCurrentHeap();
  93. return terrain;
  94. }
  95. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. //
  97. void
  98. MLR_Terrain::Save(MemoryStream *stream)
  99. {
  100. Check_Object(this);
  101. Check_Object(stream);
  102. MLR_I_DeT_TMesh::Save(stream);
  103. }
  104. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  105. //
  106. void
  107. MLR_Terrain::TestInstance() const
  108. {
  109. Verify(IsDerivedFrom(DefaultData));
  110. }
  111. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  112. //
  113. void
  114. MLR_Terrain::SetUVData(
  115. Stuff::Scalar bpf,
  116. Stuff::Scalar xmin,
  117. Stuff::Scalar xmax,
  118. Stuff::Scalar zmin,
  119. Stuff::Scalar zmax
  120. )
  121. {
  122. borderPixelFun = bpf;
  123. minX = xmin;
  124. minZ = zmin;
  125. xUVFac = (1.0f-2*borderPixelFun)/(xmax-xmin);
  126. zUVFac = (1.0f-2*borderPixelFun)/(zmax-zmin);
  127. }
  128. #define I_SAY_YES_TO_DETAIL_TEXTURES
  129. #undef I_SAY_YES_TO_DUAL_TEXTURES
  130. #undef I_SAY_YES_TO_COLOR
  131. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  132. //
  133. #undef I_SAY_YES_TO_LIGHTING
  134. #define I_SAY_YES_TO_TERRAIN
  135. #define CLASSNAME MLR_Terrain
  136. #if defined(TRACE_ENABLED) && defined(MLR_TRACE)
  137. #define SET_MLR_TMESH_CLIP() MLR_Terrain_Clip->Set()
  138. #define CLEAR_MLR_TMESH_CLIP() MLR_Terrain_Clip->Clear()
  139. #else
  140. #define SET_MLR_TMESH_CLIP()
  141. #define CLEAR_MLR_TMESH_CLIP()
  142. #endif
  143. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  144. // This include contains follwing functions:
  145. // void MLR_Terrain::TransformNoClip(Matrix4D*, GOSVertexPool*);
  146. // int MLR_Terrain::Clip(MLRClippingState, GOSVertexPool*);
  147. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  148. #include <MLR\MLRTriangleClipping.hpp>
  149. #undef I_SAY_YES_TO_DETAIL_TEXTURES
  150. #undef I_SAY_YES_TO_TERRAIN
  151. #undef CLASSNAME
  152. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  153. //
  154. MLRShape*
  155. MidLevelRenderer::CreateIndexedTriIcosahedron_TerrainTest(
  156. IcoInfo& icoInfo,
  157. MLRState *state,
  158. MLRState *stateDet
  159. )
  160. {
  161. gos_PushCurrentHeap(Heap);
  162. MLRShape *ret = new MLRShape(20);
  163. Register_Object(ret);
  164. int i, j, k;
  165. long nrTri = (long) ceil (icoInfo.all * pow (4.0f, icoInfo.depth));
  166. Point3D v[3];
  167. if(3*nrTri >= Limits::Max_Number_Vertices_Per_Mesh)
  168. {
  169. nrTri = Limits::Max_Number_Vertices_Per_Mesh/3;
  170. }
  171. Point3D *coords = new Point3D [nrTri*3];
  172. Register_Pointer(coords);
  173. Point3D *collapsedCoords = NULL;
  174. if(icoInfo.indexed==true)
  175. {
  176. collapsedCoords = new Point3D [nrTri*3];
  177. Register_Pointer(collapsedCoords);
  178. }
  179. unsigned short *index = new unsigned short [nrTri*3];
  180. Register_Pointer(index);
  181. int uniquePoints = 0;
  182. for (k=0;k<20;k++)
  183. {
  184. triDrawn = 0;
  185. MLR_Terrain *mesh = new MLR_Terrain();
  186. Register_Object(mesh);
  187. mesh->SetUVData(2.0f, -1.0f, 1.0f, -1.0f, 1.0f);
  188. // setup vertex position information
  189. for (j=0;j<3;j++)
  190. {
  191. v[j].x = vdata[tindices[k][j]][0];
  192. v[j].y = vdata[tindices[k][j]][1];
  193. v[j].z = vdata[tindices[k][j]][2];
  194. }
  195. subdivide (coords, v[0], v[1], v[2], icoInfo.depth, nrTri, icoInfo.radius);
  196. mesh->SetSubprimitiveLengths(NULL, nrTri);
  197. if(icoInfo.indexed==true)
  198. {
  199. uniquePoints = 1;
  200. collapsedCoords[0] = coords[0];
  201. index[0] = 0;
  202. for(i=1;i<nrTri*3;i++)
  203. {
  204. for(j=0;j<uniquePoints;j++)
  205. {
  206. if(coords[i] == collapsedCoords[j])
  207. {
  208. break;
  209. }
  210. }
  211. if(j==uniquePoints)
  212. {
  213. collapsedCoords[uniquePoints++] = coords[i];
  214. }
  215. index[i] = static_cast<unsigned short>(j);
  216. }
  217. mesh->SetCoordData(collapsedCoords, uniquePoints);
  218. }
  219. else
  220. {
  221. uniquePoints = nrTri*3;
  222. for(i=0;i<nrTri*3;i++)
  223. {
  224. index[i] = static_cast<unsigned short>(i);
  225. }
  226. mesh->SetCoordData(coords, nrTri*3);
  227. }
  228. mesh->SetIndexData(index, nrTri*3);
  229. mesh->FindFacePlanes();
  230. if(state != NULL)
  231. {
  232. mesh->SetReferenceState(*state);
  233. }
  234. mesh->SetDetailData(0.0f, 0.0f, 16.0f, 16.0f, 50.0f, 70.0f);
  235. mesh->SetReferenceState(*stateDet, 1);
  236. ret->Add(mesh);
  237. mesh->DetachReference();
  238. }
  239. Unregister_Pointer(index);
  240. delete [] index;
  241. if(icoInfo.indexed==true)
  242. {
  243. Unregister_Pointer(collapsedCoords);
  244. delete [] collapsedCoords;
  245. }
  246. Unregister_Pointer(coords);
  247. delete [] coords;
  248. gos_PopCurrentHeap();
  249. return ret;
  250. }