fragment.glsl (3326B)
1 #version 330 core 2 3 struct Material { 4 sampler2D Diffuse; 5 sampler2D Specular; 6 float Shiness; 7 }; 8 9 struct DirLight { 10 vec3 Dir; 11 vec3 Ambient; 12 vec3 Diffuse; 13 vec3 Specular; 14 }; 15 16 struct PointLight { 17 vec3 Pos; 18 vec3 Ambient; 19 vec3 Diffuse; 20 vec3 Specular; 21 22 float Kc, Kl, Kq; 23 }; 24 25 struct SpotLight { 26 vec3 Pos; 27 vec3 Dir; 28 vec3 Ambient; 29 vec3 Diffuse; 30 vec3 Specular; 31 32 float Kc, Kl, Kq; 33 float CutOff, OuterCutOff; 34 }; 35 36 in vec3 fnormal; 37 in vec3 fpos; 38 in vec2 texPos; 39 40 uniform vec3 CamPos; 41 uniform vec3 objCol; 42 uniform Material Cube; 43 uniform DirLight Sun; 44 uniform SpotLight Light; 45 uniform PointLight PointLights[10]; 46 47 out vec4 fcol; 48 49 vec3 calcPointLight(PointLight, Material, vec3); 50 vec3 calcSpotLight(SpotLight, Material, vec3); 51 vec3 calcDirLight(DirLight, Material, vec3); 52 53 void main() { 54 fcol = vec4(0, 0, 0, 1); 55 fcol += vec4(calcSpotLight(Light, Cube, CamPos), 0); 56 // fcol += vec4(calcDirLight(Sun, Cube, CamPos), 0); 57 for (int i = 0; i < PointLights.length(); i++) { 58 fcol += vec4(calcPointLight(PointLights[i], Cube, CamPos), 0); 59 } 60 float x; 61 } 62 63 vec3 calcPointLight(PointLight l, Material m, vec3 camPos) { 64 vec3 ambient, diffuse, specular; 65 vec3 lightDir, viewDir, reflectDir; 66 float diff, spec, dist, attenuation; 67 68 dist = length(l.Pos - fpos); 69 attenuation = 1 / (l.Kc + l.Kl * dist + l.Kq * dist * dist); 70 71 ambient = vec3(texture(m.Diffuse, texPos)) * l.Ambient; 72 73 lightDir = normalize(l.Pos - fpos); 74 diff = max(dot(fnormal, lightDir), 0.0); 75 diffuse = diff * vec3(texture(m.Diffuse, texPos)) * l.Diffuse; 76 77 viewDir = normalize(camPos - fpos); 78 reflectDir = reflect(-lightDir, normalize(fnormal)); 79 spec = pow(max(dot(viewDir, reflectDir), 0.0), m.Shiness); 80 specular = vec3(texture(m.Specular, texPos)) * spec * l.Specular; 81 82 return (ambient + diffuse + specular) * attenuation; 83 } 84 85 86 vec3 calcSpotLight(SpotLight l, Material m, vec3 camPos) { 87 vec3 ambient, diffuse, specular; 88 vec3 lightDir, viewDir, reflectDir; 89 float diff, spec, dist, attenuation; 90 float epsilon, theta, intensity; 91 92 dist = length(l.Pos - fpos); 93 attenuation = 1 / (l.Kc + l.Kl * dist + l.Kq * dist * dist); 94 95 ambient = vec3(texture(m.Diffuse, texPos)) * l.Ambient; 96 97 lightDir = normalize(l.Pos - fpos); 98 theta = dot(lightDir, normalize(-l.Dir)); 99 epsilon = l.CutOff - l.OuterCutOff; 100 intensity = clamp((theta - l.OuterCutOff) / epsilon, 0.0, 1.0); 101 diff = max(dot(fnormal, lightDir), 0.0); 102 diffuse = diff * vec3(texture(m.Diffuse, texPos)) * l.Diffuse * intensity; 103 104 viewDir = normalize(camPos - fpos); 105 reflectDir = reflect(-lightDir, normalize(fnormal)); 106 spec = pow(max(dot(viewDir, reflectDir), 0.0), m.Shiness); 107 specular = vec3(texture(m.Specular, texPos)) * spec * l.Specular * intensity; 108 109 return (ambient + diffuse + specular) * attenuation; 110 } 111 112 vec3 calcDirLight(DirLight l, Material m, vec3 camPos) { 113 vec3 ambient, diffuse, specular; 114 vec3 lightDir, viewDir, reflectDir; 115 float diff, spec; 116 117 ambient = vec3(texture(m.Diffuse, texPos)) * l.Ambient; 118 119 lightDir = normalize(-l.Dir); 120 diff = max(dot(fnormal, lightDir), 0.0); 121 diffuse = diff * vec3(texture(m.Diffuse, texPos)) * l.Diffuse; 122 123 viewDir = normalize(camPos - fpos); 124 reflectDir = reflect(-lightDir, fnormal); 125 spec = pow(max(dot(viewDir, reflectDir), 0.0), m.Shiness); 126 specular = vec3(texture(m.Specular, texPos)) * spec * l.Specular; 127 return ambient + diffuse + specular; 128 }