diff --git a/fggl/gfx/ogl4/models.cpp b/fggl/gfx/ogl4/models.cpp index 603441380848be6441d0b2dafa851cebd50b3007..3af53292cfd596b8dacfeed9e1dfe4621fdd07c8 100644 --- a/fggl/gfx/ogl4/models.cpp +++ b/fggl/gfx/ogl4/models.cpp @@ -163,18 +163,15 @@ namespace fggl::gfx::ogl4 { shader->setUniformMtx(shader->uniform("view"), viewMatrix); shader->setUniformMtx(shader->uniform("projection"), projectionMatrix); - auto* material = world.tryGet<fggl::gfx::PhongMaterial>(entity); - if ( material == nullptr) { - shader->setUniformF(shader->uniform("material.ambient"), math::vec3(0.05F, 0.05F, 0.05F)); - shader->setUniformF(shader->uniform("material.diffuse"), math::vec3(0.5F, 0.5F, 0.5F)); - shader->setUniformF(shader->uniform("material.specular"), math::vec3(0.7F, 0.7F, 0.7F)); - shader->setUniformF(shader->uniform("material.shininess"), .078125F); - } else { - shader->setUniformF(shader->uniform("material.ambient"), material->ambient); - shader->setUniformF(shader->uniform("material.diffuse"), material->diffuse); - shader->setUniformF(shader->uniform("material.specular"), material->specular); - shader->setUniformF(shader->uniform("material.shininess"), material->shininess); + // material detection with fallback + auto* material = &gfx::DEFAULT_MATERIAL; + if ( world.has<PhongMaterial>(entity) ) { + material = world.get<PhongMaterial>(entity); } + shader->setUniformF(shader->uniform("material.ambient"), material->ambient); + shader->setUniformF(shader->uniform("material.diffuse"), material->diffuse); + shader->setUniformF(shader->uniform("material.specular"), material->specular); + shader->setUniformF(shader->uniform("material.shininess"), material->shininess); auto lightPosIdx = shader->uniform("lightPos"); if ( lightPosIdx != -1 ) { diff --git a/fggl/phys/bullet/simulation.cpp b/fggl/phys/bullet/simulation.cpp index e2256ccdd9a27203cc52cb7cc89980ad0f6f831c..f0c49e53edaec5ca2806f4c1fee797e93f9518db 100644 --- a/fggl/phys/bullet/simulation.cpp +++ b/fggl/phys/bullet/simulation.cpp @@ -199,11 +199,11 @@ namespace fggl::phys::bullet { } static void handleCollisionCallbacks(ecs3::World* world, ecs3::entity_t owner, ecs3::entity_t other) { - auto* callbacks = world->tryGet<CollisionCallbacks>(owner); - if ( callbacks == nullptr ) { + if ( !world->has<CollisionCallbacks>(owner) ) { return; } + auto* callbacks = world->tryGet<CollisionCallbacks>(owner); auto* cache = world->tryGet<CollisionCache>(owner); if ( cache != nullptr ) { auto itr = cache->collisions.find(other); diff --git a/include/fggl/ecs3/prototype/world.h b/include/fggl/ecs3/prototype/world.h index 7cd6287556f0ec846d140341aaceedaba8330b58..87a6d66b2e0c1c2f5f8b28572d54aa77833ffdd0 100644 --- a/include/fggl/ecs3/prototype/world.h +++ b/include/fggl/ecs3/prototype/world.h @@ -233,6 +233,17 @@ namespace fggl::ecs3::prototype { return entities; } + template<typename ...Cs> + bool has(entity_t entityIdx) const { + if ( !alive(entityIdx)) { + return false; + } + + std::vector<ecs::component_type_t> key; + (key.push_back(Component<Cs>::typeID()), ...); + return m_entities.at(entityIdx).hasComponents(key); + } + template<typename C> C *add(entity_t entity_id) { assert( alive(entity_id) && "attempted to add component on null entity" ); diff --git a/include/fggl/gfx/phong.hpp b/include/fggl/gfx/phong.hpp index 03faae4ab5ce256647a5b162d930ac20632d4814..ca1d09e2a24c185d7f8f3a8f437522251b7b996d 100644 --- a/include/fggl/gfx/phong.hpp +++ b/include/fggl/gfx/phong.hpp @@ -29,11 +29,6 @@ namespace fggl::gfx { - constexpr math::vec3 DEFAULT_AMBIENT { 0.05F, 0.05F, 0.05F}; - constexpr math::vec3 DEFAULT_DIFFUSE { 0.5F, 0.5F, 0.5F}; - constexpr math::vec3 DEFAULT_SPECULAR { 0.7F, 0.7F, 0.7F}; - constexpr float DEFAULT_SHININESS = .078125F; - struct PhongMaterial { constexpr static const char* name = "gfx::material"; math::vec3 ambient; @@ -42,6 +37,19 @@ namespace fggl::gfx { float shininess; }; + constexpr math::vec3 DEFAULT_AMBIENT { 0.05F, 0.05F, 0.05F}; + constexpr math::vec3 DEFAULT_DIFFUSE { 0.5F, 0.5F, 0.5F}; + constexpr math::vec3 DEFAULT_SPECULAR { 0.7F, 0.7F, 0.7F}; + constexpr float DEFAULT_SHININESS = .078125F; + + constexpr PhongMaterial DEFAULT_MATERIAL { + DEFAULT_AMBIENT, + DEFAULT_DIFFUSE, + DEFAULT_SPECULAR, + DEFAULT_SHININESS + }; + + enum class LightType { Directional, Point,