mesh.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #pragma once
  2. #include <iostream>
  3. #include <initializer_list>
  4. #include <glm/glm.hpp>
  5. #include <uv.hpp>
  6. namespace gb
  7. {
  8. class mesh_compute;
  9. class Mesh
  10. {
  11. public:
  12. uv4 m_va, m_vb, m_vc;
  13. uv2 m_ta, m_tb, m_tc;
  14. uv4 m_na, m_nb, m_nc;
  15. public:
  16. explicit Mesh(
  17. const uv4& va,
  18. const uv4& vb,
  19. const uv4& vc,
  20. const uv2& vta,
  21. const uv2& vtb,
  22. const uv2& vtc,
  23. const uv4& vna,
  24. const uv4& vnb,
  25. const uv4& vnc
  26. );
  27. mesh_compute apply(const glm::mat4& mat) const;
  28. // friend ::std::ostream& operator<<(::std::ostream& stream, const Mesh& mesh);
  29. };
  30. struct mesh_compute
  31. {
  32. private:
  33. // return value: 1 if z is greater than 0
  34. static inline int test_two_points(glm::vec4 e_start, glm::vec4 e_end, const glm::vec4& p)
  35. {
  36. glm::vec3 edge = e_start - e_end;
  37. edge.z = 0;
  38. glm::vec3 v = e_start - p;
  39. v.z = 0;
  40. return glm::cross(edge, v).z > 0;
  41. }
  42. public:
  43. glm::vec4 m_va, m_vb, m_vc;
  44. glm::vec2 m_ta, m_tb, m_tc;
  45. glm::vec4 m_na, m_nb, m_nc;
  46. // return value: 1 if colored, 0 if empty
  47. inline int sample(const glm::vec4& p) const
  48. {
  49. int sign = test_two_points(m_va, m_vb, p);
  50. if (test_two_points(m_vb, m_vc, p) != sign) {
  51. return 0;
  52. }
  53. if (test_two_points(m_vc, m_va, p) != sign) {
  54. return 0;
  55. }
  56. return 1;
  57. }
  58. };
  59. }