diff --git a/demo/demo/main.cpp b/demo/demo/main.cpp index 9425d8aff808bd1a8c1cf770971843cb7b8b8441..da2ae94c398026fc5641cb60d74bbfd4e5f3c7d0 100644 --- a/demo/demo/main.cpp +++ b/demo/demo/main.cpp @@ -83,6 +83,7 @@ int main(int argc, const char* argv[]) { moduleManager.use<fggl::display::GLFW>(); moduleManager.use<fggl::assets::AssetFolders>(); moduleManager.use<fggl::entity::ECS>(); + #ifdef FGGL_MODULE_BULLET moduleManager.use<fggl::phys::Bullet3>(); #else @@ -101,13 +102,6 @@ int main(int argc, const char* argv[]) { window->setFullscreen( true ); app.setWindow(window); - // load a bunch of modules to provide game functionality - //app.use<fggl::ecs3::ecsTypes>(); - /*app.use<fggl::gfx::SceneUtils>(); - #ifdef FGGL_MODULE_BULLET - app.use<FGGL_MODULE_BULLET>(); - #endif*/ - // our test states setup_menu(app); app.addState<GameScene>("game"); diff --git a/fggl/debug/CMakeLists.txt b/fggl/debug/CMakeLists.txt index bd85e56f53c1773349991947a93d93d04820cc6e..5696ea25fe392004b41e529b7df2dbe71d8f0b0c 100644 --- a/fggl/debug/CMakeLists.txt +++ b/fggl/debug/CMakeLists.txt @@ -1,8 +1,8 @@ target_sources(fggl PRIVATE - debug.cpp - debug_draw.cpp - logging.cpp + debug.cpp + debug_draw.cpp + logging.cpp ) # spdlog for cleaner logging diff --git a/fggl/gfx/ogl4/models.cpp b/fggl/gfx/ogl4/models.cpp index 1f566eab65a6959e31b2eaa1def3f4bfa997ee17..bacdc59c38ddaf3d243e4e4563981db5192f4cdd 100644 --- a/fggl/gfx/ogl4/models.cpp +++ b/fggl/gfx/ogl4/models.cpp @@ -158,143 +158,145 @@ namespace fggl::gfx::ogl4 { } #endif - void StaticModelRenderer::renderModelsForward(const entity::EntityManager &world) { - - // fetch cameras we will need to render with - auto cameras = world.find<gfx::Camera>(); - - // if there are no cameras, we can't do anything... - if (cameras.empty()) { - spdlog::warn("asked to render static models, but there were no cameras"); - return; - } - - // perform a rendering pass for each camera (will usually only be one...) - for (const auto &cameraEnt : cameras) { - //TODO should be clipping this to only visible objects + static void forward_camera_pass(const entity::EntityID& camera, const fggl::entity::EntityManager& world) { + // enable required OpenGL state + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); - // enable required OpenGL state - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); + // enable depth testing + glEnable(GL_DEPTH_TEST); - // enable depth testing - glEnable(GL_DEPTH_TEST); + // set-up camera matrices + const auto &camTransform = world.get<fggl::math::Transform>(camera); + const auto &camComp = world.get<fggl::gfx::Camera>(camera); - // set-up camera matrices - const auto &camTransform = world.get<math::Transform>(cameraEnt); - const auto &camComp = world.get<gfx::Camera>(cameraEnt); + const math::mat4 projectionMatrix = + glm::perspective(camComp.fov, camComp.aspectRatio, camComp.nearPlane, camComp.farPlane); + const math::mat4 viewMatrix = glm::lookAt(camTransform.origin(), camComp.target, camTransform.up()); - const math::mat4 projectionMatrix = - glm::perspective(camComp.fov, camComp.aspectRatio, camComp.nearPlane, camComp.farPlane); - const math::mat4 viewMatrix = glm::lookAt(camTransform.origin(), camComp.target, camTransform.up()); + // TODO lighting needs to not be this... + math::vec3 lightPos{0.0f, 10.0f, 0.0f}; - // TODO lighting needs to not be this... - math::vec3 lightPos{0.0f, 10.0f, 0.0f}; + std::shared_ptr<ogl::Shader> shader = nullptr; + ogl::Location mvpMatrixUniform = 0; + ogl::Location mvMatrixUniform = 0; - std::shared_ptr<ogl::Shader> shader = nullptr; - ogl::Location mvpMatrixUniform = 0; - ogl::Location mvMatrixUniform = 0; + auto renderables = world.find<StaticModel>(); + for (const auto &entity : renderables) { - auto renderables = world.find<StaticModel>(); - for (const auto &entity : renderables) { + // ensure that the model pipeline actually exists... + const auto &model = world.get<StaticModel>(entity); + if (model.pipeline == nullptr) { + debug::warning("shader was null, aborting render"); + continue; + } - // ensure that the model pipeline actually exists... - const auto &model = world.get<StaticModel>(entity); - if (model.pipeline == nullptr) { - debug::warning("shader was null, aborting render"); - continue; + // check if we switched shaders + if (shader == nullptr || shader->shaderID() != model.pipeline->shaderID()) { + // new shader - need to re-send the view and projection matrices + shader = model.pipeline; + shader->use(); + if (shader->hasUniform("projection")) { + shader->setUniformMtx(shader->uniform("view"), viewMatrix); + shader->setUniformMtx(shader->uniform("projection"), projectionMatrix); } + mvpMatrixUniform = shader->uniform("MVPMatrix"); + mvMatrixUniform = shader->uniform("MVMatrix"); + } - // check if we switched shaders - if (shader == nullptr || shader->shaderID() != model.pipeline->shaderID()) { - // new shader - need to re-send the view and projection matrices - shader = model.pipeline; - shader->use(); - if (shader->hasUniform("projection")) { - shader->setUniformMtx(shader->uniform("view"), viewMatrix); - shader->setUniformMtx(shader->uniform("projection"), projectionMatrix); - } - mvpMatrixUniform = shader->uniform("MVPMatrix"); - mvMatrixUniform = shader->uniform("MVMatrix"); + // set model transform + const auto &transform = world.get<math::Transform>(entity); + shader->setUniformMtx(mvpMatrixUniform, projectionMatrix * viewMatrix * transform.model()); + shader->setUniformMtx(mvMatrixUniform, viewMatrix * transform.model()); + + auto normalMatrix = glm::mat3(glm::transpose(inverse(transform.model()))); + shader->setUniformMtx(shader->uniform("NormalMatrix"), normalMatrix); + + // setup lighting mode + if (shader->hasUniform("lights[0].isEnabled")) { + bool local = true; + + shader->setUniformI(shader->uniform("lights[0].isEnabled"), 1); + shader->setUniformI(shader->uniform("lights[0].isLocal"), local); + shader->setUniformI(shader->uniform("lights[0].isSpot"), 0); + + shader->setUniformF(shader->uniform("lights[0].constantAttenuation"), 5.0f); + shader->setUniformF(shader->uniform("lights[0].linearAttenuation"), 0.0f); + shader->setUniformF(shader->uniform("lights[0].quadraticAttenuation"), 0.0f); + + shader->setUniformF(shader->uniform("Strength"), 0.6F); + + if (!local) { + lightPos = glm::normalize(lightPos); + auto viewDir = glm::normalize(camTransform.origin() - transform.origin()); + auto halfVector = glm::normalize(lightPos + viewDir); + shader->setUniformF(shader->uniform("lights[0].halfVector"), halfVector); + shader->setUniformF(shader->uniform("EyeDirection"), viewDir); + shader->setUniformF(shader->uniform("lights[0].position"), lightPos); + } else { + auto camModelView = (viewMatrix * camTransform.model() * math::vec4(0.0f, 0.0f, 0.0f, 1.0f)); + auto modelModelView = (viewMatrix * transform.model() * math::vec4(0.0f, 0.0f, 0.0f, 1.0f)); + math::vec3 viewDir = glm::normalize(camModelView - modelModelView); + shader->setUniformF(shader->uniform("EyeDirection"), viewDir); + + shader->setUniformF(shader->uniform("lights[0].position"), + math::vec3(viewMatrix * math::vec4(lightPos, 1.0f))); } - // set model transform - const auto &transform = world.get<math::Transform>(entity); - shader->setUniformMtx(mvpMatrixUniform, projectionMatrix * viewMatrix * transform.model()); - shader->setUniformMtx(mvMatrixUniform, viewMatrix * transform.model()); - - auto normalMatrix = glm::mat3(glm::transpose(inverse(transform.model()))); - shader->setUniformMtx(shader->uniform("NormalMatrix"), normalMatrix); - - // setup lighting mode - if (shader->hasUniform("lights[0].isEnabled")) { - bool local = true; - - shader->setUniformI(shader->uniform("lights[0].isEnabled"), 1); - shader->setUniformI(shader->uniform("lights[0].isLocal"), local); - shader->setUniformI(shader->uniform("lights[0].isSpot"), 0); - - shader->setUniformF(shader->uniform("lights[0].constantAttenuation"), 5.0f); - shader->setUniformF(shader->uniform("lights[0].linearAttenuation"), 0.0f); - shader->setUniformF(shader->uniform("lights[0].quadraticAttenuation"), 0.0f); - - shader->setUniformF(shader->uniform("Strength"), 0.6F); - - if (!local) { - lightPos = glm::normalize(lightPos); - auto viewDir = glm::normalize(camTransform.origin() - transform.origin()); - auto halfVector = glm::normalize(lightPos + viewDir); - shader->setUniformF(shader->uniform("lights[0].halfVector"), halfVector); - shader->setUniformF(shader->uniform("EyeDirection"), viewDir); - shader->setUniformF(shader->uniform("lights[0].position"), lightPos); - } else { - auto camModelView = (viewMatrix * camTransform.model() * math::vec4(0.0f, 0.0f, 0.0f, 1.0f)); - auto modelModelView = (viewMatrix * transform.model() * math::vec4(0.0f, 0.0f, 0.0f, 1.0f)); - math::vec3 viewDir = glm::normalize(camModelView - modelModelView); - shader->setUniformF(shader->uniform("EyeDirection"), viewDir); - - shader->setUniformF(shader->uniform("lights[0].position"), - math::vec3(viewMatrix * math::vec4(lightPos, 1.0f))); - } - - shader->setUniformF(shader->uniform("lights[0].ambient"), {0.0f, 0.5f, 0.0f}); - shader->setUniformF(shader->uniform("lights[0].colour"), {0.5f, 0.5f, 0.5f}); - } + shader->setUniformF(shader->uniform("lights[0].ambient"), {0.0f, 0.5f, 0.0f}); + shader->setUniformF(shader->uniform("lights[0].colour"), {0.5f, 0.5f, 0.5f}); + } - // material detection with fallback - auto* material = world.tryGet<PhongMaterial>(entity); - if ( material == nullptr ) { - material = &DEFAULT_MATERIAL; - } + // material detection with fallback + auto* material = world.tryGet<PhongMaterial>(entity); + if ( material == nullptr ) { + material = &DEFAULT_MATERIAL; + } - if ( shader->hasUniform("materials[0].ambient") ) { - shader->setUniformF(shader->uniform("materials[0].emission"), material->emission); - shader->setUniformF(shader->uniform("materials[0].ambient"), material->ambient); - shader->setUniformF(shader->uniform("materials[0].diffuse"), material->diffuse); - shader->setUniformF(shader->uniform("materials[0].specular"), material->specular); - shader->setUniformF(shader->uniform("materials[0].shininess"), material->shininess); - } + if ( shader->hasUniform("materials[0].ambient") ) { + shader->setUniformF(shader->uniform("materials[0].emission"), material->emission); + shader->setUniformF(shader->uniform("materials[0].ambient"), material->ambient); + shader->setUniformF(shader->uniform("materials[0].diffuse"), material->diffuse); + shader->setUniformF(shader->uniform("materials[0].specular"), material->specular); + shader->setUniformF(shader->uniform("materials[0].shininess"), material->shininess); + } - if (shader->hasUniform("lightPos")) { - shader->setUniformF(shader->uniform("lightPos"), lightPos); - } + if (shader->hasUniform("lightPos")) { + shader->setUniformF(shader->uniform("lightPos"), lightPos); + } - auto vao = model.vao; - vao->bind(); + auto vao = model.vao; + vao->bind(); - model.vertexData->bind(); - if (model.restartIndex != NO_RESTART_IDX) { - glEnable(GL_PRIMITIVE_RESTART); - glPrimitiveRestartIndex(model.restartIndex); - } + model.vertexData->bind(); + if (model.restartIndex != NO_RESTART_IDX) { + glEnable(GL_PRIMITIVE_RESTART); + glPrimitiveRestartIndex(model.restartIndex); + } - auto *elements = model.elements.get(); - vao->drawElements(*elements, model.drawType, model.elementCount); - if (model.restartIndex != NO_RESTART_IDX) { - glDisable(GL_PRIMITIVE_RESTART); - } + auto *elements = model.elements.get(); + vao->drawElements(*elements, model.drawType, model.elementCount); + if (model.restartIndex != NO_RESTART_IDX) { + glDisable(GL_PRIMITIVE_RESTART); } + } + } + + void StaticModelRenderer::renderModelsForward(const entity::EntityManager &world) { + + // fetch cameras we will need to render with + auto cameras = world.find<gfx::Camera>(); + // if there are no cameras, we can't do anything... + if (cameras.empty()) { + spdlog::warn("asked to render static models, but there were no cameras"); + return; + } + + // perform a rendering pass for each camera (will usually only be one...) + for (const auto &cameraEnt : cameras) { + //TODO should be clipping this to only visible objects + forward_camera_pass(cameraEnt, world); } }