Skip to content

Commit 3b9c1f6

Browse files
committed
Graph connectedness algorithm.
1 parent f0deaef commit 3b9c1f6

File tree

9 files changed

+297
-131
lines changed

9 files changed

+297
-131
lines changed

MFGD.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<ClCompile Include="math\color.cpp" />
7979
<ClCompile Include="math\euler.cpp" />
8080
<ClCompile Include="math\frustum.cpp" />
81+
<ClCompile Include="math\graph.cpp" />
8182
<ClCompile Include="math\matrix.cpp" />
8283
<ClCompile Include="math\quaternion.cpp" />
8384
<ClCompile Include="math\vector.cpp" />

MFGD.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
<ClCompile Include="renderer\stanfordbunny.cpp">
9797
<Filter>Source Files\renderer</Filter>
9898
</ClCompile>
99+
<ClCompile Include="math\graph.cpp">
100+
<Filter>Source Files\math</Filter>
101+
</ClCompile>
99102
</ItemGroup>
100103
<ItemGroup>
101104
<Library Include="lib\libglfw.lib">

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ SRCS_CPP= \
2121
math/matrix.cpp \
2222
math/quaternion.cpp \
2323
math/vector.cpp \
24+
math/graph.cpp \
2425
renderer/application.cpp \
2526
renderer/image_read.cpp \
2627
renderer/renderer.cpp \

game/game.cpp

Lines changed: 169 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ void CGame::Load()
4444
m_iMonsterTexture = GetRenderer()->LoadTextureIntoGL("monster.png");
4545
m_iCrateTexture = GetRenderer()->LoadTextureIntoGL("crate.png");
4646
m_iNormalTexture = GetRenderer()->LoadTextureIntoGL("normal.png");
47+
48+
GraphReset();
4749
}
4850

4951
void CGame::MakePuff(const Point& p)
@@ -91,6 +93,16 @@ bool CGame::KeyPress(int c)
9193
m_hPlayer->m_vecVelocity.y = 7;
9294
return true;
9395
}
96+
else if (c == 'G')
97+
{
98+
GraphStep();
99+
return true;
100+
}
101+
else if (c == 'R')
102+
{
103+
GraphReset();
104+
return true;
105+
}
94106
else
95107
return CApplication::KeyPress(c);
96108
}
@@ -346,15 +358,12 @@ void CGame::Draw()
346358

347359
r.SetUniform("vecSunlight", vecSunlight);
348360

349-
r.SetUniform("bLighted", true);
361+
r.SetUniform("bLighted", false);
350362
r.SetUniform("bDiffuse", false);
351363

352364
// Render the ground.
353365
r.SetUniform("vecColor", Vector4D(0.6f, 0.7f, 0.9f, 1));
354366
r.SetUniform("vecCameraPosition", GetRenderer()->GetCameraPosition());
355-
r.SetUniform("bNormal", true);
356-
r.SetUniform("iNormal", 1);
357-
r.BindTexture(m_iNormalTexture, 1);
358367
r.BeginRenderTriFan();
359368
r.Normal(Vector(0, 1, 0));
360369
r.Tangent(Vector(1, 0, 0));
@@ -369,43 +378,7 @@ void CGame::Draw()
369378
r.Vertex(Vector(30, 0, -30));
370379
r.EndRender();
371380

372-
r.SetUniform("bNormal", false);
373-
374-
{
375-
CRenderingContext r(pRenderer, true);
376-
377-
r.UseProgram("model");
378-
379-
r.SetUniform("vecSunlight", vecSunlight);
380-
381-
r.SetUniform("vecColor", Vector4D(1, 1, 1, 1));
382-
383-
r.SetUniform("bDiffuse", true);
384-
r.BindTexture(m_iCrateTexture);
385-
386-
Matrix4x4 mScale;
387-
float flScale = Remap(sin(Game()->GetTime()), -1, 1, 1, 2);
388-
mScale.AddScale(Vector(flScale, flScale, flScale));
389-
390-
Matrix4x4 mRotation;
391-
mRotation.SetRotation(Game()->GetTime()*50, Vector(0, 1, 0));
392-
393-
Matrix4x4 mTranslation;
394-
mTranslation.SetTranslation(Vector(cos(Game()->GetTime()), 0, sin(Game()->GetTime()))*10);
395-
396-
Matrix4x4 mModel;
397-
mModel = mTranslation * mRotation * mScale;
398-
r.LoadTransform(mModel);
399-
400-
// Render the triangles.
401-
r.BeginRenderVertexArray(m_iMeshVB);
402-
r.SetPositionBuffer(0U * sizeof(float), 8 * sizeof(float));
403-
r.SetNormalsBuffer(3 * sizeof(float), 8 * sizeof(float));
404-
r.SetTexCoordBuffer(6 * sizeof(float), 8 * sizeof(float));
405-
r.EndRenderVertexArray(m_iMeshSize);
406-
407-
r.SetUniform("bDiffuse", false);
408-
}
381+
r.SetUniform("bLighted", true);
409382

410383
// Prepare a list of entities to render.
411384
m_apRenderOpaqueList.clear();
@@ -433,23 +406,6 @@ void CGame::Draw()
433406
m_apRenderOpaqueList.push_back(pCharacter);
434407
}
435408

436-
{
437-
CRenderingContext r(pRenderer, true);
438-
439-
r.SetUniform("bDiffuse", false);
440-
r.SetUniform("bRimLighting", true);
441-
442-
r.Scale(10, 10, 10);
443-
r.Translate(Vector(0, 0.5f, -2));
444-
445-
r.BeginRenderVertexArray(GetRenderer()->GetBunnyVerts());
446-
r.SetPositionBuffer(GetRenderer()->BunnyPositionOffsetBytes(), GetRenderer()->BunnyStrideBytes());
447-
r.SetNormalsBuffer(GetRenderer()->BunnyNormalOffsetBytes(), GetRenderer()->BunnyStrideBytes());
448-
r.EndRenderVertexArray(GetRenderer()->GetBunnyNumVerts());
449-
450-
r.SetUniform("bRimLighting", false);
451-
}
452-
453409
// Draw all opaque characters first.
454410
DrawCharacters(m_apRenderOpaqueList, false);
455411

@@ -519,6 +475,8 @@ void CGame::Draw()
519475
}
520476
}
521477

478+
GraphDraw();
479+
522480
pRenderer->FinishRendering(&r);
523481

524482
// Call this last. Your rendered stuff won't appear on the screen until you call this.
@@ -819,3 +777,156 @@ CCharacter* CGame::GetCharacterIndex(size_t i)
819777
{
820778
return m_apEntityList[i];
821779
}
780+
781+
void CGame::GraphReset()
782+
{
783+
m_eGraphStep = GRAPHSTEP_PICKUNSEEN;
784+
m_iCurrentGroup = 0;
785+
m_aiCurrentNodes.clear();
786+
787+
m_Graph = CGraph();
788+
789+
m_Graph.AddNode();
790+
m_Graph.AddNode();
791+
m_Graph.AddNode();
792+
793+
m_Graph.AddNode();
794+
m_Graph.AddNode();
795+
m_Graph.AddNode();
796+
m_Graph.AddNode();
797+
798+
m_Graph.AddEdge(0, 1);
799+
m_Graph.AddEdge(1, 2);
800+
801+
m_Graph.AddEdge(3, 4);
802+
m_Graph.AddEdge(3, 5);
803+
m_Graph.AddEdge(3, 6);
804+
m_Graph.AddEdge(4, 5);
805+
m_Graph.AddEdge(5, 6);
806+
807+
m_Graph.GetNode(0)->debug_position = Vector(0, 0, -5) * 4;
808+
m_Graph.GetNode(1)->debug_position = Vector(-1, 0, -4) * 4;
809+
m_Graph.GetNode(2)->debug_position = Vector(0, 0, -3) * 4;
810+
811+
m_Graph.GetNode(3)->debug_position = Vector(2, 0, -5) * 4;
812+
m_Graph.GetNode(4)->debug_position = Vector(4, 0, -5) * 4;
813+
m_Graph.GetNode(5)->debug_position = Vector(4, 0, -3) * 4;
814+
m_Graph.GetNode(6)->debug_position = Vector(2, 0, -3) * 4;
815+
}
816+
817+
void CGame::GraphStep()
818+
{
819+
if (m_eGraphStep == GRAPHSTEP_PICKUNSEEN)
820+
{
821+
int iCurrentNode = -1;
822+
for (int i = 0; i < m_Graph.GetNumNodes(); i++)
823+
{
824+
if (m_Graph.GetNode(i)->HasBeenSeen())
825+
continue;
826+
827+
iCurrentNode = i;
828+
break;
829+
}
830+
831+
if (iCurrentNode >= 0)
832+
{
833+
m_aiCurrentNodes.push_back(iCurrentNode);
834+
m_eGraphStep = GRAPHSTEP_SKIPSEEN;
835+
}
836+
}
837+
else if (m_eGraphStep == GRAPHSTEP_SKIPSEEN)
838+
{
839+
while (m_aiCurrentNodes.size() && m_Graph.GetNode(m_aiCurrentNodes.back())->HasBeenSeen())
840+
m_aiCurrentNodes.pop_back();
841+
842+
if (!m_aiCurrentNodes.size())
843+
m_eGraphStep = GRAPHSTEP_INCREASEGROUP;
844+
else
845+
m_eGraphStep = GRAPHSTEP_MARKGROUP;
846+
}
847+
else if (m_eGraphStep == GRAPHSTEP_MARKGROUP)
848+
{
849+
m_Graph.GetNode(m_aiCurrentNodes.back())->group = m_iCurrentGroup;
850+
851+
m_eGraphStep = GRAPHSTEP_FOLLOWEDGES;
852+
}
853+
else if (m_eGraphStep == GRAPHSTEP_FOLLOWEDGES)
854+
{
855+
int node = m_aiCurrentNodes.back();
856+
m_aiCurrentNodes.pop_back();
857+
858+
for (edge_t i = 0; i < (edge_t)m_Graph.GetNode(node)->edges.size(); i++)
859+
{
860+
edge_t edge = m_Graph.GetNode(node)->edges[i];
861+
node_t new_node = m_Graph.FollowEdge(node, edge);
862+
m_aiCurrentNodes.push_back(new_node);
863+
}
864+
865+
m_eGraphStep = GRAPHSTEP_SKIPSEEN;
866+
}
867+
else if (m_eGraphStep == GRAPHSTEP_INCREASEGROUP)
868+
{
869+
m_iCurrentGroup++;
870+
871+
m_eGraphStep = GRAPHSTEP_PICKUNSEEN;
872+
}
873+
}
874+
875+
void CGame::GraphDraw()
876+
{
877+
CRenderingContext c(GetRenderer(), true);
878+
879+
for (int i = 0; i < m_Graph.GetNumNodes(); i++)
880+
{
881+
CGraph::CNode* node = m_Graph.GetNode(i);
882+
883+
if (node->group >= 0)
884+
{
885+
switch (node->group)
886+
{
887+
case 0:
888+
c.SetUniform("vecColor", Color(255, 255, 0, 255));
889+
break;
890+
case 1:
891+
c.SetUniform("vecColor", Color(255, 0, 255, 255));
892+
break;
893+
default:
894+
c.SetUniform("vecColor", Color(255, 255, 255, 255));
895+
break;
896+
}
897+
}
898+
else
899+
{
900+
if (m_aiCurrentNodes.size() && m_aiCurrentNodes.back() == i)
901+
c.SetUniform("vecColor", Color(0, 255, 0, 255));
902+
else
903+
{
904+
c.SetUniform("vecColor", Color(255, 255, 255, 255));
905+
906+
for (size_t j = 0; j < m_aiCurrentNodes.size(); j++)
907+
{
908+
if (m_aiCurrentNodes[j] == i)
909+
{
910+
c.SetUniform("vecColor", Color(0, 0, 255, 255));
911+
break;
912+
}
913+
}
914+
}
915+
}
916+
917+
c.RenderBox(node->debug_position - Vector(1, 1, 1), node->debug_position + Vector(1, 1, 1));
918+
}
919+
920+
for (int i = 0; i < m_Graph.GetNumEdges(); i++)
921+
{
922+
CGraph::CEdge* edge = m_Graph.GetEdge(i);
923+
CGraph::CNode* node1 = m_Graph.GetNode(edge->first);
924+
CGraph::CNode* node2 = m_Graph.GetNode(edge->second);
925+
926+
c.SetUniform("vecColor", Vector4D(1, 1, 1, 1));
927+
c.BeginRenderLines();
928+
c.Vertex(node1->debug_position + Vector(0, 0.1f, 0));
929+
c.Vertex(node2->debug_position + Vector(0, 0.1f, 0));
930+
c.EndRender();
931+
}
932+
}

game/game.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON A
2222
#include <common/common.h>
2323

2424
#include <math/frustum.h>
25+
#include <math/graph.h>
2526

2627
#include <renderer/application.h>
2728

@@ -87,6 +88,10 @@ class CGame : public CApplication
8788

8889
size_t GetMonsterTexture() { return m_iMonsterTexture; }
8990

91+
void GraphStep();
92+
void GraphReset();
93+
void GraphDraw();
94+
9095
private:
9196
int m_iLastMouseX;
9297
int m_iLastMouseY;
@@ -109,6 +114,21 @@ class CGame : public CApplication
109114

110115
size_t m_iMeshVB;
111116
size_t m_iMeshSize;
117+
118+
typedef enum
119+
{
120+
GRAPHSTEP_PICKUNSEEN,
121+
GRAPHSTEP_SKIPSEEN,
122+
GRAPHSTEP_MARKGROUP,
123+
GRAPHSTEP_FOLLOWEDGES,
124+
GRAPHSTEP_INCREASEGROUP,
125+
} graph_step_t;
126+
127+
graph_step_t m_eGraphStep;
128+
std::vector<int> m_aiCurrentNodes;
129+
int m_iCurrentGroup;
130+
131+
CGraph m_Graph;
112132
};
113133

114134
inline CGame* Game()

0 commit comments

Comments
 (0)