summaryrefslogtreecommitdiff
path: root/data/shaders/phong-frag.glsl
blob: 7cab3f8ed4deb67a8158e2c32d0d196ec4a15398 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// -*- mode: c -*-

struct Material {
  vec3 ambient;
  sampler2D ambientMap;
  bool useAmbientMap;
  vec3 diffuse;
  sampler2D diffuseMap;
  bool useDiffuseMap;
  vec3 specular;
  sampler2D specularMap;
  bool useSpecularMap;
  float shininess;
  sampler2D bumpMap;
  bool useBumpMap;
};

struct Light {
  bool enabled;
  int type;
  vec3 position;
  vec3 direction;
  vec4 color;
  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 Material material;
uniform Light lights[MAX_LIGHTS];
uniform vec3 cameraPosition;
uniform vec4 ambientLightColor;

const float GAMMA = 2.2;

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

float posDot(vec3 v1, vec3 v2) {
  return max(dot(v1, v2), 0.0);
}

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 * 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;
  } 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() {
  if(material.useAmbientMap) {
    return texture(material.ambientMap, fragTex).rgb;
  } else {
    return material.ambient;
  }
}

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

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

vec3 materialNormal() {
  if(material.useBumpMap) {
    // Compute tangent space using fragment data rather than relying
    // on tangent attributes.  See:
    // http://www.thetenthplanet.de/archives/1180
    vec3 tangentNormal = normalize(texture(material.bumpMap, 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);
  } else {
    return normalize(fragNorm);
  }
}

void main() {
  vec3 viewDir = normalize(cameraPosition - fragWorldPos);
  vec3 ambientOcclusion = materialAmbient();
  vec3 baseDiffuseColor = materialDiffuse();
  vec3 baseSpecularColor = materialSpecular();
  vec3 normal = materialNormal();
  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 radiance = lightRadiance(light, lightDir);
    float diffuseFactor = posDot(lightDir, normal);
    vec3 reflectDir = reflect(-lightDir, normal);
    vec3 diffuseColor = baseDiffuseColor * diffuseFactor;
    vec3 halfVector = normalize(lightDir + viewDir);
    float specularFactor = pow(posDot(halfVector, normal), material.shininess);
    vec3 specularColor = baseSpecularColor * specularFactor;
    color += (diffuseColor + specularColor) * radiance;
  }

  // Apply ambient lighting.
  vec3 ambientColor = baseDiffuseColor * ambientLightColor.rgb * 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
}