shape.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. uint64_t objectId;
  42. protected:
  43. virtual void localIntersect(Ray r, Intersect &xs) = 0;
  44. virtual Tuple localNormalAt(Tuple point, Intersection *hit) = 0;
  45. static uint64_t newObjectId();
  46. public:
  47. Matrix transformMatrix;
  48. Matrix inverseTransform;
  49. Matrix transposedInverseTransform;
  50. Material material;
  51. bool dropShadow;
  52. Shape *parent;
  53. bool materialSet;
  54. public:
  55. Shape(ShapeType = Shape::NONE);
  56. ShapeType getType() { return this->type; };
  57. virtual void intersect(Ray &r, Intersect &xs);
  58. Tuple normalAt(Tuple point, Intersection *hit = nullptr);
  59. uint64_t getObjectId() { return this->objectId; };
  60. void setObjectId(uint64_t oid) { this->objectId = oid; };
  61. /* Bounding box points are always world value */
  62. virtual BoundingBox getLocalBounds();
  63. virtual BoundingBox getBounds();
  64. virtual bool haveFiniteBounds() { return true; };
  65. virtual void updateTransform();
  66. virtual bool includes(Shape *b) { return this == b; };
  67. virtual void dumpMe(FILE *fp);
  68. /* When an object is locked, the matrix transformation and bounding box can't be updated. This is
  69. * usefull to move object between group without changing the real hierarchy between them.
  70. * It will also not change the parent member.
  71. * This is supposed to be used only before a render is going to start so we can optimise the
  72. * way the object are stored to prefer lots of un-needed intersections.
  73. */
  74. virtual void lock() { this->locked = true; };
  75. Tuple worldToObject(Tuple point) { return this->inverseTransform * point; };
  76. Tuple objectToWorld(Tuple point) { return this->transformMatrix * point; };
  77. Tuple normalToWorld(Tuple normalVector);
  78. void setParent(Shape *parent) { if (!this->locked) { this->parent = parent; };};
  79. void setTransform(Matrix transform);
  80. void setMaterial(Material material) { this->material = material; this->materialSet = true; };
  81. Material *getMaterial();
  82. Ray transform(Ray &r) { return Ray(this->transformMatrix * r.origin, this->transformMatrix * r.direction); };
  83. Ray invTransform(Ray &r) { return Ray(this->inverseTransform * r.origin, this->inverseTransform * r.direction); };
  84. bool operator==(const Shape &b) const { return this->material == b.material &&
  85. this->type == b.type &&
  86. this->transformMatrix == b.transformMatrix; };
  87. };
  88. #endif /* DORAYME_SHAPE_H */