Skip to content

Commit

Permalink
Added normal mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
skdeng committed Feb 27, 2015
1 parent 3715965 commit 98f5411
Show file tree
Hide file tree
Showing 41 changed files with 257 additions and 143 deletions.
Binary file modified Debug/astria.exe
Binary file not shown.
Binary file modified Debug/astria.ilk
Binary file not shown.
Binary file modified Debug/astria.pdb
Binary file not shown.
2 changes: 1 addition & 1 deletion Debug/params.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
WindowName Desolation
VersionNumber 0.2.2
VersionNumber 0.2.3
ResolutionWidth 1024
ResolutionHeight 768
IconImage gfx/img/dota2sf_icon.png
Expand Down
Binary file modified Release/astria.exe
Binary file not shown.
Binary file modified Release/astria.pdb
Binary file not shown.
Binary file modified Release/gfx/Thumbs.db
Binary file not shown.
Binary file modified Release/gfx/barrel/Thumbs.db
Binary file not shown.
Binary file modified Release/gfx/coffee_tree/Thumbs.db
Binary file not shown.
Binary file modified Release/gfx/img/Thumbs.db
Binary file not shown.
Binary file modified Release/gfx/img/sprites/Thumbs.db
Binary file not shown.
Binary file modified Release/gfx/nanosuit/Thumbs.db
Binary file not shown.
1 change: 0 additions & 1 deletion Release/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ WASD - Movement
Mouse - Look
MouseWheel - Zoom In/Out
Hold Shift - Toggle Sprint
Q - Toggle Wireframe Mode
F - Toggle Fog (to see the beautiful skybox)
G - Toggle Gravity (also collision with map)
Escape - Pause
2 changes: 1 addition & 1 deletion Release/params.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
WindowName Desolation
VersionNumber 0.2.2
VersionNumber 0.2.3
ResolutionWidth 1024
ResolutionHeight 768
IconImage gfx/img/dota2sf_icon.png
Expand Down
23 changes: 19 additions & 4 deletions Release/shaders/instancing.vert
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
#version 330

layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec2 inCoords;
layout(location = 2) in vec3 inNormal;
layout(location = 3) in mat4 inModelMatrix;
layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec2 inCoords;
layout (location = 2) in vec3 inNormal;
layout (location = 3) in vec3 inTangent;
layout (location = 4) in vec3 inBitangent;
layout (location = 5) in mat4 inModelMatrix;

struct DirectionalLight
{
vec3 vColor;
vec3 vDirection;
float fAmbient;
float fBrightness;
};

uniform struct Matrices
{
Expand All @@ -15,6 +25,11 @@ smooth out vec3 vNormal;
smooth out vec2 vTexCoord;
smooth out vec3 vWorldPos;

out vec3 vLightDir_tangentspace;

uniform DirectionalLight sunLight;
uniform bool bEnableNormalMap;

void main()
{
mat4 mMVP = matrices.mProjection * matrices.mView * inModelMatrix;
Expand Down
35 changes: 23 additions & 12 deletions Release/shaders/main_shader.frag
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
#version 330

#include

smooth in vec2 vTexCoord;
smooth in vec3 vNormal;
smooth in vec4 vEyeSpacePos;
smooth in vec3 vWorldPos;
out vec4 outputColor;

struct Material
{
sampler2D diffuse;
sampler2D normal;
sampler2D specular;
};

Expand Down Expand Up @@ -54,6 +47,16 @@ uniform FogParameters fogParams;

uniform int bSkybox;
uniform int bFog;
uniform bool bEnableNormalMap;

smooth in vec2 vTexCoord;
smooth in vec3 vNormal;
smooth in vec4 vEyeSpacePos;
smooth in vec3 vWorldPos;
in vec3 vLightDir_tangentspace;
in vec3 vEyeDir_tangentspace;

out vec4 outputColor;

void main()
{
Expand All @@ -69,21 +72,29 @@ void main()
return;
}

vec3 vNormalized = normalize(vNormal);
vec3 vNormal_extended = normalize(vNormal);
vec3 vLightDir = sunLight.vDirection;

if(bEnableNormalMap)
{
vNormal_extended = normalize(texture2D(mat.normal, vTexCoord).rgb*2.0-1.0);
vLightDir = vLightDir_tangentspace;
}

//Diffuse light
float fDiffuseIntensity = clamp(dot(vNormal, -sunLight.vDirection), 0.0, 1.0);
float fDiffuseIntensity = clamp(dot(vNormal_extended, -vLightDir), 0.0, 1.0);
vec3 vDiffuseColor = pow(sunLight.fBrightness,2) * sunLight.vColor * fDiffuseIntensity * vec3(texture2D(mat.diffuse, vTexCoord));
//vDiffuseColor = vec3(0,0,0);

//Ambient
vec3 vAmbientColor = sunLight.vColor * sunLight.fAmbient * vec3(texture2D(mat.diffuse, vTexCoord));

//Specular
vec3 vReflected = normalize(reflect(sunLight.vDirection, vNormalized));
vec3 vReflected = (reflect(sunLight.vDirection, vNormal)); //Not using normal map yet
vec3 vView = normalize(vEyePosition - vWorldPos);
float fSpecularIntensity = clamp(dot(vReflected, vView), 0, 1);
vec3 vSpecularColor = 0.6 * sunLight.vColor * fSpecularIntensity * vec3(texture2D(mat.specular, vTexCoord));

outputColor = vec4(vAmbientColor + vDiffuseColor + vSpecularColor, 1.0f);

if (bFog == 1)
Expand Down
34 changes: 34 additions & 0 deletions Release/shaders/main_shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,33 @@ uniform struct Matrices
mat4 mNormal;
} matrices;

struct DirectionalLight
{
vec3 vColor;
vec3 vDirection;
float fAmbient;
float fBrightness;
};

layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec2 inCoord;
layout (location = 2) in vec3 inNormal;
layout (location = 3) in vec3 inTangent;
layout (location = 4) in vec3 inBitangent;


smooth out vec3 vNormal;
smooth out vec2 vTexCoord;
smooth out vec3 vWorldPos;

smooth out vec4 vEyeSpacePos;

out vec3 vEyeDir_tangentspace;
out vec3 vLightDir_tangentspace;

uniform DirectionalLight sunLight;
uniform bool bEnableNormalMap;

void main()
{
mat4 mMV = matrices.mView*matrices.mModel; //Matrix multiplication is from left to right
Expand All @@ -30,4 +47,21 @@ void main()

vNormal = (matrices.mNormal*vec4(inNormal, 1.0)).xyz;
vWorldPos = (matrices.mModel*vec4(inPosition, 1.0)).xyz;

vec3 vEyeDir_eyespace = vec3(0,0,0) - vEyeSpacePos.xyz;

if(bEnableNormalMap)
{
mat3 mMV3x3 = mat3(mMV);

vec3 vNormal_eyespace = mMV3x3 * normalize(vNormal);
vec3 vTangent_eyespace = mMV3x3 * inTangent;
vec3 vBitangent_eyespace = mMV3x3 * inBitangent;

mat3 mTBN = transpose(mat3(vTangent_eyespace, vBitangent_eyespace, vNormal_eyespace));
vLightDir_tangentspace = normalize(mTBN * sunLight.vDirection);
vEyeDir_tangentspace = normalize(mTBN * vEyeDir_eyespace);
}
else
vLightDir_tangentspace = vec3(0,0,0);
}
Binary file modified astria.v12.suo
Binary file not shown.
22 changes: 5 additions & 17 deletions astria/AppStateMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ void CAppStateMain::OnRender()
ProgramMain.SetUniform("matrices.mProjection", &ProjectionMatrix);
ProgramMain.SetUniform("matrices.mView", &ViewMatrix);
ProgramMain.SetUniform("mat.diffuse", 0);
ProgramMain.SetUniform("mat.normal", 1);
ProgramMain.SetUniform("mat.specular", 2);
ProgramMain.SetUniform("matrices.mModel", glm::mat4(1.0));
ProgramMain.SetUniform("matrices.mNormal", glm::mat4(1.0));
Expand All @@ -209,6 +210,7 @@ void CAppStateMain::OnRender()
newPos.y = Map.GetHeight(newPos);
ModelMatrix = glm::translate(glm::mat4(1.0), newPos);
ProgramMain.SetModelAndNormalMatrix("matrices.mModel", "matrices.mNormal", ModelMatrix);
ProgramMain.SetUniform("bEnableNormalMap", true);
models[0].Render();

newPos = glm::vec3(64, 0, 193);
Expand All @@ -217,6 +219,7 @@ void CAppStateMain::OnRender()
ModelMatrix = glm::scale(ModelMatrix, glm::vec3(5.0));
ModelMatrix = glm::rotate(ModelMatrix, -90.0f, glm::vec3(1, 0, 0));
ProgramMain.SetModelAndNormalMatrix("matrices.mModel", "matrices.mNormal", ModelMatrix);
ProgramMain.SetUniform("bEnableNormalMap", models[1].NormalMap());
models[1].Render();

//Render instanced models
Expand All @@ -225,6 +228,7 @@ void CAppStateMain::OnRender()
ProgramInstancing.SetUniform("matrices.mView", ViewMatrix);
ProgramInstancing.SetUniform("mat.diffuse", 0);
ProgramInstancing.SetUniform("mat.specular", 2);
ProgramInstancing.SetUniform("bEnableNormalMap", false);
Sun.SetUniform(&ProgramInstancing, "sunLight");
CFog::SetUniforms(&ProgramInstancing, FogEnabled);
SmallTree.RenderInstanced();
Expand Down Expand Up @@ -428,9 +432,6 @@ int CAppStateMain::OnLoad()
0.1f,
30);

//Used for wire frame
PolyMode = GL_FILL;

//Model matrix //Identity matrix
ModelMatrix = glm::mat4(1.0f);

Expand Down Expand Up @@ -489,7 +490,7 @@ void CAppStateMain::OnKeyDown(SDL_Keycode sym, Uint16 mod, SDL_Scancode scancode
}
break;
case SDLK_F12:
//Prompt current position
//Prompt current position, for debug purposes
pos = Stringify::Float(Position.x) + " " + Stringify::Float(Position.y) + " " + Stringify::Float(Position.z) + "\n";
Warning("Position", pos);
break;
Expand All @@ -514,19 +515,6 @@ void CAppStateMain::OnKeyDown(SDL_Keycode sym, Uint16 mod, SDL_Scancode scancode
Speed = 3;
break;

//Switch between normal model and wireframe
case SDLK_q:
if (PolyMode == GL_FILL)
{
PolyMode = GL_LINE;
glPolygonMode(GL_FRONT_AND_BACK, PolyMode);
}
else if (PolyMode == GL_LINE) //Useless but pretty cool
{
PolyMode = GL_FILL;
glPolygonMode(GL_FRONT_AND_BACK, PolyMode);
}
break;
case SDLK_f:
FogEnabled = 1 - FogEnabled;
break;
Expand Down
2 changes: 0 additions & 2 deletions astria/AppStateMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ class CAppStateMain : public CAppState
CShader ShaderFontFragment;
CShaderProgram ProgramFont;

GLuint PolyMode; //Used for wireframe

//Skybox and lighting
CSkybox Skybox;
CDirectLight Sun;
Expand Down
4 changes: 2 additions & 2 deletions astria/LoadingScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ void CLoadingScreen::OnUpdate()
{
CSpark::AddRandomSpark();
}
if (SDL_GetTicks() - StartTime >= 3000)
*Condition = -1;
// if (SDL_GetTicks() - StartTime >= 3000)
// *Condition = -1;
}

void CLoadingScreen::OnRender()
Expand Down
Loading

0 comments on commit 98f5411

Please sign in to comment.