material.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef TEXTURE_HPP
  2. #define TEXTURE_HPP
  3. #include <Eigen/Core>
  4. #include <memory>
  5. #include "warping.hpp"
  6. using Color = Eigen::Vector3f;
  7. struct texture{
  8. virtual Eigen::Vector3f eval(const Eigen::Vector2f& uv)const{
  9. return Eigen::Vector3f(1,1,1);
  10. }
  11. };
  12. struct uni_texture : texture{
  13. Color c;
  14. uni_texture(const Color& _c) : c(_c){}
  15. virtual Eigen::Vector3f eval(const Eigen::Vector2f& uv)const override{
  16. return c;
  17. }
  18. };
  19. struct checkerboard_texture : texture{
  20. Color c1;
  21. Color c2;
  22. checkerboard_texture(const Color& _c1, const Color& _c2) : c1(_c1), c2(_c2){}
  23. virtual Eigen::Vector3f eval(const Eigen::Vector2f& uv)const override{
  24. int i = uv(0) * 100.f;
  25. int j = uv(1) * 100.f;
  26. return ((i ^ j) & 1) ? c1 : c2;
  27. }
  28. };
  29. struct quad_checkerboard_texture : texture{
  30. Color c1;
  31. Color c2;
  32. Color c3;
  33. Color c4;
  34. quad_checkerboard_texture(const Color& _c1, const Color& _c2, const Color& _c3, const Color& _c4) : c1(_c1), c2(_c2), c3(_c3), c4(_c4){}
  35. virtual Eigen::Vector3f eval(const Eigen::Vector2f& uv)const override{
  36. int i = uv(0) * 100.f;
  37. int j = uv(1) * 100.f;
  38. int id = ((i ^ j) & 3);
  39. switch(id){
  40. case 0:
  41. return c1;
  42. case 1:
  43. return c2;
  44. case 2:
  45. return c3;
  46. case 3:
  47. default:
  48. return c4;
  49. }
  50. }
  51. };
  52. struct bsdf{
  53. virtual Color eval(const Eigen::Vector3f& in, const Eigen::Vector3f& out, const Eigen::Vector3f& normal)const{
  54. throw 5;
  55. return Eigen::Vector3f(1,1,1);
  56. }
  57. };
  58. struct lambertian_bsdf : bsdf{
  59. virtual Color eval(const Eigen::Vector3f& in, const Eigen::Vector3f& out, const Eigen::Vector3f& normal)const override{
  60. float x = in.dot(-normal);
  61. return Eigen::Vector3f(x,x,x);
  62. }
  63. };
  64. struct microfacet_bsdf : bsdf{
  65. const float alpha;
  66. microfacet_bsdf() : alpha(0.1f){}
  67. microfacet_bsdf(float _alpha) : alpha(_alpha){}
  68. virtual Color eval(const Eigen::Vector3f& in, const Eigen::Vector3f& out, const Eigen::Vector3f& normal)const override{
  69. //float x = in.dot(-normal);
  70. //return Eigen::Vector3f(x,x,x);
  71. Eigen::Vector3f mirror_ref = in + normal * (in.dot(normal));
  72. //float x = beckmann_eval(mirror_ref, alpha);
  73. float x = 1.0f * std::exp(mirror_ref.dot(out) - 1);
  74. return Color(x,x,x);
  75. }
  76. };
  77. struct material{
  78. std::unique_ptr<texture> tex;
  79. std::unique_ptr<bsdf> m_bsdf;
  80. material() : material(Color(1,1,1)){
  81. }
  82. material(const Color& c, std::unique_ptr<bsdf>&& bsdf) : tex(std::make_unique<uni_texture>(c)), m_bsdf(std::move(bsdf)){
  83. }
  84. material(const Color& c) : tex(std::make_unique<uni_texture>(c)), m_bsdf(std::make_unique<lambertian_bsdf>()){
  85. }
  86. };
  87. #endif