Skip to content

Commit 3a55f65

Browse files
committed
Index our triangles to save space. (This example doesn't actually save any space, but much space would be saved for more complex meshes, I promise.)
1 parent 0fa2f63 commit 3a55f65

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

game/game.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ void CGame::Draw()
356356
// Render the triangles.
357357
r.BeginRenderVertexArray(m_iBillboardVB);
358358
r.SetPositionBuffer(0);
359-
r.EndRenderVertexArray(6);
359+
r.EndRenderVertexArrayIndexed(m_iBillboardIB, 6);
360360

361361
// Prepare a list of entities to render.
362362
m_apRenderOpaqueList.clear();
@@ -662,22 +662,31 @@ void CGame::GameLoop()
662662
pProp4->m_aabbSize.vecMax = vecPropMax;
663663
pProp4->m_clrRender = Color(0.4f, 0.8f, 0.2f, 1.0f);
664664

665-
// Create an array of vectors to store our triangles.
665+
// Create an array of vectors to store our vertexes.
666666
vector<Vector> avecPoints;
667667

668-
// First triangle.
669-
avecPoints.push_back(Vector(1, 1, 0));
670-
avecPoints.push_back(Vector(0, 1, 0));
671-
avecPoints.push_back(Vector(1, 0, 0));
668+
avecPoints.push_back(Vector(1, 1, 0)); // A
669+
avecPoints.push_back(Vector(0, 1, 0)); // B
670+
avecPoints.push_back(Vector(0, 0, 0)); // C
671+
avecPoints.push_back(Vector(1, 0, 0)); // D
672672

673-
// Second triangle.
674-
avecPoints.push_back(Vector(1, 0, 0));
675-
avecPoints.push_back(Vector(0, 1, 0));
676-
avecPoints.push_back(Vector(0, 0, 0));
673+
vector<unsigned int> aiIndices;
674+
675+
// First triangle
676+
aiIndices.push_back(3);
677+
aiIndices.push_back(0);
678+
aiIndices.push_back(1);
679+
680+
// Second triangle
681+
aiIndices.push_back(3);
682+
aiIndices.push_back(1);
683+
aiIndices.push_back(2);
677684

678685
m_iBillboardVB = CRenderer::LoadVertexDataIntoGL(avecPoints.size() * sizeof(Vector), &avecPoints[0].x);
686+
m_iBillboardIB = CRenderer::LoadIndexDataIntoGL(aiIndices.size() * sizeof(unsigned int), &aiIndices[0]);
679687

680688
avecPoints.clear();
689+
aiIndices.clear();
681690

682691
float flPreviousTime = 0;
683692
float flCurrentTime = Application()->GetTime();

game/game.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class CGame : public CApplication
106106
CHandle m_hPlayer;
107107

108108
size_t m_iBillboardVB;
109+
size_t m_iBillboardIB;
109110
};
110111

111112
inline CGame* Game()

0 commit comments

Comments
 (0)