Fixed attenuation for lights.
This commit is contained in:
@@ -13,10 +13,12 @@ struct PointLight : Component
|
||||
: Specular(1.0f, 1.0f, 1.0f)
|
||||
, Diffuse(0.4f, 0.4f, 0.4f)
|
||||
, specularExponent(50.0f)
|
||||
, Scale(10.0f)
|
||||
, ConstantAttenuation(1.05f)
|
||||
, LinearAttenuation(0.f)
|
||||
, QuadraticAttenuation(2.55f)
|
||||
{ }
|
||||
|
||||
float constantAttenuation, linearAttenuation, quadraticAttenuation;
|
||||
float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation;
|
||||
Color color;
|
||||
|
||||
glm::vec3 Specular;
|
||||
|
||||
+2
-2
@@ -224,11 +224,11 @@ void GameWorld::Initialize()
|
||||
CommitEntity(car);
|
||||
}
|
||||
*/
|
||||
for(int i = 0; i < 20; i++)
|
||||
for(int i = 0; i < 50; i++)
|
||||
{
|
||||
auto Light = CreateEntity();
|
||||
auto transform = AddComponent<Components::Transform>(Light, "Transform");
|
||||
transform->Position = glm::vec3(i*cos(i), 3, 0 + i*sin(i));
|
||||
transform->Position = glm::vec3(i*cos(i), (float)(0.1*i), 0 + i*sin(i));
|
||||
auto light = AddComponent<Components::PointLight>(Light, "PointLight");
|
||||
//light->Specular = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
auto model = AddComponent<Components::Model>(Light, "Model");
|
||||
|
||||
+21
-9
@@ -132,7 +132,7 @@ void Renderer::LoadContent()
|
||||
m_FinalPassProgram.AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/FinalPass.frag.glsl")));
|
||||
m_FinalPassProgram.Compile();
|
||||
m_FinalPassProgram.Link();
|
||||
Gamma = 1;
|
||||
Gamma = 2.2f;
|
||||
m_ScreenQuad = CreateQuad();
|
||||
|
||||
FrameBufferTextures();
|
||||
@@ -215,10 +215,10 @@ void Renderer::DrawScene()
|
||||
// glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "position"), Lights, Light_position.data());
|
||||
// glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "specular"), Lights, Light_specular.data());
|
||||
// glUniform3fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "diffuse"), Lights, Light_diffuse.data());
|
||||
glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "constantAttenuation"), Lights.size(), Light_constantAttenuation.data());
|
||||
glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "linearAttenuation"), Lights.size(), Light_linearAttenuation.data());
|
||||
glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "quadraticAttenuation"), Lights.size(), Light_quadraticAttenuation.data());
|
||||
glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "spotExponent"), Lights.size(), Light_spotExponent.data());
|
||||
// glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "constantAttenuation"), Lights.size(), Light_constantAttenuation.data());
|
||||
// glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "linearAttenuation"), Lights.size(), Light_linearAttenuation.data());
|
||||
// glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "quadraticAttenuation"), Lights.size(), Light_quadraticAttenuation.data());
|
||||
// glUniform1fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "spotExponent"), Lights.size(), Light_spotExponent.data());
|
||||
if (m_DrawWireframe)
|
||||
{
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
@@ -373,15 +373,19 @@ void Renderer::AddPointLightToDraw(
|
||||
glm::vec3 _specular,
|
||||
glm::vec3 _diffuse,
|
||||
float _specularExponent,
|
||||
float _scale
|
||||
float _ConstantAttenuation,
|
||||
float _LinearAttenuation,
|
||||
float _QuadraticAttenuation
|
||||
)
|
||||
{
|
||||
Light light;
|
||||
light.Position = _position;
|
||||
light.Diffuse = _diffuse;
|
||||
light.Specular = _specular;
|
||||
light.Scale = _scale;
|
||||
light.SpecularExponent = _specularExponent;
|
||||
light.ConstantAttenuation = _ConstantAttenuation;
|
||||
light.LinearAttenuation = _LinearAttenuation;
|
||||
light.QuadraticAttenuation = _QuadraticAttenuation;
|
||||
light.SphereModelMatrix = CreateLightMatrix(light);
|
||||
Lights.push_back(light);
|
||||
}
|
||||
@@ -723,9 +727,12 @@ void Renderer::DrawLightScene()
|
||||
glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ls"), 1, glm::value_ptr(light.Specular));
|
||||
glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ld"), 1, glm::value_ptr(light.Diffuse));
|
||||
glUniform3fv(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "lp"), 1, glm::value_ptr(light.Position));
|
||||
glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LightRadius"), light.Scale/2.f);
|
||||
glUniform3f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "CameraPosition"), m_Camera->Position().x, m_Camera->Position().y, m_Camera->Position().z);
|
||||
glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "specularExponent"), light.SpecularExponent);
|
||||
glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "ConstantAttenuation"), light.ConstantAttenuation);
|
||||
glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "LinearAttenuation"), light.LinearAttenuation);
|
||||
glUniform1f(glGetUniformLocation(m_SecondPassProgram.GetHandle(), "QuadraticAttenuation"), light.QuadraticAttenuation);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, m_sphereModel->Vertices.size());
|
||||
};
|
||||
glEnable (GL_DEPTH_TEST);
|
||||
@@ -740,9 +747,14 @@ void Renderer::SetSphereModel( Model* _model )
|
||||
|
||||
glm::mat4 Renderer::CreateLightMatrix(Light &_light)
|
||||
{
|
||||
float c = _light.ConstantAttenuation;
|
||||
float l = _light.LinearAttenuation;
|
||||
float q = _light.QuadraticAttenuation;
|
||||
float cutOffRadius = abs(sqrt((-4*c*q) + pow(l, 2) + (1024*q) - l) / (2*q));
|
||||
|
||||
glm::mat4 model;
|
||||
model *= glm::translate(_light.Position);
|
||||
model *= glm::scale(glm::vec3(_light.Scale));
|
||||
model *= glm::scale(glm::vec3(cutOffRadius));
|
||||
return model;
|
||||
}
|
||||
|
||||
|
||||
+4
-8
@@ -24,12 +24,6 @@ public:
|
||||
int HEIGHT, WIDTH;
|
||||
|
||||
std::list<std::tuple<Model*, glm::mat4, bool, bool>> ModelsToRender;
|
||||
|
||||
std::vector<float> Light_constantAttenuation;
|
||||
std::vector<float> Light_linearAttenuation;
|
||||
std::vector<float> Light_quadraticAttenuation;
|
||||
std::vector<float> Light_spotExponent;
|
||||
|
||||
std::list<std::tuple<glm::mat4, bool>> AABBsToRender;
|
||||
|
||||
Renderer();
|
||||
@@ -45,7 +39,9 @@ public:
|
||||
glm::vec3 _specular,
|
||||
glm::vec3 _diffuse,
|
||||
float _specularExponent,
|
||||
float _scale
|
||||
float _ConstantAttenuation,
|
||||
float _LinearAttenuation,
|
||||
float _QuadraticAttenuation
|
||||
);
|
||||
void AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding);
|
||||
|
||||
@@ -74,8 +70,8 @@ private:
|
||||
glm::vec3 Specular;
|
||||
glm::vec3 Diffuse;
|
||||
float SpecularExponent;
|
||||
float Scale;
|
||||
glm::mat4 SphereModelMatrix;
|
||||
float ConstantAttenuation, LinearAttenuation, QuadraticAttenuation;
|
||||
};
|
||||
|
||||
float Gamma;
|
||||
|
||||
@@ -16,10 +16,10 @@ out vec4 frag_Normal;
|
||||
void main()
|
||||
{
|
||||
// Diffuse Texture
|
||||
frag_Diffuse = texture2D(DiffuseTexture, Input.TextureCoord);
|
||||
frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord);
|
||||
|
||||
// G-buffer Position
|
||||
frag_Position = vec4(Input.Position.xyz, 0.0);
|
||||
frag_Position = vec4(Input.Position.xyz, 1.0);
|
||||
|
||||
// G-buffer Normal
|
||||
frag_Normal = vec4(Input.Normal, 0.0);
|
||||
|
||||
@@ -12,15 +12,19 @@ uniform vec3 la;
|
||||
uniform vec3 ls;
|
||||
uniform vec3 ld;
|
||||
uniform vec3 lp;
|
||||
uniform float LightRadius;
|
||||
uniform float specularExponent;
|
||||
uniform vec3 CameraPosition;
|
||||
uniform float ConstantAttenuation;
|
||||
uniform float LinearAttenuation;
|
||||
uniform float QuadraticAttenuation;
|
||||
|
||||
const vec3 ks = vec3(1.0, 1.0, 1.0);
|
||||
const vec3 kd = vec3(1.0, 1.0, 1.0);
|
||||
const vec3 ka = vec3(1.0, 1.0, 1.0);
|
||||
const float kshine = 1.0;
|
||||
|
||||
|
||||
|
||||
in VertexData
|
||||
{
|
||||
vec3 Position;
|
||||
@@ -44,12 +48,15 @@ vec4 phong(vec3 position, vec3 normal)
|
||||
vec3 surfaceToViewer = normalize(-position);
|
||||
vec3 halfWay = normalize(surfaceToViewer + directionToLight);
|
||||
float dotSpecular = max(dot(halfWay, normal), 0.0);
|
||||
float specularFactor = pow(dotSpecular, specularExponent * 2);
|
||||
float specularFactor = pow(dotSpecular, specularExponent * 2.0);
|
||||
vec3 Is = ks * ls * specularFactor;
|
||||
|
||||
//Attenuation
|
||||
float dist = distance(lightPos, position);
|
||||
float attenuation = -log(min(1.0, dist / LightRadius));
|
||||
//float attenuation = -log(min(1.0, dist / LightRadius));
|
||||
|
||||
float attenuation = 1.0 / (ConstantAttenuation + (LinearAttenuation * dist) + (QuadraticAttenuation * dist * dist));
|
||||
|
||||
//float attenuation = 1.0 / (1.0 - 0.0001 * pow(dist, 2));
|
||||
|
||||
//float attenuation = clamp(0.0, 1.0, 1.0 / (0.001 + (0.001 * dist) + (0.001 * dist * dist)));
|
||||
@@ -62,9 +69,9 @@ vec4 phong(vec3 position, vec3 normal)
|
||||
//att_s = 1.0 / (1.0 + att_s);
|
||||
//attenuation = attenuation / (1.0 - att_s);
|
||||
|
||||
float radius = 5.0;
|
||||
float alpha = dist / radius;
|
||||
float dampingFactor = 1.0 - pow(alpha, 3);
|
||||
//float radius = 5.0;
|
||||
//float alpha = dist / radius;
|
||||
//float dampingFactor = 1.0 - pow(alpha, 3);
|
||||
|
||||
|
||||
return vec4((Id + Is) * attenuation, 1.0);
|
||||
|
||||
@@ -15,5 +15,5 @@ void main()
|
||||
{
|
||||
gl_Position = MVP * vec4(Position, 1.0);
|
||||
Output.Position = Position;
|
||||
Output.TextureCoord = (vec2(Position) + 1) / 2;
|
||||
Output.TextureCoord = (vec2(Position) + 1.0) / 2.0;
|
||||
}
|
||||
@@ -39,7 +39,9 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa
|
||||
pointLightComponent->Specular,
|
||||
pointLightComponent->Diffuse,
|
||||
pointLightComponent->specularExponent,
|
||||
pointLightComponent->Scale
|
||||
pointLightComponent->ConstantAttenuation,
|
||||
pointLightComponent->LinearAttenuation,
|
||||
pointLightComponent->QuadraticAttenuation
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user