Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
7.3 KiB

  1. #ifndef NV_TRISTRIP_OBJECTS_H
  2. #define NV_TRISTRIP_OBJECTS_H
  3. #include <assert.h>
  4. #include <windows.h>
  5. #include <vector>
  6. #include <list>
  7. #include "VertexCache.h"
  8. /////////////////////////////////////////////////////////////////////////////////
  9. //
  10. // Types defined for stripification
  11. //
  12. /////////////////////////////////////////////////////////////////////////////////
  13. struct MyVertex {
  14. float x, y, z;
  15. float nx, ny, nz;
  16. };
  17. typedef MyVertex MyVector;
  18. struct MyFace {
  19. int v1, v2, v3;
  20. float nx, ny, nz;
  21. };
  22. class NvFaceInfo {
  23. public:
  24. // vertex indices
  25. NvFaceInfo(int v0, int v1, int v2){
  26. m_v0 = v0; m_v1 = v1; m_v2 = v2;
  27. m_stripId = -1;
  28. m_testStripId = -1;
  29. m_experimentId = -1;
  30. }
  31. // data members are left public
  32. int m_v0, m_v1, m_v2;
  33. int m_stripId; // real strip Id
  34. int m_testStripId; // strip Id in an experiment
  35. int m_experimentId; // in what experiment was it given an experiment Id?
  36. };
  37. // nice and dumb edge class that points knows its
  38. // indices, the two faces, and the next edge using
  39. // the lesser of the indices
  40. class NvEdgeInfo {
  41. public:
  42. // constructor puts 1 ref on us
  43. NvEdgeInfo (int v0, int v1){
  44. m_v0 = v0;
  45. m_v1 = v1;
  46. m_face0 = NULL;
  47. m_face1 = NULL;
  48. m_nextV0 = NULL;
  49. m_nextV1 = NULL;
  50. // we will appear in 2 lists. this is a good
  51. // way to make sure we delete it the second time
  52. // we hit it in the edge infos
  53. m_refCount = 2;
  54. }
  55. // ref and unref
  56. void Unref () { if (--m_refCount == 0) delete this; }
  57. // data members are left public
  58. UINT m_refCount;
  59. NvFaceInfo *m_face0, *m_face1;
  60. int m_v0, m_v1;
  61. NvEdgeInfo *m_nextV0, *m_nextV1;
  62. };
  63. // This class is a quick summary of parameters used
  64. // to begin a triangle strip. Some operations may
  65. // want to create lists of such items, so they were
  66. // pulled out into a class
  67. class NvStripStartInfo {
  68. public:
  69. NvStripStartInfo(NvFaceInfo *startFace, NvEdgeInfo *startEdge, bool toV1){
  70. m_startFace = startFace;
  71. m_startEdge = startEdge;
  72. m_toV1 = toV1;
  73. }
  74. NvFaceInfo *m_startFace;
  75. NvEdgeInfo *m_startEdge;
  76. bool m_toV1;
  77. };
  78. typedef std::vector<NvFaceInfo*> NvFaceInfoVec;
  79. typedef std::list <NvFaceInfo*> NvFaceInfoList;
  80. typedef std::list <NvFaceInfoVec*> NvStripList;
  81. typedef std::vector<NvEdgeInfo*> NvEdgeInfoVec;
  82. typedef std::vector<WORD> WordVec;
  83. typedef std::vector<int> IntVec;
  84. typedef std::vector<MyVertex> MyVertexVec;
  85. typedef std::vector<MyFace> MyFaceVec;
  86. template<class T>
  87. inline void SWAP(T& first, T& second)
  88. {
  89. T temp = first;
  90. first = second;
  91. second = temp;
  92. }
  93. // This is a summary of a strip that has been built
  94. class NvStripInfo {
  95. public:
  96. // A little information about the creation of the triangle strips
  97. NvStripInfo(const NvStripStartInfo &startInfo, int stripId, int experimentId = -1) :
  98. m_startInfo(startInfo)
  99. {
  100. m_stripId = stripId;
  101. m_experimentId = experimentId;
  102. visited = false;
  103. m_numDegenerates = 0;
  104. }
  105. // This is an experiment if the experiment id is >= 0
  106. inline bool IsExperiment () const { return m_experimentId >= 0; }
  107. inline bool IsInStrip (const NvFaceInfo *faceInfo) const
  108. {
  109. if(faceInfo == NULL)
  110. return false;
  111. return (m_experimentId >= 0 ? faceInfo->m_testStripId == m_stripId : faceInfo->m_stripId == m_stripId);
  112. }
  113. bool SharesEdge(const NvFaceInfo* faceInfo, NvEdgeInfoVec &edgeInfos);
  114. // take the given forward and backward strips and combine them together
  115. void Combine(const NvFaceInfoVec &forward, const NvFaceInfoVec &backward);
  116. //returns true if the face is "unique", i.e. has a vertex which doesn't exist in the faceVec
  117. bool Unique(NvFaceInfoVec& faceVec, NvFaceInfo* face);
  118. // mark the triangle as taken by this strip
  119. bool IsMarked (NvFaceInfo *faceInfo);
  120. void MarkTriangle(NvFaceInfo *faceInfo);
  121. // build the strip
  122. void Build(NvEdgeInfoVec &edgeInfos, NvFaceInfoVec &faceInfos);
  123. // public data members
  124. NvStripStartInfo m_startInfo;
  125. NvFaceInfoVec m_faces;
  126. int m_stripId;
  127. int m_experimentId;
  128. bool visited;
  129. int m_numDegenerates;
  130. };
  131. typedef std::vector<NvStripInfo*> NvStripInfoVec;
  132. //The actual stripifier
  133. class NvStripifier {
  134. public:
  135. // Constructor
  136. NvStripifier();
  137. ~NvStripifier();
  138. //the target vertex cache size, the structure to place the strips in, and the input indices
  139. void Stripify(const WordVec &in_indices, const int in_cacheSize, const int in_minStripLength,
  140. const unsigned short maxIndex, NvStripInfoVec &allStrips, NvFaceInfoVec &allFaces);
  141. void CreateStrips(const NvStripInfoVec& allStrips, IntVec& stripIndices, const bool bStitchStrips, unsigned int& numSeparateStrips);
  142. static int GetUniqueVertexInB(NvFaceInfo *faceA, NvFaceInfo *faceB);
  143. //static int GetSharedVertex(NvFaceInfo *faceA, NvFaceInfo *faceB);
  144. static void GetSharedVertices(NvFaceInfo *faceA, NvFaceInfo *faceB, int* vertex0, int* vertex1);
  145. static bool IsDegenerate(const NvFaceInfo* face);
  146. protected:
  147. WordVec indices;
  148. int cacheSize;
  149. int minStripLength;
  150. float meshJump;
  151. bool bFirstTimeResetPoint;
  152. /////////////////////////////////////////////////////////////////////////////////
  153. //
  154. // Big mess of functions called during stripification
  155. //
  156. /////////////////////////////////////////////////////////////////////////////////
  157. //********************
  158. bool IsMoneyFace(const NvFaceInfo& face);
  159. bool FaceContainsIndex(const NvFaceInfo& face, const unsigned int index);
  160. bool IsCW(NvFaceInfo *faceInfo, int v0, int v1);
  161. bool NextIsCW(const int numIndices);
  162. bool IsDegenerate(const unsigned short v0, const unsigned short v1, const unsigned short v2);
  163. static int GetNextIndex(const WordVec &indices, NvFaceInfo *face);
  164. static NvEdgeInfo *FindEdgeInfo(NvEdgeInfoVec &edgeInfos, int v0, int v1);
  165. static NvFaceInfo *FindOtherFace(NvEdgeInfoVec &edgeInfos, int v0, int v1, NvFaceInfo *faceInfo);
  166. NvFaceInfo *FindGoodResetPoint(NvFaceInfoVec &faceInfos, NvEdgeInfoVec &edgeInfos);
  167. void FindAllStrips(NvStripInfoVec &allStrips, NvFaceInfoVec &allFaceInfos, NvEdgeInfoVec &allEdgeInfos, int numSamples);
  168. void SplitUpStripsAndOptimize(NvStripInfoVec &allStrips, NvStripInfoVec &outStrips, NvEdgeInfoVec& edgeInfos, NvFaceInfoVec& outFaceList);
  169. void RemoveSmallStrips(NvStripInfoVec& allStrips, NvStripInfoVec& allBigStrips, NvFaceInfoVec& faceList);
  170. bool FindTraversal(NvFaceInfoVec &faceInfos, NvEdgeInfoVec &edgeInfos, NvStripInfo *strip, NvStripStartInfo &startInfo);
  171. int CountRemainingTris(std::list<NvStripInfo*>::iterator iter, std::list<NvStripInfo*>::iterator end);
  172. void CommitStrips(NvStripInfoVec &allStrips, const NvStripInfoVec &strips);
  173. float AvgStripSize(const NvStripInfoVec &strips);
  174. int FindStartPoint(NvFaceInfoVec &faceInfos, NvEdgeInfoVec &edgeInfos);
  175. void UpdateCacheStrip(VertexCache* vcache, NvStripInfo* strip);
  176. void UpdateCacheFace(VertexCache* vcache, NvFaceInfo* face);
  177. float CalcNumHitsStrip(VertexCache* vcache, NvStripInfo* strip);
  178. int CalcNumHitsFace(VertexCache* vcache, NvFaceInfo* face);
  179. int NumNeighbors(NvFaceInfo* face, NvEdgeInfoVec& edgeInfoVec);
  180. void BuildStripifyInfo(NvFaceInfoVec &faceInfos, NvEdgeInfoVec &edgeInfos, const unsigned short maxIndex);
  181. bool AlreadyExists(NvFaceInfo* faceInfo, NvFaceInfoVec& faceInfos);
  182. // let our strip info classes and the other classes get
  183. // to these protected stripificaton methods if they want
  184. friend NvStripInfo;
  185. };
  186. #endif