Skip to content

Commit a9ac97b

Browse files
committed
Add normals, some smooth and some creased, to our mesh, to help simulate lighting.
1 parent 8a58aef commit a9ac97b

File tree

2 files changed

+181
-20
lines changed

2 files changed

+181
-20
lines changed

game/game.cpp

Lines changed: 176 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ void CGame::Draw()
338338
r.UseProgram("model");
339339

340340
// Set the sunlight direction. The y component is -1 so the light is pointing down.
341-
r.SetUniform("vecSunlight", Vector(-1, -1, 0.5f).Normalized());
341+
Vector vecSunlight = Vector(cos(Game()->GetTime()), -1, sin(Game()->GetTime())).Normalized();
342+
343+
r.SetUniform("vecSunlight", vecSunlight);
342344

343345
r.SetUniform("bDiffuse", false);
344346

@@ -352,21 +354,39 @@ void CGame::Draw()
352354
r.Vertex(Vector(30, 0, -30));
353355
r.EndRender();
354356

355-
r.SetUniform("vecColor", Vector4D(1, 1, 1, 1));
357+
{
358+
CRenderingContext r(pRenderer, true);
359+
360+
r.UseProgram("model");
356361

357-
r.SetUniform("bDiffuse", true);
358-
r.BindTexture(m_iCrateTexture);
362+
r.SetUniform("vecSunlight", vecSunlight);
359363

360-
r.Scale(4, 4, 4);
364+
r.SetUniform("vecColor", Vector4D(1, 1, 1, 1));
361365

362-
// Render the triangles.
363-
r.BeginRenderVertexArray(m_iBillboardVB);
364-
r.SetPositionBuffer(0U * sizeof(float), 5 * sizeof(float));
365-
r.SetTexCoordBuffer(3 * sizeof(float), 5 * sizeof(float));
366-
r.EndRenderVertexArrayIndexed(m_iBillboardIB, 6);
366+
r.SetUniform("bDiffuse", true);
367+
r.BindTexture(m_iCrateTexture);
367368

368-
r.SetUniform("bDiffuse", false);
369-
r.Scale(.25, .25, .25);
369+
r.Scale(4, 4, 4);
370+
371+
r.Translate(Vector(1, 0, 0));
372+
373+
// Render the triangles.
374+
r.BeginRenderVertexArray(m_iMeshSmoothVB);
375+
r.SetPositionBuffer(0U * sizeof(float), 8 * sizeof(float));
376+
r.SetTexCoordBuffer(3 * sizeof(float), 8 * sizeof(float));
377+
r.SetNormalsBuffer(5 * sizeof(float), 8 * sizeof(float));
378+
r.EndRenderVertexArrayIndexed(m_iMeshSmoothIB, 12);
379+
380+
r.Translate(Vector(-1, 0, -1));
381+
382+
r.BeginRenderVertexArray(m_iMeshCreasedVB);
383+
r.SetPositionBuffer(0U * sizeof(float), 8 * sizeof(float));
384+
r.SetTexCoordBuffer(3 * sizeof(float), 8 * sizeof(float));
385+
r.SetNormalsBuffer(5 * sizeof(float), 8 * sizeof(float));
386+
r.EndRenderVertexArrayIndexed(m_iMeshCreasedIB, 12);
387+
388+
r.SetUniform("bDiffuse", false);
389+
}
370390

371391
// Prepare a list of entities to render.
372392
m_apRenderOpaqueList.clear();
@@ -686,47 +706,185 @@ void CGame::GameLoop()
686706
vector<float> aflPoints;
687707

688708
// A
689-
aflPoints.push_back(1); // Three floats - Position x
709+
aflPoints.push_back(1); // Position - Three floats x
690710
aflPoints.push_back(1); // y
691711
aflPoints.push_back(0); // z
692-
aflPoints.push_back(1); // Two floats - Vertex coordinate u
712+
aflPoints.push_back(1); // Vertex coordinates - Two floats u
693713
aflPoints.push_back(1); // v
714+
aflPoints.push_back(0); // Normal - Three floats x
715+
aflPoints.push_back(0); // y
716+
aflPoints.push_back(1); // z
694717

695718
// B
696719
aflPoints.push_back(0);
697720
aflPoints.push_back(1);
698721
aflPoints.push_back(0);
699722
aflPoints.push_back(0);
700723
aflPoints.push_back(1);
724+
aflPoints.push_back(0);
725+
aflPoints.push_back(0);
726+
aflPoints.push_back(1);
701727

702728
// C
703729
aflPoints.push_back(0);
704730
aflPoints.push_back(0);
705731
aflPoints.push_back(0);
706732
aflPoints.push_back(0);
707733
aflPoints.push_back(0);
734+
aflPoints.push_back(0);
735+
aflPoints.push_back(0);
736+
aflPoints.push_back(1);
708737

709738
// D
710739
aflPoints.push_back(1);
711740
aflPoints.push_back(0);
712741
aflPoints.push_back(0);
713742
aflPoints.push_back(1);
714743
aflPoints.push_back(0);
744+
aflPoints.push_back(0);
745+
aflPoints.push_back(0);
746+
aflPoints.push_back(1);
747+
748+
// A
749+
aflPoints.push_back(0);
750+
aflPoints.push_back(1);
751+
aflPoints.push_back(0);
752+
aflPoints.push_back(1);
753+
aflPoints.push_back(1);
754+
aflPoints.push_back(-1);
755+
aflPoints.push_back(0);
756+
aflPoints.push_back(0);
757+
758+
// B
759+
aflPoints.push_back(0);
760+
aflPoints.push_back(1);
761+
aflPoints.push_back(-1);
762+
aflPoints.push_back(0);
763+
aflPoints.push_back(1);
764+
aflPoints.push_back(-1);
765+
aflPoints.push_back(0);
766+
aflPoints.push_back(0);
767+
768+
// C
769+
aflPoints.push_back(0);
770+
aflPoints.push_back(0);
771+
aflPoints.push_back(-1);
772+
aflPoints.push_back(0);
773+
aflPoints.push_back(0);
774+
aflPoints.push_back(-1);
775+
aflPoints.push_back(0);
776+
aflPoints.push_back(0);
777+
778+
// D
779+
aflPoints.push_back(0);
780+
aflPoints.push_back(0);
781+
aflPoints.push_back(0);
782+
aflPoints.push_back(1);
783+
aflPoints.push_back(0);
784+
aflPoints.push_back(-1);
785+
aflPoints.push_back(0);
786+
aflPoints.push_back(0);
715787

716788
vector<unsigned int> aiIndices;
717789

718-
// First triangle
719790
aiIndices.push_back(3);
720791
aiIndices.push_back(0);
721792
aiIndices.push_back(1);
722793

723-
// Second triangle
724794
aiIndices.push_back(3);
725795
aiIndices.push_back(1);
726796
aiIndices.push_back(2);
727797

728-
m_iBillboardVB = CRenderer::LoadVertexDataIntoGL(aflPoints.size() * sizeof(float), &aflPoints[0]);
729-
m_iBillboardIB = CRenderer::LoadIndexDataIntoGL(aiIndices.size() * sizeof(unsigned int), &aiIndices[0]);
798+
aiIndices.push_back(7);
799+
aiIndices.push_back(4);
800+
aiIndices.push_back(5);
801+
802+
aiIndices.push_back(7);
803+
aiIndices.push_back(5);
804+
aiIndices.push_back(6);
805+
806+
m_iMeshCreasedVB = CRenderer::LoadVertexDataIntoGL(aflPoints.size() * sizeof(float), &aflPoints[0]);
807+
m_iMeshCreasedIB = CRenderer::LoadIndexDataIntoGL(aiIndices.size() * sizeof(unsigned int), &aiIndices[0]);
808+
809+
aflPoints.clear();
810+
aiIndices.clear();
811+
812+
Vector vecFront(0, 0, 1);
813+
Vector vecSide(-1, 0, 0);
814+
Vector vecAverage = ((vecFront + vecSide)*0.5f).Normalized();
815+
816+
aflPoints.push_back(1); // Three floats - Position x
817+
aflPoints.push_back(1); // y
818+
aflPoints.push_back(0); // z
819+
aflPoints.push_back(1); // Two floats - Vertex coordinate u
820+
aflPoints.push_back(1); // v
821+
aflPoints.push_back(vecFront.x);
822+
aflPoints.push_back(vecFront.y);
823+
aflPoints.push_back(vecFront.z);
824+
825+
aflPoints.push_back(0);
826+
aflPoints.push_back(1);
827+
aflPoints.push_back(0);
828+
aflPoints.push_back(0);
829+
aflPoints.push_back(1);
830+
aflPoints.push_back(vecAverage.x);
831+
aflPoints.push_back(vecAverage.y);
832+
aflPoints.push_back(vecAverage.z);
833+
834+
aflPoints.push_back(0);
835+
aflPoints.push_back(0);
836+
aflPoints.push_back(0);
837+
aflPoints.push_back(0);
838+
aflPoints.push_back(0);
839+
aflPoints.push_back(vecAverage.x);
840+
aflPoints.push_back(vecAverage.y);
841+
aflPoints.push_back(vecAverage.z);
842+
843+
aflPoints.push_back(1);
844+
aflPoints.push_back(0);
845+
aflPoints.push_back(0);
846+
aflPoints.push_back(1);
847+
aflPoints.push_back(0);
848+
aflPoints.push_back(vecFront.x);
849+
aflPoints.push_back(vecFront.y);
850+
aflPoints.push_back(vecFront.z);
851+
852+
aflPoints.push_back(0);
853+
aflPoints.push_back(0);
854+
aflPoints.push_back(-1);
855+
aflPoints.push_back(-1);
856+
aflPoints.push_back(0);
857+
aflPoints.push_back(vecSide.x);
858+
aflPoints.push_back(vecSide.y);
859+
aflPoints.push_back(vecSide.z);
860+
861+
aflPoints.push_back(0);
862+
aflPoints.push_back(1);
863+
aflPoints.push_back(-1);
864+
aflPoints.push_back(-1);
865+
aflPoints.push_back(1);
866+
aflPoints.push_back(vecSide.x);
867+
aflPoints.push_back(vecSide.y);
868+
aflPoints.push_back(vecSide.z);
869+
870+
aiIndices.push_back(3);
871+
aiIndices.push_back(0);
872+
aiIndices.push_back(1);
873+
874+
aiIndices.push_back(3);
875+
aiIndices.push_back(1);
876+
aiIndices.push_back(2);
877+
878+
aiIndices.push_back(2);
879+
aiIndices.push_back(1);
880+
aiIndices.push_back(5);
881+
882+
aiIndices.push_back(2);
883+
aiIndices.push_back(5);
884+
aiIndices.push_back(4);
885+
886+
m_iMeshSmoothVB = CRenderer::LoadVertexDataIntoGL(aflPoints.size() * sizeof(float), &aflPoints[0]);
887+
m_iMeshSmoothIB = CRenderer::LoadIndexDataIntoGL(aiIndices.size() * sizeof(unsigned int), &aiIndices[0]);
730888

731889
aflPoints.clear();
732890
aiIndices.clear();

game/game.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,11 @@ class CGame : public CApplication
106106
// This is the player character
107107
CHandle m_hPlayer;
108108

109-
size_t m_iBillboardVB;
110-
size_t m_iBillboardIB;
109+
size_t m_iMeshSmoothVB;
110+
size_t m_iMeshSmoothIB;
111+
112+
size_t m_iMeshCreasedVB;
113+
size_t m_iMeshCreasedIB;
111114
};
112115

113116
inline CGame* Game()

0 commit comments

Comments
 (0)