Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #vertex shader
- #version 330 core
- layout(location = 0) in vec3 layout_vert_position;
- layout(location = 1) in vec3 layout_vert_normal;
- layout(location = 2) in vec2 layout_vert_text_coords;
- uniform mat4 u_ModelViewMatrix;
- uniform mat4 u_CameraViewMatrix;
- uniform mat4 u_CameraProjectionMatrix;
- uniform vec3 u_CameraPosition;
- out vec3 fs_vert_position;
- out vec3 fs_camera_position;
- out vec3 fs_vert_normal;
- out vec2 fs_texture_coord;
- void main()
- {
- vec4 vert_position = u_ModelViewMatrix * vec4(layout_vert_position, 1.0);
- vec3 vert_normal = mat3(transpose(inverse(u_ModelViewMatrix))) * layout_vert_normal;
- vec3 norm = normalize(vert_normal);
- vec4 world_position = u_CameraViewMatrix * vert_position;
- gl_Position = u_CameraProjectionMatrix * world_position;
- // send data to fragment shader
- fs_vert_position = vert_position.xyz;
- fs_camera_position = u_CameraPosition;
- fs_vert_normal = norm;
- fs_texture_coord = layout_vert_text_coords;
- }
- #fragment shader
- #version 330 core
- out vec4 out_color;
- in vec3 fs_vert_position;
- in vec3 fs_camera_position;
- in vec3 fs_vert_normal;
- in vec2 fs_texture_coord;
- uniform vec3 u_MaterialKa;
- uniform vec3 u_MaterialKd;
- uniform vec3 u_MaterialKs;
- uniform float u_MaterialNs;
- uniform float u_MaterialNi;
- uniform float u_MaterialD;
- uniform sampler2D u_MaterialMapKa;
- uniform sampler2D u_MaterialMapKd;
- uniform sampler2D u_MaterialMapKs;
- #define MAX_LIGHTS 4
- uniform vec3 u_Gamma;
- uniform float u_FogDensity;
- uniform float u_FogGradient;
- uniform vec4 u_FogColor;
- struct PointLight
- {
- vec3 position;
- vec3 ka;
- vec3 kd;
- vec3 ks;
- float constant;
- float linear;
- float quadratic;
- };
- uniform PointLight u_PointLights[MAX_LIGHTS];
- uniform int u_NumPointLights;
- struct DirLight
- {
- vec3 direction;
- vec3 ka;
- vec3 kd;
- vec3 ks;
- };
- uniform DirLight u_DirectionalLight;
- struct SpotLight
- {
- vec3 position;
- vec3 direction;
- float cutOff;
- float outerCutOff;
- float constant;
- float linear;
- float quadratic;
- vec3 ka;
- vec3 kd;
- vec3 ks;
- };
- uniform int u_NumSpotLights;
- uniform SpotLight u_SpotLights[MAX_LIGHTS];
- vec3 calc_directional_light(DirLight light, vec3 normal, vec3 view_direction)
- {
- vec3 light_direction = normalize(-light.direction);
- // diffuse shading
- float diff = max(dot(normal, light_direction), 0.0);
- // specular shading
- vec3 reflect_direction = reflect(-light_direction, normal);
- float spec = pow(max(dot(view_direction, reflect_direction), 0.0), u_MaterialNs);
- // combine results
- vec3 ambient = light.ka * vec3(texture(u_MaterialMapKa, fs_texture_coord));
- vec3 diffuse = light.kd * diff * vec3(texture(u_MaterialMapKd, fs_texture_coord));
- vec3 specular = light.ks * spec * vec3(texture(u_MaterialMapKs, fs_texture_coord));
- return (ambient + diffuse + specular);
- }
- vec3 calc_point_light(PointLight light, vec3 normal, vec3 vert_position, vec3 view_direction)
- {
- vec3 light_direction = normalize(light.position - vert_position);
- // diffuse shading
- float diff = max(dot(normal, light_direction), 0.0);
- // specular shading
- vec3 reflect_direction = reflect(-light_direction, normal);
- float spec = pow(max(dot(view_direction, reflect_direction), 0.0), u_MaterialNs);
- // attenuation
- float distance = length(light.position - vert_position);
- float attenuation = 1.0 / (light.constant + light.linear * distance +
- light.quadratic * (distance * distance));
- // combine results
- vec3 ambient = light.ka * vec3(texture(u_MaterialMapKa, fs_texture_coord));
- vec3 diffuse = light.kd * diff * vec3(texture(u_MaterialMapKd, fs_texture_coord));
- vec3 specular = light.ks * spec * vec3(texture(u_MaterialMapKs, fs_texture_coord));
- ambient *= attenuation;
- diffuse *= attenuation;
- specular *= attenuation;
- return (ambient + diffuse + specular);
- }
- vec3 calc_spot_light(SpotLight light, vec3 normal, vec3 vert_position, vec3 view_direction)
- {
- vec3 light_direction = normalize(light.position - vert_position);
- // diffuse shading
- float diff = max(dot(normal, light_direction), 0.0);
- // specular shading
- vec3 reflect_direction = reflect(-light_direction, normal);
- float spec = pow(max(dot(view_direction, reflect_direction), 0.0), u_MaterialNs);
- // attenuation
- float distance = length(light.position - vert_position);
- float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
- // spotlight intensity
- float theta = dot(light_direction, normalize(-light.direction));
- float epsilon = light.cutOff - light.outerCutOff;
- float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
- // combine results
- vec3 ambient = light.ka * vec3(texture(u_MaterialMapKa, fs_texture_coord));
- vec3 diffuse = light.kd * diff * vec3(texture(u_MaterialMapKd, fs_texture_coord));
- vec3 specular = light.ks * spec * vec3(texture(u_MaterialMapKs, fs_texture_coord));
- ambient *= attenuation * intensity;
- diffuse *= attenuation * intensity;
- specular *= attenuation * intensity;
- return (ambient + diffuse + specular);
- }
- void main()
- {
- // properties
- vec3 view_direction = normalize(fs_camera_position - fs_vert_position);
- vec3 result = u_MaterialKa + u_MaterialKd + u_MaterialKs;
- // phase 1: Directional lighting
- result = calc_directional_light(u_DirectionalLight, fs_vert_normal, view_direction);
- // phase 2: Point lights
- for (int i = 0; i < u_NumPointLights; i++)
- {
- result += calc_point_light(u_PointLights[i], fs_vert_normal, fs_vert_position, view_direction);
- }
- // phase 3: Spot lights
- for (int i = 0; i < u_NumSpotLights; i++)
- {
- result += calc_spot_light(u_SpotLights[i], fs_vert_normal, fs_vert_position, view_direction);
- }
- // TODO : Fog
- out_color = vec4(result, 1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement