Team Fortress 2 Source Code as on 22/4/2020
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.

104 lines
2.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A set of utilities to render standard shapes
  4. //
  5. //===========================================================================//
  6. #include "tier2/meshutils.h"
  7. //-----------------------------------------------------------------------------
  8. // Helper methods to create various standard index buffer types
  9. //-----------------------------------------------------------------------------
  10. void GenerateSequentialIndexBuffer( unsigned short* pIndices, int nIndexCount, int nFirstVertex )
  11. {
  12. if ( !pIndices )
  13. return;
  14. // Format the sequential buffer
  15. for ( int i = 0; i < nIndexCount; ++i )
  16. {
  17. pIndices[i] = (unsigned short)( i + nFirstVertex );
  18. }
  19. }
  20. void GenerateQuadIndexBuffer( unsigned short* pIndices, int nIndexCount, int nFirstVertex )
  21. {
  22. if ( !pIndices )
  23. return;
  24. // Format the quad buffer
  25. int i;
  26. int numQuads = nIndexCount / 6;
  27. int baseVertex = nFirstVertex;
  28. for ( i = 0; i < numQuads; ++i)
  29. {
  30. // Triangle 1
  31. pIndices[0] = (unsigned short)( baseVertex );
  32. pIndices[1] = (unsigned short)( baseVertex + 1 );
  33. pIndices[2] = (unsigned short)( baseVertex + 2 );
  34. // Triangle 2
  35. pIndices[3] = (unsigned short)( baseVertex );
  36. pIndices[4] = (unsigned short)( baseVertex + 2 );
  37. pIndices[5] = (unsigned short)( baseVertex + 3 );
  38. baseVertex += 4;
  39. pIndices += 6;
  40. }
  41. }
  42. void GeneratePolygonIndexBuffer( unsigned short* pIndices, int nIndexCount, int nFirstVertex )
  43. {
  44. if ( !pIndices )
  45. return;
  46. int i;
  47. int numPolygons = nIndexCount / 3;
  48. for ( i = 0; i < numPolygons; ++i)
  49. {
  50. // Triangle 1
  51. pIndices[0] = (unsigned short)( nFirstVertex );
  52. pIndices[1] = (unsigned short)( nFirstVertex + i + 1 );
  53. pIndices[2] = (unsigned short)( nFirstVertex + i + 2 );
  54. pIndices += 3;
  55. }
  56. }
  57. void GenerateLineStripIndexBuffer( unsigned short* pIndices, int nIndexCount, int nFirstVertex )
  58. {
  59. if ( !pIndices )
  60. return;
  61. int i;
  62. int numLines = nIndexCount / 2;
  63. for ( i = 0; i < numLines; ++i)
  64. {
  65. pIndices[0] = (unsigned short)( nFirstVertex + i );
  66. pIndices[1] = (unsigned short)( nFirstVertex + i + 1 );
  67. pIndices += 2;
  68. }
  69. }
  70. void GenerateLineLoopIndexBuffer( unsigned short* pIndices, int nIndexCount, int nFirstVertex )
  71. {
  72. if ( !pIndices )
  73. {
  74. return;
  75. }
  76. int i;
  77. int numLines = nIndexCount / 2;
  78. pIndices[0] = (unsigned short)( nFirstVertex + numLines - 1 );
  79. pIndices[1] = (unsigned short)( nFirstVertex );
  80. pIndices += 2;
  81. for ( i = 1; i < numLines; ++i)
  82. {
  83. pIndices[0] = (unsigned short)( nFirstVertex + i - 1 );
  84. pIndices[1] = (unsigned short)( nFirstVertex + i );
  85. pIndices += 2;
  86. }
  87. }