tofu

Making something with OpenGL in Go
Log | Files | Refs

fragment.glsl (2058B)


      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 SpotLight {
     17 	vec3 Pos;
     18 	vec3 Dir;
     19 	vec3 Ambient;
     20 	vec3 Diffuse;
     21 	vec3 Specular;
     22 
     23 	float Kc, Kl, Kq;
     24 	float CutOff, OuterCutOff;
     25 };
     26 in vec3 fnormal;
     27 in vec3 fpos;
     28 in vec2 texPos;
     29 
     30 uniform vec3 CamPos;
     31 uniform vec3 objCol;
     32 uniform material Material;
     33 uniform DirLight Sun;
     34 uniform SpotLight Light;
     35 
     36 out vec4 fcol;
     37 
     38 void main() {
     39 	vec3 ambient;
     40 	vec3 lightDir, diffuse;
     41 	vec3 viewDir, specular;
     42 	vec3 reflectDir;
     43 	float diff, spec, dist, attenuation;
     44 	float epsilon, theta, intensity;
     45 
     46 	fcol = vec4(0, 0, 0, 1);
     47 	diffuse = vec3(0, 0, 0);
     48 	specular = vec3(0, 0, 0);
     49 
     50 	dist = length(Light.Pos - fpos);
     51 	attenuation = 1 / (Light.Kc + Light.Kl * dist + Light.Kq * dist * dist);
     52 
     53 	ambient = vec3(texture(Material.Diffuse, texPos)) * Light.Ambient;
     54 
     55 	lightDir = normalize(Light.Pos - fpos);
     56 	theta = dot(lightDir, normalize(-lightDir));
     57 	epsilon = Light.CutOff - Light.OuterCutOff;
     58 	intensity = clamp((theta - Light.OuterCutOff) / epsilon, 0.0, 1.0);
     59 	diff = max(dot(fnormal, lightDir), 0.0);
     60 	diffuse = diff * vec3(texture(Material.Diffuse, texPos)) * Light.Diffuse * intensity;
     61 
     62 	viewDir = normalize(CamPos - fpos);
     63 	reflectDir = reflect(-lightDir, normalize(fnormal));
     64 	spec = pow(max(dot(viewDir, reflectDir), 0), Material.Shiness);
     65 	specular = vec3(texture(Material.Specular, texPos)) * spec * Light.Specular * intensity;
     66 
     67 	fcol += vec4(ambient + diffuse + specular, 0) * attenuation;
     68 
     69 
     70 	ambient = vec3(texture(Material.Diffuse, texPos)) * Sun.Ambient;
     71 
     72 	lightDir = normalize(-Sun.Dir);
     73 	diff = max(dot(fnormal, lightDir), 0.0);
     74 	diffuse = diff * vec3(texture(Material.Diffuse, texPos)) * Sun.Diffuse;
     75 
     76 	viewDir = normalize(CamPos - fpos);
     77 	reflectDir = reflect(-lightDir, fnormal);
     78 	spec = pow(max(dot(viewDir, reflectDir), 0.0), Material.Shiness);
     79 	specular = vec3(texture(Material.Specular, texPos)) * spec * Sun.Specular;
     80 
     81 	fcol += vec4(ambient + diffuse + specular, 0);
     82 
     83 }
     84