summaryrefslogtreecommitdiff
path: root/tier2/meshutils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tier2/meshutils.cpp')
-rw-r--r--tier2/meshutils.cpp104
1 files changed, 104 insertions, 0 deletions
diff --git a/tier2/meshutils.cpp b/tier2/meshutils.cpp
new file mode 100644
index 0000000..4aa0144
--- /dev/null
+++ b/tier2/meshutils.cpp
@@ -0,0 +1,104 @@
+//========= Copyright Valve Corporation, All rights reserved. ============//
+//
+// Purpose: A set of utilities to render standard shapes
+//
+//===========================================================================//
+
+#include "tier2/meshutils.h"
+
+
+//-----------------------------------------------------------------------------
+// Helper methods to create various standard index buffer types
+//-----------------------------------------------------------------------------
+void GenerateSequentialIndexBuffer( unsigned short* pIndices, int nIndexCount, int nFirstVertex )
+{
+ if ( !pIndices )
+ return;
+
+ // Format the sequential buffer
+ for ( int i = 0; i < nIndexCount; ++i )
+ {
+ pIndices[i] = (unsigned short)( i + nFirstVertex );
+ }
+}
+
+void GenerateQuadIndexBuffer( unsigned short* pIndices, int nIndexCount, int nFirstVertex )
+{
+ if ( !pIndices )
+ return;
+
+ // Format the quad buffer
+ int i;
+ int numQuads = nIndexCount / 6;
+ int baseVertex = nFirstVertex;
+ for ( i = 0; i < numQuads; ++i)
+ {
+ // Triangle 1
+ pIndices[0] = (unsigned short)( baseVertex );
+ pIndices[1] = (unsigned short)( baseVertex + 1 );
+ pIndices[2] = (unsigned short)( baseVertex + 2 );
+
+ // Triangle 2
+ pIndices[3] = (unsigned short)( baseVertex );
+ pIndices[4] = (unsigned short)( baseVertex + 2 );
+ pIndices[5] = (unsigned short)( baseVertex + 3 );
+
+ baseVertex += 4;
+ pIndices += 6;
+ }
+}
+
+void GeneratePolygonIndexBuffer( unsigned short* pIndices, int nIndexCount, int nFirstVertex )
+{
+ if ( !pIndices )
+ return;
+
+ int i;
+ int numPolygons = nIndexCount / 3;
+ for ( i = 0; i < numPolygons; ++i)
+ {
+ // Triangle 1
+ pIndices[0] = (unsigned short)( nFirstVertex );
+ pIndices[1] = (unsigned short)( nFirstVertex + i + 1 );
+ pIndices[2] = (unsigned short)( nFirstVertex + i + 2 );
+ pIndices += 3;
+ }
+}
+
+
+void GenerateLineStripIndexBuffer( unsigned short* pIndices, int nIndexCount, int nFirstVertex )
+{
+ if ( !pIndices )
+ return;
+
+ int i;
+ int numLines = nIndexCount / 2;
+ for ( i = 0; i < numLines; ++i)
+ {
+ pIndices[0] = (unsigned short)( nFirstVertex + i );
+ pIndices[1] = (unsigned short)( nFirstVertex + i + 1 );
+ pIndices += 2;
+ }
+}
+
+void GenerateLineLoopIndexBuffer( unsigned short* pIndices, int nIndexCount, int nFirstVertex )
+{
+ if ( !pIndices )
+ {
+ return;
+ }
+
+ int i;
+ int numLines = nIndexCount / 2;
+
+ pIndices[0] = (unsigned short)( nFirstVertex + numLines - 1 );
+ pIndices[1] = (unsigned short)( nFirstVertex );
+ pIndices += 2;
+
+ for ( i = 1; i < numLines; ++i)
+ {
+ pIndices[0] = (unsigned short)( nFirstVertex + i - 1 );
+ pIndices[1] = (unsigned short)( nFirstVertex + i );
+ pIndices += 2;
+ }
+}