Flashlight
This commit is contained in:
@@ -17,7 +17,9 @@ uniform Material u_material;
|
||||
|
||||
struct Light {
|
||||
vec3 position;
|
||||
//vec3 direction;
|
||||
vec3 direction;
|
||||
float innerCutOff;
|
||||
float outerCutOff;
|
||||
|
||||
vec3 ambient;
|
||||
vec3 diffuse;
|
||||
@@ -50,15 +52,23 @@ void main() {
|
||||
// Specular lighting
|
||||
vec3 viewDir = normalize(u_viewPosition - v_fragmentPosition);
|
||||
vec3 reflectDir = reflect(-lightDir, normal);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), u_material.shininess);
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0f), u_material.shininess);
|
||||
vec3 specular = u_light.specular * spec * vec3(texture(u_material.texture_specular0, v_texCoord));
|
||||
|
||||
float distanceLightFragment = length(vecLightToFragment);
|
||||
float attenuation = 1.0 / (u_light.K_c + u_light.K_l * distanceLightFragment + u_light.K_q * distanceLightFragment * distanceLightFragment);
|
||||
float attenuation = 1.0f / (u_light.K_c + u_light.K_l * distanceLightFragment + u_light.K_q * distanceLightFragment * distanceLightFragment);
|
||||
|
||||
ambient *= attenuation;
|
||||
diffuse *= attenuation;
|
||||
specular *= attenuation;
|
||||
|
||||
f_color = vec4((ambient + diffuse + specular), 1.0f);
|
||||
float theta = dot(lightDir, normalize(-u_light.direction));
|
||||
float epsilon = u_light.innerCutOff - u_light.outerCutOff;
|
||||
float intensity = clamp((theta - u_light.outerCutOff) / epsilon, 0.0f, 1.0f);
|
||||
|
||||
diffuse *= intensity;
|
||||
specular *= intensity;
|
||||
|
||||
f_color = vec4(ambient + diffuse + specular, 1.0f);
|
||||
|
||||
}
|
||||
|
||||
@@ -65,6 +65,7 @@ void Camera::updateDirectionFromMouseInput(float deltaCursorX, float deltaCursor
|
||||
if(pitch < -89.0f)
|
||||
pitch = -89.0f;
|
||||
|
||||
glm::vec3 direction;
|
||||
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
direction.y = sin(glm::radians(pitch));
|
||||
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
|
||||
@@ -22,6 +22,7 @@ public:
|
||||
|
||||
glm::mat4 getViewProj() { return viewProjectionMatrix; }
|
||||
glm::vec3 getPosition() { return position; }
|
||||
glm::vec3 getDirection() { return frontVec; }
|
||||
|
||||
private:
|
||||
|
||||
@@ -31,7 +32,6 @@ private:
|
||||
glm::mat4 viewProjectionMatrix;
|
||||
|
||||
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||
glm::vec3 direction = glm::vec3(0.0f, 0.0f, 1.0f);
|
||||
glm::vec3 frontVec = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||
glm::vec3 upVec = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||
|
||||
|
||||
@@ -103,6 +103,12 @@ void Controller::run() {
|
||||
|
||||
// Update game
|
||||
// ...
|
||||
shaderProgram.bind();
|
||||
shaderProgram.setUniform("u_light.position", camera->getPosition());
|
||||
shaderProgram.setUniform("u_light.direction", camera->getDirection());
|
||||
shaderProgram.setUniform("u_light.innerCutOff", glm::cos(glm::radians(12.5f)));
|
||||
shaderProgram.setUniform("u_light.outerCutOff", glm::cos(glm::radians(25.0f)));
|
||||
shaderProgram.unbind();
|
||||
|
||||
// Render and buffer swap
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
Reference in New Issue
Block a user