tofu

Making something with OpenGL in Go
Log | Files | Refs

teapot.glsl (1901B)


      1 #version 330 core
      2 
      3 struct Material {
      4 	vec3  diffuse;
      5 	vec3  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 theta, epsilon, 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 = material.diffuse * Light.Ambient;
     54 
     55 	lightDir = normalize(Light.Pos - fpos);
     56 	theta = dot(lightDir, normalize(-Light.Dir));
     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 * material.diffuse * Light.Diffuse * intensity;
     61 
     62 	viewDir = normalize(camPos - fpos);
     63 	reflectDir = reflect(-lightDir, fnormal);
     64 	spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shiness);
     65 	specular = material.specular * spec * Light.Specular * intensity;
     66 
     67 	fcol += vec4((ambient+diffuse+specular) * attenuation, 0);
     68 
     69 
     70 	ambient = material.diffuse * Sun.Ambient;
     71 
     72 	lightDir = normalize(-Sun.Dir);
     73 	diff = max(dot(fnormal, lightDir), 0.0);
     74 	diffuse = diff * material.diffuse * 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 = material.specular * spec * Sun.Specular;
     80 
     81 	fcol += vec4(ambient + diffuse + specular, 0);
     82 }
     83