#ifndef EX02_RAY_TRACER_HPP #define EX02_RAY_TRACER_HPP constexpr float EX02_RAY_TRACER_VIEW_RATIO = 0.5F; constexpr float EX02_RAY_TRACER_DEFAULT_NEAR_PLANE = 0.2F; constexpr float EX02_RAY_TRACER_DEFAULT_FAR_PLANE = 4.0F; constexpr float EX02_RAY_TRACER_COLL_INCREMENT = 2.0F; constexpr float EX02_RAY_TRACER_GRAY_SPHERE_RADIUS = 1.5F; #include #include #include #include #include #include #include namespace Ex02::RT { struct alignas(4) Pixel { Pixel(); unsigned char r, g, b; }; class Image { public: Image(unsigned int width, unsigned int height); Pixel &getPixel(unsigned int x, unsigned int y); const Pixel &getPixel(unsigned int x, unsigned int y) const; // returns actual output filename (it appends the file extension) std::string writeToFile(const std::string &filename) const; // returns actual output filename (it appends the file extension) std::string writeToPNG(const std::string &filename) const; private: unsigned int width; mutable std::vector data; }; namespace Internal { using RTSVisibleType = std::optional>; struct LightSource { LightSource(); glm::vec3 pos; float falloffStart; float falloffEnd; glm::vec3 color; void applyLight(glm::vec3 pos, Pixel &pixelOut) const; }; struct Sphere { Sphere(); glm::vec3 pos; float radius; std::optional rayToSphere(glm::vec3 rayPos, glm::vec3 rayDirUnit) const; RTSVisibleType rayToSphereVisible(glm::vec3 rayPos, glm::vec3 rayDirUnit, const LightSource &light) const; }; // returns pos of collision std::optional rayToSphere(glm::vec3 rayPos, glm::vec3 rayDirUnit, glm::vec3 spherePos, float sphereRadius); float angleBetweenRays(glm::vec3 a, glm::vec3 b); float distBetweenPositions(glm::vec3 a, glm::vec3 b); // first vec3 is result from rayToSphere(), second is ray to light source RTSVisibleType rayToSphereVisible(glm::vec3 rayPos, glm::vec3 rayDirUnit, glm::vec3 spherePos, float sphereRadius, glm::vec3 lightPos); } // namespace Internal Image renderGraySphere(unsigned int outputWidth, unsigned int outputHeight, unsigned int threadCount = 1); Image renderColorsWithSpheres(unsigned int outputWidth, unsigned int outputHeight, unsigned int threadCount = 1); } // namespace Ex02::RT #endif