mirror of
https://github.com/Linloir/SceneEditor.git
synced 2025-12-17 07:28:12 +08:00
[CORE][ADD] Illuminer Implementation
This commit is contained in:
parent
2ecf3bd14c
commit
9604e68aa4
@ -108,6 +108,7 @@
|
|||||||
<ClCompile Include="ebo.cpp" />
|
<ClCompile Include="ebo.cpp" />
|
||||||
<ClCompile Include="editorpage.cpp" />
|
<ClCompile Include="editorpage.cpp" />
|
||||||
<ClCompile Include="framelesswindow.cpp" />
|
<ClCompile Include="framelesswindow.cpp" />
|
||||||
|
<ClCompile Include="illuminer.cpp" />
|
||||||
<ClCompile Include="lightCaster.cpp" />
|
<ClCompile Include="lightCaster.cpp" />
|
||||||
<ClCompile Include="lineeditwidget.cpp" />
|
<ClCompile Include="lineeditwidget.cpp" />
|
||||||
<ClCompile Include="logger.cpp" />
|
<ClCompile Include="logger.cpp" />
|
||||||
|
|||||||
@ -168,6 +168,9 @@
|
|||||||
<ClCompile Include="lightCaster.cpp">
|
<ClCompile Include="lightCaster.cpp">
|
||||||
<Filter>Source Files\OpenGL Abstractions</Filter>
|
<Filter>Source Files\OpenGL Abstractions</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="illuminer.cpp">
|
||||||
|
<Filter>Source Files\OpenGL Abstractions</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="camera.h">
|
<ClInclude Include="camera.h">
|
||||||
|
|||||||
136
FinalProject/illuminer.cpp
Normal file
136
FinalProject/illuminer.cpp
Normal file
@ -0,0 +1,136 @@
|
|||||||
|
#include "illuminer.h"
|
||||||
|
#include "logger.h"
|
||||||
|
|
||||||
|
Illuminer::Illuminer(glm::vec3 color) :
|
||||||
|
_lightColor(color)
|
||||||
|
{}
|
||||||
|
|
||||||
|
Illuminer::~Illuminer() {}
|
||||||
|
|
||||||
|
DirLight::DirLight(glm::vec3 direction, glm::vec3 color) :
|
||||||
|
Illuminer(color), _direction(direction)
|
||||||
|
{}
|
||||||
|
|
||||||
|
DirLight::~DirLight() {}
|
||||||
|
|
||||||
|
void DirLight::updateShader(ShaderProgram shader) const {
|
||||||
|
// Recall DirLight structure in fragment shader
|
||||||
|
// -------------
|
||||||
|
// struct DirLight {
|
||||||
|
// vec3 direction;
|
||||||
|
// vec3 ambient;
|
||||||
|
// vec3 diffuse;
|
||||||
|
// vec3 specular;
|
||||||
|
// };
|
||||||
|
|
||||||
|
shader.setUniform("DirLight.direction", -_direction);
|
||||||
|
shader.setUniform("DirLight.ambient", ambientLightColor());
|
||||||
|
shader.setUniform("DirLight.diffuse", diffuseLightColor());
|
||||||
|
shader.setUniform("DirLight.specular", specularLightColor());
|
||||||
|
}
|
||||||
|
|
||||||
|
ScopedLight::ScopedLight(glm::vec3 position, glm::vec3 direction, glm::vec3 color) :
|
||||||
|
Illuminer(color), _position(position), _direction(direction)
|
||||||
|
{
|
||||||
|
updateLinear();
|
||||||
|
updateQuadratic();
|
||||||
|
}
|
||||||
|
|
||||||
|
ScopedLight::ScopedLight(int distance, glm::vec3 position, glm::vec3 direction, glm::vec3 color) :
|
||||||
|
Illuminer(color), _position(position), _direction(direction), _idealDistance(distance)
|
||||||
|
{
|
||||||
|
updateLinear();
|
||||||
|
updateQuadratic();
|
||||||
|
}
|
||||||
|
|
||||||
|
ScopedLight::~ScopedLight() {}
|
||||||
|
|
||||||
|
inline void ScopedLight::updateLinear() {
|
||||||
|
// Double is used here to prevent precision loss
|
||||||
|
double linear = -0.0001 + 4.7688 / (double)_idealDistance;
|
||||||
|
_attLinear = (float)linear;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void ScopedLight::updateQuadratic() {
|
||||||
|
// Double is used here to prevent precision loss
|
||||||
|
double quadratic = 82.4448 * glm::pow((double)_idealDistance, -2.0192);
|
||||||
|
_attQuadratic = (float)quadratic;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScopedLight::setIdealDistance(int distance) {
|
||||||
|
if (distance < 10) {
|
||||||
|
distance = 10;
|
||||||
|
}
|
||||||
|
if (distance > 3500) {
|
||||||
|
distance = 3500;
|
||||||
|
}
|
||||||
|
_idealDistance = distance;
|
||||||
|
updateLinear();
|
||||||
|
updateQuadratic();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScopedLight::setCutOffAngle(float angle) {
|
||||||
|
if (angle < 0.0f) {
|
||||||
|
angle = 0.0f;
|
||||||
|
}
|
||||||
|
if (angle > 180.0f) {
|
||||||
|
angle = 180.0f;
|
||||||
|
}
|
||||||
|
_cutOffAngle = angle;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline float ScopedLight::innerCutOffAngle() const {
|
||||||
|
return 0.0011 * glm::pow(_cutOffAngle, 2) + 0.6440 * _cutOffAngle;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScopedLight::updateShader(ShaderProgram shader) const {
|
||||||
|
// Recall PointLight and SpotLight structure in fragment shader
|
||||||
|
// -------------
|
||||||
|
// struct PointLight {
|
||||||
|
// vec3 position;
|
||||||
|
// vec3 ambient;
|
||||||
|
// vec3 diffuse;
|
||||||
|
// vec3 specular;
|
||||||
|
// float constant;
|
||||||
|
// float linear;
|
||||||
|
// float quadratic;
|
||||||
|
// };
|
||||||
|
// ------------
|
||||||
|
// struct SpotLight {
|
||||||
|
// vec3 position;
|
||||||
|
// vec3 direction;
|
||||||
|
// vec3 ambient;
|
||||||
|
// vec3 diffuse;
|
||||||
|
// vec3 specular;
|
||||||
|
// float constant;
|
||||||
|
// float linear;
|
||||||
|
// float quadratic;
|
||||||
|
// float cutOff;
|
||||||
|
// float outerCutOff;
|
||||||
|
// };
|
||||||
|
|
||||||
|
// Check the cutoff angle to determine the type of light
|
||||||
|
if (abs(_cutOffAngle - 180.0f) < 1e-6) {
|
||||||
|
// Point light
|
||||||
|
shader.setUniform("PointLight.position", _position);
|
||||||
|
shader.setUniform("PointLight.ambient", ambientLightColor());
|
||||||
|
shader.setUniform("PointLight.diffuse", diffuseLightColor());
|
||||||
|
shader.setUniform("PointLight.specular", specularLightColor());
|
||||||
|
shader.setUniform("PointLight.constant", _attConstant);
|
||||||
|
shader.setUniform("PointLight.linear", _attLinear);
|
||||||
|
shader.setUniform("PointLight.quadratic", _attQuadratic);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Spot light
|
||||||
|
shader.setUniform("SpotLight.position", _position);
|
||||||
|
shader.setUniform("SpotLight.direction", -_direction);
|
||||||
|
shader.setUniform("SpotLight.ambient", ambientLightColor());
|
||||||
|
shader.setUniform("SpotLight.diffuse", diffuseLightColor());
|
||||||
|
shader.setUniform("SpotLight.specular", specularLightColor());
|
||||||
|
shader.setUniform("SpotLight.constant", _attConstant);
|
||||||
|
shader.setUniform("SpotLight.linear", _attLinear);
|
||||||
|
shader.setUniform("SpotLight.quadratic", _attQuadratic);
|
||||||
|
shader.setUniform("SpotLight.innercutoff", glm::cos(glm::radians(innerCutOffAngle())));
|
||||||
|
shader.setUniform("SpotLight.outercutoff", glm::cos(glm::radians(_cutOffAngle)));
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,7 +5,6 @@
|
|||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
#include "logger.h"
|
|
||||||
|
|
||||||
class Illuminer {
|
class Illuminer {
|
||||||
protected:
|
protected:
|
||||||
@ -15,7 +14,7 @@ public:
|
|||||||
Illuminer(glm::vec3 color);
|
Illuminer(glm::vec3 color);
|
||||||
~Illuminer();
|
~Illuminer();
|
||||||
|
|
||||||
public:
|
protected:
|
||||||
virtual glm::vec3 ambientLightColor() const = 0;
|
virtual glm::vec3 ambientLightColor() const = 0;
|
||||||
virtual glm::vec3 diffuseLightColor() const = 0;
|
virtual glm::vec3 diffuseLightColor() const = 0;
|
||||||
virtual glm::vec3 specularLightColor() const = 0;
|
virtual glm::vec3 specularLightColor() const = 0;
|
||||||
@ -36,11 +35,13 @@ public:
|
|||||||
DirLight(glm::vec3 direction = glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3 color = glm::vec3(1.0f));
|
DirLight(glm::vec3 direction = glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3 color = glm::vec3(1.0f));
|
||||||
~DirLight();
|
~DirLight();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual glm::vec3 ambientLightColor() const override { return _lightColor * 0.2f; }
|
||||||
|
virtual glm::vec3 diffuseLightColor() const override { return _lightColor * 0.5f; }
|
||||||
|
virtual glm::vec3 specularLightColor() const override { return _lightColor * 0.9f; }
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Getter APIs
|
|
||||||
glm::vec3 lightDirection() const { return _direction; } // The same direction as the outgoing direction
|
glm::vec3 lightDirection() const { return _direction; } // The same direction as the outgoing direction
|
||||||
|
|
||||||
// Setter
|
|
||||||
void setLightDirection(glm::vec3 direction) { _direction = direction; }
|
void setLightDirection(glm::vec3 direction) { _direction = direction; }
|
||||||
|
|
||||||
// Render util function
|
// Render util function
|
||||||
@ -56,8 +57,7 @@ protected:
|
|||||||
// Light source status
|
// Light source status
|
||||||
glm::vec3 _position;
|
glm::vec3 _position;
|
||||||
glm::vec3 _direction;
|
glm::vec3 _direction;
|
||||||
float _innerCutOffAngle = 162.5f;
|
float _cutOffAngle = 180.0f;
|
||||||
float _outerCutOffAngle = 180.0f;
|
|
||||||
|
|
||||||
// Light property
|
// Light property
|
||||||
int _idealDistance = 32; // ideally calculated distance
|
int _idealDistance = 32; // ideally calculated distance
|
||||||
@ -67,16 +67,27 @@ protected:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
ScopedLight(glm::vec3 position, glm::vec3 direction = glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3 color = glm::vec3(1.0f));
|
ScopedLight(glm::vec3 position, glm::vec3 direction = glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3 color = glm::vec3(1.0f));
|
||||||
ScopedLight(glm::vec3 distance, glm::vec3 position, glm::vec3 direction = glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3 color = glm::vec3(1.0f));
|
ScopedLight(int distance, glm::vec3 position, glm::vec3 direction = glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3 color = glm::vec3(1.0f));
|
||||||
~ScopedLight();
|
~ScopedLight();
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Util function
|
||||||
|
inline void updateLinear();
|
||||||
|
inline void updateQuadratic();
|
||||||
|
inline float innerCutOffAngle() const;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual glm::vec3 ambientLightColor() const override { return _lightColor * 0.2f; }
|
||||||
|
virtual glm::vec3 diffuseLightColor() const override { return _lightColor * 0.5f; }
|
||||||
|
virtual glm::vec3 specularLightColor() const override { return _lightColor * 0.9f; }
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Property setters and getters
|
// Property setters and getters
|
||||||
int idealDistance() const { return _idealDistance; }
|
int idealDistance() const { return _idealDistance; }
|
||||||
void setIdealDistance(int distance);
|
void setIdealDistance(int distance);
|
||||||
glm::vec3 lightDirection() const { return _direction; }
|
glm::vec3 lightDirection() const { return _direction; }
|
||||||
void setLightDirection(glm::vec3 direction) { _direction = direction; }
|
void setLightDirection(glm::vec3 direction) { _direction = direction; }
|
||||||
float cutOffAngle() const { return _outerCutOffAngle; }
|
float cutOffAngle() const { return _cutOffAngle; }
|
||||||
void setCutOffAngle(float angle);
|
void setCutOffAngle(float angle);
|
||||||
|
|
||||||
// Render util function
|
// Render util function
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user