material.hpp 1.8 KB

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