ThreadedExamples/example02_threaded_raytracing/src/rayTracer.hpp

97 lines
2.6 KiB
C++
Raw Normal View History

2021-08-20 12:25:24 +00:00
#ifndef EX02_RAY_TRACER_HPP
#define EX02_RAY_TRACER_HPP
#define EX02_RAY_TRACER_VIEW_RATIO 0.5f
2021-08-20 12:25:24 +00:00
#define EX02_RAY_TRACER_DEFAULT_NEAR_PLANE 0.2f
#define EX02_RAY_TRACER_DEFAULT_FAR_PLANE 4.0f
#define EX02_RAY_TRACER_COLL_INCREMENT 2.0f
2021-08-23 09:30:20 +00:00
#define EX02_RAY_TRACER_GRAY_SPHERE_RADIUS 1.5f
2021-08-20 12:25:24 +00:00
2021-08-24 03:42:18 +00:00
#include <mutex>
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
#include <glm/mat4x4.hpp>
#include <glm/matrix.hpp>
2021-08-24 03:42:18 +00:00
#include <glm/vec3.hpp>
2021-08-20 12:25:24 +00:00
namespace Ex02 {
namespace RT {
2021-08-24 03:42:18 +00:00
struct Pixel {
2021-08-23 09:30:20 +00:00
Pixel();
2021-08-24 03:42:18 +00:00
unsigned char r, g, b;
};
2021-08-23 09:30:20 +00:00
2021-08-24 03:42:18 +00:00
class Image {
public:
2021-08-23 09:30:20 +00:00
Image(unsigned int width, unsigned int height);
2021-08-24 03:42:18 +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
void writeToFile(const std::string &filename) const;
2021-08-24 03:42:18 +00:00
private:
2021-08-23 09:30:20 +00:00
unsigned int width;
std::vector<Pixel> data;
2021-08-24 03:42:18 +00:00
};
2021-08-23 09:30:20 +00:00
2021-08-24 03:42:18 +00:00
namespace Internal {
2021-08-23 12:01:46 +00:00
typedef std::optional<std::tuple<glm::vec3, glm::vec3>> RTSVisibleType;
struct LightSource {
2021-08-24 03:42:18 +00:00
LightSource();
2021-08-23 12:01:46 +00:00
2021-08-24 03:42:18 +00:00
glm::vec3 pos;
float falloffStart;
float falloffEnd;
glm::vec3 color;
2021-08-23 12:01:46 +00:00
2021-08-24 03:42:18 +00:00
void applyLight(glm::vec3 pos, Pixel &pixelOut) const;
void applyLight(glm::vec3 pos, Pixel &pixelOut, std::mutex *mutex) const;
2021-08-23 12:01:46 +00:00
};
2021-08-23 09:30:20 +00:00
2021-08-23 12:01:46 +00:00
struct Sphere {
2021-08-24 03:42:18 +00:00
Sphere();
2021-08-23 12:01:46 +00:00
2021-08-24 03:42:18 +00:00
glm::vec3 pos;
float radius;
2021-08-23 12:01:46 +00:00
2021-08-24 03:42:18 +00:00
std::optional<glm::vec3> rayToSphere(glm::vec3 rayPos,
glm::vec3 rayDirUnit) const;
2021-08-23 12:01:46 +00:00
2021-08-24 03:42:18 +00:00
RTSVisibleType rayToSphereVisible(glm::vec3 rayPos, glm::vec3 rayDirUnit,
const LightSource &light) const;
2021-08-23 12:01:46 +00:00
};
2021-08-21 04:10:39 +00:00
2021-08-23 09:30:20 +00:00
// returns pos of collision
2021-08-24 03:42:18 +00:00
std::optional<glm::vec3> rayToSphere(glm::vec3 rayPos, glm::vec3 rayDirUnit,
glm::vec3 spherePos,
float sphereRadius);
2021-08-21 04:10:39 +00:00
2021-08-24 03:42:18 +00:00
float angleBetweenRays(glm::vec3 a, glm::vec3 b);
2021-08-23 09:30:20 +00:00
2021-08-24 03:42:18 +00:00
float distBetweenPositions(glm::vec3 a, glm::vec3 b);
2021-08-23 12:01:46 +00:00
2021-08-23 09:30:20 +00:00
// first vec3 is result from rayToSphere(), second is ray to light source
2021-08-24 03:42:18 +00:00
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);
2021-08-20 12:25:24 +00:00
} // namespace RT
} // namespace Ex02
#endif