Pushing what I have.
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 5m57s

This commit is contained in:
2024-12-16 16:33:16 -05:00
parent 401e2c0883
commit 7f5ee5cf0c
6 changed files with 71 additions and 20 deletions

View File

@@ -77,7 +77,7 @@ namespace JGL::J2D {
/// Provide a list of lights to be used in 2D space. Typically directly after J2D::Begin();
/// 8 lights maximum for now. Some kind of light sorting will eventually be needed per j2d element.
void LightArray(const LightBase** lights, const size_t& light_count);
void OptionalLights(const LightBase** lights, const size_t& light_count);
/// Specifies a light which is required for every object in the scene.
void RequiredLight(const LightBase* light);
@@ -381,7 +381,7 @@ namespace JGL::J3D {
void RequiredLight(const LightBase* light);
/// When each 3D object is drawn, We'll do our best to determine which lights would effect it the most and use those ones.
void LightArray(const LightBase** lights, const size_t& light_count);
void OptionalLights(const LightBase** lights, const size_t& light_count);
/// Helper function to conveniently change the Field-Of-View.
void ChangeFOV(float fov);

View File

@@ -22,7 +22,7 @@ protected:
float linear_attenuation;
float quadratic_attenuation;
public:
[[nodiscard]] Vector3 GetPosition() const;
[[nodiscard]] Vector4 GetPosition() const;
[[nodiscard]] Color4 GetAmbient() const;
[[nodiscard]] Color4 GetDiffuse() const;
[[nodiscard]] Color4 GetSpecular() const;

View File

@@ -2,10 +2,11 @@
#pragma once
#include <J3ML/LinearAlgebra/Vector2.hpp>
#include <JGL/types/RenderTarget.h>
#include <JGL/logger/logger.h>
#include <glad/glad.h>
namespace JGL {
inline std::array<const LightBase*, 8> empty_light_array = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
inline constexpr std::array<const LightBase*, 8> empty_light_array = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
inline bool inJ2D = false;
inline bool inJ3D = false;
}
@@ -41,9 +42,67 @@ namespace JGL::J3D {
// List of lights required for each objects in the scene. up-to 8. For example, the sun. Or a flash-light.
inline std::array<const LightBase*, 8> required_lights;
// List of all lights in the scene.
inline std::vector<const LightBase*> light_array;
inline std::vector<const LightBase*> optional_lights;
inline float far_plane = 0;
inline float fov = 0;
// TODO handle the case that a required light is in the list of optional lights.
inline void SelectLights(const Vector3& position) {
std::array<const LightBase*, 8> result = {};
unsigned int required_light_count = 0;
for (const auto* light : required_lights)
if (light)
result[required_light_count++] = light;
else
break;
// If there is optional lights.
if (!optional_lights.empty()) {
// The number of lights we need to solve for
unsigned int remaining_lights = 8 - required_light_count;
std::vector<std::pair<float, const LightBase*>> light_influence;
for (const auto* light: optional_lights) {
float influence = 0.0f;
if (auto *point_light = dynamic_cast<const PointLight*>(light))
influence = point_light->GetAttenuationAtPosition(position);
else if (auto *spot_light = dynamic_cast<const SpotLight*>(light))
influence = spot_light->GetAttenuationAtPosition(position);
else
Logger::Warning("Couldn't determine the light type.");
light_influence.emplace_back(influence, light);
}
// Sort by biggest influence.
std::sort(light_influence.begin(), light_influence.end(),
[](const auto &a, const auto &b) { return a.first > b.first; });
// Add in optional lights.
for (unsigned int i = 0; i < remaining_lights && i < light_influence.size(); i++)
result[required_light_count++] = light_influence[i].second;
}
for (unsigned int i = 0; i < result.size(); i++) {
// If we've reached the end of an array with less than 8 lights.
if (i > required_light_count)
break;
Vector4 ambient = { result[i]->GetAmbient().RN(), result[i]->GetAmbient().BN(), result[i]->GetAmbient().GN(), result[i]->GetAmbient().AN() };
Vector4 diffuse = { result[i]->GetDiffuse().RN(), result[i]->GetDiffuse().BN(), result[i]->GetDiffuse().GN(), result[i]->GetDiffuse().AN() };
Vector4 specular = { result[i]->GetSpecular().RN(), result[i]->GetSpecular().BN(), result[i]->GetSpecular().GN(), result[i]->GetSpecular().AN() };
GLenum current_light = GL_LIGHT0 + i;
glEnable(GL_LIGHT0 + i);
glLightfv(current_light, GL_POSITION, result[i]->GetPosition().ptr());
glLightfv(current_light, GL_AMBIENT, ambient.ptr());
glLightfv(current_light, GL_DIFFUSE, diffuse.ptr());
glLightfv(current_light, GL_SPECULAR, specular.ptr());
}
}
}

View File

@@ -157,7 +157,7 @@ void JGL::J2D::RequiredLight(const JGL::LightBase* light) {
Logger::Error("You cannot specify more than 8 required lights.");
}
void JGL::J2D::LightArray(const LightBase** lights, const size_t& light_count) {
void JGL::J2D::OptionalLights(const LightBase** lights, const size_t& light_count) {
for (size_t i = 0; i < light_count; i++)
light_array.push_back(lights[i]);
}

View File

@@ -18,14 +18,6 @@ std::array<GLfloat, 16> JGL::OpenGLPerspectiveProjectionRH(float fovY, float asp
return result;
}
float EffectOfLightOnPointIn3DSpace(const JGL::PointLight* light, const Vector3& position) {
Vector3 light_pos = light->GetPosition();
Vector3 vector_to_position = position - light_pos;
float distance = vector_to_position.Length();
return 1.0f / (light->GetConstantAttenuation() + light->GetLinearAttenuation() * distance + light->GetQuadraticAttenuation() * distance * distance);
}
void JGL::J3D::ChangeFOV(float fov) {
JGL::J3D::fov = fov;
}
@@ -47,9 +39,9 @@ void JGL::J3D::RequiredLight(const JGL::LightBase* light) {
Logger::Error("You cannot specify more than 8 required lights.");
}
void JGL::J3D::LightArray(const JGL::LightBase** lights, const size_t& light_count) {
void JGL::J3D::OptionalLights(const JGL::LightBase** lights, const size_t& light_count) {
for (size_t i = 0; i < light_count; i++)
light_array.push_back(lights[i]);
optional_lights.push_back(lights[i]);
}
void JGL::J3D::Begin(bool two_pass) {
@@ -96,7 +88,7 @@ void JGL::J3D::Begin(bool two_pass) {
// Reset the lights.
required_lights = empty_light_array;
light_array = {};
optional_lights = {};
glDisable(GL_LIGHTING);
if (!inJ2D)
@@ -137,7 +129,7 @@ void JGL::J3D::End() {
}
required_lights = empty_light_array;
light_array = {};
optional_lights = {};
//Put the draw color back how it was before.
glColor4fv(OpenGLState::oldColor);

View File

@@ -33,8 +33,8 @@ JGL::SpotLight::SpotLight(const Vector3& position, const Matrix3x3& ro_mat, floa
this->quadratic_attenuation = quadratic_attenuation;
}
Vector3 JGL::LightBase::GetPosition() const {
return {position.x, position.y, position.z};
Vector4 JGL::LightBase::GetPosition() const {
return position;
}
Color4 JGL::LightBase::GetAmbient() const {