add: added lighting classes and a shader to use multiple lights
This commit is contained in:
parent
ee9e540b5b
commit
2610da8f8c
4 changed files with 359 additions and 64 deletions
143
include/myEngine/lighting.hpp
Normal file
143
include/myEngine/lighting.hpp
Normal file
|
@ -0,0 +1,143 @@
|
|||
#ifndef LIGHTING_HPP
|
||||
#define LIGHTING_HPP
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include <myEngine/shader.hpp>
|
||||
|
||||
class Light {
|
||||
private:
|
||||
glm::vec3 color;
|
||||
glm::vec3 ambient;
|
||||
glm::vec3 diffuse;
|
||||
glm::vec3 specular;
|
||||
public:
|
||||
Light(const glm::vec3& color, const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular)
|
||||
: color(color), ambient(ambient), diffuse(diffuse), specular(specular) {}
|
||||
|
||||
glm::vec3 getColor() const { return color; }
|
||||
glm::vec3 getAmbient() const { return ambient; }
|
||||
glm::vec3 getDiffuse() const { return diffuse; }
|
||||
glm::vec3 getSpecular() const { return specular; }
|
||||
|
||||
void setColor(const glm::vec3& newColor) { color = newColor; }
|
||||
void setAmbient(const glm::vec3& newAmbient) { ambient = newAmbient; }
|
||||
void setDiffuse(const glm::vec3& newDiffuse) { diffuse = newDiffuse; }
|
||||
void setSpecular(const glm::vec3& newSpecular) { specular = newSpecular; }
|
||||
virtual void use(Shader& shader) {}
|
||||
};
|
||||
|
||||
class DirectLight : public Light {
|
||||
private:
|
||||
glm::vec3 direction;
|
||||
public:
|
||||
DirectLight(const glm::vec3& direction, const glm::vec3& color, const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular)
|
||||
: direction(direction), Light(color, ambient, diffuse, specular) {}
|
||||
|
||||
glm::vec3 getDirection() const
|
||||
{
|
||||
return direction;
|
||||
}
|
||||
|
||||
void setDirection(const glm::vec3& newDirection)
|
||||
{
|
||||
direction = newDirection;
|
||||
}
|
||||
|
||||
void use(Shader& shader) override
|
||||
{
|
||||
shader.setVec3("dirLight.direction", direction);
|
||||
shader.setVec3("dirLight.color", getColor());
|
||||
shader.setVec3("dirLight.ambient", getAmbient());
|
||||
shader.setVec3("dirLight.diffuse", getDiffuse());
|
||||
shader.setVec3("dirLight.specular", getSpecular());
|
||||
}
|
||||
};
|
||||
|
||||
class SpotLight : public Light {
|
||||
private:
|
||||
glm::vec3 position;
|
||||
glm::vec3 direction;
|
||||
float cutOff;
|
||||
float outerCutOff;
|
||||
float constant;
|
||||
float linear;
|
||||
float quadratic;
|
||||
public:
|
||||
SpotLight(const glm::vec3& position, const glm::vec3& direction,
|
||||
float cutOff, float outerCutOff,
|
||||
const glm::vec3& color, const glm::vec3& ambient,
|
||||
const glm::vec3& diffuse, const glm::vec3& specular,
|
||||
float constant, float linear, float quadratic)
|
||||
: position(position), direction(direction), cutOff(cutOff), outerCutOff(outerCutOff),
|
||||
Light(color, ambient, diffuse, specular), constant(constant), linear(linear), quadratic(quadratic) {}
|
||||
|
||||
glm::vec3 getPosition() const { return position; }
|
||||
glm::vec3 getDirection() const { return direction; }
|
||||
float getCutOff() const { return cutOff; }
|
||||
float getOuterCutOff() const { return outerCutOff; }
|
||||
float getConstant() const { return constant; }
|
||||
float getLinear() const { return linear; }
|
||||
float getQuadratic() const { return quadratic; }
|
||||
|
||||
void setPosition(const glm::vec3& newPosition) { position = newPosition; }
|
||||
void setDirection(const glm::vec3& newDirection) { direction = newDirection; }
|
||||
void setCutOff(float newCutOff) { cutOff = newCutOff; }
|
||||
void setOuterCutOff(float newOuterCutOff) { outerCutOff = newOuterCutOff; }
|
||||
void setConstant(float newConstant) { constant = newConstant; }
|
||||
void setLinear(float newLinear) { linear = newLinear; }
|
||||
void setQuadratic(float newQuadratic) { quadratic = newQuadratic; }
|
||||
|
||||
void use(Shader& shader) override
|
||||
{
|
||||
shader.setVec3("spotLight.position", position);
|
||||
shader.setVec3("spotLight.direction", direction);
|
||||
shader.setFloat("spotLight.cutOff", glm::cos(glm::radians(cutOff)));
|
||||
shader.setFloat("spotLight.outerCutOff", glm::cos(glm::radians(outerCutOff)));
|
||||
shader.setVec3("spotLight.color", getColor());
|
||||
shader.setVec3("spotLight.ambient", getAmbient());
|
||||
shader.setVec3("spotLight.diffuse", getDiffuse());
|
||||
shader.setVec3("spotLight.specular", getSpecular());
|
||||
shader.setFloat("spotLight.constant", constant);
|
||||
shader.setFloat("spotLight.linear", linear);
|
||||
shader.setFloat("spotLight.quadratic", quadratic);
|
||||
}
|
||||
};
|
||||
|
||||
class PointLight : public Light {
|
||||
private:
|
||||
glm::vec3 position;
|
||||
float constant;
|
||||
float linear;
|
||||
float quadratic;
|
||||
public:
|
||||
PointLight(const glm::vec3& position, const glm::vec3& color,
|
||||
const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular,
|
||||
float constant, float linear, float quadratic)
|
||||
: position(position), Light(color, ambient, diffuse, specular),
|
||||
constant(constant), linear(linear), quadratic(quadratic) {}
|
||||
|
||||
glm::vec3 getPosition() const { return position; }
|
||||
float getConstant() const { return constant; }
|
||||
float getLinear() const { return linear; }
|
||||
float getQuadratic() const { return quadratic; }
|
||||
|
||||
void setPosition(const glm::vec3& newPosition) { position = newPosition; }
|
||||
void setConstant(float newConstant) { constant = newConstant; }
|
||||
void setLinear(float newLinear) { linear = newLinear; }
|
||||
void setQuadratic(float newQuadratic) { quadratic = newQuadratic; }
|
||||
|
||||
void use(Shader& shader, int index) const
|
||||
{
|
||||
shader.setVec3("pointLights[" + std::to_string(index) + "].position", position);
|
||||
shader.setVec3("pointLights[" + std::to_string(index) + "].color", getColor());
|
||||
shader.setVec3("pointLights[" + std::to_string(index) + "].ambient", getAmbient());
|
||||
shader.setVec3("pointLights[" + std::to_string(index) + "].diffuse", getDiffuse());
|
||||
shader.setVec3("pointLights[" + std::to_string(index) + "].specular", getSpecular());
|
||||
shader.setFloat("pointLights[" + std::to_string(index) + "].constant", constant);
|
||||
shader.setFloat("pointLights[" + std::to_string(index) + "].linear", linear);
|
||||
shader.setFloat("pointLights[" + std::to_string(index) + "].quadratic", quadratic);
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,3 +1,6 @@
|
|||
#ifndef OBJECT_HPP
|
||||
#define OBJECT_HPP
|
||||
|
||||
#include <myEngine/model.hpp>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
@ -47,4 +50,6 @@ public:
|
|||
shader.setMat4("model", modelMatrix);
|
||||
model.Draw(shader);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue