Commit 646095b3 authored by Sadman Kazi's avatar Sadman Kazi 🎸

Add specular lighting, complete Blinn-Phong model

parent 45d77648
Pipeline #167 passed with stage
in 3 minutes and 51 seconds
......@@ -51,6 +51,9 @@ camera.
![Texture support](res/demo/texture.gif)
Showcasing texture support.
![Phong Lighting](res/demo/phong.gif)
Phong lighting model.
## Documentation
Refer to the [wiki](https://github.com/sadmansk/GameEngine/wiki) for more information.
Also if you are interested in contributing, follow these [guidelines](https://github.com/sadmansk/GameEngine/wiki/Contributing-to-the-Wiki).
......
This diff is collapsed.
......@@ -2,6 +2,7 @@
in vec2 texCoord0;
in vec3 normal0;
in vec3 worldPos0;
out vec4 frag_color;
struct BaseLight {
......@@ -18,17 +19,31 @@ uniform vec3 u_base_color;
uniform vec3 u_ambient_light;
uniform sampler2D u_sampler;
uniform DirectionalLight u_dir_light;
uniform float u_spec_intensity;
uniform float u_spec_power;
uniform vec3 u_eye_pos;
vec4 calcLight(BaseLight base, vec3 dir, vec3 normal) {
float diffuseFactor = dot(normal, -dir);
vec4 diffuseColor = vec4(1, 1, 1, 1);
vec4 diffuseColor = vec4(0,0,0,0);
vec4 specColor = vec4(0,0,0,0);
if (diffuseFactor > 0) {
if (diffuseFactor > 0)
{
diffuseColor = vec4(base.color, 1.0) * base.intensity * diffuseFactor;
// specular lighting
vec3 dir_to_eye = normalize(u_eye_pos - worldPos0);
vec3 reflection_dir = normalize(reflect(dir, normal));
float specularFactor = pow(dot(dir_to_eye, reflection_dir), u_spec_power);
if (specularFactor > 0) {
specColor = vec4(base.color, 1.0) * u_spec_intensity * specularFactor;
}
}
return diffuseColor;
return diffuseColor + specColor;
}
vec4 calcDirectionalLight(DirectionalLight dir_light, vec3 normal) {
......
......@@ -6,6 +6,7 @@ layout (location = 2) in vec3 normal;
out vec2 texCoord0;
out vec3 normal0;
out vec3 worldPos0;
uniform mat4 u_transform;
uniform mat4 u_proj_transform;
......@@ -14,4 +15,5 @@ void main () {
gl_Position = u_proj_transform * vec4(position, 1.0);
texCoord0 = texCoord;
normal0 = (u_transform * vec4(normal, 0.0)).xyz;
worldPos0 = (u_transform * vec4(position, 1.0)).xyz;
}
......@@ -16,7 +16,8 @@ BasicShader::BasicShader() :
void BasicShader::updateUniforms(const glm::mat4& world_matrix,
const glm::mat4& proj_matrix,
const Material* material) {
const Material* material,
const glm::vec3&) {
if (material->getTexture()) {
material->bindTexture();
......
......@@ -8,7 +8,8 @@ public:
static BasicShader* getInstance();
virtual void updateUniforms(const glm::mat4& world_matrix,
const glm::mat4& proj_matrix,
const Material* material);
const Material* material,
const glm::vec3&);
private:
BasicShader();
......
......@@ -80,6 +80,10 @@ public:
m_position += displacement;
}
inline glm::vec3 getPos() const {
return m_position;
}
~Camera() {};
private:
......
......@@ -90,7 +90,7 @@ void Game::update() {
m_transform->getRot().y = sinCounter;
//transform->GetRot().z = sinCounter;
m_shader->updateUniforms(m_transform->getModel(), m_transform->getProjectedModel(m_camera), m_material);
m_shader->updateUniforms(m_transform->getModel(), m_transform->getProjectedModel(m_camera), m_material, m_camera->getPos());
}
void Game::loadMesh(const std::string& model_path) {
......
......@@ -2,7 +2,10 @@
Material::Material(Texture* texture, glm::vec3* color) :
m_texture(texture),
m_color(color) {}
m_color(color) {
m_spec_power = 32;
m_spec_intensity = 2;
}
const Texture* Material::getTexture() const {
return m_texture;
......@@ -21,6 +24,22 @@ void Material::setColor(const glm::vec3& color) {
m_color = new glm::vec3(color);
}
const float Material::getSpecIntensity() const {
return m_spec_intensity;
}
void Material::setSpecIntensity(float value) {
m_spec_intensity = value;
}
const float Material::getSpecPower() const {
return m_spec_power;
}
void Material::setSpecPower(float value) {
m_spec_power = value;
}
Material::~Material() {
delete m_texture;
delete m_color;
......
......@@ -11,11 +11,17 @@ public:
void bindTexture(int index = 0) const;
const glm::vec3* getColor() const;
void setColor(const glm::vec3& color);
const float getSpecIntensity() const;
void setSpecIntensity(float value);
const float getSpecPower() const;
void setSpecPower(float value);
virtual ~Material();
private:
Texture* m_texture;
glm::vec3* m_color;
float m_spec_intensity;
float m_spec_power;
};
#endif // MATERIAL_H_
......@@ -11,6 +11,7 @@ Mesh::Mesh(Vertex* vertices, unsigned int numVertices, unsigned int* indices, un
m_drawCount = numVertices; //binding the vertex array object
glGenVertexArrays(1, &m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject);
Mesh::calcNormals(vertices, numVertices, indices, numIndices);
// retrieve the data from the vertices
std::vector<glm::vec3*> posCoords;
......@@ -121,3 +122,23 @@ void Mesh::InitMesh(const IndexedModel& model) {
glBindVertexArray(0);
}
void Mesh::calcNormals(Vertex* vertices, unsigned int vertSize, unsigned int* indices, unsigned int indexSize) {
for(int i = 0; i < indexSize; i += 3) {
int i0 = indices[i];
int i1 = indices[i + 1];
int i2 = indices[i + 2];
glm::vec3 v1 = *vertices[i1].getPos() - *vertices[i0].getPos();
glm::vec3 v2 = *vertices[i2].getPos() - *vertices[i0].getPos();
glm::vec3 normal = glm::normalize(glm::cross(v1, v2));
*vertices[i0].getNormal() = *vertices[i0].getNormal() + normal;
*vertices[i1].getNormal() = *vertices[i1].getNormal() + normal;
*vertices[i2].getNormal() = *vertices[i2].getNormal() + normal;
}
for(int i = 0; i < vertSize; i++)
*vertices[i].getNormal() = glm::normalize(*vertices[i].getNormal());
}
......@@ -38,6 +38,8 @@ private:
};
void InitMesh(const IndexedModel& model);
static void calcNormals(Vertex* vertices, unsigned int num_vertices,
unsigned int* indices, unsigned int num_indices);
GLuint m_vertexArrayObject;
GLuint m_vertexArrayBuffers[NUM_BUFFERS];
......
#include "phong_shader.h"
const std::string PhongShader::PHONG_SHADER_FILE = "../res/phongShader";
glm::vec3 PhongShader::m_ambient_light = glm::vec3(0.1f, 0.1f, 0.1f);
glm::vec3 PhongShader::m_ambient_light = glm::vec3(0.2f, 0.2f, 0.2f);
DirectionalLight PhongShader::m_dir_light = DirectionalLight(
BaseLight(glm::vec3(1,1,1), 0.8f),
glm::vec3(1,1,1));
glm::vec3(1,0,-1));
PhongShader* PhongShader::getInstance() {
static PhongShader instance;
......@@ -21,11 +21,15 @@ PhongShader::PhongShader() :
addUniform("u_dir_light.base.color");
addUniform("u_dir_light.base.intensity");
addUniform("u_dir_light.direction");
addUniform("u_spec_intensity");
addUniform("u_spec_power");
addUniform("u_eye_pos");
}
void PhongShader::updateUniforms(const glm::mat4& world_matrix,
const glm::mat4& proj_matrix,
const Material* material) {
const Material* material,
const glm::vec3& camera_pos) {
if (material->getTexture()) {
material->bindTexture();
......@@ -38,6 +42,9 @@ void PhongShader::updateUniforms(const glm::mat4& world_matrix,
setUniformVec3("u_base_color", *material->getColor());
setUniformVec3("u_ambient_light", PhongShader::m_ambient_light);
setUniformDirectionalLight("u_dir_light", PhongShader::m_dir_light);
setUniformF("u_spec_intensity", material->getSpecIntensity());
setUniformF("u_spec_power", material->getSpecPower());
setUniformVec3("u_eye_pos", camera_pos);
}
void PhongShader::setAmbientLight(const glm::vec3& amb_light) {
......
......@@ -7,7 +7,8 @@
class PhongShader : public Shader {
public:
static PhongShader* getInstance();
virtual void updateUniforms(const glm::mat4&, const glm::mat4&, const Material*);
virtual void updateUniforms(const glm::mat4&, const glm::mat4&,
const Material*, const glm::vec3&);
void setUniformBaseLight(const std::string&, const BaseLight&);
void setUniformDirectionalLight(const std::string&, const DirectionalLight&);
static void setAmbientLight(const glm::vec3&);
......
......@@ -20,7 +20,7 @@ public:
void setUniformF(const std::string& name, float value);
void setUniformVec3(const std::string& name, const glm::vec3& value);
void setUniformMat4(const std::string& name, const glm::mat4& value);
virtual void updateUniforms(const glm::mat4&, const glm::mat4&, const Material*) = 0;
virtual void updateUniforms(const glm::mat4&, const glm::mat4&, const Material*, const glm::vec3&) = 0;
virtual ~Shader();
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment