summaryrefslogtreecommitdiff
path: root/data/shaders/phong-frag.glsl
blob: 979869ed38a520c51a0a989482246b676ea3e43b (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// -*- mode: c -*-

struct Material {
  vec3 ambient;
  vec3 diffuse;
  vec3 specular;
  float shininess;
};

struct Light {
  bool enabled;
  int type;
  vec3 position;
  vec3 direction;
  vec4 color;
  float intensity;
  float cutOff;
};

#define MAX_LIGHTS 4

#ifdef GLSL120
varying vec3 fragWorldPos;
varying vec3 fragNorm;
varying vec2 fragTex;
#else
in vec3 fragWorldPos;
in vec3 fragNorm;
in vec2 fragTex;
#endif

#ifdef GLSL330
out vec4 fragColor;
#endif

uniform samplerCube skybox;
uniform sampler2D ambientMap;
uniform sampler2D diffuseMap;
uniform sampler2D specularMap;
uniform sampler2D normalMap;
uniform Material material;
uniform Light lights[MAX_LIGHTS];
uniform vec3 cameraPosition;

const float GAMMA = 2.2;

#ifndef GLSL330
// Compatibility shim for older GLSL versions.
vec4 texture(sampler2D tex, vec2 coord) {
  return texture2D(tex, coord);
}
#endif

vec3 gammaCorrect(vec3 color) {
  return pow(color, vec3(1.0 / GAMMA));
}

vec3 toneMap(vec3 color) {
  return color / (color + vec3(1.0));
}

vec3 lightDirection(Light light) {
  if(light.type == 0 || light.type == 2) { // point and spot lights
    return normalize(light.position - fragWorldPos);
  } else if(light.type == 1) { // directional light
    return normalize(-light.direction);
  }

  return vec3(0.0); // should never be reached.
}

vec3 lightAttenuate(Light light) {
  float distance = length(light.position - fragWorldPos);
  float attenuation = 1.0 / (distance * distance);
  return light.color.rgb * light.intensity * attenuation;
}

vec3 lightRadiance(Light light, vec3 direction) {
  if(light.type == 0) { // point light
    return lightAttenuate(light);
  } else if(light.type == 1) { // directional light
    return light.color.rgb * light.intensity;
  } else if(light.type == 2) { // spot light
    float theta = dot(direction, normalize(-light.direction));
    // Spot lights only shine light in a specific conical area.
    // They have no effect outside of that area.
    if(theta > light.cutOff) {
      // Feather out the light as it approaches the edge of its cone.
      float intensity = (theta - light.cutOff) / (1.0 - light.cutOff);
      return lightAttenuate(light) * intensity;
    } else {
      return vec3(0.0);
    }
  }

  return vec3(0.0); // should never be reached.
}

vec3 materialAmbient() {
  return texture(ambientMap, fragTex).rgb * material.ambient;
}

vec3 materialDiffuse() {
  vec4 color = texture(diffuseMap, fragTex);
  // discard transparent fragments.
  if(color.a == 0.0) {
    discard;
  }
  return color.rgb * material.diffuse;
}

vec3 materialSpecular() {
  return texture(specularMap, fragTex).rgb * material.specular;
}

vec3 materialNormal() {
  // Compute tangent space using fragment data rather than relying
  // on tangent attributes.  See:
  // http://www.thetenthplanet.de/archives/1180
  vec3 tangentNormal = normalize(texture(normalMap, fragTex).xyz * 2.0 - 1.0);
  vec3 q1 = dFdx(fragWorldPos);
  vec3 q2 = dFdy(fragWorldPos);
  vec2 st1 = dFdx(fragTex);
  vec2 st2 = dFdy(fragTex);
  vec3 N = normalize(fragNorm);
  vec3 T = normalize(q1 * st2.t - q2 * st1.t);
  vec3 B = -normalize(cross(N, T));
  mat3 TBN = mat3(T, B, N);

  return normalize(TBN * tangentNormal);
}

void main() {
  vec3 viewDir = normalize(cameraPosition - fragWorldPos);
  vec3 ambientOcclusion = materialAmbient();
  vec3 diffuseColor = materialDiffuse();
  vec3 specularColor = materialSpecular();
  vec3 normal = materialNormal();
  vec3 reflection = reflect(-viewDir, normal);
  vec3 color = vec3(0.0);

  // Apply direct lighting.
  for(int i = 0; i < MAX_LIGHTS; ++i) {
    Light light = lights[i];

    if(!light.enabled) {
      continue;
    }

    vec3 lightDir = lightDirection(light);
    vec3 halfVector = normalize(lightDir + viewDir);
    vec3 radiance = lightRadiance(light, lightDir);
    float lambert = clamp(dot(normal, lightDir), 0.0, 1.0);
    vec3 diffuseLight = radiance * lambert;
    float specularFactor = clamp(dot(halfVector, normal), 0.0, 1.0) * float(lambert > 0.0);
    vec3 specularLight = radiance * pow(specularFactor, material.shininess);
    color += diffuseLight * diffuseColor + specularLight * specularColor;
  }

  // Apply image based ambient lighting.
  float fresnel = pow(1.0 - clamp(dot(viewDir, normal), 0.0, 1.0), 5);
  float roughness = 1.0 - (material.shininess / 1000.0);
  vec3 ambientDiffuse = textureCube(skybox, normal).rgb * diffuseColor;
  vec3 ambientSpecular = textureLod(skybox, reflection, roughness * 7.0).rgb * fresnel;
  vec3 ambientColor = (ambientDiffuse + ambientSpecular) * ambientOcclusion;
  color += ambientColor;

  // Apply gamma correction and HDR tone mapping to get the final
  // color.
  vec4 finalColor = vec4(toneMap(gammaCorrect(color)), 1.0);
#ifdef GLSL330
  fragColor = finalColor;
#else
  gl_FragColor = finalColor;
#endif
}