1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

// Vertex Shader
#version 150 core
uniform mat4 Projection;
uniform mat4 View;
uniform mat4 Model;
uniform vec3 LightPosition_WS;
uniform vec4 Color;
in vec3 Position;
in vec4 VertexColor;
in vec2 TexCoord;
in vec3 Normal;
out vec4 InitColor;
out vec2 UV;
out vec3 Normal_CS;
out vec3 EyeDirection_CS;
out vec3 LightDirection_CS;
out vec3 Position_WS;
void main()
{
//Regular Stuff
mat4 PVM = Projection * View * Model;
gl_Position = PVM * vec4( Position, 1 );
InitColor = VertexColor * Color;
UV = TexCoord;
///Lighting
Position_WS = ( Model * vec4(Position,1) ).xyz;
vec3 Position_CS = ( View * Model * vec4(Position,1) ).xyz;
EyeDirection_CS = Position_CS;
vec3 LightPosition_CS = ( View * vec4(LightPosition_WS,1) ).xyz;
LightDirection_CS = LightPosition_CS + EyeDirection_CS;
Normal_CS = ( transpose( inverse(Model) ) * vec4(Normal,0) ).xyz;
}
// Fragment Shader
#version 150 core
uniform sampler2D TexSamp;
uniform vec3 LightPosition_WS;
uniform vec3 LightColor;
uniform float LightPower;
uniform bool LightExists;
uniform bool UsingTex;
in vec4 InitColor; //Initial Color
in vec2 UV;
in vec3 Normal_CS;
in vec3 EyeDirection_CS;
in vec3 LightDirection_CS;
in vec3 Position_WS;
out vec4 FinalColor;
void main()
{
vec4 MaterialColor;
if ( UsingTex )
MaterialColor = texture2D(TexSamp, UV) * InitColor;
else
MaterialColor = InitColor;
vec3 Norm = normalize( Normal_CS );
vec3 LDCS = normalize( LightDirection_CS );
vec3 Ambient = vec3(.15, .15, .15) * MaterialColor.rgb;
vec3 Specular = vec3(.3, .3, .3);
vec3 Eye = normalize( EyeDirection_CS );
vec3 Reflect = reflect( LDCS, Norm );
float CosTheta = clamp( dot( Norm, LDCS ), 0, 1 );
float CosAlpha = clamp( dot( Eye, Reflect ), 0, 1 );
float Distance = length( LightPosition_WS  Position_WS);
float DistanceSquared = Distance * Distance;
FinalColor =
Ambient +
(Specular * LightPower * LightColor * CosAlpha )/DistanceSquared +
(MaterialColor * LightPower * LightColor * CosTheta)/DistanceSquared;
FinalColor[3] = MaterialColor[3]; //Lighting shouldn't change transperancy
if ( !LightExists )
FinalColor = MaterialColor;
}
