safrScene.hpp 3.67 KiB
#include <iostream>
#include <vkcv/Core.hpp>
#include <GLFW/glfw3.h>
#include <vkcv/camera/CameraManager.hpp>
#include <vkcv/asset/asset_loader.hpp>
#include <vkcv/shader/GLSLCompiler.hpp>
#include <chrono>
#include <limits>
#include <cmath>
#include <vector>
#include <string.h> // memcpy(3)
class safrScene {
public:
/**
* WORKAROUND
* moved the texture data struct from the old asset loader version, because it is necessary for our rendering for now
*/
struct TextureData {
int width;
int height;
int componentCount;
std::vector<char*> data;
};
/*
* Light struct with a position and intensity of the light source
*/
struct Light {
Light(const glm::vec3& p, const float& i) : position(p), intensity(i) {}
glm::vec3 position;
float intensity;
};
/*
* Material struct with defuse color, albedo and specular component
*/
struct Material {
Material(const glm::vec3& a, const glm::vec3& color, const float& spec) : albedo(a), diffuse_color(color), specular_exponent(spec) {}
Material() : albedo(1, 0, 0), diffuse_color(), specular_exponent() {}
glm::vec3 albedo;
alignas(16) glm::vec3 diffuse_color;
float specular_exponent;
};
/*
* the sphere is defined by it's center, the radius and the material
*
* the ray_intersect function checks, if a ray from the raytracer passes through the sphere, hits the sphere or passes by the the sphere
* @param vec3: origin of ray
* @param vec3: direction of ray
* @param float: distance of the ray to the sphere
* @return bool: if ray interesects sphere or not
*/
struct Sphere {
glm::vec3 center;
float radius;
Material material;
Sphere(const glm::vec3& c, const float& r, const Material& m) : center(c), radius(r), material(m) {}
bool ray_intersect(const glm::vec3& origin, const glm::vec3& dir, float& t0) const {
glm::vec3 L = center - origin;
float tca = glm::dot(L, dir);
float d2 = glm::dot(L, L) - tca * tca;
if (d2 > radius * radius) return false;
float thc = sqrtf(radius * radius - d2);
t0 = tca - thc;
float t1 = tca + thc;