#include "illuminator.h" #include "gfx/gl/sceneShader.h" #include "gfx/gl/vertexArrayObject.h" #include "gfx/models/texture.h" // IWYU pragma: keep bool Illuminator::SpotLight::persist(Persistence::PersistenceStore & store) { return STORE_TYPE && STORE_MEMBER(position) && STORE_MEMBER(direction) && STORE_MEMBER(colour) && STORE_MEMBER(kq) && STORE_MEMBER(arc); } bool Illuminator::PointLight::persist(Persistence::PersistenceStore & store) { return STORE_TYPE && STORE_MEMBER(position) && STORE_MEMBER(colour) && STORE_MEMBER(kq); } bool Illuminator::persist(Persistence::PersistenceStore & store) { return STORE_TYPE && STORE_HELPER(bodyMesh, Asset::MeshConstruct) && STORE_HELPER(pointLight, Persistence::Appender) && STORE_HELPER(spotLight, Persistence::Appender) && Asset::persist(store); } void Illuminator::postLoad() { if (spotLight.empty() && pointLight.empty()) { throw std::logic_error {"Illuminator has no lights"}; } texture = getTexture(); bodyMesh->configureVAO(instanceVAO) .addAttribs(instances.bufferName(), 1); VertexArrayObject {instancesSpotLightVAO} .addAttribs( instancesSpotLight.bufferName(), 0) .addAttribs(instances.bufferName(), 1); std::transform(spotLight.begin(), spotLight.end(), std::back_inserter(spotLightInstances), [this](const auto & s) { return instancesSpotLight.acquire(*s); }); VertexArrayObject {instancesPointLightVAO} .addAttribs(instancesPointLight.bufferName(), 0) .addAttribs(instances.bufferName(), 1); std::transform( pointLight.begin(), pointLight.end(), std::back_inserter(pointLightInstances), [this](const auto & s) { return instancesPointLight.acquire(*s); }); } void Illuminator::render(const SceneShader & shader) const { if (const auto count = instances.size()) { shader.basicInst.use(); if (texture) { texture->bind(); } bodyMesh->DrawInstanced(instanceVAO, static_cast(count)); } } void Illuminator::lights(const SceneShader & shader) const { if (const auto count = instances.size()) { if (const auto scount = instancesSpotLight.size()) { shader.spotLightInst.use(); glBindVertexArray(instancesSpotLightVAO); glDrawArraysInstanced(GL_POINTS, 0, static_cast(scount), static_cast(count)); } if (const auto pcount = instancesPointLight.size()) { shader.pointLightInst.use(); glBindVertexArray(instancesPointLightVAO); glDrawArraysInstanced(GL_POINTS, 0, static_cast(pcount), static_cast(count)); } glBindVertexArray(0); } }