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.

140 lines
3.5 KiB

  1. //===== Copyright � 2005-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: A set of utilities to render standard shapes
  4. //
  5. //===========================================================================//
  6. #include "tier2/meshutils.h"
  7. #include "tier0/platform.h"
  8. // NOTE: This has to be the last file included!
  9. #include "tier0/memdbgon.h"
  10. //-----------------------------------------------------------------------------
  11. // Helper methods to create various standard index buffer types
  12. //-----------------------------------------------------------------------------
  13. void GenerateSequentialIndexBuffer( unsigned short* pIndices, int nIndexCount, int nFirstVertex )
  14. {
  15. if ( !pIndices )
  16. return;
  17. // Format the sequential buffer
  18. for ( int i = 0; i < nIndexCount; ++i )
  19. {
  20. pIndices[i] = (unsigned short)( i + nFirstVertex );
  21. }
  22. }
  23. void GenerateQuadIndexBuffer( unsigned short *pIndices, int nIndexCount, int nFirstVertex )
  24. {
  25. if ( !pIndices )
  26. return;
  27. // Format the quad buffer
  28. int i;
  29. int numQuads = nIndexCount / 6;
  30. unsigned short baseVertex = (unsigned short)nFirstVertex;
  31. if ( ( (size_t)pIndices & 0x3 ) == 0 )
  32. {
  33. // Fast version, requires aligned indices
  34. int *pWrite = (int*)pIndices;
  35. int nWrite = ( baseVertex << 16 ) | baseVertex;
  36. for ( i = 0; i < numQuads; ++i )
  37. {
  38. // Have to deal with endian-ness
  39. if ( IsX360() || IsPS3() )
  40. {
  41. // this avoids compiler write reodering and prevents the write-combined out-of-order penalty
  42. // _WriteBarrier won't compile here, and volatile is ignored
  43. // the compiler otherwise scrambles these writes
  44. *pWrite++ = nWrite + 1;
  45. *pWrite++ = nWrite + ( 2 << 16 );
  46. *pWrite++ = nWrite + ( 2 << 16 ) + 3;
  47. }
  48. else
  49. {
  50. pWrite[0] = nWrite + ( 1 << 16 );
  51. pWrite[1] = nWrite + 2;
  52. pWrite[2] = nWrite + ( 3 << 16 ) + 2;
  53. pWrite += 3;
  54. }
  55. nWrite += ( 4 << 16 ) | 4;
  56. }
  57. }
  58. else
  59. {
  60. for ( i = 0; i < numQuads; ++i )
  61. {
  62. pIndices[0] = baseVertex;
  63. pIndices[1] = baseVertex + 1;
  64. pIndices[2] = baseVertex + 2;
  65. // Triangle 2
  66. pIndices[3] = baseVertex;
  67. pIndices[4] = baseVertex + 2;
  68. pIndices[5] = baseVertex + 3;
  69. baseVertex += 4;
  70. pIndices += 6;
  71. }
  72. }
  73. }
  74. void GeneratePolygonIndexBuffer( unsigned short* pIndices, int nIndexCount, int nFirstVertex )
  75. {
  76. if ( !pIndices )
  77. return;
  78. int i, baseVertex;
  79. int numPolygons = nIndexCount / 3;
  80. baseVertex = nFirstVertex;
  81. for ( i = 0; i < numPolygons; ++i)
  82. {
  83. // Triangle 1
  84. pIndices[0] = (unsigned short)( nFirstVertex );
  85. pIndices[1] = (unsigned short)( nFirstVertex + i + 1 );
  86. pIndices[2] = (unsigned short)( nFirstVertex + i + 2 );
  87. pIndices += 3;
  88. }
  89. }
  90. void GenerateLineStripIndexBuffer( unsigned short* pIndices, int nIndexCount, int nFirstVertex )
  91. {
  92. if ( !pIndices )
  93. return;
  94. int i, baseVertex;
  95. int numLines = nIndexCount / 2;
  96. baseVertex = nFirstVertex;
  97. for ( i = 0; i < numLines; ++i)
  98. {
  99. pIndices[0] = (unsigned short)( nFirstVertex + i );
  100. pIndices[1] = (unsigned short)( nFirstVertex + i + 1 );
  101. pIndices += 2;
  102. }
  103. }
  104. void GenerateLineLoopIndexBuffer( unsigned short* pIndices, int nIndexCount, int nFirstVertex )
  105. {
  106. if ( !pIndices )
  107. {
  108. return;
  109. }
  110. int i, baseVertex;
  111. int numLines = nIndexCount / 2;
  112. baseVertex = nFirstVertex;
  113. pIndices[0] = (unsigned short)( nFirstVertex + numLines - 1 );
  114. pIndices[1] = (unsigned short)( nFirstVertex );
  115. pIndices += 2;
  116. for ( i = 1; i < numLines; ++i)
  117. {
  118. pIndices[0] = (unsigned short)( nFirstVertex + i - 1 );
  119. pIndices[1] = (unsigned short)( nFirstVertex + i );
  120. pIndices += 2;
  121. }
  122. }