shape.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /* Base class for all object that can be presented in the world */
  20. class Shape
  21. {
  22. public:
  23. enum ShapeType
  24. {
  25. NONE,
  26. SPHERE,
  27. PLANE,
  28. CUBE,
  29. CYLINDER,
  30. CONE,
  31. GROUP,
  32. TRIANGLE,
  33. OBJFILE,
  34. SMOOTHTRIANGLE,
  35. CSG,
  36. };
  37. protected:
  38. ShapeType type;
  39. Matrix localTransformMatrix;
  40. bool locked;
  41. protected:
  42. virtual void localIntersect(Ray r, Intersect &xs) = 0;
  43. virtual Tuple localNormalAt(Tuple point, Intersection *hit) = 0;
  44. public:
  45. Matrix transformMatrix;
  46. Matrix inverseTransform;
  47. Matrix transposedInverseTransform;
  48. Material material;
  49. bool dropShadow;
  50. Shape *parent;
  51. bool materialSet;
  52. public:
  53. Shape(ShapeType = Shape::NONE);
  54. ShapeType getType() { return this->type; };
  55. virtual void intersect(Ray &r, Intersect &xs);
  56. Tuple normalAt(Tuple point, Intersection *hit = nullptr);
  57. /* Bounding box points are always world value */
  58. virtual BoundingBox getLocalBounds();
  59. virtual BoundingBox getBounds();
  60. virtual bool haveFiniteBounds() { return true; };
  61. virtual void updateTransform();
  62. virtual bool includes(Shape *b) { return this == b; };
  63. virtual void dumpMe(FILE *fp);
  64. /* When an object is locked, the matrix transformation and bounding box can't be updated. This is
  65. * usefull to move object between group without changing the real hierarchy between them.
  66. * It will also not change the parent member.
  67. * This is supposed to be used only before a render is going to start so we can optimise the
  68. * way the object are stored to prefer lots of un-needed intersections.
  69. */
  70. virtual void lock() { this->locked = true; };
  71. Tuple worldToObject(Tuple point) { return this->inverseTransform * point; };
  72. Tuple objectToWorld(Tuple point) { return this->transformMatrix * point; };
  73. Tuple normalToWorld(Tuple normalVector);
  74. void setParent(Shape *parent) { if (!this->locked) { this->parent = parent; };};
  75. void setTransform(Matrix transform);
  76. void setMaterial(Material material) { this->material = material; this->materialSet = true; };
  77. Ray transform(Ray &r) { return Ray(this->transformMatrix * r.origin, this->transformMatrix * r.direction); };
  78. Ray invTransform(Ray &r) { return Ray(this->inverseTransform * r.origin, this->inverseTransform * r.direction); };
  79. bool operator==(const Shape &b) const { return this->material == b.material &&
  80. this->type == b.type &&
  81. this->transformMatrix == b.transformMatrix; };
  82. };
  83. #endif /* DORAYME_SHAPE_H */