tofu

Making something with OpenGL in Go
Log | Files | Refs

teapot.glsl (4139B)


      1 #version 330 core
      2 
      3 struct Material {
      4 	bool  useTexture;
      5 	vec3  Diffuse;
      6 	vec3  Specular;
      7 	sampler2D map_Kd;
      8 	sampler2D map_Bump;
      9 	sampler2D map_Ks;
     10 	float Shiness;
     11 };
     12 
     13 struct DirLight {
     14 	vec3 Dir;
     15 	vec3 Ambient;
     16 	vec3 Diffuse;
     17 	vec3 Specular;
     18 };
     19 struct PointLight {
     20 	vec3 Pos;
     21 	vec3 Ambient;
     22 	vec3 Diffuse;
     23 	vec3 Specular;
     24 
     25 	float Kc, Kl, Kq;
     26 };
     27 struct SpotLight {
     28 	vec3 Pos;
     29 	vec3 Dir;
     30 	vec3 Ambient;
     31 	vec3 Diffuse;
     32 	vec3 Specular;
     33 
     34 	float Kc, Kl, Kq;
     35 	float CutOff, OuterCutOff;
     36 };
     37 in vec3 fnormal;
     38 in vec3 fpos;
     39 in vec2 texPos;
     40 
     41 uniform vec3 CamPos;
     42 uniform vec3 objCol;
     43 uniform Material Teapot;
     44 uniform DirLight Sun;
     45 uniform SpotLight Light;
     46 uniform PointLight PointLights[10];
     47 
     48 out vec4 fcol;
     49 
     50 vec3 calcPointLight(PointLight, Material, vec3);
     51 vec3 calcSpotLight(SpotLight, Material, vec3);
     52 vec3 calcDirLight(DirLight, Material, vec3);
     53 
     54 void main() {
     55 	fcol = vec4(0, 0, 0, 1);
     56 	fcol += vec4(calcSpotLight(Light, Teapot, CamPos), 0);
     57 //	fcol += vec4(calcDirLight(Sun, Teapot, CamPos), 0);
     58 /*
     59 	for (int i = 0; i < PointLights.length(); i++) {
     60 		fcol += vec4(calcPointLight(PointLights[i], Teapot, CamPos), 0);
     61 	}
     62 */
     63 }
     64 
     65 vec3 calcPointLight(PointLight l, Material m, vec3 camPos) {
     66 	vec3 ambient, diffuse, specular;
     67 	vec3 lightDir, viewDir, reflectDir;
     68 	vec3 normal;
     69 	float diff, spec, dist, attenuation;
     70 
     71 	if (m.useTexture) {
     72 		normal = normalize(vec3(texture(m.map_Bump, texPos)));
     73 	} else {
     74 		normal = fnormal;
     75 	}
     76 
     77 	dist = length(l.Pos - fpos);
     78 	attenuation = 1 / (l.Kc + l.Kl * dist + l.Kq * dist * dist);
     79 
     80 	if (m.useTexture) {
     81 		ambient = vec3(texture(m.map_Kd, texPos)) * l.Ambient;
     82 	} else {
     83 		ambient = m.Diffuse * l.Ambient;
     84 	}
     85 
     86 	lightDir = normalize(l.Pos - fpos);
     87 	diff = max(dot(normal, lightDir), 0.0);
     88 	if (m.useTexture) {
     89 		diffuse = diff * vec3(texture(m.map_Kd, texPos)) * l.Diffuse;
     90 	} else {
     91 		diffuse = diff * m.Diffuse * l.Diffuse;
     92 	}
     93 
     94 	viewDir = normalize(camPos - fpos);
     95 	reflectDir = reflect(-lightDir, normal);
     96 	if (m.useTexture) {
     97 		spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
     98 		specular = vec3(texture(m.map_Ks, texPos)) * spec * l.Specular;
     99 	} else {
    100 		spec = pow(max(dot(viewDir, reflectDir), 0.0), m.Shiness);
    101 		specular = m.Specular * spec * l.Specular;
    102 	}
    103 	return (ambient + diffuse + specular) * attenuation;
    104 }
    105 
    106 vec3 calcSpotLight(SpotLight l, Material m, vec3 camPos) {
    107 	vec3 ambient, diffuse, specular;
    108 	vec3 lightDir, viewDir, reflectDir;
    109 	vec3 normal;
    110 	float diff, spec, dist, attenuation;
    111 	float theta, epsilon, intensity;
    112 
    113 	if (m.useTexture) {
    114 		normal = normalize(vec3(texture(m.map_Bump, texPos))*2 - 1);
    115 	} else {
    116 		normal = fnormal;
    117 	}
    118 
    119 	dist = length(l.Pos - fpos);
    120 	attenuation = 1 / (l.Kc + l.Kl * dist + l.Kq * dist * dist);
    121 
    122 	if (m.useTexture) {
    123 		ambient = vec3(texture(m.map_Kd, texPos)) * l.Ambient;
    124 	} else {
    125 		ambient = m.Diffuse * l.Ambient;
    126 	}
    127 
    128 	lightDir = normalize(l.Pos - fpos);
    129 	theta = dot(lightDir, normalize(-l.Dir));
    130 	epsilon = l.CutOff - l.OuterCutOff;
    131 	intensity = clamp((theta - l.OuterCutOff) / epsilon, 0.0, 1.0);
    132 	diff = max(dot(normal, lightDir), 0.0);
    133 	if (m.useTexture) {
    134 		diffuse = diff * vec3(texture(m.map_Kd, texPos)) * l.Diffuse * intensity;
    135 	} else {
    136 		diffuse = diff * m.Diffuse * l.Diffuse * intensity;
    137 	}
    138 
    139 	viewDir = normalize(camPos - fpos);
    140 	reflectDir = reflect(-lightDir, normal);
    141 	if (m.useTexture) {
    142 		spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
    143 		specular = vec3(texture(m.map_Ks, texPos)) * spec * l.Specular * intensity;
    144 	} else {
    145 		spec = pow(max(dot(viewDir, reflectDir), 0.0), m.Shiness);
    146 		specular = m.Specular * spec * l.Specular * intensity;
    147 	}
    148 
    149 	return (ambient + diffuse + specular) * attenuation;
    150 }
    151 
    152 vec3 calcDirLight(DirLight l, Material m, vec3 camPos) {
    153 	vec3 ambient, diffuse, specular;
    154 	vec3 lightDir, viewDir, reflectDir;
    155 	float diff, spec;
    156 
    157 	ambient = m.Diffuse * l.Ambient;
    158 
    159 	lightDir = normalize(-l.Dir);
    160 	diff = max(dot(fnormal, lightDir), 0.0);
    161 	diffuse = diff * m.Diffuse * l.Diffuse;
    162 
    163 	viewDir = normalize(camPos - fpos);
    164 	reflectDir = reflect(-lightDir, fnormal);
    165 	spec = pow(max(dot(viewDir, reflectDir), 0.0), m.Shiness);
    166 	specular = m.Specular * spec * l.Specular;
    167 
    168 	return ambient + diffuse + specular;
    169 }