Direction light working.
Cleaned up some code in Renderer.cpp
This commit is contained in:
@@ -21,11 +21,10 @@ void main()
|
||||
vec4 LightingTexel = texture(LightingTexture, Input.TextureCoord);
|
||||
vec4 ShadowTexel = texture(ShadowTexture, Input.TextureCoord);
|
||||
|
||||
//FragmentColor = LightingTexel + vec4(LightingTexel.a, LightingTexel.a, LightingTexel.a, 0.0);
|
||||
//FragmentColor = DiffuseTexel;
|
||||
|
||||
FragmentColor = DiffuseTexel * (vec4(La, 0.0) + vec4(LightingTexel.rgb, 0.0)) + vec4(LightingTexel.a, LightingTexel.a, LightingTexel.a, 0.0);
|
||||
//FragmentColor = vec4(pow(_FragmentColor.rgb, vec3(1.0 / Gamma)), _FragmentColor.a);
|
||||
|
||||
|
||||
|
||||
}
|
||||
+18
-16
@@ -30,20 +30,20 @@ out vec4 frag_Position;
|
||||
out vec4 frag_Normal;
|
||||
out vec4 frag_Specular;
|
||||
|
||||
float Shadow(vec4 ShadowCoord)
|
||||
float Shadow(vec4 ShadowCoord, vec3 normal)
|
||||
{
|
||||
if (Input.ShadowCoord.x < 0.0 || Input.ShadowCoord.x > 1.0 || Input.ShadowCoord.y < 0.0 || Input.ShadowCoord.y > 1.0)
|
||||
return 0.9;
|
||||
|
||||
//Fixed bias
|
||||
//float bias = 0.00005;
|
||||
|
||||
//Variable bias
|
||||
vec3 n = normalize(Input.Normal);
|
||||
vec3 n = normalize(normal);
|
||||
vec3 l = normalize(SunDirection_cameraspace);
|
||||
float cosTheta = clamp(dot(n, l), 0.0, 1.0);
|
||||
float bias = tan(acos(cosTheta));
|
||||
bias = clamp(bias, 0.0, 0.00005);
|
||||
bias = clamp(bias, 0.0, 0.00003);
|
||||
|
||||
//Fixed bias
|
||||
//float bias = 0.0005;
|
||||
|
||||
if( texture(ShadowTexture, Input.ShadowCoord.xy).z < ShadowCoord.z + bias)
|
||||
{
|
||||
@@ -60,23 +60,21 @@ void main()
|
||||
//Fixa så den bara gör detta om modellen har en blend map
|
||||
//vvvvvv
|
||||
|
||||
vec4 Blend = texture2D(BlendMap, TextureCoord.st ).rgb;
|
||||
vec4 AsphaltTexel = texture2D(AsphaltTexture, TextureCoord.st * texScale).rgb;
|
||||
vec4 GrassTexel = texture2D(GrassTexture, TextureCoord.st * texScale).rgb;
|
||||
vec4 SandTexel = texture2D(SandTexture, TextureCoord.st * texScale).rgb;
|
||||
vec4 Blend = texture2D(BlendMap, Input.TextureCoord.st );
|
||||
vec4 AsphaltTexel = texture2D(AsphaltTexture, Input.TextureCoord.st * texScale);
|
||||
vec4 GrassTexel = texture2D(GrassTexture, Input.TextureCoord.st * texScale);
|
||||
vec4 SandTexel = texture2D(SandTexture, Input.TextureCoord.st * texScale);
|
||||
|
||||
//Mix the Terrain-textures together
|
||||
AsphaltTexel *= mixmap.r;
|
||||
GrassTexel = mix(AsphaltTexel, GrassTexel, mixmap.g);
|
||||
vec4 tex = mix(GrassTexel, SandTexel, mixmap.b);
|
||||
AsphaltTexel *= Blend.r;
|
||||
GrassTexel = mix(AsphaltTexel, GrassTexel, Blend.g);
|
||||
vec4 tex = mix(GrassTexel, SandTexel, Blend.b);
|
||||
|
||||
//^^^^^^
|
||||
//Fixa så den bara gör detta om modellen har en blend map
|
||||
|
||||
|
||||
// Diffuse Texture
|
||||
frag_Diffuse = tex;
|
||||
//frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord) * Shadow(Input.ShadowCoord);
|
||||
|
||||
|
||||
// G-buffer Position
|
||||
frag_Position = vec4(Input.Position.xyz, 1.0);
|
||||
@@ -87,6 +85,10 @@ void main()
|
||||
//frag_Diffuse = normalize(vec4(TBN * vec3(texture(NormalMapTexture, Input.TextureCoord)), 0.0));
|
||||
//frag_Normal = vec4(Input.Normal, 0.0);
|
||||
|
||||
// Diffuse Texture
|
||||
//frag_Diffuse = tex;
|
||||
frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord) * Shadow(Input.ShadowCoord, vec3(frag_Normal));
|
||||
|
||||
//G-buffer Specular
|
||||
frag_Specular = texture(SpecularMapTexture, Input.TextureCoord);
|
||||
}
|
||||
@@ -49,7 +49,7 @@ vec4 phong(vec3 position, vec3 normal, vec3 specular)
|
||||
vec3 surfaceToViewer = normalize(-position);
|
||||
vec3 halfWay = normalize(surfaceToViewer + directionToLight);
|
||||
float dotSpecular = max(dot(halfWay, normal), 0.0);
|
||||
float specularFactor = pow(dotSpecular, specularExponent * 2.0);
|
||||
float specularFactor = pow(dotSpecular, specularExponent);
|
||||
vec3 Is = specular.r * ls * specularFactor;
|
||||
|
||||
//Attenuation
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
#version 430
|
||||
|
||||
layout (binding=0) uniform sampler2D PositionTexture;
|
||||
layout (binding=1) uniform sampler2D NormalsTexture;
|
||||
layout (binding=2) uniform sampler2D SpecularTexture;
|
||||
|
||||
uniform vec2 ViewportSize;
|
||||
uniform mat4 MVP;
|
||||
uniform mat4 M;
|
||||
uniform mat4 V;
|
||||
uniform mat4 P;
|
||||
uniform vec3 CameraPosition;
|
||||
uniform vec3 directionToSun;
|
||||
|
||||
const vec3 ks = vec3(1.0, 1.0, 1.0);
|
||||
const vec3 kd = vec3(1.0, 1.0, 1.0);
|
||||
const vec3 ka = vec3(1.0, 1.0, 1.0);
|
||||
const float kshine = 1.0;
|
||||
const vec3 SunDiffuseLight = vec3(0.3, 0.3, 0.3);
|
||||
const vec3 SunSpecularLight = vec3(1.0, 1.0, 1.0);
|
||||
const vec3 SunPos = directionToSun*vec3(100);
|
||||
const float specularExponent = 5;
|
||||
|
||||
|
||||
in VertexData
|
||||
{
|
||||
vec3 Position;
|
||||
vec2 TextureCoord;
|
||||
} Input;
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
vec4 phong(vec3 position, vec3 normal, vec3 specular)
|
||||
{
|
||||
|
||||
//Diffuse Sunlight
|
||||
vec3 directionToLight = normalize(vec3(V * vec4(directionToSun, 0.0)));
|
||||
float dotProdLight = dot(directionToLight, normal);
|
||||
dotProdLight = max(dotProdLight, 0.0);
|
||||
vec3 sId = kd * SunDiffuseLight * dotProdLight;
|
||||
|
||||
//Specular Sunlight
|
||||
vec3 surfaceToViewer = normalize(-position);
|
||||
vec3 halfWay = normalize(surfaceToViewer + directionToLight);
|
||||
float dotSpecular = max(dot(halfWay, normal), 0.0);
|
||||
float specularFactorSun = pow(dotSpecular, specularExponent);
|
||||
vec3 sIs = specular.r * SunSpecularLight * specularFactorSun;
|
||||
|
||||
return vec4((sId), sIs.r);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 TextureCoord = gl_FragCoord.xy / ViewportSize;
|
||||
vec4 PositionTexel = texture(PositionTexture, TextureCoord);
|
||||
vec4 NormalTexel = texture(NormalsTexture, TextureCoord);
|
||||
vec4 SpecularTexel = texture(SpecularTexture, TextureCoord);
|
||||
|
||||
FragColor = phong(vec3(PositionTexel), vec3(normalize(NormalTexel)), vec3(SpecularTexel));
|
||||
//FragColor = NormalTexel;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#version 430
|
||||
|
||||
uniform mat4 MVP;
|
||||
|
||||
layout (location = 0) in vec3 Position;
|
||||
layout (location = 2) in vec2 TextureCoord;
|
||||
|
||||
uniform mat4 depthBiasMVP;
|
||||
|
||||
out VertexData
|
||||
{
|
||||
vec3 Position;
|
||||
vec2 TextureCoord;
|
||||
} Output;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = MVP * vec4(Position, 1.0);
|
||||
Output.Position = Position;
|
||||
Output.TextureCoord = (vec2(Position) + 1.0) / 2.0;
|
||||
}
|
||||
Reference in New Issue
Block a user