|
| 1 | +#version 400 |
| 2 | +uniform highp mat4 model; |
| 3 | +uniform highp mat4 normModel; |
| 4 | +uniform highp mat4 projView; |
| 5 | +uniform highp vec3 cameraPos; |
| 6 | + |
| 7 | +struct Material |
| 8 | +{ |
| 9 | + vec3 ambient; |
| 10 | + vec3 diffuse; |
| 11 | + vec3 specular; |
| 12 | + float shininess; |
| 13 | +}; |
| 14 | + |
| 15 | +struct DirLightSource |
| 16 | +{ |
| 17 | + lowp vec3 color; |
| 18 | + float intensity; |
| 19 | + highp vec3 direction; |
| 20 | +}; |
| 21 | + |
| 22 | +struct PointLightSource |
| 23 | +{ |
| 24 | + lowp vec3 color; |
| 25 | + highp vec3 position; |
| 26 | + |
| 27 | + float intensity; |
| 28 | + float constFactor; |
| 29 | + float linFactor; |
| 30 | + float quadFactor; |
| 31 | +}; |
| 32 | + |
| 33 | +struct SpotLightSource |
| 34 | +{ |
| 35 | + lowp vec3 color; |
| 36 | + highp vec3 position; |
| 37 | + highp vec3 direction; |
| 38 | + |
| 39 | + float cutOff; |
| 40 | + float outerCutOff; |
| 41 | + |
| 42 | + float intensity; |
| 43 | + float constFactor; |
| 44 | + float linFactor; |
| 45 | + float quadFactor; |
| 46 | +}; |
| 47 | + |
| 48 | +uniform DirLightSource dirLights[10]; |
| 49 | +uniform int dirLightsCount; |
| 50 | + |
| 51 | +uniform PointLightSource pointLights[10]; |
| 52 | +uniform int pointLightsCount; |
| 53 | + |
| 54 | +uniform SpotLightSource spotLights[10]; |
| 55 | +uniform int spotLightsCount; |
| 56 | + |
| 57 | +uniform Material material; |
| 58 | + |
| 59 | +vec3 CalcDirLight(DirLightSource light, vec3 normal, vec3 toEye) |
| 60 | +{ |
| 61 | + vec3 lightDir = normalize(-light.direction); |
| 62 | + // diffuse shading |
| 63 | + float diff = max(dot(normal, lightDir), 0.0); |
| 64 | + // specular shading |
| 65 | + vec3 reflectDir = reflect(-lightDir, normal); |
| 66 | + float spec = pow(max(dot(toEye, reflectDir), 0.0), material.shininess) * sign(diff); |
| 67 | + // combine results |
| 68 | + vec3 ambient = material.ambient; |
| 69 | + vec3 diffuse = diff * material.diffuse; |
| 70 | + vec3 specular = spec * material.specular; |
| 71 | + return light.intensity * light.color * (ambient + diffuse + specular); |
| 72 | +} |
| 73 | + |
| 74 | +vec3 CalcPointLight(PointLightSource light, vec3 normal, vec3 vertexPos, vec3 toEye) |
| 75 | +{ |
| 76 | + vec3 lightDir = normalize(light.position - vertexPos); |
| 77 | + // diffuse shading |
| 78 | + float diff = max(dot(normal, lightDir), 0.0); |
| 79 | + // specular shading |
| 80 | + vec3 reflectDir = reflect(-lightDir, normal); |
| 81 | + float spec = pow(max(dot(toEye, reflectDir), 0.0), 256) * sign(diff); |
| 82 | + // attenuation |
| 83 | + float distance = length(light.position - vertexPos); |
| 84 | + float attenuation = 1.0 / (light.constFactor + light.linFactor * distance + |
| 85 | + light.quadFactor * distance * distance); |
| 86 | + // combine results |
| 87 | + vec3 diffuse = diff * material.diffuse; |
| 88 | + vec3 specular = spec * material.specular; |
| 89 | + |
| 90 | + return light.intensity * attenuation * (diffuse + specular) * light.color; |
| 91 | +} |
| 92 | + |
| 93 | +vec3 CalcSpotLight(SpotLightSource light, vec3 normal, vec3 vertexPos, vec3 toEye) |
| 94 | +{ |
| 95 | + vec3 lightDir = normalize(light.position - vertexPos); |
| 96 | + // diffuse shading |
| 97 | + float diff = max(dot(normal, lightDir), 0.0); |
| 98 | + // specular shading |
| 99 | + vec3 reflectDir = reflect(-lightDir, normal); |
| 100 | + float spec = pow(max(dot(toEye, reflectDir), 0.0), material.shininess) * sign(diff); |
| 101 | + // attenuation |
| 102 | + float distance = length(light.position - vertexPos); |
| 103 | + float attenuation = 1.0 / (light.constFactor + light.linFactor * distance + |
| 104 | + light.quadFactor * distance * distance); |
| 105 | + // spotlight intensity |
| 106 | + float theta = acos(dot(lightDir, normalize(-light.direction))); |
| 107 | + float epsilon = light.cutOff - light.outerCutOff; |
| 108 | + float intensity = light.intensity * clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0); |
| 109 | + // combine results |
| 110 | + vec3 diffuse = diff * material.diffuse; |
| 111 | + vec3 specular = spec * material.specular; |
| 112 | + |
| 113 | + return attenuation * intensity * (diffuse + specular) * light.color; |
| 114 | +} |
| 115 | + |
| 116 | +layout (location = 0) in highp vec3 position; |
| 117 | +layout (location = 1) in highp vec3 normal; |
| 118 | + |
| 119 | +out lowp vec4 vertexColor; |
| 120 | + |
| 121 | +void main() |
| 122 | +{ |
| 123 | + vec3 norm = normalize(mat3(normModel) * normal); |
| 124 | + vec4 worldVertexPos = model * vec4(position, 1.f); |
| 125 | + vec3 toEye = normalize(cameraPos - worldVertexPos.xyz); |
| 126 | + vec3 resultCol = vec3(0.0); |
| 127 | + |
| 128 | + for (int i = 0; i < min(dirLightsCount, 10); ++i) |
| 129 | + { |
| 130 | + resultCol += CalcDirLight(dirLights[i], norm, toEye); |
| 131 | + } |
| 132 | + for (int i = 0; i < min(pointLightsCount, 10); ++i) |
| 133 | + { |
| 134 | + resultCol += CalcPointLight(pointLights[i], norm, worldVertexPos.xyz, toEye); |
| 135 | + } |
| 136 | + for (int i = 0; i < min(spotLightsCount, 10); ++i) |
| 137 | + { |
| 138 | + resultCol += CalcSpotLight(spotLights[i], norm, worldVertexPos.xyz, toEye); |
| 139 | + } |
| 140 | + |
| 141 | + vertexColor = vec4(resultCol, 1.0); |
| 142 | + gl_Position = projView * worldVertexPos; |
| 143 | +} |
0 commit comments