Added directional light type
This commit is contained in:
parent
5c10fdc1a4
commit
f749b29c31
@ -22,5 +22,4 @@ void MainState::render(nf::Renderer& renderer) {
|
||||
void MainState::onExit() {
|
||||
Log("MainState onExit!");
|
||||
|
||||
|
||||
}
|
@ -13,5 +13,4 @@ public:
|
||||
void onExit() override;
|
||||
private:
|
||||
|
||||
|
||||
};
|
@ -14,17 +14,11 @@ struct Material {
|
||||
};
|
||||
|
||||
struct Light {
|
||||
//Directional = 1, Point = 2, Spotlight = 3
|
||||
//Directional = 1, Point = 2
|
||||
int type;
|
||||
vec3 pos;
|
||||
vec3 direction;
|
||||
vec3 color;
|
||||
|
||||
float falloffConstant;
|
||||
float falloffLinear;
|
||||
float falloffQuad;
|
||||
|
||||
float cutoff;
|
||||
float strength;
|
||||
};
|
||||
|
||||
uniform sampler2D modelTexture;
|
||||
@ -48,18 +42,37 @@ void main() {
|
||||
break;
|
||||
}
|
||||
|
||||
vec3 norm = normalize(normals);
|
||||
vec3 lightDir = normalize(light[i].pos - fragPos);
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = light[i].color * (diff * texColor.rgb);
|
||||
if (light[i].type == 1) {
|
||||
vec3 lightDir = normalize(-light[i].pos);
|
||||
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;
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -5,9 +5,9 @@
|
||||
|
||||
namespace nf {
|
||||
Gamestate::Gamestate(Application* app) :
|
||||
m_camera(app)
|
||||
camera(app)
|
||||
{
|
||||
m_app = app;
|
||||
this->app = app;
|
||||
}
|
||||
|
||||
void Gamestate::onEnter() {
|
||||
@ -19,7 +19,7 @@ namespace nf {
|
||||
}
|
||||
|
||||
Camera* Gamestate::getCamera() {
|
||||
return &m_camera;
|
||||
return &camera;
|
||||
}
|
||||
|
||||
void Gamestate::render(Renderer& renderer) {
|
||||
|
@ -18,7 +18,7 @@ namespace nf {
|
||||
|
||||
void IntroGamestate::update(double deltaTime) {
|
||||
if (m_counter >= 120) {
|
||||
m_app->changeState("Main State");
|
||||
app->changeState("Main State");
|
||||
}
|
||||
m_counter++;
|
||||
}
|
||||
|
@ -67,6 +67,10 @@ namespace nf {
|
||||
return m_position;
|
||||
}
|
||||
|
||||
const Vec3& Camera::getRotation() {
|
||||
return m_front;
|
||||
}
|
||||
|
||||
void Camera::bind(Shader* shader) {
|
||||
glm::mat4 view;
|
||||
|
||||
|
@ -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_position = position;
|
||||
m_color = color;
|
||||
@ -32,37 +32,33 @@ namespace nf {
|
||||
m_color = color;
|
||||
}
|
||||
|
||||
void Light::setStrength(double strength) {
|
||||
m_strength = (float)strength;
|
||||
}
|
||||
|
||||
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 stringLocation = std::to_string(lightNumber);
|
||||
stringPos += stringLocation;
|
||||
stringPos += std::to_string(lightNumber);
|
||||
stringPos += "].";
|
||||
std::string lightPos = stringPos + (std::string)"pos";
|
||||
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(lightColor, color);
|
||||
//switch (m_type) {
|
||||
// case Type::DIRECTIONAL: {
|
||||
// shader->setUniform(lightType.c_str(), 1);
|
||||
// std::string lightConsant = light + (std::string)"falloffConstant";
|
||||
// std::string lightLinear = light + (std::string)"falloffLinear";
|
||||
// std::string lightQuad = light + (std::string)"falloffQuad";
|
||||
// shader->setUniform(lightConsant.c_str(), 1.0f);
|
||||
// shader->setUniform(lightLinear.c_str(), 0.7f);
|
||||
// shader->setUniform(lightQuad.c_str(), 1.8f);
|
||||
// break;
|
||||
// }
|
||||
// case Type::POINT: {
|
||||
// //shader->setUniform(lightType.c_str(), 2);
|
||||
// break;
|
||||
// }
|
||||
// case Type::SPOTLIGHT: {
|
||||
// shader->setUniform(lightType.c_str(), 3);
|
||||
// break;
|
||||
// }
|
||||
//}
|
||||
shader->setUniform(lightStrength, m_strength);
|
||||
std::string lightType = stringPos + (std::string)"type";
|
||||
switch (m_type) {
|
||||
case Type::DIRECTIONAL: {
|
||||
shader->setUniform(lightType, 1);
|
||||
break;
|
||||
}
|
||||
case Type::POINT: {
|
||||
shader->setUniform(lightType, 2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Light::Type Light::getType() {
|
||||
|
@ -25,6 +25,7 @@ namespace nf {
|
||||
void setPosition(double x, double y, double z);
|
||||
void setPosition(const Vec3& position);
|
||||
const Vec3& getPosition();
|
||||
const Vec3& getRotation();
|
||||
|
||||
void bind(Shader* shader);
|
||||
|
||||
|
@ -19,7 +19,7 @@ namespace nf {
|
||||
|
||||
virtual void onExit();
|
||||
protected:
|
||||
Application* m_app;
|
||||
Camera m_camera;
|
||||
Application* app;
|
||||
Camera camera;
|
||||
};
|
||||
}
|
@ -8,15 +8,15 @@ namespace nf {
|
||||
public:
|
||||
enum class Type {
|
||||
DIRECTIONAL,
|
||||
POINT,
|
||||
SPOTLIGHT
|
||||
POINT
|
||||
};
|
||||
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();
|
||||
void setPosition(const Vec3& position);
|
||||
void setColor(const Vec3& color);
|
||||
void setStrength(double strength);
|
||||
|
||||
void bind(Shader* shader, unsigned int lightNumber);
|
||||
|
||||
|
Reference in New Issue
Block a user