Skip to content
This repository was archived by the owner on May 2, 2024. It is now read-only.

Commit 2c8d844

Browse files
committed
Added specular component
1 parent 334cf4e commit 2c8d844

File tree

3 files changed

+594
-21
lines changed

3 files changed

+594
-21
lines changed

shaders/mesh_pointlight.frag

+31-18
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,61 @@
1-
// Fragment Shader – file "minimal.frag"
1+
// Fragment Shader – file "mesh_pointlight.frag"
22

33
#version 140
44

55
precision highp float; // needed only for version 1.30
66

77
// Light vars
8-
uniform vec3 lightPositionW = vec3(0.0f, 1.5f, 0.0f);
9-
uniform vec4 lightColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
10-
uniform float lightIntensity = 1.0f;
8+
uniform vec4 ambLightColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
9+
uniform float ambLightIntensity = 0.15f;
10+
11+
uniform vec4 diffuseLightColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
12+
uniform float diffuseLightIntensity = 1.0f;
1113
uniform float cAttenuation = 1.0f;
1214
uniform float lAttenuation = 0.08f;
1315
uniform float qAttenuation = 0.01f;
1416

15-
// Viewer position
16-
uniform vec3 eyePositionW;
17+
uniform float specularHardness = 32.0f;
18+
uniform vec4 specularLightColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
19+
uniform float specularLightIntensity = 1.0f;
1720

1821
// Texture vars
1922
uniform sampler2D texDiff;
2023
uniform sampler2D texAmb;
2124
uniform int diffuseTexEnabled;
2225
uniform int ambientTexEnabled;
23-
uniform vec4 ambLightColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
24-
uniform float ambLightIntensity = 0.15f;
2526

2627
in vec3 ex_Color;
2728
in vec3 ex_Normal;
2829
in vec2 ex_UV;
29-
in vec3 ex_PositionW;
30+
in vec3 ex_LightDirW;
31+
in vec3 ex_ViewDirW;
3032

3133
out vec4 out_Color;
3234

3335
void main(void)
3436
{
35-
vec4 diffuseLight;
36-
vec4 ambientLight;
37-
vec3 lightDir = lightPositionW - ex_PositionW;
38-
float distance = length(lightDir);
39-
lightDir = normalize(lightDir);
37+
vec4 ambientLight, diffuseLight, specularLight;
38+
39+
specularLight = vec4(0.0f, 0.0f, 0.0f, 0.0f);
4040

41-
ambientLight = ambLightIntensity*ambLightColor;
41+
vec3 viewDirW = normalize(ex_ViewDirW);
42+
vec3 normal = normalize(ex_Normal);
43+
44+
float distance = length(ex_LightDirW);
45+
vec3 lightDirW = normalize(ex_LightDirW);
46+
47+
ambientLight = vec4(ambLightIntensity*ambLightColor.xyz, ambLightColor.w);
4248

43-
float NdL = clamp(dot(normalize(ex_Normal), lightDir), 0.0f, 1.0f);
4449
float attenuation = 1.0f / (cAttenuation + lAttenuation*distance + qAttenuation*distance*distance);
45-
diffuseLight = vec4(lightIntensity*lightColor.xyz*NdL*attenuation, lightColor.w);
50+
51+
float NdL = clamp(dot(normal, lightDirW), 0.0f, 1.0f);
52+
diffuseLight = vec4(diffuseLightIntensity*diffuseLightColor.xyz*NdL*attenuation, diffuseLightColor.w);
53+
54+
if(NdL > 0.0f) {
55+
vec3 H = normalize(lightDirW + viewDirW);
56+
float NdH = clamp(dot(normal, H), 0.0f, 1.0f);
57+
specularLight = vec4(specularLightIntensity*diffuseLightColor.xyz*pow(NdH, specularHardness), diffuseLightColor.w);
58+
}
4659

4760
if (ambientTexEnabled >= 1) {
4861
vec4 ambTextureColor = texture(texAmb,ex_UV);
@@ -54,5 +67,5 @@ void main(void)
5467
diffuseLight *= diffTextureColor;
5568
}
5669

57-
out_Color = vec4(ex_Color, 1.0f)*(clamp(diffuseLight+ambientLight, 0.0f, 1.0f));
70+
out_Color = vec4(ex_Color, 1.0f)*(clamp((diffuseLight+specularLight) + ambientLight, 0.0f, 1.0f));
5871
}

shaders/mesh_pointlight.vert

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
// Vertex Shader – file "icosphere.vert"
1+
// Vertex Shader – file "mesh_pointlight.vert"
22

33
#version 140
44

55
uniform mat4 in_Model;
66
uniform mat4 in_View;
77
uniform mat4 in_Proj;
8+
9+
// Viewer position
10+
uniform vec3 viewPosW;
11+
uniform vec3 lightPositionW = vec3(0.0f, 1.5f, 0.0f);
812

913
in vec3 in_Position;
1014
in vec3 in_Color;
@@ -14,7 +18,8 @@ in vec2 in_UV;
1418
out vec2 ex_UV;
1519
out vec3 ex_Color;
1620
out vec3 ex_Normal;
17-
out vec3 ex_PositionW;
21+
out vec3 ex_LightDirW;
22+
out vec3 ex_ViewDirW;
1823

1924
void main(void)
2025
{
@@ -23,6 +28,7 @@ void main(void)
2328
ex_UV = in_UV;
2429

2530
vec4 position = (in_Model * vec4(in_Position,1));
26-
ex_PositionW = position.xyz;
31+
ex_LightDirW = lightPositionW - position.xyz;
32+
ex_ViewDirW = viewPosW - position.xyz;
2733
gl_Position = in_Proj * (in_View * position);
2834
}

0 commit comments

Comments
 (0)