shader.fs
unknown
c_cpp
a year ago
2.8 kB
3
Indexable
#version 330 core
out vec4 FragColor;
in vec3 vertex_color;
in vec3 vertex_normal;
in vec3 vertex_pos;
struct PhongMaterial{
vec3 Ka;
vec3 Kd;
vec3 Ks;
};
uniform PhongMaterial material;
struct Lightinfo{
vec3 position;
vec3 ambient;
vec3 diffuse;
vec3 specular;
float Constant;
float Linear;
float Quadratic;
};
uniform Lightinfo light;
struct SpotLight{
vec3 direction;
float exponent;
float cutoff;
};
uniform SpotLight spotlight;
uniform vec3 cameraPosition;
uniform int Lightmode;
uniform float shininess;
void main() {
// [TODO]
vec3 color;
switch(Lightmode){
case 0:
{
vec3 i = normalize(light.position);
vec3 j = normalize(cameraPosition - vertex_pos);
vec3 k = normalize(i + j);
vec3 l = normalize(vertex_normal);
vec3 ambient = light.ambient * material.Ka;
vec3 diffuse = max(dot(i, l), 0.0) * light.diffuse * material.Kd;
vec3 specular = pow(max(dot(k, l), 0.0), shininess) * light.specular * material.Ks;
color = ambient + diffuse + specular;
break;
}
case 1:
{
vec3 i = normalize(light.position - vertex_pos);
vec3 j = normalize(cameraPosition - vertex_pos);
vec3 k = normalize(i + j);
vec3 l = normalize(vertex_normal);
vec3 ambient = light.ambient * material.Ka;
vec3 diffuse = max(dot(i, l), 0.0) * light.diffuse * material.Kd;
vec3 specular = pow(max(dot(k, l), 0.0), shininess) * light.specular * material.Ks;
float dist = length(light.position - vertex_pos);
float attenuation = light.Constant + light.Linear * dist + light.Quadratic * dist * dist;
float f = min(1. / attenuation, 1.);
color = ambient + f * (diffuse + specular);
break;
}
case 2:
{
vec3 i = normalize(light.position - vertex_pos);
vec3 j = normalize(cameraPosition - vertex_pos);
vec3 k = normalize(i + j);
vec3 l = normalize(vertex_normal);
vec3 ambient = light.ambient * material.Ka;
vec3 diffuse = max(dot(i, l), 0.0) * light.diffuse * material.Kd;
vec3 specular = pow(max(dot(k, l), 0.0), shininess) * light.specular * material.Ks;
float dist = length(light.position - vertex_pos);
float attenuation = light.Constant + light.Linear * dist + light.Quadratic * dist * dist;
float f = min(1. / attenuation, 1.);
vec3 x = normalize(vertex_pos - light.position);
vec3 y = normalize(spotlight.direction);
float xy = dot(x, y);
float spoteffect = 0;
if(xy > cos(spotlight.cutoff * acos(-1) / 180.0))
spoteffect = pow(max(xy, 0.0), spotlight.exponent);
color = ambient + spoteffect * f * (diffuse + specular);
break;
}
default:
break;
}
FragColor = vec4(vertex_normal, 1.0f);
}
Editor is loading...
Leave a Comment