material.hpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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(float u, float v)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(float u, float v)const override{
  16. return c;
  17. }
  18. };
  19. struct bsdf{
  20. virtual Color eval(const Eigen::Vector3f& in, const Eigen::Vector3f& out, const Eigen::Vector3f& normal)const{
  21. return Eigen::Vector3f(1,1,1);
  22. }
  23. };
  24. struct lambertian_bsdf : bsdf{
  25. virtual Color eval(const Eigen::Vector3f& in, const Eigen::Vector3f& out, const Eigen::Vector3f& normal)const override{
  26. float x = in.dot(-normal);
  27. return Eigen::Vector3f(x,x,x);
  28. }
  29. };
  30. struct microfacet_bsdf : bsdf{
  31. const float alpha;
  32. microfacet_bsdf() : alpha(0.1f){}
  33. microfacet_bsdf(float _alpha) : alpha(_alpha){}
  34. virtual Color eval(const Eigen::Vector3f& in, const Eigen::Vector3f& out, const Eigen::Vector3f& normal)const override{
  35. float x = in.dot(-normal);
  36. return Eigen::Vector3f(x,x,x);
  37. Eigen::Vector3f mirror_ref = in + normal * (in.dot(normal));
  38. //float x = beckmann_eval(mirror_ref, alpha);
  39. float xd = 3.0f * std::exp(-mirror_ref.dot(out));
  40. return Color(x,x,x);
  41. }
  42. };
  43. struct material{
  44. std::unique_ptr<texture> tex;
  45. std::unique_ptr<bsdf> m_bsdf;
  46. material() : material(Color(1,1,1)){
  47. }
  48. material(const Color& c, std::unique_ptr<bsdf>&& bsdf) : tex(std::make_unique<uni_texture>(c)), m_bsdf(std::move(bsdf)){
  49. }
  50. material(const Color& c) : tex(std::make_unique<uni_texture>(c)), m_bsdf(std::make_unique<lambertian_bsdf>()){
  51. }
  52. };
  53. #endif