ThreadedExamples/example02_threaded_raytracing/src/rayTracer.hpp

112 lines
2.4 KiB
C++

#ifndef EX02_RAY_TRACER_HPP
#define EX02_RAY_TRACER_HPP
#define EX02_RAY_TRACER_VIEW_RATIO 0.5f
#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
#define EX02_RAY_TRACER_GRAY_SPHERE_RADIUS 1.5f
#include <vector>
#include <optional>
#include <string>
#include <tuple>
#include <mutex>
#include <glm/vec3.hpp>
#include <glm/mat4x4.hpp>
#include <glm/matrix.hpp>
namespace Ex02 {
namespace RT {
struct 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;
void writeToFile(const std::string &filename) const;
private:
unsigned int width;
std::vector<Pixel> data;
};
namespace Internal {
typedef std::optional<std::tuple<glm::vec3, glm::vec3>> RTSVisibleType;
struct LightSource {
LightSource();
glm::vec3 pos;
float falloffStart;
float falloffEnd;
glm::vec3 color;
void applyLight(glm::vec3 pos, Pixel &pixelOut) const;
void applyLight(glm::vec3 pos, Pixel &pixelOut, std::mutex *mutex) const;
};
struct Sphere {
Sphere();
glm::vec3 pos;
float radius;
std::optional<glm::vec3> 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<glm::vec3> 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);
}
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 RT
} // namespace Ex02
#endif