shader.fs

 avatar
unknown
c_cpp
10 months ago
4.9 kB
4
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;
uniform int PixelLighting;

vec3 directionalLight(vec3 vertexPosition, vec3 vertexNormal){
	vec3 i = normalize(light.position);
	vec3 j = normalize(cameraPosition - vertexPosition);
	vec3 k = normalize(i + j);
	vec3 l = normalize(vertexNormal);

	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;
	return ambient + diffuse + specular;
}

vec3 positionLight(vec3 vertexPosition, vec3 vertexNormal){
	vec3 i = normalize(light.position - vertexPosition);
	vec3 j = normalize(cameraPosition - vertexPosition);
	vec3 k = normalize(i + j);
	vec3 l = normalize(vertexNormal);

	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 - vertexPosition);
	float attenuation = light.Constant + light.Linear * dist + light.Quadratic * dist * dist;
	float f = min(1. / attenuation, 1.);
	return ambient + f * (diffuse + specular);
}

vec3 spotLight(vec3 vertexPosition, vec3 vertexNormal){
	vec3 i = normalize(light.position - vertexPosition);
	vec3 j = normalize(cameraPosition - vertexPosition);
	vec3 k = normalize(i + j);
	vec3 l = normalize(vertexNormal);

	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 - vertexPosition);
	float attenuation = light.Constant + light.Linear * dist + light.Quadratic * dist * dist;
	float f = min(1. / attenuation, 1.);

	vec3 x = normalize(vertexPosition - 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);
	return  ambient + spoteffect * f * (diffuse + specular);
}


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 = PixelLighting == 1 ? vec4(color, 1.0f) : vec4(vertex_color, 1.0f);
}
Editor is loading...
Leave a Comment