SoftwareRasterizer.h 2.1 KB

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