GlobalIlluminationProbeComponent.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. ANKI_SCENE_COMPONENT_STATICS(GlobalIlluminationProbeComponent)
  12. GlobalIlluminationProbeComponent::GlobalIlluminationProbeComponent(SceneNode* node)
  13. : SceneComponent(node, getStaticClassId())
  14. , m_node(node)
  15. , m_uuid(node->getSceneGraph().getNewUuid())
  16. , m_markedForRendering(false)
  17. , m_shapeDirty(true)
  18. {
  19. if(node->getSceneGraph().getResourceManager().loadResource("EngineAssets/GiProbe.ankitex", m_debugImage))
  20. {
  21. ANKI_SCENE_LOGF("Failed to load resources");
  22. }
  23. }
  24. void GlobalIlluminationProbeComponent::draw(RenderQueueDrawContext& ctx) const
  25. {
  26. const Aabb box = getAabbWorldSpace();
  27. const Vec3 tsl = (box.getMax().xyz() + box.getMin().xyz()) / 2.0f;
  28. const Vec3 scale = (tsl - box.getMin().xyz());
  29. // Set non uniform scale.
  30. Mat3 rot = Mat3::getIdentity();
  31. rot(0, 0) *= scale.x();
  32. rot(1, 1) *= scale.y();
  33. rot(2, 2) *= scale.z();
  34. const Mat4 mvp = ctx.m_viewProjectionMatrix * Mat4(tsl.xyz1(), rot, 1.0f);
  35. const Bool enableDepthTest = ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DEPTH_TEST_ON);
  36. if(enableDepthTest)
  37. {
  38. ctx.m_commandBuffer->setDepthCompareOperation(CompareOperation::LESS);
  39. }
  40. else
  41. {
  42. ctx.m_commandBuffer->setDepthCompareOperation(CompareOperation::ALWAYS);
  43. }
  44. m_node->getSceneGraph().getDebugDrawer().drawCubes(
  45. ConstWeakArray<Mat4>(&mvp, 1), Vec4(0.729f, 0.635f, 0.196f, 1.0f), 1.0f,
  46. ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DITHERED_DEPTH_TEST_ON), 2.0f, *ctx.m_stagingGpuAllocator,
  47. ctx.m_commandBuffer);
  48. m_node->getSceneGraph().getDebugDrawer().drawBillboardTextures(
  49. ctx.m_projectionMatrix, ctx.m_viewMatrix, ConstWeakArray<Vec3>(&m_worldPosition, 1), Vec4(1.0f),
  50. ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DITHERED_DEPTH_TEST_ON), m_debugImage->getTextureView(),
  51. ctx.m_sampler, Vec2(0.75f), *ctx.m_stagingGpuAllocator, ctx.m_commandBuffer);
  52. // Restore state
  53. if(!enableDepthTest)
  54. {
  55. ctx.m_commandBuffer->setDepthCompareOperation(CompareOperation::LESS);
  56. }
  57. }
  58. } // end namespace anki