shape.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Object header
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #ifndef DORAYME_SHAPE_H
  10. #define DORAYME_SHAPE_H
  11. class Shape;
  12. #include <stdio.h>
  13. #include <ray.h>
  14. #include <tuple.h>
  15. #include <matrix.h>
  16. #include <intersect.h>
  17. #include <material.h>
  18. #include <boundingbox.h>
  19. enum ShapeType
  20. {
  21. SHAPE_NONE,
  22. SHAPE_SPHERE,
  23. SHAPE_PLANE,
  24. SHAPE_CUBE,
  25. SHAPE_CYLINDER,
  26. SHAPE_CONE,
  27. SHAPE_GROUP,
  28. SHAPE_TRIANGLE,
  29. SHAPE_OBJFILE,
  30. SHAPE_SMOOTHTRIANGLE,
  31. };
  32. /* Base class for all object that can be presented in the world */
  33. class Shape
  34. {
  35. protected:
  36. ShapeType type;
  37. Matrix localTransformMatrix;
  38. protected:
  39. virtual Intersect localIntersect(Ray r) = 0;
  40. virtual Tuple localNormalAt(Tuple point, Intersection *hit) = 0;
  41. public:
  42. Matrix transformMatrix;
  43. Matrix inverseTransform;
  44. Matrix transposedInverseTransform;
  45. Material material;
  46. bool dropShadow;
  47. Shape *parent;
  48. bool materialSet;
  49. public:
  50. Shape(ShapeType = SHAPE_NONE);
  51. virtual Intersect intersect(Ray r);
  52. virtual Intersect intersectOOB(Ray r) { return this->intersect(r); };
  53. Tuple normalAt(Tuple point, Intersection *hit = nullptr);
  54. /* Bounding box points are always world value */
  55. virtual BoundingBox getLocalBounds();
  56. virtual BoundingBox getBounds();
  57. virtual bool haveFiniteBounds() { return true; };
  58. virtual void updateTransform();
  59. virtual void dumpMe(FILE *fp);
  60. Tuple worldToObject(Tuple point) { return this->inverseTransform * point; };
  61. Tuple objectToWorld(Tuple point) { return this->transformMatrix * point; };
  62. Tuple normalToWorld(Tuple normalVector);
  63. void setTransform(Matrix transform);
  64. void setMaterial(Material material) { this->material = material; };
  65. Ray transform(Ray r) { return Ray(this->transformMatrix * r.origin, this->transformMatrix * r.direction); };
  66. Ray invTransform(Ray r) { return Ray(this->inverseTransform * r.origin, this->inverseTransform * r.direction); };
  67. bool operator==(const Shape &b) const { return this->material == b.material &&
  68. this->type == b.type &&
  69. this->transformMatrix == b.transformMatrix; };
  70. };
  71. #endif /* DORAYME_SHAPE_H */