ReflectionProbeComponent.cpp 2.3 KB

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