Skip to content

Commit

Permalink
issue #11: changed the light direction vector to a vec4, now using th…
Browse files Browse the repository at this point in the history
…e w component to determine if a light is a directional light or a point light
  • Loading branch information
cpadilla committed Aug 23, 2019
1 parent 662bf7f commit c9d87a4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
8 changes: 4 additions & 4 deletions SpacePirates/levels/TestLevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ void TestLevel::load() {
cube->shader.setFloat("material.specular", 0.7f, 0.6f, 0.6f); // red shiny
cube->shader.setFloat("material.shininess", 25);

cube->shader.setFloat("dlight.ambient", 0.2f, 0.2f, 0.2f);
cube->shader.setFloat("dlight.diffuse", 1.0f, 1.0f, 1.0f); // darken the light a bit to fit the scene
cube->shader.setFloat("dlight.specular", 1.0f, 1.0f, 1.0f);
cube->shader.setFloat("light.ambient", 0.2f, 0.2f, 0.2f);
cube->shader.setFloat("light.diffuse", 1.0f, 1.0f, 1.0f, 1.0f); // darken the light a bit to fit the scene
cube->shader.setFloat("light.specular", 1.0f, 1.0f, 1.0f);
}

void TestLevel::update() {
Expand All @@ -117,7 +117,7 @@ void TestLevel::update() {
float lightZ = cos(getTime()) * radius;
lamp->setPosition(vec3(lightX, 1.0f, lightZ));
// cout << "lightPos: " << lamp->getPosition().x << ", " << lamp->getPosition().y << ", " << lamp->getPosition().z << endl;
cube->shader.setFloat("dlight.direction", lamp->getPosition().x, lamp->getPosition().y, lamp->getPosition().z);
cube->shader.setFloat("light.direction", lamp->getPosition().x, lamp->getPosition().y, lamp->getPosition().z, 1.0f);

vec3 camPos = GameWindow::getInstance()->mainCamera.get()->getPosition();
// cout << "camPos: " << camPos.x << ", " << camPos.y << ", " << camPos.z << endl;
Expand Down
42 changes: 26 additions & 16 deletions SpacePirates/normal.frag
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ struct Material {
float shininess;
};

struct DirectionalLight {
vec3 direction;
struct Light {
// directional light if w == 0
vec4 direction;

vec3 ambient;
vec3 diffuse;
vec3 specular;
};

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

uniform vec3 viewPos;
uniform Material material;
uniform DirectionalLight dlight;
uniform Light light;
uniform float time;

void main()
Expand All @@ -32,20 +33,29 @@ void main()
vec3 textSpec = texture(material.specular, TexCoords).rgb;

// ambient
vec3 ambient = dlight.ambient * textDiff;

// diffuse
vec3 ambient = light.ambient * textDiff;

vec3 lightDir;
// check if this is a directional light
if (light.direction.w == 0.0) {
lightDir = normalize(-light.direction.xyz);
} // else if it's a point light
else if (light.direction.w == 1.0) {
lightDir = normalize(light.direction.xyz);
}

// diffuse
vec3 norm = normalize(Normal);
//vec3 lightDir = normalize(light.position - FragPos);
vec3 lightDir = normalize(dlight.direction);
//vec3 lightDir = normalize(light.direction.xyz);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = dlight.diffuse * diff * textDiff;
vec3 diffuse = light.diffuse * diff * textDiff;

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

vec3 textEmit;

Expand All @@ -54,7 +64,7 @@ void main()
} else {
textEmit = texture(material.emission, vec2(TexCoords.x, TexCoords.y + time)).rgb;
}

vec3 result = ambient + diffuse + specular + textEmit;
FragColor = vec4(result, 1.0);
}
}

0 comments on commit c9d87a4

Please sign in to comment.