SoftwareRasterizer.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #pragma once
  6. #include <AnKi/Scene/Common.h>
  7. #include <AnKi/Math.h>
  8. #include <AnKi/Collision/Plane.h>
  9. #include <AnKi/Util/WeakArray.h>
  10. namespace anki {
  11. /// @addtogroup scene
  12. /// @{
  13. /// Software rasterizer for visibility tests.
  14. class SoftwareRasterizer
  15. {
  16. public:
  17. SoftwareRasterizer()
  18. {
  19. }
  20. ~SoftwareRasterizer()
  21. {
  22. m_zbuffer.destroy(m_alloc);
  23. }
  24. /// Initialize.
  25. void init(const GenericMemoryPoolAllocator<U8>& alloc)
  26. {
  27. m_alloc = alloc;
  28. }
  29. /// Prepare for rendering. Call it before every draw.
  30. void prepare(const Mat4& mv, const Mat4& p, U32 width, U32 height);
  31. /// Render some verts.
  32. /// @param[in] verts Pointer to the first vertex to draw.
  33. /// @param vertCount The number of verts to draw.
  34. /// @param stride The stride (in bytes) of the next vertex.
  35. /// @param backfaceCulling If true it will do backface culling.
  36. /// @note It's thread-safe against other draw() invocations only.
  37. void draw(const F32* verts, U vertCount, U stride, Bool backfaceCulling);
  38. /// Fill the depth buffer with some values.
  39. void fillDepthBuffer(ConstWeakArray<F32> depthValues);
  40. /// Perform visibility tests.
  41. /// @param aabb The Aabb in of the cs in world space.
  42. /// @return Return true if it's visible and false otherwise.
  43. Bool visibilityTest(const Aabb& aabb) const;
  44. private:
  45. GenericMemoryPoolAllocator<U8> m_alloc;
  46. Mat4 m_mv; ///< ModelView.
  47. Mat4 m_p; ///< Projection.
  48. Mat4 m_mvp;
  49. Array<Plane, 6> m_planesL; ///< In view space.
  50. Array<Plane, 6> m_planesW; ///< In world space.
  51. U32 m_width;
  52. U32 m_height;
  53. DynamicArray<Atomic<U32>> m_zbuffer;
  54. /// @param tri In clip space.
  55. void rasterizeTriangle(const Vec4* tri);
  56. Bool computeBarycetrinc(const Vec2& a, const Vec2& b, const Vec2& c, const Vec2& p, Vec3& uvw) const;
  57. /// Clip triangle in the near plane.
  58. /// @note Triangles in view space.
  59. void clipTriangle(const Vec4* inTriangle, Vec4* outTriangles, U& outTriangleCount) const;
  60. Bool visibilityTestInternal(const Aabb& aabb) const;
  61. };
  62. /// @}
  63. } // end namespace anki