GlobalIlluminationProbeComponent.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Scene/Components/GlobalIlluminationProbeComponent.h>
  6. #include <AnKi/Scene/SceneNode.h>
  7. #include <AnKi/Scene/SceneGraph.h>
  8. #include <AnKi/Resource/ImageResource.h>
  9. #include <AnKi/Resource/ResourceManager.h>
  10. namespace anki
  11. {
  12. ANKI_SCENE_COMPONENT_STATICS(GlobalIlluminationProbeComponent)
  13. GlobalIlluminationProbeComponent::GlobalIlluminationProbeComponent(SceneNode* node)
  14. : SceneComponent(node, getStaticClassId())
  15. , m_node(node)
  16. , m_uuid(node->getSceneGraph().getNewUuid())
  17. , m_markedForRendering(false)
  18. , m_shapeDirty(true)
  19. {
  20. if(node->getSceneGraph().getResourceManager().loadResource("EngineAssets/GiProbe.ankitex", m_debugImage))
  21. {
  22. ANKI_SCENE_LOGF("Failed to load resources");
  23. }
  24. }
  25. void GlobalIlluminationProbeComponent::draw(RenderQueueDrawContext& ctx) const
  26. {
  27. const Aabb box = getAabbWorldSpace();
  28. const Vec3 tsl = (box.getMax().xyz() + box.getMin().xyz()) / 2.0f;
  29. const Vec3 scale = (tsl - box.getMin().xyz());
  30. // Set non uniform scale.
  31. Mat3 rot = Mat3::getIdentity();
  32. rot(0, 0) *= scale.x();
  33. rot(1, 1) *= scale.y();
  34. rot(2, 2) *= scale.z();
  35. const Mat4 mvp = ctx.m_viewProjectionMatrix * Mat4(tsl.xyz1(), rot, 1.0f);
  36. const Bool enableDepthTest = ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DEPTH_TEST_ON);
  37. if(enableDepthTest)
  38. {
  39. ctx.m_commandBuffer->setDepthCompareOperation(CompareOperation::LESS);
  40. }
  41. else
  42. {
  43. ctx.m_commandBuffer->setDepthCompareOperation(CompareOperation::ALWAYS);
  44. }
  45. m_node->getSceneGraph().getDebugDrawer().drawCubes(
  46. ConstWeakArray<Mat4>(&mvp, 1), Vec4(0.729f, 0.635f, 0.196f, 1.0f), 1.0f,
  47. ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DITHERED_DEPTH_TEST_ON), 2.0f, *ctx.m_stagingGpuAllocator,
  48. ctx.m_commandBuffer);
  49. m_node->getSceneGraph().getDebugDrawer().drawBillboardTextures(
  50. ctx.m_projectionMatrix, ctx.m_viewMatrix, ConstWeakArray<Vec3>(&m_worldPosition, 1), Vec4(1.0f),
  51. ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DITHERED_DEPTH_TEST_ON), m_debugImage->getTextureView(),
  52. ctx.m_sampler, Vec2(0.75f), *ctx.m_stagingGpuAllocator, ctx.m_commandBuffer);
  53. // Restore state
  54. if(!enableDepthTest)
  55. {
  56. ctx.m_commandBuffer->setDepthCompareOperation(CompareOperation::LESS);
  57. }
  58. }
  59. } // end namespace anki