cpp-raytracer/rtweekend.hpp
2021-08-24 20:55:39 +02:00

66 lines
1.4 KiB
C++

#ifndef RTWEEKEND_H
#define RTWEEKEND_H
#include <math.h>
#include <memory>
/* Utility macros */
#define TIMED_BLOCK_2(c, flags) rmt_ScopedCPUSample(Counter##c, flags)
#define TIMED_BLOCK_1(c, flags) TIMED_BLOCK_2(c, flags)
#define TIMED_BLOCK(flags) TIMED_BLOCK_1(__COUNTER__, flags)
// #define TIMED_BLOCK_(counter, flags) rmt_ScopedCPUSample(counter, flags)
// #define TIMED_BLOCK(flags) TIMED_BLOCK_(__COUNTER__, flags)
/* Utility functions */
double degrees_to_radians(double d)
{
return d * M_PI / 180;
}
/* Returns a double in the range [0,1) */
inline double random_double()
{
return rand() * (1.0 / RAND_MAX);
}
/* Returns a double in the range [min,max) */
inline double random_double(double min, double max)
{
return min + (max-min) * random_double();
}
/* Clamps a value between [min,max] */
inline double clamp(double v, double min, double max)
{
return v < min ? min : v > max ? max : v;
}
/* Common internal headers */
#include "ray.hpp"
#include "vec3.hpp"
struct material;
/* Common data structures */
struct hit_record {
point3 p;
vec3 normal;
std::shared_ptr<material> mat_ptr;
double t;
bool front_face;
inline void set_face_normal(const ray& r, const vec3& outward_normal)
{
front_face = dot(r.direction, outward_normal) < 0;
normal = front_face ? outward_normal : -outward_normal;
}
};
#endif