Skip to content

Commit

Permalink
Light Casters
Browse files Browse the repository at this point in the history
  • Loading branch information
SeriousSamV committed Feb 21, 2017
1 parent a71b7cf commit 8faf52d
Show file tree
Hide file tree
Showing 6 changed files with 1,782 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 12/12.vertex.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#version 330 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoords;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoords;

void main()
{
gl_Position = projection * view * model * vec4(position, 1.0f);
FragPos = vec3(model * vec4(position, 1.0f));
Normal = mat3(transpose(inverse(model))) * normal;
TexCoords = texCoords;
}
61 changes: 61 additions & 0 deletions 12/attenuation.fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#version 330 core

struct Material
{
sampler2D diffuse;
sampler2D specular;
float shininess;
};

struct Light
{
vec3 position;
//vec3 direction;

vec3 ambient;
vec3 diffuse;
vec3 specular;

float constant;
float linear;
float quadratic;
};

in vec2 TexCoords;
in vec3 FragPos;
in vec3 Normal;

out vec4 color;

uniform vec3 viewPos;
uniform Material material;
uniform Light light;

void main()
{
// Ambient
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));

// Diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(light.position - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));

// Specular
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));

float distance = length(light.position - FragPos);
float attenuation = 1.0f / (light.constant +
(light.linear * distance) +
(light.quadratic * (distance * distance)));
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;

vec3 result = ambient + diffuse + specular;
color = vec4(result, 1.0f);
}
49 changes: 49 additions & 0 deletions 12/directional.fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#version 330 core

struct Material
{
sampler2D diffuse;
sampler2D specular;
float shininess;
};

struct Light
{
//vec3 position;
vec3 direction;

vec3 ambient;
vec3 diffuse;
vec3 specular;
};

in vec2 TexCoords;
in vec3 FragPos;
in vec3 Normal;

out vec4 color;

uniform vec3 viewPos;
uniform Material material;
uniform Light light;

void main()
{
// Ambient
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));

// Diffuse
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(-light.direction);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));

// Specular
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));

vec3 result = ambient + diffuse + specular;
color = vec4(result, 1.0f);
}
68 changes: 68 additions & 0 deletions 12/flashlight.spotlight.fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#version 330 core

struct Material
{
sampler2D diffuse;
sampler2D specular;
float shininess;
};

struct Light
{
vec3 position;
vec3 direction;
float cutOff;

vec3 ambient;
vec3 diffuse;
vec3 specular;

float constant;
float linear;
float quadratic;
};

in vec2 TexCoords;
in vec3 FragPos;
in vec3 Normal;

out vec4 color;

uniform vec3 viewPos;
uniform Material material;
uniform Light light;

void main()
{
vec3 lightDir = normalize(light.position - FragPos);
float cosTheta = dot(lightDir, normalize(-light.direction));
if (cosTheta > light.cutOff) {
// Ambient
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));

// Diffuse
vec3 norm = normalize(Normal);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));

// Specular
vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));

// Attenuation
float distance = length(light.position - FragPos);
float attenuation = 1.0f / (light.constant +
(light.linear * distance) +
(light.quadratic * (distance * distance)));
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;

color = vec4(ambient + diffuse + specular, 1.0f);
}
else {
color = vec4(light.ambient * vec3(texture(material.diffuse, TexCoords)), 1.0f);
}
}
Loading

0 comments on commit 8faf52d

Please sign in to comment.