Browse Source

Chapter 7 (PC)

Sanjay Madhav 8 years ago
parent
commit
225600f1c1
56 changed files with 255 additions and 232 deletions
  1. 1 1
      Chapter06/Assets/Cube.gpmesh
  2. 1 1
      Chapter06/Assets/Plane.gpmesh
  3. 1 1
      Chapter06/Assets/Sphere.gpmesh
  4. 2 2
      Chapter06/Shaders/BasicMesh.frag
  5. 2 2
      Chapter06/Shaders/BasicMesh.vert
  6. 2 2
      Chapter06/Shaders/Phong.frag
  7. 2 2
      Chapter06/Shaders/Phong.vert
  8. 2 2
      Chapter06/Shaders/Sprite.frag
  9. 2 2
      Chapter06/Shaders/Sprite.vert
  10. 1 0
      Chapter07/Assets/Cube.gpmesh
  11. 1 0
      Chapter07/Assets/Plane.gpmesh
  12. 1 0
      Chapter07/Assets/Sphere.gpmesh
  13. 1 1
      Chapter07/AudioComponent.cpp
  14. 1 1
      Chapter07/AudioComponent.h
  15. 1 1
      Chapter07/AudioSystem.cpp
  16. 1 1
      Chapter07/AudioSystem.h
  17. 2 2
      Chapter07/CameraActor.cpp
  18. 2 2
      Chapter07/CameraActor.h
  19. 0 0
      Chapter07/Chapter07-windows.sln
  20. 16 3
      Chapter07/CircleComponent.cpp
  21. 6 4
      Chapter07/CircleComponent.h
  22. 0 20
      Chapter07/CollisionComponent.cpp
  23. 0 17
      Chapter07/CollisionComponent.h
  24. 49 24
      Chapter07/Game.cpp
  25. 5 1
      Chapter07/Game.h
  26. 1 2
      Chapter07/Game.vcxproj
  27. 0 6
      Chapter07/Game.vcxproj.filters
  28. 1 1
      Chapter07/Main.cpp
  29. 17 18
      Chapter07/Math.cpp
  30. 1 1
      Chapter07/Math.h
  31. 6 3
      Chapter07/Mesh.cpp
  32. 5 1
      Chapter07/Mesh.h
  33. 4 2
      Chapter07/MeshComponent.cpp
  34. 2 2
      Chapter07/MeshComponent.h
  35. 2 2
      Chapter07/MoveComponent.cpp
  36. 2 2
      Chapter07/MoveComponent.h
  37. 1 1
      Chapter07/PlaneActor.cpp
  38. 1 1
      Chapter07/PlaneActor.h
  39. 51 43
      Chapter07/Renderer.cpp
  40. 8 6
      Chapter07/Renderer.h
  41. 12 12
      Chapter07/Shader.cpp
  42. 4 5
      Chapter07/Shader.h
  43. 2 2
      Chapter07/Shaders/BasicMesh.frag
  44. 2 2
      Chapter07/Shaders/BasicMesh.vert
  45. 7 8
      Chapter07/Shaders/Phong.frag
  46. 2 2
      Chapter07/Shaders/Phong.vert
  47. 2 2
      Chapter07/Shaders/Sprite.frag
  48. 2 2
      Chapter07/Shaders/Sprite.vert
  49. 1 1
      Chapter07/SoundEvent.cpp
  50. 1 1
      Chapter07/SoundEvent.h
  51. 1 1
      Chapter07/SpriteComponent.cpp
  52. 1 1
      Chapter07/SpriteComponent.h
  53. 2 2
      Chapter07/Texture.cpp
  54. 2 2
      Chapter07/Texture.h
  55. 3 4
      Chapter07/VertexArray.cpp
  56. 7 2
      Chapter07/VertexArray.h

+ 1 - 1
Chapter06/Assets/Cube.gpmesh

@@ -5,7 +5,7 @@
 	"textures":[
 		"Assets/Cube.png"
 	],
-   "specularPower":100.0,
+	"specularPower":100.0,
 	"vertices":[
 		[-0.5,-0.5,-0.5,0,0,-1,0,0],
 		[0.5,-0.5,-0.5,0,0,-1,1,0],

+ 1 - 1
Chapter06/Assets/Plane.gpmesh

@@ -5,7 +5,7 @@
 	"textures":[
 		"Assets/Plane.png"
 	],
-   "specularPower":100.0,
+	"specularPower":100.0,
 	"vertices":[
 		[50.000000,50.000000,-0.000000,-0.003922,-0.003922,1.000000,1.000000,1.000000],
 		[50.000000,25.000000,-0.000000,-0.003922,-0.003922,1.000000,1.000000,0.750000],

+ 1 - 1
Chapter06/Assets/Sphere.gpmesh

@@ -5,7 +5,7 @@
 	"textures":[
 		"Assets/Sphere.png"
 	],
-   "specularPower":10.0,
+	"specularPower":10.0,
 	"vertices":[
 		[0.000000,0.000001,12.500000,-0.003922,-0.003922,0.992157,0.968750,0.000000],
 		[0.475748,-2.391772,12.259815,0.027451,-0.215686,0.968628,0.968750,0.062500],

+ 2 - 2
Chapter06/Shaders/BasicMesh.frag

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 2 - 2
Chapter06/Shaders/BasicMesh.vert

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 2 - 2
Chapter06/Shaders/Phong.frag

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 2 - 2
Chapter06/Shaders/Phong.vert

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 2 - 2
Chapter06/Shaders/Sprite.frag

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 2 - 2
Chapter06/Shaders/Sprite.vert

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 1 - 0
Chapter07/Assets/Cube.gpmesh

@@ -5,6 +5,7 @@
 	"textures":[
 		"Assets/Cube.png"
 	],
+	"specularPower":100.0,
 	"vertices":[
 		[-0.5,-0.5,-0.5,0,0,-1,0,0],
 		[0.5,-0.5,-0.5,0,0,-1,1,0],

+ 1 - 0
Chapter07/Assets/Plane.gpmesh

@@ -5,6 +5,7 @@
 	"textures":[
 		"Assets/Plane.png"
 	],
+	"specularPower":100.0,
 	"vertices":[
 		[50.000000,50.000000,-0.000000,-0.003922,-0.003922,1.000000,1.000000,1.000000],
 		[50.000000,25.000000,-0.000000,-0.003922,-0.003922,1.000000,1.000000,0.750000],

+ 1 - 0
Chapter07/Assets/Sphere.gpmesh

@@ -5,6 +5,7 @@
 	"textures":[
 		"Assets/Sphere.png"
 	],
+	"specularPower":10.0,
 	"vertices":[
 		[0.000000,0.000001,12.500000,-0.003922,-0.003922,0.992157,0.968750,0.000000],
 		[0.475748,-2.391772,12.259815,0.027451,-0.215686,0.968628,0.968750,0.062500],

+ 1 - 1
Chapter07/AudioComponent.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "AudioComponent.h"

+ 1 - 1
Chapter07/AudioComponent.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 1 - 1
Chapter07/AudioSystem.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "AudioSystem.h"

+ 1 - 1
Chapter07/AudioSystem.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 2 - 2
Chapter07/CameraActor.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "CameraActor.h"

+ 2 - 2
Chapter07/CameraActor.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 0 - 0
Chapter07/Chapter8-windows.sln → Chapter07/Chapter07-windows.sln


+ 16 - 3
Chapter07/CircleComponent.cpp

@@ -1,16 +1,16 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "CircleComponent.h"
 #include "Actor.h"
 
 CircleComponent::CircleComponent(class Actor* owner)
-:CollisionComponent(owner)
+:Component(owner)
 ,mRadius(0.0f)
 {
 	
@@ -25,3 +25,16 @@ float CircleComponent::GetRadius() const
 {
 	return mOwner->GetScale() * mRadius;
 }
+
+bool Intersect(const CircleComponent& a, const CircleComponent& b)
+{
+	// Calculate distance squared
+	Vector3 diff = a.GetCenter() - b.GetCenter();
+	float distSq = diff.LengthSq();
+
+	// Calculate sum of radii squared
+	float radiiSq = a.GetRadius() + b.GetRadius();
+	radiiSq *= radiiSq;
+
+	return distSq <= radiiSq;
+}

+ 6 - 4
Chapter07/CircleComponent.h

@@ -1,16 +1,16 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
-#include "CollisionComponent.h"
+#include "Component.h"
 #include "Math.h"
 
-class CircleComponent : public CollisionComponent
+class CircleComponent : public Component
 {
 public:
 	CircleComponent(class Actor* owner);
@@ -22,3 +22,5 @@ public:
 private:
 	float mRadius;
 };
+
+bool Intersect(const CircleComponent& a, const CircleComponent& b);

+ 0 - 20
Chapter07/CollisionComponent.cpp

@@ -1,20 +0,0 @@
-// ----------------------------------------------------------------
-// From Game Programming in C++ by Sanjay Madhav
-// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
-// Released under the BSD License
-// See LICENSE.txt for full details.
-// ----------------------------------------------------------------
-
-#include "CollisionComponent.h"
-
-CollisionComponent::CollisionComponent(class Actor* owner,int updateOrder)
-:Component(owner, updateOrder)
-{
-	
-}
-
-CollisionComponent::~CollisionComponent()
-{
-	
-}

+ 0 - 17
Chapter07/CollisionComponent.h

@@ -1,17 +0,0 @@
-// ----------------------------------------------------------------
-// From Game Programming in C++ by Sanjay Madhav
-// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
-// Released under the BSD License
-// See LICENSE.txt for full details.
-// ----------------------------------------------------------------
-
-#pragma once
-#include "Component.h"
-class CollisionComponent : public Component
-{
-public:
-	CollisionComponent(class Actor* owner, int updateOrder = 100);
-	~CollisionComponent();
-};
-

+ 49 - 24
Chapter07/Game.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Game.h"
@@ -20,6 +20,7 @@
 Game::Game()
 :mRenderer(nullptr)
 ,mIsRunning(true)
+,mUpdatingActors(false)
 {
 	
 }
@@ -34,7 +35,7 @@ bool Game::Initialize()
 
 	// Create the renderer
 	mRenderer = new Renderer(this);
-	if (!mRenderer->Initialize())
+	if (!mRenderer->Initialize(1024.0f, 768.0f))
 	{
 		SDL_Log("Failed to initialize renderer");
 		delete mRenderer;
@@ -170,15 +171,21 @@ void Game::UpdateGame()
 	}
 	mTicksCount = SDL_GetTicks();
 
-	// Make copy of actor vector
-	// (iterate over this in case new actors are created)
-	std::vector<Actor*> copy = mActors;
-
 	// Update all actors
-	for (auto actor : copy)
+	mUpdatingActors = true;
+	for (auto actor : mActors)
 	{
 		actor->Update(deltaTime);
 	}
+	mUpdatingActors = false;
+
+	// Move any pending actors to mActors
+	for (auto pending : mPendingActors)
+	{
+		pending->ComputeWorldTransform();
+		mActors.emplace_back(pending);
+	}
+	mPendingActors.clear();
 
 	// Add any dead actors to a temp vector
 	std::vector<Actor*> deadActors;
@@ -190,8 +197,7 @@ void Game::UpdateGame()
 		}
 	}
 
-	// Delete any of the dead actors (which will
-	// remove them from mActors)
+	// Delete dead actors (which removes them from mActors)
 	for (auto actor : deadActors)
 	{
 		delete actor;
@@ -208,19 +214,21 @@ void Game::GenerateOutput()
 
 void Game::LoadData()
 {
-	mRenderer->LoadTexture("Assets/Default.png");
-	mRenderer->LoadTexture("Assets/HealthBar.png");
-	mRenderer->LoadTexture("Assets/Radar.png");
-
-	// Meshes
-	mRenderer->LoadMesh("Assets/Cube.gpmesh");
-	mRenderer->LoadMesh("Assets/Sphere.gpmesh");
-	mRenderer->LoadMesh("Assets/Plane.gpmesh");
-
 	// Create actors
-	Actor* a = nullptr;
-	Quaternion q;
-	MeshComponent* mc = nullptr;
+	Actor* a = new Actor(this);
+	a->SetPosition(Vector3(200.0f, 75.0f, 0.0f));
+	a->SetScale(100.0f);
+	Quaternion q(Vector3::UnitY, -Math::PiOver2);
+	q = Quaternion::Concatenate(q, Quaternion(Vector3::UnitZ, Math::Pi + Math::Pi / 4.0f));
+	a->SetRotation(q);
+	MeshComponent* mc = new MeshComponent(a);
+	mc->SetMesh(mRenderer->GetMesh("Assets/Cube.gpmesh"));
+
+	a = new Actor(this);
+	a->SetPosition(Vector3(200.0f, -75.0f, 0.0f));
+	a->SetScale(3.0f);
+	mc = new MeshComponent(a);
+	mc->SetMesh(mRenderer->GetMesh("Assets/Sphere.gpmesh"));
 
 	// Setup floor
 	const float start = -1250.0f;
@@ -266,7 +274,6 @@ void Game::LoadData()
 	dir.mDirection = Vector3(0.0f, -0.707f, -0.707f);
 	dir.mDiffuseColor = Vector3(0.78f, 0.88f, 1.0f);
 	dir.mSpecColor = Vector3(0.8f, 0.8f, 0.8f);
-	dir.mSpecPower = 100.0f;
 
 	// Camera actor
 	mCameraActor = new CameraActor(this);
@@ -327,12 +334,30 @@ void Game::Shutdown()
 
 void Game::AddActor(Actor* actor)
 {
-	mActors.emplace_back(actor);
+	// If we're updating actors, need to add to pending
+	if (mUpdatingActors)
+	{
+		mPendingActors.emplace_back(actor);
+	}
+	else
+	{
+		mActors.emplace_back(actor);
+	}
 }
 
 void Game::RemoveActor(Actor* actor)
 {
-	auto iter = std::find(mActors.begin(), mActors.end(), actor);
+	// Is it in pending actors?
+	auto iter = std::find(mPendingActors.begin(), mPendingActors.end(), actor);
+	if (iter != mPendingActors.end())
+	{
+		// Swap to end of vector and pop off (avoid erase copies)
+		std::iter_swap(iter, mPendingActors.end() - 1);
+		mPendingActors.pop_back();
+	}
+
+	// Is it in actors?
+	iter = std::find(mActors.begin(), mActors.end(), actor);
 	if (iter != mActors.end())
 	{
 		// Swap to end of vector and pop off (avoid erase copies)

+ 5 - 1
Chapter07/Game.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -37,12 +37,16 @@ private:
 	
 	// All the actors in the game
 	std::vector<class Actor*> mActors;
+	// Any pending actors
+	std::vector<class Actor*> mPendingActors;
 
 	class Renderer* mRenderer;
 	class AudioSystem* mAudioSystem;
 
 	Uint32 mTicksCount;
 	bool mIsRunning;
+	// Track if we're updating actors right now
+	bool mUpdatingActors;
 
 	// Game-specific code
 	class CameraActor* mCameraActor;

+ 1 - 2
Chapter07/Game.vcxproj

@@ -16,7 +16,6 @@
     <ClCompile Include="AudioSystem.cpp" />
     <ClCompile Include="CameraActor.cpp" />
     <ClCompile Include="CircleComponent.cpp" />
-    <ClCompile Include="CollisionComponent.cpp" />
     <ClCompile Include="Component.cpp" />
     <ClCompile Include="Game.cpp" />
     <ClCompile Include="Main.cpp" />
@@ -38,7 +37,6 @@
     <ClInclude Include="AudioSystem.h" />
     <ClInclude Include="CameraActor.h" />
     <ClInclude Include="CircleComponent.h" />
-    <ClInclude Include="CollisionComponent.h" />
     <ClInclude Include="Component.h" />
     <ClInclude Include="Game.h" />
     <ClInclude Include="Math.h" />
@@ -65,6 +63,7 @@
     <ProjectGuid>{BC508D87-495F-4554-932D-DD68388B63CC}</ProjectGuid>
     <Keyword>Win32Proj</Keyword>
     <RootNamespace>Game</RootNamespace>
+    <WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">

+ 0 - 6
Chapter07/Game.vcxproj.filters

@@ -31,9 +31,6 @@
     <ClCompile Include="CircleComponent.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="CollisionComponent.cpp">
-      <Filter>Source Files</Filter>
-    </ClCompile>
     <ClCompile Include="MoveComponent.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
@@ -90,9 +87,6 @@
     <ClInclude Include="CircleComponent.h">
       <Filter>Source Files</Filter>
     </ClInclude>
-    <ClInclude Include="CollisionComponent.h">
-      <Filter>Source Files</Filter>
-    </ClInclude>
     <ClInclude Include="MoveComponent.h">
       <Filter>Source Files</Filter>
     </ClInclude>

+ 1 - 1
Chapter07/Main.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Game.h"

+ 17 - 18
Chapter07/Math.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Math.h"
@@ -99,12 +99,13 @@ Vector3 Vector3::Transform(const Vector3& v, const Quaternion& q)
 void Matrix4::Invert()
 {
 	// Thanks slow math
-	float tmp[12]; /* temp array for pairs */
-	float src[16]; /* array of transpose source matrix */
-	float dst[16]; /* storage */
-	float det; /* determinant */
-	/* transpose matrix */
+	// This is a really janky way to unroll everything...
+	float tmp[12];
+	float src[16];
+	float dst[16];
+	float det;
 
+	// Transpose matrix
 	// row 1 to col 1
 	src[0] = mat[0][0];
 	src[4] = mat[0][1];
@@ -129,13 +130,7 @@ void Matrix4::Invert()
 	src[11] = mat[3][2];
 	src[15] = mat[3][3];
 
-	// 	for (int i = 0; i < 4; i++) {
-	// 		src[i] = mat[i*4];
-	// 		src[i + 4] = mat[i*4 + 1];
-	// 		src[i + 8] = mat[i*4 + 2];
-	// 		src[i + 12] = mat[i*4 + 3];
-	// 	}
-	/* calculate pairs for first 8 elements (cofactors) */
+	// Calculate cofactors
 	tmp[0] = src[10] * src[15];
 	tmp[1] = src[11] * src[14];
 	tmp[2] = src[9] * src[15];
@@ -148,7 +143,7 @@ void Matrix4::Invert()
 	tmp[9] = src[10] * src[12];
 	tmp[10] = src[8] * src[13];
 	tmp[11] = src[9] * src[12];
-	/* calculate first 8 elements (cofactors) */
+	
 	dst[0] = tmp[0] * src[5] + tmp[3] * src[6] + tmp[4] * src[7];
 	dst[0] -= tmp[1] * src[5] + tmp[2] * src[6] + tmp[5] * src[7];
 	dst[1] = tmp[1] * src[4] + tmp[6] * src[6] + tmp[9] * src[7];
@@ -165,7 +160,7 @@ void Matrix4::Invert()
 	dst[6] -= tmp[2] * src[0] + tmp[7] * src[1] + tmp[10] * src[3];
 	dst[7] = tmp[4] * src[0] + tmp[9] * src[1] + tmp[10] * src[2];
 	dst[7] -= tmp[5] * src[0] + tmp[8] * src[1] + tmp[11] * src[2];
-	/* calculate pairs for second 8 elements (cofactors) */
+	
 	tmp[0] = src[2] * src[7];
 	tmp[1] = src[3] * src[6];
 	tmp[2] = src[1] * src[7];
@@ -178,7 +173,7 @@ void Matrix4::Invert()
 	tmp[9] = src[2] * src[4];
 	tmp[10] = src[0] * src[5];
 	tmp[11] = src[1] * src[4];
-	/* calculate second 8 elements (cofactors) */
+	
 	dst[8] = tmp[0] * src[13] + tmp[3] * src[14] + tmp[4] * src[15];
 	dst[8] -= tmp[1] * src[13] + tmp[2] * src[14] + tmp[5] * src[15];
 	dst[9] = tmp[1] * src[12] + tmp[6] * src[14] + tmp[9] * src[15];
@@ -195,12 +190,16 @@ void Matrix4::Invert()
 	dst[14] -= tmp[10] * src[11] + tmp[2] * src[8] + tmp[7] * src[9];
 	dst[15] = tmp[10] * src[10] + tmp[4] * src[8] + tmp[9] * src[9];
 	dst[15] -= tmp[8] * src[9] + tmp[11] * src[10] + tmp[5] * src[8];
-	/* calculate determinant */
+	
+	// Calculate determinant
 	det = src[0] * dst[0] + src[1] * dst[1] + src[2] * dst[2] + src[3] * dst[3];
-	/* calculate matrix inverse */
+	
+	// Inverse of matrix is divided by determinant
 	det = 1 / det;
 	for (int j = 0; j < 16; j++)
+	{
 		dst[j] *= det;
+	}
 
 	// Set it back
 	for (int i = 0; i < 4; i++)

+ 1 - 1
Chapter07/Math.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 6 - 3
Chapter07/Mesh.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Mesh.h"
@@ -19,6 +19,7 @@
 Mesh::Mesh()
 	:mVertexArray(nullptr)
 	,mRadius(0.0f)
+	,mSpecPower(100.0f)
 {
 }
 
@@ -26,7 +27,7 @@ Mesh::~Mesh()
 {
 }
 
-bool Mesh::Load(const std::string & fileName, Renderer* renderer)
+bool Mesh::Load(const std::string& fileName, Renderer* renderer)
 {
 	std::ifstream file(fileName);
 	if (!file.is_open())
@@ -71,6 +72,8 @@ bool Mesh::Load(const std::string & fileName, Renderer* renderer)
 		return false;
 	}
 
+	mSpecPower = static_cast<float>(doc["specularPower"].GetDouble());
+
 	for (rapidjson::SizeType i = 0; i < textures.Size(); i++)
 	{
 		// Is this texture already loaded?
@@ -79,7 +82,7 @@ bool Mesh::Load(const std::string & fileName, Renderer* renderer)
 		if (t == nullptr)
 		{
 			// Try loading the texture
-			t = renderer->LoadTexture(texName.c_str());
+			t = renderer->GetTexture(texName);
 			if (t == nullptr)
 			{
 				// If it's still null, just use the default texture

+ 5 - 1
Chapter07/Mesh.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -26,6 +26,8 @@ public:
 	const std::string& GetShaderName() const { return mShaderName; }
 	// Get object space bounding sphere radius
 	float GetRadius() const { return mRadius; }
+	// Get specular power of mesh
+	float GetSpecPower() const { return mSpecPower; }
 private:
 	// Textures associated with this mesh
 	std::vector<class Texture*> mTextures;
@@ -35,4 +37,6 @@ private:
 	std::string mShaderName;
 	// Stores object space bounding sphere radius
 	float mRadius;
+	// Specular power of surface
+	float mSpecPower;
 };

+ 4 - 2
Chapter07/MeshComponent.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "MeshComponent.h"
@@ -35,6 +35,8 @@ void MeshComponent::Draw(Shader* shader)
 		// Set the world transform
 		shader->SetMatrixUniform("uWorldTransform", 
 			mOwner->GetWorldTransform());
+		// Set specular power
+		shader->SetFloatUniform("uSpecPower", mMesh->GetSpecPower());
 		// Set the active texture
 		Texture* t = mMesh->GetTexture(mTextureIndex);
 		if (t)

+ 2 - 2
Chapter07/MeshComponent.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 2 - 2
Chapter07/MoveComponent.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "MoveComponent.h"

+ 2 - 2
Chapter07/MoveComponent.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 1 - 1
Chapter07/PlaneActor.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "PlaneActor.h"

+ 1 - 1
Chapter07/PlaneActor.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 51 - 43
Chapter07/Renderer.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Renderer.h"
@@ -27,8 +27,11 @@ Renderer::~Renderer()
 {
 }
 
-bool Renderer::Initialize()
+bool Renderer::Initialize(float screenWidth, float screenHeight)
 {
+	mScreenWidth = screenWidth;
+	mScreenHeight = screenHeight;
+
 	// Set OpenGL attributes
 	// Use the core OpenGL profile
 	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
@@ -46,8 +49,8 @@ bool Renderer::Initialize()
 	// Force OpenGL to use hardware acceleration
 	SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
 
-	mWindow = SDL_CreateWindow("Game Programming in C++ (Chapter 8)", 100, 100,
-		1024, 768, SDL_WINDOW_OPENGL);
+	mWindow = SDL_CreateWindow("Game Programming in C++ (Chapter 7)", 100, 100,
+		static_cast<int>(mScreenWidth), static_cast<int>(mScreenHeight), SDL_WINDOW_OPENGL);
 	if (!mWindow)
 	{
 		SDL_Log("Failed to create window: %s", SDL_GetError());
@@ -156,11 +159,22 @@ void Renderer::Draw()
 
 void Renderer::AddSprite(SpriteComponent* sprite)
 {
-	mSprites.emplace_back(sprite);
-	// Resort sprites by draw order
-	std::sort(mSprites.begin(), mSprites.end(), [](SpriteComponent* a, SpriteComponent* b) {
-		return a->GetDrawOrder() < b->GetDrawOrder();
-	});
+	// Find the insertion point in the sorted vector
+	// (The first element with a higher draw order than me)
+	int myDrawOrder = sprite->GetDrawOrder();
+	auto iter = mSprites.begin();
+	for (;
+		iter != mSprites.end();
+		++iter)
+	{
+		if (myDrawOrder < (*iter)->GetDrawOrder())
+		{
+			break;
+		}
+	}
+
+	// Inserts element before position of iterator
+	mSprites.insert(iter, sprite);
 }
 
 void Renderer::RemoveSprite(SpriteComponent* sprite)
@@ -180,21 +194,6 @@ void Renderer::RemoveMeshComp(MeshComponent* mesh)
 	mMeshComps.erase(iter);
 }
 
-Texture* Renderer::LoadTexture(const char* fileName)
-{
-	Texture* tex = new Texture();
-	if (tex->Load(fileName))
-	{
-		mTextures.emplace(fileName, tex);
-		return tex;
-	}
-	else
-	{
-		delete tex;
-		return nullptr;
-	}
-}
-
 Texture* Renderer::GetTexture(const std::string& fileName)
 {
 	Texture* tex = nullptr;
@@ -203,22 +202,20 @@ Texture* Renderer::GetTexture(const std::string& fileName)
 	{
 		tex = iter->second;
 	}
-	return tex;
-}
-
-Mesh* Renderer::LoadMesh(const char * fileName)
-{
-	Mesh* m = new Mesh();
-	if (m->Load(fileName, this))
-	{
-		mMeshes.emplace(fileName, m);
-		return m;
-	}
 	else
 	{
-		delete m;
-		return nullptr;
+		tex = new Texture();
+		if (tex->Load(fileName))
+		{
+			mTextures.emplace(fileName, tex);
+		}
+		else
+		{
+			delete tex;
+			tex = nullptr;
+		}
 	}
+	return tex;
 }
 
 Mesh* Renderer::GetMesh(const std::string & fileName)
@@ -229,6 +226,19 @@ Mesh* Renderer::GetMesh(const std::string & fileName)
 	{
 		m = iter->second;
 	}
+	else
+	{
+		m = new Mesh();
+		if (m->Load(fileName, this))
+		{
+			mMeshes.emplace(fileName, m);
+		}
+		else
+		{
+			delete m;
+			m = nullptr;
+		}
+	}
 	return m;
 }
 
@@ -236,19 +246,19 @@ bool Renderer::LoadShaders()
 {
 	// Create sprite shader
 	mSpriteShader = new Shader();
-	if (!mSpriteShader->Load("Shaders/Sprite"))
+	if (!mSpriteShader->Load("Shaders/Sprite.vert", "Shaders/Sprite.frag"))
 	{
 		return false;
 	}
 
 	mSpriteShader->SetActive();
 	// Set the view-projection matrix
-	Matrix4 viewProj = Matrix4::CreateSimpleViewProj(1024.f, 768.f);
+	Matrix4 viewProj = Matrix4::CreateSimpleViewProj(mScreenWidth, mScreenHeight);
 	mSpriteShader->SetMatrixUniform("uViewProj", viewProj);
 
 	// Create basic mesh shader
 	mMeshShader = new Shader();
-	if (!mMeshShader->Load("Shaders/Phong"))
+	if (!mMeshShader->Load("Shaders/Phong.vert", "Shaders/Phong.frag"))
 	{
 		return false;
 	}
@@ -257,7 +267,7 @@ bool Renderer::LoadShaders()
 	// Set the view-projection matrix
 	mView = Matrix4::CreateLookAt(Vector3::Zero, Vector3::UnitX, Vector3::UnitZ);
 	mProjection = Matrix4::CreatePerspectiveFOV(Math::ToRadians(70.0f),
-		1024.0f, 768.0f, 25.0f, 10000.0f);
+		mScreenWidth, mScreenHeight, 25.0f, 10000.0f);
 	mMeshShader->SetMatrixUniform("uViewProj", mView * mProjection);
 	return true;
 }
@@ -294,6 +304,4 @@ void Renderer::SetLightUniforms(Shader* shader)
 		mDirLight.mDiffuseColor);
 	shader->SetVectorUniform("uDirLight.mSpecColor",
 		mDirLight.mSpecColor);
-	shader->SetFloatUniform("uDirLight.mSpecPower",
-		mDirLight.mSpecPower);
 }

+ 8 - 6
Chapter07/Renderer.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -21,8 +21,6 @@ struct DirectionalLight
 	Vector3 mDiffuseColor;
 	// Specular color
 	Vector3 mSpecColor;
-	// Specular power
-	float mSpecPower;
 };
 
 class Renderer
@@ -31,7 +29,7 @@ public:
 	Renderer(class Game* game);
 	~Renderer();
 
-	bool Initialize();
+	bool Initialize(float screenWidth, float screenHeight);
 	void Shutdown();
 	void UnloadData();
 
@@ -43,15 +41,16 @@ public:
 	void AddMeshComp(class MeshComponent* mesh);
 	void RemoveMeshComp(class MeshComponent* mesh);
 
-	class Texture* LoadTexture(const char* fileName);
 	class Texture* GetTexture(const std::string& fileName);
-	class Mesh* LoadMesh(const char* fileName);
 	class Mesh* GetMesh(const std::string& fileName);
 
 	void SetViewMatrix(const Matrix4& view) { mView = view; }
 
 	void SetAmbientLight(const Vector3& ambient) { mAmbientLight = ambient; }
 	DirectionalLight& GetDirectionalLight() { return mDirLight; }
+
+	float GetScreenWidth() const { return mScreenWidth; }
+	float GetScreenHeight() const { return mScreenHeight; }
 private:
 	bool LoadShaders();
 	void CreateSpriteVerts();
@@ -82,6 +81,9 @@ private:
 	// View/projection for 3D shaders
 	Matrix4 mView;
 	Matrix4 mProjection;
+	// Width/height of screen
+	float mScreenWidth;
+	float mScreenHeight;
 
 	// Lighting data
 	Vector3 mAmbientLight;

+ 12 - 12
Chapter07/Shader.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Shader.h"
@@ -25,15 +25,15 @@ Shader::~Shader()
 
 }
 
-bool Shader::Load(const std::string& name)
+bool Shader::Load(const std::string& vertName, const std::string& fragName)
 {
 	// Compile vertex and pixel shaders
-	if (!CompileShader(name + ".vert",
-					   GL_VERTEX_SHADER,
-					   mVertexShader) ||
-		!CompileShader(name + ".frag",
-					   GL_FRAGMENT_SHADER,
-					   mFragShader))
+	if (!CompileShader(vertName,
+		GL_VERTEX_SHADER,
+		mVertexShader) ||
+		!CompileShader(fragName,
+			GL_FRAGMENT_SHADER,
+			mFragShader))
 	{
 		return false;
 	}
@@ -91,14 +91,14 @@ void Shader::SetFloatUniform(const char* name, float value)
 }
 
 bool Shader::CompileShader(const std::string& fileName,
-				   GLenum shaderType,
-				   GLuint& outShader)
+	GLenum shaderType,
+	GLuint& outShader)
 {
 	// Open file
 	std::ifstream shaderFile(fileName);
 	if (shaderFile.is_open())
 	{
-		// Read all of the text into a string
+		// Read all the text into a string
 		std::stringstream sstream;
 		sstream << shaderFile.rdbuf();
 		std::string contents = sstream.str();

+ 4 - 5
Chapter07/Shader.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -16,9 +16,8 @@ class Shader
 public:
 	Shader();
 	~Shader();
-	// Load shader of the specified name, excluding
-	// the .frag/.vert extension
-	bool Load(const std::string& name);
+	// Load the vertex/fragment shaders with the given names
+	bool Load(const std::string& vertName, const std::string& fragName);
 	void Unload();
 	// Set this as the active shader program
 	void SetActive();

+ 2 - 2
Chapter07/Shaders/BasicMesh.frag

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 2 - 2
Chapter07/Shaders/BasicMesh.vert

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 7 - 8
Chapter07/Shaders/Phong.frag

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3
@@ -32,15 +32,16 @@ struct DirectionalLight
 	vec3 mDiffuseColor;
 	// Specular color
 	vec3 mSpecColor;
-	// Specular power
-	float mSpecPower;
 };
 
 // Uniforms for lighting
 // Camera position (in world space)
 uniform vec3 uCameraPos;
+// Specular power for this surface
+uniform float uSpecPower;
 // Ambient light level
 uniform vec3 uAmbientLight;
+
 // Directional Light
 uniform DirectionalLight uDirLight;
 
@@ -60,12 +61,10 @@ void main()
 	float NdotL = dot(N, L);
 	if (NdotL > 0)
 	{
-		vec3 Diffuse = uDirLight.mDiffuseColor * dot(N, L);
-		vec3 Specular = uDirLight.mSpecColor * pow(max(0.0, dot(R, V)), uDirLight.mSpecPower);
+		vec3 Diffuse = uDirLight.mDiffuseColor * NdotL;
+		vec3 Specular = uDirLight.mSpecColor * pow(max(0.0, dot(R, V)), uSpecPower);
 		Phong += Diffuse + Specular;
 	}
-	// Clamp light between 0-1 RGB values
-	Phong = clamp(Phong, 0.0, 1.0);
 
 	// Final color is texture color times phong light (alpha = 1)
     outColor = texture(uTexture, fragTexCoord) * vec4(Phong, 1.0f);

+ 2 - 2
Chapter07/Shaders/Phong.vert

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 2 - 2
Chapter07/Shaders/Sprite.frag

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 2 - 2
Chapter07/Shaders/Sprite.vert

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 // Request GLSL 3.3

+ 1 - 1
Chapter07/SoundEvent.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "SoundEvent.h"

+ 1 - 1
Chapter07/SoundEvent.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 1 - 1
Chapter07/SpriteComponent.cpp

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "SpriteComponent.h"

+ 1 - 1
Chapter07/SpriteComponent.h

@@ -3,7 +3,7 @@
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
 // 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once

+ 2 - 2
Chapter07/Texture.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "Texture.h"

+ 2 - 2
Chapter07/Texture.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include <string>

+ 3 - 4
Chapter07/VertexArray.cpp

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #include "VertexArray.h"
@@ -26,7 +26,7 @@ VertexArray::VertexArray(const float* verts, unsigned int numVerts,
 	// Create index buffer
 	glGenBuffers(1, &mIndexBuffer);
 	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
-	glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(GLuint), indices, GL_STATIC_DRAW);
+	glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices * sizeof(unsigned int), indices, GL_STATIC_DRAW);
 
 	// Specify the vertex attributes
 	// (For now, assume one vertex format)
@@ -53,5 +53,4 @@ VertexArray::~VertexArray()
 void VertexArray::SetActive()
 {
 	glBindVertexArray(mVertexArray);
-	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
 }

+ 7 - 2
Chapter07/VertexArray.h

@@ -1,9 +1,9 @@
 // ----------------------------------------------------------------
 // From Game Programming in C++ by Sanjay Madhav
 // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
-//
+// 
 // Released under the BSD License
-// See LICENSE.txt for full details.
+// See LICENSE in root directory for full details.
 // ----------------------------------------------------------------
 
 #pragma once
@@ -18,9 +18,14 @@ public:
 	unsigned int GetNumIndices() const { return mNumIndices; }
 	unsigned int GetNumVerts() const { return mNumVerts; }
 private:
+	// How many vertices in the vertex buffer?
 	unsigned int mNumVerts;
+	// How many indices in the index buffer
 	unsigned int mNumIndices;
+	// OpenGL ID of the vertex buffer
 	unsigned int mVertexBuffer;
+	// OpenGL ID of the index buffer
 	unsigned int mIndexBuffer;
+	// OpenGL ID of the vertex array object
 	unsigned int mVertexArray;
 };