SoftwareRasterizer.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (C) 2009-present, 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. /// Prepare for rendering. Call it before every draw.
  18. void prepare(const Mat4& mv, const Mat4& p, U32 width, U32 height);
  19. /// Render some verts.
  20. /// @param[in] verts Pointer to the first vertex to draw.
  21. /// @param vertCount The number of verts to draw.
  22. /// @param stride The stride (in bytes) of the next vertex.
  23. /// @param backfaceCulling If true it will do backface culling.
  24. /// @note It's thread-safe against other draw() invocations only.
  25. void draw(const F32* verts, U vertCount, U stride, Bool backfaceCulling);
  26. /// Fill the depth buffer with some values.
  27. void fillDepthBuffer(ConstWeakArray<F32> depthValues);
  28. /// Perform visibility tests.
  29. /// @param aabb The Aabb in of the cs in world space.
  30. /// @return Return true if it's visible and false otherwise.
  31. Bool visibilityTest(const Aabb& aabb) const;
  32. private:
  33. Mat4 m_mv; ///< ModelView.
  34. Mat4 m_p; ///< Projection.
  35. Mat4 m_mvp;
  36. Array<Plane, 6> m_planesL; ///< In view space.
  37. Array<Plane, 6> m_planesW; ///< In world space.
  38. U32 m_width;
  39. U32 m_height;
  40. SceneDynamicArray<Atomic<U32>> m_zbuffer;
  41. /// @param tri In clip space.
  42. void rasterizeTriangle(const Vec4* tri);
  43. Bool computeBarycetrinc(const Vec2& a, const Vec2& b, const Vec2& c, const Vec2& p, Vec3& uvw) const;
  44. /// Clip triangle in the near plane.
  45. /// @note Triangles in view space.
  46. void clipTriangle(const Vec4* inTriangle, Vec4* outTriangles, U& outTriangleCount) const;
  47. Bool visibilityTestInternal(const Aabb& aabb) const;
  48. };
  49. /// @}
  50. } // end namespace anki