ThreadedExamples/example02_threaded_raytracing/src/rayTracer.hpp

96 lines
2.6 KiB
C++
Raw Permalink Normal View History

2021-08-20 12:25:24 +00:00
#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;
2021-08-20 12:25:24 +00:00
2021-08-21 04:10:39 +00:00
#include <optional>
#include <string>
2021-08-23 09:30:20 +00:00
#include <tuple>
2021-08-24 03:42:18 +00:00
#include <vector>
2021-08-20 12:25:24 +00:00
2021-08-25 02:18:22 +00:00
#include <glm/mat4x4.hpp>
#include <glm/matrix.hpp>
#include <glm/vec3.hpp>
2021-08-20 12:25:24 +00:00
namespace Ex02::RT {
2021-08-20 12:25:24 +00:00
2021-08-25 05:32:19 +00:00
struct alignas(4) Pixel {
Pixel();
2021-08-23 09:30:20 +00:00
unsigned char r, g, b;
};
2021-08-23 09:30:20 +00:00
class Image {
public:
Image(unsigned int width, unsigned int height);
2021-08-23 09:30:20 +00:00
Pixel &getPixel(unsigned int x, unsigned int y);
const Pixel &getPixel(unsigned int x, unsigned int y) const;
2021-08-23 09:30:20 +00:00
2021-08-25 02:57:54 +00:00
// returns actual output filename (it appends the file extension)
2021-08-24 10:28:21 +00:00
std::string writeToFile(const std::string &filename) const;
2021-08-23 09:30:20 +00:00
2021-08-25 05:32:19 +00:00
// returns actual output filename (it appends the file extension)
std::string writeToPNG(const std::string &filename) const;
private:
unsigned int width;
2021-08-25 05:32:19 +00:00
mutable std::vector<Pixel> data;
};
2021-08-23 09:30:20 +00:00
namespace Internal {
using RTSVisibleType = std::optional<std::tuple<glm::vec3, glm::vec3>>;
2021-08-23 12:01:46 +00:00
struct LightSource {
LightSource();
2021-08-23 12:01:46 +00:00
glm::vec3 pos;
float falloffStart;
float falloffEnd;
glm::vec3 color;
2021-08-23 12:01:46 +00:00
void applyLight(glm::vec3 pos, Pixel &pixelOut) const;
};
2021-08-23 09:30:20 +00:00
struct Sphere {
Sphere();
2021-08-23 12:01:46 +00:00
glm::vec3 pos;
float radius;
2021-08-23 12:01:46 +00:00
std::optional<glm::vec3> rayToSphere(glm::vec3 rayPos,
glm::vec3 rayDirUnit) const;
2021-08-23 12:01:46 +00:00
RTSVisibleType rayToSphereVisible(glm::vec3 rayPos, glm::vec3 rayDirUnit,
const LightSource &light) const;
};
2021-08-21 04:10:39 +00:00
// returns pos of collision
std::optional<glm::vec3> rayToSphere(glm::vec3 rayPos, glm::vec3 rayDirUnit,
glm::vec3 spherePos, float sphereRadius);
2021-08-21 04:10:39 +00:00
float angleBetweenRays(glm::vec3 a, glm::vec3 b);
2021-08-23 09:30:20 +00:00
float distBetweenPositions(glm::vec3 a, glm::vec3 b);
2021-08-23 12:01:46 +00:00
// 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
2021-08-24 03:42:18 +00:00
Image renderGraySphere(unsigned int outputWidth, unsigned int outputHeight,
unsigned int threadCount = 1);
2021-08-24 03:42:18 +00:00
Image renderColorsWithSpheres(unsigned int outputWidth,
unsigned int outputHeight,
unsigned int threadCount = 1);
2021-08-20 12:25:24 +00:00
} // namespace Ex02::RT
2021-08-20 12:25:24 +00:00
#endif