ReflectionProbeComponent.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/ReflectionProbeComponent.h>
  6. #include <AnKi/Scene/SceneGraph.h>
  7. #include <AnKi/Scene/SceneNode.h>
  8. #include <AnKi/Resource/ResourceManager.h>
  9. #include <AnKi/Resource/ImageResource.h>
  10. namespace anki
  11. {
  12. ANKI_SCENE_COMPONENT_STATICS(ReflectionProbeComponent)
  13. ReflectionProbeComponent::ReflectionProbeComponent(SceneNode* node)
  14. : SceneComponent(node, getStaticClassId())
  15. , m_node(node)
  16. , m_uuid(node->getSceneGraph().getNewUuid())
  17. , m_markedForRendering(false)
  18. , m_markedForUpdate(true)
  19. {
  20. if(node->getSceneGraph().getResourceManager().loadResource("EngineAssets/Mirror.ankitex", m_debugImage))
  21. {
  22. ANKI_SCENE_LOGF("Failed to load resources");
  23. }
  24. }
  25. ReflectionProbeComponent::~ReflectionProbeComponent()
  26. {
  27. }
  28. void ReflectionProbeComponent::draw(RenderQueueDrawContext& ctx) const
  29. {
  30. const Vec3 tsl = m_worldPos;
  31. const Vec3 scale = getBoxVolumeSize() / 2.0f;
  32. // Set non uniform scale.
  33. Mat3 rot = Mat3::getIdentity();
  34. rot(0, 0) *= scale.x();
  35. rot(1, 1) *= scale.y();
  36. rot(2, 2) *= scale.z();
  37. const Mat4 mvp = ctx.m_viewProjectionMatrix * Mat4(tsl.xyz1(), rot, 1.0f);
  38. const Bool enableDepthTest = ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DEPTH_TEST_ON);
  39. if(enableDepthTest)
  40. {
  41. ctx.m_commandBuffer->setDepthCompareOperation(CompareOperation::LESS);
  42. }
  43. else
  44. {
  45. ctx.m_commandBuffer->setDepthCompareOperation(CompareOperation::ALWAYS);
  46. }
  47. m_node->getSceneGraph().getDebugDrawer().drawCubes(
  48. ConstWeakArray<Mat4>(&mvp, 1), Vec4(0.0f, 0.0f, 1.0f, 1.0f), 1.0f,
  49. ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DITHERED_DEPTH_TEST_ON), 2.0f, *ctx.m_stagingGpuAllocator,
  50. ctx.m_commandBuffer);
  51. m_node->getSceneGraph().getDebugDrawer().drawBillboardTextures(
  52. ctx.m_projectionMatrix, ctx.m_viewMatrix, ConstWeakArray<Vec3>(&m_worldPos, 1), Vec4(1.0f),
  53. ctx.m_debugDrawFlags.get(RenderQueueDebugDrawFlag::DITHERED_DEPTH_TEST_ON), m_debugImage->getTextureView(),
  54. ctx.m_sampler, Vec2(0.75f), *ctx.m_stagingGpuAllocator, ctx.m_commandBuffer);
  55. // Restore state
  56. if(!enableDepthTest)
  57. {
  58. ctx.m_commandBuffer->setDepthCompareOperation(CompareOperation::LESS);
  59. }
  60. }
  61. } // end namespace anki