Added directional light type

This commit is contained in:
Grayson Riffe (Desktop) 2021-09-04 20:59:27 -05:00
parent 5c10fdc1a4
commit f749b29c31
10 changed files with 66 additions and 54 deletions

View File

@ -22,5 +22,4 @@ void MainState::render(nf::Renderer& renderer) {
void MainState::onExit() { void MainState::onExit() {
Log("MainState onExit!"); Log("MainState onExit!");
} }

View File

@ -13,5 +13,4 @@ public:
void onExit() override; void onExit() override;
private: private:
}; };

View File

@ -14,17 +14,11 @@ struct Material {
}; };
struct Light { struct Light {
//Directional = 1, Point = 2, Spotlight = 3 //Directional = 1, Point = 2
int type; int type;
vec3 pos; vec3 pos;
vec3 direction;
vec3 color; vec3 color;
float strength;
float falloffConstant;
float falloffLinear;
float falloffQuad;
float cutoff;
}; };
uniform sampler2D modelTexture; uniform sampler2D modelTexture;
@ -48,18 +42,37 @@ void main() {
break; break;
} }
if (light[i].type == 1) {
vec3 lightDir = normalize(-light[i].pos);
vec3 norm = normalize(normals); vec3 norm = normalize(normals);
vec3 lightDir = normalize(light[i].pos - fragPos);
float diff = max(dot(norm, lightDir), 0.0); float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light[i].color * (diff * texColor.rgb); vec3 diffuse = light[i].color * (diff * texColor.rgb) * light[i].strength;
vec3 viewDir = normalize(camera.pos - fragPos); vec3 viewDir = normalize(camera.pos - fragPos);
vec3 reflectDir = reflect(-lightDir, norm); vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess * 32.0f); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess * 32.0f);
vec3 specular = light[i].color * spec; vec3 specular = light[i].color * spec * light[i].strength;
color += (ambient + diffuse + specular); color += (ambient + diffuse + specular);
continue;
} }
if (light[i].type == 2) {
vec3 lightDir = normalize(light[i].pos - fragPos);
vec3 norm = normalize(normals);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light[i].color * (diff * texColor.rgb) * light[i].strength;
vec3 viewDir = normalize(camera.pos - fragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess * 32.0f);
vec3 specular = light[i].color * spec * (light[i].strength) / 8.0f;
float length = length(light[i].pos - fragPos);
float att = clamp(10.0 / length, 0.0, 1.0) * light[i].strength;
color += ((ambient + diffuse + specular) * att);
continue;
}
}
outColor = vec4(color, texColor.a); outColor = vec4(color, texColor.a);
} }

View File

@ -5,9 +5,9 @@
namespace nf { namespace nf {
Gamestate::Gamestate(Application* app) : Gamestate::Gamestate(Application* app) :
m_camera(app) camera(app)
{ {
m_app = app; this->app = app;
} }
void Gamestate::onEnter() { void Gamestate::onEnter() {
@ -19,7 +19,7 @@ namespace nf {
} }
Camera* Gamestate::getCamera() { Camera* Gamestate::getCamera() {
return &m_camera; return &camera;
} }
void Gamestate::render(Renderer& renderer) { void Gamestate::render(Renderer& renderer) {

View File

@ -18,7 +18,7 @@ namespace nf {
void IntroGamestate::update(double deltaTime) { void IntroGamestate::update(double deltaTime) {
if (m_counter >= 120) { if (m_counter >= 120) {
m_app->changeState("Main State"); app->changeState("Main State");
} }
m_counter++; m_counter++;
} }

View File

@ -67,6 +67,10 @@ namespace nf {
return m_position; return m_position;
} }
const Vec3& Camera::getRotation() {
return m_front;
}
void Camera::bind(Shader* shader) { void Camera::bind(Shader* shader) {
glm::mat4 view; glm::mat4 view;

View File

@ -11,7 +11,7 @@ namespace nf {
} }
void Light::create(const Vec3& position, const Vec3& color, Type type, float strength) { void Light::create(const Vec3& position, const Vec3& color, float strength, Type type) {
m_constructed = true; m_constructed = true;
m_position = position; m_position = position;
m_color = color; m_color = color;
@ -32,37 +32,33 @@ namespace nf {
m_color = color; m_color = color;
} }
void Light::setStrength(double strength) {
m_strength = (float)strength;
}
void Light::bind(Shader* shader, unsigned int lightNumber) { void Light::bind(Shader* shader, unsigned int lightNumber) {
glm::vec3 pos(m_position.x, m_position.y, m_position.z);
glm::vec3 color(m_color.x, m_color.y, m_color.z);
std::string stringPos = "light["; std::string stringPos = "light[";
std::string stringLocation = std::to_string(lightNumber); stringPos += std::to_string(lightNumber);
stringPos += stringLocation;
stringPos += "]."; stringPos += "].";
std::string lightPos = stringPos + (std::string)"pos"; std::string lightPos = stringPos + (std::string)"pos";
std::string lightColor = stringPos + (std::string)"color"; std::string lightColor = stringPos + (std::string)"color";
std::string lightStrength = stringPos + (std::string)"strength";
glm::vec3 pos(m_position.x, m_position.y, m_position.z);
glm::vec3 color(m_color.x, m_color.y, m_color.z);
shader->setUniform(lightPos, pos); shader->setUniform(lightPos, pos);
shader->setUniform(lightColor, color); shader->setUniform(lightColor, color);
//switch (m_type) { shader->setUniform(lightStrength, m_strength);
// case Type::DIRECTIONAL: { std::string lightType = stringPos + (std::string)"type";
// shader->setUniform(lightType.c_str(), 1); switch (m_type) {
// std::string lightConsant = light + (std::string)"falloffConstant"; case Type::DIRECTIONAL: {
// std::string lightLinear = light + (std::string)"falloffLinear"; shader->setUniform(lightType, 1);
// std::string lightQuad = light + (std::string)"falloffQuad"; break;
// shader->setUniform(lightConsant.c_str(), 1.0f); }
// shader->setUniform(lightLinear.c_str(), 0.7f); case Type::POINT: {
// shader->setUniform(lightQuad.c_str(), 1.8f); shader->setUniform(lightType, 2);
// break; break;
// } }
// case Type::POINT: { }
// //shader->setUniform(lightType.c_str(), 2);
// break;
// }
// case Type::SPOTLIGHT: {
// shader->setUniform(lightType.c_str(), 3);
// break;
// }
//}
} }
Light::Type Light::getType() { Light::Type Light::getType() {

View File

@ -25,6 +25,7 @@ namespace nf {
void setPosition(double x, double y, double z); void setPosition(double x, double y, double z);
void setPosition(const Vec3& position); void setPosition(const Vec3& position);
const Vec3& getPosition(); const Vec3& getPosition();
const Vec3& getRotation();
void bind(Shader* shader); void bind(Shader* shader);

View File

@ -19,7 +19,7 @@ namespace nf {
virtual void onExit(); virtual void onExit();
protected: protected:
Application* m_app; Application* app;
Camera m_camera; Camera camera;
}; };
} }

View File

@ -8,15 +8,15 @@ namespace nf {
public: public:
enum class Type { enum class Type {
DIRECTIONAL, DIRECTIONAL,
POINT, POINT
SPOTLIGHT
}; };
Light(); Light();
void create(const Vec3& position, const Vec3& color, Type type = Type::POINT, float strength = 1.0f); void create(const Vec3& position, const Vec3& color, float strength = 1.0f, Type type = Type::POINT);
bool isConstructed(); bool isConstructed();
void setPosition(const Vec3& position); void setPosition(const Vec3& position);
void setColor(const Vec3& color); void setColor(const Vec3& color);
void setStrength(double strength);
void bind(Shader* shader, unsigned int lightNumber); void bind(Shader* shader, unsigned int lightNumber);